source: Dev/trunk/src/client/dojox/widget/UpgradeBar.js @ 485

Last change on this file since 485 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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