| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
var Switch = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiSwitch" : "dojox.mobile.Switch", [WidgetBase, Contained],{ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
value: "on", |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
name: "", |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
leftLabel: "ON", |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
rightLabel: "OFF", |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
shape: "mblSwDefaultShape", |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
tabIndex: "0", |
| 59 |
_setTabIndexAttr: "", |
| 60 |
|
| 61 |
|
| 62 |
baseClass: "mblSwitch", |
| 63 |
|
| 64 |
|
| 65 |
role: "", |
| 66 |
|
| 67 |
buildRendering: function(){ |
| 68 |
if(!this.templateString){ |
| 69 |
this.domNode = (this.srcNodeRef && this.srcNodeRef.tagName === "SPAN") ? |
| 70 |
this.srcNodeRef : domConstruct.create("span"); |
| 71 |
} |
| 72 |
|
| 73 |
dm._setTouchAction(this.domNode, "none"); |
| 74 |
this.inherited(arguments); |
| 75 |
if(!this.templateString){ |
| 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"); |
| 98 |
domAttr.set(this.domNode, "aria-checked", (this.value === "on") ? "true" : "false"); |
| 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"); |
| 121 |
this._startHandle = this.connect(this.switchNode, touch.press, "onTouchStart"); |
| 122 |
this._initialValue = this.value; |
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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(state, 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"); |
| 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 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if(e && e.type === "keydown" && e.keyCode !== 13){ return; } |
| 209 |
if(this.onClick(e) === false){ return; } |
| 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( ){ |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
}, |
| 222 |
|
| 223 |
onTouchStart: function(e){ |
| 224 |
|
| 225 |
|
| 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 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 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(e){ |
| 251 |
|
| 252 |
|
| 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(e){ |
| 272 |
|
| 273 |
|
| 274 |
array.forEach(this._conn, connect.disconnect); |
| 275 |
this._conn = null; |
| 276 |
if(this.innerStartX == this.inner.offsetLeft){ |
| 277 |
|
| 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(newState){ |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
if (this.labelNode) { |
| 300 |
this.labelNode.innerHTML = newState=='off' ? this.rightLabel : this.leftLabel; |
| 301 |
} |
| 302 |
}, |
| 303 |
|
| 304 |
_setValueAttr: function(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(label){ |
| 313 |
this.leftLabel = label; |
| 314 |
this.left.firstChild.innerHTML = this._cv ? this._cv(label) : label; |
| 315 |
}, |
| 316 |
|
| 317 |
_setRightLabelAttr: function(label){ |
| 318 |
this.rightLabel = label; |
| 319 |
this.right.firstChild.innerHTML = this._cv ? this._cv(label) : label; |
| 320 |
}, |
| 321 |
|
| 322 |
reset: function(){ |
| 323 |
|
| 324 |
|
| 325 |
this.set("value", this._initialValue); |
| 326 |
} |
| 327 |
}); |
| 328 |
|
| 329 |
return has("dojo-bidi") ? declare("dojox.mobile.Switch", [Switch, BidiSwitch]) : Switch; |
| 330 |
}); |