Debian Codegraph @

dojox/mobile/Switch.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/connect",
4 "dojo/_base/declare",
5 "dojo/_base/event",
6 "dojo/_base/window",
7 "dojo/dom-class",
8 "dojo/dom-construct",
9 "dojo/dom-style",
10 "dojo/dom-attr",
11 "dojo/touch",
12 "dijit/_Contained",
13 "dijit/_WidgetBase",
14 "./sniff",
15 "./_maskUtils",
16 "./common",
17 "dojo/has!dojo-bidi?dojox/mobile/bidi/Switch"
18 ], function(array, connect, declare, event, win, domClass, domConstruct, domStyle, domAttr, touch, Contained, WidgetBase, has, maskUtils, dm, BidiSwitch){
19
20 // module:
21 // dojox/mobile/Switch
22
23 var Switch = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiSwitch" : "dojox.mobile.Switch", [WidgetBase, Contained],{
24 // summary:
25 // A toggle switch with a sliding knob.
26 // description:
27 // Switch is a toggle switch with a sliding knob. You can either
28 // tap or slide the knob to toggle the switch. The onStateChanged
29 // handler is called when the switch is manipulated.
30
31 // value: String
32 // The initial state of the switch: "on" or "off". The default
33 // value is "on".
34 value: "on",
35
36 // name: String
37 // A name for a hidden input field, which holds the current value.
38 name: "",
39
40 // leftLabel: String
41 // The left-side label of the switch.
42 leftLabel: "ON",
43
44 // rightLabel: String
45 // The right-side label of the switch.
46 rightLabel: "OFF",
47
48 // shape: String
49 // The shape of the switch.
50 // "mblSwDefaultShape", "mblSwSquareShape", "mblSwRoundShape1",
51 // "mblSwRoundShape2", "mblSwArcShape1" or "mblSwArcShape2".
52 // The default value is "mblSwDefaultShape".
53 shape: "mblSwDefaultShape",
54
55 // tabIndex: String
56 // Tabindex setting for this widget so users can hit the tab key to
57 // focus on it.
58 tabIndex: "0",
59 _setTabIndexAttr: "", // sets tabIndex to domNode
60
61 /* internal properties */
62 baseClass: "mblSwitch",
63 // role: [private] String
64 // The accessibility role.
65 role: "", // a11y
66
67 buildRendering: function(){
68 if(!this.templateString){ // true if this widget is not templated
69 this.domNode = (this.srcNodeRef && this.srcNodeRef.tagName === "SPAN") ?
70 this.srcNodeRef : domConstruct.create("span");
71 }
72 // prevent browser scrolling on IE10 (evt.preventDefault() is not enough)
73 dm._setTouchAction(this.domNode, "none");
74 this.inherited(arguments);
75 if(!this.templateString){ // true if this widget is not templated
76 var c = (this.srcNodeRef && this.srcNodeRef.className) || this.className || this["class"];
77 if((c = c.match(/mblSw.*Shape\d*/))){ this.shape = c; }
78 domClass.add(this.domNode, this.shape);
79 var nameAttr = this.name ? " name=\"" + this.name + "\"" : "";
80 this.domNode.innerHTML =
81 '<div class="mblSwitchInner">'
82 + '<div class="mblSwitchBg mblSwitchBgLeft">'
83 + '<div class="mblSwitchText mblSwitchTextLeft"></div>'
84 + '</div>'
85 + '<div class="mblSwitchBg mblSwitchBgRight">'
86 + '<div class="mblSwitchText mblSwitchTextRight"></div>'
87 + '</div>'
88 + '<div class="mblSwitchKnob"></div>'
89 + '<input type="hidden"'+nameAttr+' value="'+this.value+'"></div>'
90 + '</div>';
91 var n = this.inner = this.domNode.firstChild;
92 this.left = n.childNodes[0];
93 this.right = n.childNodes[1];
94 this.knob = n.childNodes[2];
95 this.input = n.childNodes[3];
96 }
97 domAttr.set(this.domNode, "role", "checkbox"); //a11y
98 domAttr.set(this.domNode, "aria-checked", (this.value === "on") ? "true" : "false"); //a11y
99
100 this.switchNode = this.domNode;
101
102 if(has("windows-theme")) {
103 var rootNode = domConstruct.create("div", {className: "mblSwitchContainer"});
104 this.labelNode = domConstruct.create("label", {"class": "mblSwitchLabel", "for": this.id}, rootNode);
105 rootNode.appendChild(this.domNode.cloneNode(true));
106 this.domNode = rootNode;
107 this.focusNode = rootNode.childNodes[1];
108 this.labelNode.innerHTML = (this.value=="off") ? this.rightLabel : this.leftLabel;
109 this.switchNode = this.domNode.childNodes[1];
110 var inner = this.inner = this.domNode.childNodes[1].firstChild;
111 this.left = inner.childNodes[0];
112 this.right = inner.childNodes[1];
113 this.knob = inner.childNodes[2];
114 this.input = inner.childNodes[3];
115 }
116 },
117
118 postCreate: function(){
119 this.connect(this.switchNode, "onclick", "_onClick");
120 this.connect(this.switchNode, "onkeydown", "_onClick"); // for desktop browsers
121 this._startHandle = this.connect(this.switchNode, touch.press, "onTouchStart");
122 this._initialValue = this.value; // for reset()
123 },
124
125 startup: function(){
126 var started = this._started;
127 this.inherited(arguments);
128 if(!started){
129 this.resize();
130 }
131 },
132
133 resize: function(){
134 if(has("windows-theme")){
135 // Override the custom CSS width (if any) to avoid misplacement.
136 // Per design, the windows theme does not allow resizing the controls.
137 // The label of the switch is placed next to the switch, and a custom
138 // width would only have the effect to increase the distance between the
139 // label and the switch, which is undesired. Hence, on windows theme,
140 // ensure the width of root DOM node is 100%.
141 domStyle.set(this.domNode, "width", "100%");
142 }else{
143 var value = domStyle.get(this.domNode,"width");
144 var outWidth = value + "px";
145 var innWidth = (value - domStyle.get(this.knob,"width")) + "px";
146 domStyle.set(this.left, "width", outWidth);
147 domStyle.set(this.right,this.isLeftToRight()?{width: outWidth, left: innWidth}:{width: outWidth});
148 domStyle.set(this.left.firstChild, "width", innWidth);
149 domStyle.set(this.right.firstChild, "width", innWidth);
150 domStyle.set(this.knob, "left", innWidth);
151 if(this.value == "off"){
152 domStyle.set(this.inner, "left", this.isLeftToRight()?("-" + innWidth):0);
153 }
154 this._hasMaskImage = false;
155 this._createMaskImage();
156 }
157 },
158
159 _changeState: function(/*String*/state, /*Boolean*/anim){
160 var on = (state === "on");
161 this.left.style.display = "";
162 this.right.style.display = "";
163 this.inner.style.left = "";
164 if(anim){
165 domClass.add(this.switchNode, "mblSwitchAnimation");
166 }
167 domClass.remove(this.switchNode, on ? "mblSwitchOff" : "mblSwitchOn");
168 domClass.add(this.switchNode, on ? "mblSwitchOn" : "mblSwitchOff");
169 domAttr.set(this.switchNode, "aria-checked", on ? "true" : "false"); //a11y
170
171 if(!on && !has("windows-theme")){
172 this.inner.style.left = (this.isLeftToRight()?(-(domStyle.get(this.domNode,"width") - domStyle.get(this.knob,"width"))):0) + "px";
173 }
174
175 var _this = this;
176 _this.defer(function(){
177 _this.left.style.display = on ? "" : "none";
178 _this.right.style.display = !on ? "" : "none";
179 domClass.remove(_this.switchNode, "mblSwitchAnimation");
180 }, anim ? 300 : 0);
181 },
182
183 _createMaskImage: function(){
184 if(this._timer){
185 this._timer.remove();
186 delete this._timer;
187 }
188 if(this._hasMaskImage){ return; }
189 var w = domStyle.get(this.domNode,"width"), h = domStyle.get(this.domNode,"height");
190 this._width = (w - domStyle.get(this.knob,"width"));
191 this._hasMaskImage = true;
192 if(!(has("mask-image"))){ return; }
193 var rDef = domStyle.get(this.left, "borderTopLeftRadius");
194 if(!rDef || rDef == "0px"){ return; }
195 var rDefs = rDef.split(" ");
196 var rx = parseFloat(rDefs[0]), ry = (rDefs.length == 1) ? rx : parseFloat(rDefs[1]);
197
198 if(rx && ry){
199 maskUtils.createRoundMask(this.switchNode, 0, 0, 0, 0, w, h, rx, ry, 1);
200 }
201 },
202
203 _onClick: function(e){
204 // summary:
205 // Internal handler for click events.
206 // tags:
207 // private
208 if(e && e.type === "keydown" && e.keyCode !== 13){ return; }
209 if(this.onClick(e) === false){ return; } // user's click action
210 if(this._moved){ return; }
211 this._set("value", this.input.value = (this.value == "on") ? "off" : "on");
212 this._changeState(this.value, true);
213 this.onStateChanged(this.value);
214 },
215
216 onClick: function(/*Event*/ /*===== e =====*/){
217 // summary:
218 // User defined function to handle clicks
219 // tags:
220 // callback
221 },
222
223 onTouchStart: function(/*Event*/e){
224 // summary:
225 // Internal function to handle touchStart events.
226 this._moved = false;
227 this.innerStartX = this.inner.offsetLeft;
228 if(!this._conn){
229 this._conn = [
230 this.connect(this.inner, touch.move, "onTouchMove"),
231 this.connect(win.doc, touch.release, "onTouchEnd")
232 ];
233
234 /* While moving the slider knob sometimes IE fires MSPointerCancel event. That prevents firing
235 MSPointerUp event (http://msdn.microsoft.com/ru-ru/library/ie/hh846776%28v=vs.85%29.aspx) so the
236 knob can be stuck in the middle of the switch. As a fix we handle MSPointerCancel event with the
237 same lintener as for MSPointerUp event.
238 */
239 if(has("windows-theme")){
240 this._conn.push(this.connect(win.doc, "MSPointerCancel", "onTouchEnd"));
241 }
242 }
243 this.touchStartX = e.touches ? e.touches[0].pageX : e.clientX;
244 this.left.style.display = "";
245 this.right.style.display = "";
246 event.stop(e);
247 this._createMaskImage();
248 },
249
250 onTouchMove: function(/*Event*/e){
251 // summary:
252 // Internal function to handle touchMove events.
253 e.preventDefault();
254 var dx;
255 if(e.targetTouches){
256 if(e.targetTouches.length != 1){ return; }
257 dx = e.targetTouches[0].clientX - this.touchStartX;
258 }else{
259 dx = e.clientX - this.touchStartX;
260 }
261 var pos = this.innerStartX + dx;
262 var d = 10;
263 if(pos <= -(this._width-d)){ pos = -this._width; }
264 if(pos >= -d){ pos = 0; }
265 this.inner.style.left = pos + "px";
266 if(Math.abs(dx) > d){
267 this._moved = true;
268 }
269 },
270
271 onTouchEnd: function(/*Event*/e){
272 // summary:
273 // Internal function to handle touchEnd events.
274 array.forEach(this._conn, connect.disconnect);
275 this._conn = null;
276 if(this.innerStartX == this.inner.offsetLeft){
277 // need to send a synthetic click?
278 if(has("touch") && has("clicks-prevented")){
279 dm._sendClick(this.inner, e);
280 }
281 return;
282 }
283 var newState = (this.inner.offsetLeft < -(this._width/2)) ? "off" : "on";
284 newState = this._newState(newState);
285 this._changeState(newState, true);
286 if(newState != this.value){
287 this._set("value", this.input.value = newState);
288 this.onStateChanged(newState);
289 }
290 },
291 _newState: function(newState){
292 return newState;
293 },
294 onStateChanged: function(/*String*/newState){
295 // summary:
296 // Stub function to connect to from your application.
297 // description:
298 // Called when the state has been changed.
299 if (this.labelNode) {
300 this.labelNode.innerHTML = newState=='off' ? this.rightLabel : this.leftLabel;
301 }
302 },
303
304 _setValueAttr: function(/*String*/value){
305 this._changeState(value, false);
306 if(this.value != value){
307 this._set("value", this.input.value = value);
308 this.onStateChanged(value);
309 }
310 },
311
312 _setLeftLabelAttr: function(/*String*/label){
313 this.leftLabel = label;
314 this.left.firstChild.innerHTML = this._cv ? this._cv(label) : label;
315 },
316
317 _setRightLabelAttr: function(/*String*/label){
318 this.rightLabel = label;
319 this.right.firstChild.innerHTML = this._cv ? this._cv(label) : label;
320 },
321
322 reset: function(){
323 // summary:
324 // Reset the widget's value to what it was at initialization time
325 this.set("value", this._initialValue);
326 }
327 });
328
329 return has("dojo-bidi") ? declare("dojox.mobile.Switch", [Switch, BidiSwitch]) : Switch;
330 });