Debian Codegraph @

dojox/form/_BusyButtonMixin.js

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