source: Dev/trunk/src/client/dojox/form/BusyButton.js @ 532

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

Added Dojo 1.9.3 release.

File size: 3.7 KB
Line 
1define([
2        "dojo/_base/lang",
3        "dojo/dom-attr",
4        "dojo/dom-class",
5        "dijit/form/Button",
6        "dijit/form/DropDownButton",
7        "dijit/form/ComboButton",
8        "dojo/i18n",
9        "dojo/i18n!dijit/nls/loading",
10        "dojo/_base/declare"
11], function(lang, domAttr, domClass, Button, DropDownButton, ComboButton, i18n, nlsLoading, declare){
12
13var _BusyButtonMixin = declare("dojox.form._BusyButtonMixin", null, {
14
15        // isBusy: Boolean
16        isBusy: false,
17       
18        // busyLabel: String
19        //              text while button is busy
20        busyLabel: "",
21       
22        timeout: null, // timeout, should be controlled by xhr call
23       
24        // useIcon: Boolean
25        //              use a busy icon
26        useIcon: true,
27
28        postMixInProperties: function(){
29                this.inherited(arguments);
30                if(!this.busyLabel){
31                        this.busyLabel = i18n.getLocalization("dijit", "loading", this.lang).loadingState;
32                }
33        },
34
35        postCreate: function(){
36                // summary:
37                //              stores initial label and timeout for reference
38                this.inherited(arguments);
39                this._label = this.containerNode.innerHTML;
40                this._initTimeout = this.timeout;
41
42                // for initial busy buttons
43                if(this.isBusy){
44                        this.makeBusy();
45                }
46        },
47
48        makeBusy: function(){
49                // summary:
50                //              sets state from idle to busy
51                this.isBusy = true;
52                this.set("disabled", true);
53
54                this.setLabel(this.busyLabel, this.timeout);
55        },
56
57        cancel: function(){
58                // summary:
59                //              if no timeout is set or for other reason the user can put the button back
60                //              to being idle
61                this.set("disabled", false);
62                this.isBusy = false;
63                this.setLabel(this._label);
64                if(this._timeout){      clearTimeout(this._timeout); }
65                this.timeout = this._initTimeout;
66        },
67
68        resetTimeout: function(/*Int*/ timeout){
69                // summary:
70                //              to reset existing timeout and setting a new timeout
71                if(this._timeout){
72                        clearTimeout(this._timeout);
73                }
74
75                // new timeout
76                if(timeout){
77                        this._timeout = setTimeout(lang.hitch(this, function(){
78                                this.cancel();
79                        }), timeout);
80                }else if(timeout == undefined || timeout === 0){
81                        this.cancel();
82                }
83        },
84
85        setLabel: function(/*String*/ content, /*Int*/ timeout){
86                // summary:
87                //              setting a label and optional timeout of the labels state
88
89                // this.inherited(arguments); FIXME: throws an Unknown runtime error
90
91                // Begin IE hack
92                this.label = content;
93                // remove children
94                while(this.containerNode.firstChild){
95                        this.containerNode.removeChild(this.containerNode.firstChild);
96                }
97                this.containerNode.innerHTML = this.label;
98
99                if(this.showLabel == false && !domAttr.get(this.domNode, "title")){
100                        this.titleNode.title=lang.trim(this.containerNode.innerText || this.containerNode.textContent || '');
101                }
102                // End IE hack
103
104                // setting timeout
105                if(timeout){
106                        this.resetTimeout(timeout);
107                }else{
108                        this.timeout = null;
109                }
110
111                // create optional busy image
112                if(this.useIcon && this.isBusy){
113                        var node = new Image();
114                        node.src = this._blankGif;
115                        domAttr.set(node, "id", this.id+"_icon");
116                        domClass.add(node, "dojoxBusyButtonIcon");
117                        this.containerNode.appendChild(node);
118                }
119        },
120
121        _onClick: function(e){
122                // summary:
123                //              on button click the button state gets changed
124
125                // only do something if button is not busy
126                if(!this.isBusy){
127                        this.inherited(arguments);      // calls onClick()
128                        this.makeBusy();
129                }
130        }
131});
132
133var BusyButton = declare("dojox.form.BusyButton", [Button, _BusyButtonMixin], {
134        // summary:
135        //              BusyButton is a simple widget which provides implementing more
136        //              user friendly form submission.
137        // description:
138        //              When a form gets submitted by a user, many times it is recommended to disable
139        //              the submit buttons to prevent double submission. BusyButton provides a simple set
140        //              of features for this purpose
141
142});
143declare("dojox.form.BusyComboButton", [ComboButton, _BusyButtonMixin], {});
144declare("dojox.form.BusyDropDownButton", [DropDownButton, _BusyButtonMixin], {});
145return BusyButton;
146});
Note: See TracBrowser for help on using the repository browser.