1 | dojo.provide("dojox.widget.UpgradeBar"); |
---|
2 | |
---|
3 | dojo.require("dojo.window"); |
---|
4 | dojo.require("dojo.fx"); |
---|
5 | dojo.require("dojo.cookie"); |
---|
6 | |
---|
7 | dojo.require("dijit._Widget"); |
---|
8 | dojo.require("dijit._Templated"); |
---|
9 | |
---|
10 | dojo.experimental("dojox.widget.UpgradeBar"); |
---|
11 | |
---|
12 | |
---|
13 | dojo.declare("dojox.widget.UpgradeBar", [dijit._Widget, dijit._Templated], { |
---|
14 | // summary: |
---|
15 | // Shows a bar at the top of the screen when the user is to |
---|
16 | // be notified that they should upgrade their browser or a |
---|
17 | // plugin. |
---|
18 | // |
---|
19 | // description: |
---|
20 | // You can insert custom validations to trigger the UpgradeBar |
---|
21 | // to display. An evaluation of 'true' shows the bar (as this |
---|
22 | // version *is* less than it should be). Multiple validations |
---|
23 | // may be checked, although only the first in the list will be |
---|
24 | // displayed. |
---|
25 | // Markup and programmatic are supported. Markup is a little |
---|
26 | // cleaner, since a majority of the parameters are the HTML |
---|
27 | // snippets to be displayed. In markup, the validate code should |
---|
28 | // be an expression that will evaluate to true or false. This |
---|
29 | // expression is wrapped in a try/catch, so if it blows up, it |
---|
30 | // is assumed to be true and trigger the bar. |
---|
31 | // In programmtic, a function should be used that returns true |
---|
32 | // or false. You would need to use your own try/catch in that. |
---|
33 | // |
---|
34 | // example: See tests for examples. |
---|
35 | // |
---|
36 | // notifications: Array |
---|
37 | // An array of objects that hold the criteria for upgrades. |
---|
38 | // message: String |
---|
39 | // The message to display in the bar. Can be HTML. |
---|
40 | // validate:Function |
---|
41 | // The expression to evaluate to determine if the |
---|
42 | // bar should show or not. Should be a simple expression |
---|
43 | // if used in HTML: |
---|
44 | // | <div validate="!google.gears"> |
---|
45 | // | <div validate="dojo.isIE<8"> |
---|
46 | notifications:[], |
---|
47 | // |
---|
48 | // buttonCancel:String |
---|
49 | // The HTML tip show when hovering over the close button. |
---|
50 | buttonCancel:"Close for now", |
---|
51 | // |
---|
52 | // noRemindButton:String |
---|
53 | // The text link shown that when clicked, permanently dismisses |
---|
54 | // the message (sets a cookie). If this string is blank, this |
---|
55 | // link is not displayed. |
---|
56 | noRemindButton:"Don't Remind Me Again", |
---|
57 | |
---|
58 | templateString: dojo.cache("dojox.widget","UpgradeBar/UpgradeBar.html"), |
---|
59 | |
---|
60 | constructor: function(props, node){ |
---|
61 | |
---|
62 | if(!props.notifications && node){ |
---|
63 | // From markup. Create the notifications Array from the |
---|
64 | // srcRefNode children. |
---|
65 | dojo.forEach(node.childNodes, function(n){ |
---|
66 | if(n.nodeType==1){ |
---|
67 | var val = dojo.attr(n, "validate"); |
---|
68 | this.notifications.push({ |
---|
69 | message:n.innerHTML, |
---|
70 | validate:function(){ |
---|
71 | // the function that fires to determine if the |
---|
72 | // bar shows or not. |
---|
73 | var evals = true; |
---|
74 | try{ |
---|
75 | evals = dojo.eval(val); |
---|
76 | }catch(e){ /* squelch. it's true.*/ } |
---|
77 | return evals; |
---|
78 | } |
---|
79 | }); |
---|
80 | } |
---|
81 | }, this); |
---|
82 | } |
---|
83 | |
---|
84 | }, |
---|
85 | |
---|
86 | checkNotifications: function(){ |
---|
87 | // summary: |
---|
88 | // Internal. Go through the notifications Array |
---|
89 | // and check for any that evaluate to true. |
---|
90 | // tags: |
---|
91 | // private |
---|
92 | // |
---|
93 | if(!this.notifications.length){ |
---|
94 | // odd. why use the bar but not set any notifications? |
---|
95 | return; |
---|
96 | } |
---|
97 | |
---|
98 | for(var i=0;i<this.notifications.length;i++){ |
---|
99 | var evals = this.notifications[i].validate(); |
---|
100 | if(evals){ |
---|
101 | this.notify(this.notifications[i].message); |
---|
102 | // Validation resulted in true, meaning a feature is missing |
---|
103 | // Don't check any other messages. One at a time. |
---|
104 | break; |
---|
105 | } |
---|
106 | } |
---|
107 | }, |
---|
108 | |
---|
109 | postCreate: function(){ |
---|
110 | this.inherited(arguments); |
---|
111 | if(this.domNode.parentNode){ |
---|
112 | dojo.style(this.domNode, "display", "none"); |
---|
113 | } |
---|
114 | dojo.mixin(this.attributeMap, { |
---|
115 | message:{ node:"messageNode", type:"innerHTML" } |
---|
116 | }); |
---|
117 | if(!this.noRemindButton){ |
---|
118 | dojo.destroy(this.dontRemindButtonNode) |
---|
119 | } |
---|
120 | if(dojo.isIE==6){ |
---|
121 | // IE6 is challenged when it comes to 100% width. |
---|
122 | // It thinks the body has more padding and more |
---|
123 | // margin than it really does. It would work to |
---|
124 | // set the body pad and margin to 0, but we can't |
---|
125 | // set that and disturb a potential layout. |
---|
126 | // |
---|
127 | var self = this; |
---|
128 | var setWidth = function(){ |
---|
129 | var v = dojo.window.getBox(); |
---|
130 | dojo.style(self.domNode, "width", v.w+"px"); |
---|
131 | } |
---|
132 | this.connect(window, "resize", function(){ |
---|
133 | setWidth(); |
---|
134 | }); |
---|
135 | |
---|
136 | setWidth(); |
---|
137 | } |
---|
138 | dojo.addOnLoad(this, "checkNotifications"); |
---|
139 | //this.checkNotifications(); |
---|
140 | }, |
---|
141 | |
---|
142 | notify: function(msg){ |
---|
143 | // summary: |
---|
144 | // Triggers the bar to display. An internal function, |
---|
145 | // but could ne called externally for fun. |
---|
146 | // tags: |
---|
147 | // protected |
---|
148 | // |
---|
149 | if(dojo.cookie("disableUpgradeReminders")){ |
---|
150 | return; |
---|
151 | } |
---|
152 | if(!this.domNode.parentNode || !this.domNode.parentNode.innerHTML){ |
---|
153 | document.body.appendChild(this.domNode); |
---|
154 | } |
---|
155 | dojo.style(this.domNode, "display", ""); |
---|
156 | if(msg){ |
---|
157 | this.set("message", msg); |
---|
158 | } |
---|
159 | |
---|
160 | }, |
---|
161 | |
---|
162 | show: function(){ |
---|
163 | // summary: |
---|
164 | // Internal. Shows the bar. Do not call directly. |
---|
165 | // Use notify(); |
---|
166 | // tags: |
---|
167 | // private |
---|
168 | // |
---|
169 | this._bodyMarginTop = dojo.style(dojo.body(), "marginTop"); |
---|
170 | this._size = dojo.contentBox(this.domNode).h; |
---|
171 | dojo.style(this.domNode, { display:"block", height:0, opacity:0 }); |
---|
172 | |
---|
173 | if(!this._showAnim){ |
---|
174 | this._showAnim = dojo.fx.combine([ |
---|
175 | dojo.animateProperty({ node:dojo.body(), duration:500, properties:{ marginTop:this._bodyMarginTop+this._size } }), |
---|
176 | dojo.animateProperty({ node:this.domNode, duration:500, properties:{ height:this._size, opacity:1 } }) |
---|
177 | ]); |
---|
178 | } |
---|
179 | this._showAnim.play(); |
---|
180 | }, |
---|
181 | |
---|
182 | hide: function(){ |
---|
183 | // summary: |
---|
184 | // Hides the bar. May be called externally. |
---|
185 | // |
---|
186 | if(!this._hideAnim){ |
---|
187 | this._hideAnim = dojo.fx.combine([ |
---|
188 | dojo.animateProperty({ node:dojo.body(), duration:500, properties:{ marginTop:this._bodyMarginTop } }), |
---|
189 | dojo.animateProperty({ node:this.domNode, duration:500, properties:{ height:0, opacity:0 } }) |
---|
190 | ]); |
---|
191 | dojo.connect(this._hideAnim, "onEnd", this, function(){ |
---|
192 | dojo.style(this.domNode, "display", "none"); |
---|
193 | }); |
---|
194 | } |
---|
195 | this._hideAnim.play(); |
---|
196 | }, |
---|
197 | |
---|
198 | _onDontRemindClick: function(){ |
---|
199 | // summary: |
---|
200 | // Called when user clicks the "do not remind" link. |
---|
201 | // tags: |
---|
202 | // private |
---|
203 | dojo.cookie("disableUpgradeReminders", true, { expires:3650 }); |
---|
204 | this.hide(); |
---|
205 | }, |
---|
206 | |
---|
207 | _onCloseEnter: function(){ |
---|
208 | // summary: |
---|
209 | // Called when user hovers over close icon |
---|
210 | // tags: |
---|
211 | // private |
---|
212 | dojo.addClass(this.closeButtonNode, "dojoxUpgradeBarCloseIcon-hover"); |
---|
213 | }, |
---|
214 | |
---|
215 | _onCloseLeave: function(){ |
---|
216 | // summary: |
---|
217 | // Called when user stops hovering over close icon |
---|
218 | // tags: |
---|
219 | // private |
---|
220 | dojo.removeClass(this.closeButtonNode, "dojoxUpgradeBarCloseIcon-hover"); |
---|
221 | } |
---|
222 | |
---|
223 | }); |
---|