source: Dev/branches/rest-dojo-ui/client/dojox/form/BusyButton.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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