| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/connect", |
| 4 |
"dojo/_base/declare", |
| 5 |
"dojo/dom", |
| 6 |
"dojo/dom-class", |
| 7 |
"dijit/registry", |
| 8 |
"./View", |
| 9 |
"./_ScrollableMixin", |
| 10 |
"./sniff", |
| 11 |
"./_css3", |
| 12 |
"dojo/has!dojo-bidi?dojox/mobile/bidi/SwapView" |
| 13 |
], function(array, connect, declare, dom, domClass, registry, View, ScrollableMixin, has, css3, BidiSwapView){ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var SwapView = declare(has("dojo-bidi") ? "dojox.mobile.NonBidiSwapView" : "dojox.mobile.SwapView", [View, ScrollableMixin], { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
scrollDir: "f", |
| 34 |
|
| 35 |
|
| 36 |
weight: 1.2, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
_endOfTransitionTimeoutHandle: null, |
| 42 |
|
| 43 |
buildRendering: function(){ |
| 44 |
this.inherited(arguments); |
| 45 |
domClass.add(this.domNode, "mblSwapView"); |
| 46 |
this.setSelectable(this.domNode, false); |
| 47 |
this.containerNode = this.domNode; |
| 48 |
this.subscribe("/dojox/mobile/nextPage", "handleNextPage"); |
| 49 |
this.subscribe("/dojox/mobile/prevPage", "handlePrevPage"); |
| 50 |
this.noResize = true; |
| 51 |
}, |
| 52 |
|
| 53 |
startup: function(){ |
| 54 |
if(this._started){ return; } |
| 55 |
this.inherited(arguments); |
| 56 |
}, |
| 57 |
|
| 58 |
resize: function(){ |
| 59 |
|
| 60 |
|
| 61 |
this.inherited(arguments); |
| 62 |
array.forEach(this.getChildren(), function(child){ |
| 63 |
if(child.resize){ child.resize(); } |
| 64 |
}); |
| 65 |
}, |
| 66 |
|
| 67 |
onTouchStart: function(e){ |
| 68 |
|
| 69 |
|
| 70 |
if(this._siblingViewsInMotion()){ |
| 71 |
this.propagatable ? e.preventDefault() : event.stop(e); |
| 72 |
return; |
| 73 |
} |
| 74 |
var fromTop = this.domNode.offsetTop; |
| 75 |
var nextView = this.nextView(this.domNode); |
| 76 |
if(nextView){ |
| 77 |
nextView.stopAnimation(); |
| 78 |
domClass.add(nextView.domNode, "mblIn"); |
| 79 |
|
| 80 |
nextView.containerNode.style.paddingTop = fromTop + "px"; |
| 81 |
} |
| 82 |
var prevView = this.previousView(this.domNode); |
| 83 |
if(prevView){ |
| 84 |
prevView.stopAnimation(); |
| 85 |
domClass.add(prevView.domNode, "mblIn"); |
| 86 |
|
| 87 |
prevView.containerNode.style.paddingTop = fromTop + "px"; |
| 88 |
} |
| 89 |
this._setSiblingViewsInMotion(true); |
| 90 |
this.inherited(arguments); |
| 91 |
}, |
| 92 |
|
| 93 |
onTouchEnd: function(e){ |
| 94 |
if(e){ |
| 95 |
if(!this._moved){ |
| 96 |
this._setSiblingViewsInMotion(false); |
| 97 |
}else{ |
| 98 |
|
| 99 |
|
| 100 |
this._endOfTransitionTimeoutHandle = this.defer(function(){ |
| 101 |
this._setSiblingViewsInMotion(false); |
| 102 |
}, 1000); |
| 103 |
} |
| 104 |
} |
| 105 |
this.inherited(arguments); |
| 106 |
}, |
| 107 |
|
| 108 |
handleNextPage: function(w){ |
| 109 |
|
| 110 |
|
| 111 |
var refNode = w.refId && dom.byId(w.refId) || w.domNode; |
| 112 |
if(this.domNode.parentNode !== refNode.parentNode){ return; } |
| 113 |
if(this.getShowingView() !== this){ return; } |
| 114 |
this.goTo(1); |
| 115 |
}, |
| 116 |
|
| 117 |
handlePrevPage: function(w){ |
| 118 |
|
| 119 |
|
| 120 |
var refNode = w.refId && dom.byId(w.refId) || w.domNode; |
| 121 |
if(this.domNode.parentNode !== refNode.parentNode){ return; } |
| 122 |
if(this.getShowingView() !== this){ return; } |
| 123 |
this.goTo(-1); |
| 124 |
}, |
| 125 |
|
| 126 |
goTo: function(dir, moveTo){ |
| 127 |
|
| 128 |
|
| 129 |
var view = moveTo ? registry.byId(moveTo) : |
| 130 |
((dir == 1) ? this.nextView(this.domNode) : this.previousView(this.domNode)); |
| 131 |
if(view && view !== this){ |
| 132 |
this.stopAnimation(); |
| 133 |
view.stopAnimation(); |
| 134 |
this.domNode._isShowing = false; |
| 135 |
view.domNode._isShowing = true; |
| 136 |
this.performTransition(view.id, dir, "slide", null, function(){ |
| 137 |
connect.publish("/dojox/mobile/viewChanged", [view]); |
| 138 |
}); |
| 139 |
} |
| 140 |
}, |
| 141 |
|
| 142 |
isSwapView: function(node){ |
| 143 |
|
| 144 |
|
| 145 |
return (node && node.nodeType === 1 && domClass.contains(node, "mblSwapView")); |
| 146 |
}, |
| 147 |
|
| 148 |
nextView: function(node){ |
| 149 |
|
| 150 |
|
| 151 |
for(var n = node.nextSibling; n; n = n.nextSibling){ |
| 152 |
if(this.isSwapView(n)){ return registry.byNode(n); } |
| 153 |
} |
| 154 |
return null; |
| 155 |
}, |
| 156 |
|
| 157 |
previousView: function(node){ |
| 158 |
|
| 159 |
|
| 160 |
for(var n = node.previousSibling; n; n = n.previousSibling){ |
| 161 |
if(this.isSwapView(n)){ return registry.byNode(n); } |
| 162 |
} |
| 163 |
return null; |
| 164 |
}, |
| 165 |
|
| 166 |
scrollTo: function(to){ |
| 167 |
|
| 168 |
|
| 169 |
if(!this._beingFlipped){ |
| 170 |
var newView, x; |
| 171 |
if(to.x){ |
| 172 |
if(to.x < 0){ |
| 173 |
newView = this.nextView(this.domNode); |
| 174 |
x = to.x + this.domNode.offsetWidth; |
| 175 |
}else{ |
| 176 |
newView = this.previousView(this.domNode); |
| 177 |
x = to.x - this.domNode.offsetWidth; |
| 178 |
} |
| 179 |
} |
| 180 |
if(newView){ |
| 181 |
if(newView.domNode.style.display === "none"){ |
| 182 |
newView.domNode.style.display = ""; |
| 183 |
newView.resize(); |
| 184 |
} |
| 185 |
newView._beingFlipped = true; |
| 186 |
newView.scrollTo({x:x}); |
| 187 |
newView._beingFlipped = false; |
| 188 |
} |
| 189 |
} |
| 190 |
this.inherited(arguments); |
| 191 |
}, |
| 192 |
|
| 193 |
findDisp: function(node){ |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if(!domClass.contains(node, "mblSwapView")){ |
| 202 |
return this.inherited(arguments); |
| 203 |
} |
| 204 |
if(!node.parentNode){ return null; } |
| 205 |
var nodes = node.parentNode.childNodes; |
| 206 |
for(var i = 0; i < nodes.length; i++){ |
| 207 |
var n = nodes[i]; |
| 208 |
if(n.nodeType === 1 && domClass.contains(n, "mblSwapView") |
| 209 |
&& !domClass.contains(n, "mblIn") && n.style.display !== "none"){ |
| 210 |
return n; |
| 211 |
} |
| 212 |
} |
| 213 |
return node; |
| 214 |
}, |
| 215 |
|
| 216 |
slideTo: function(to, duration, easing, fake_pos){ |
| 217 |
|
| 218 |
|
| 219 |
if(!this._beingFlipped){ |
| 220 |
var w = this.domNode.offsetWidth; |
| 221 |
var pos = fake_pos || this.getPos(); |
| 222 |
var newView, newX; |
| 223 |
if(pos.x < 0){ |
| 224 |
newView = this.nextView(this.domNode); |
| 225 |
if(pos.x < -w/4){ |
| 226 |
if(newView){ |
| 227 |
to.x = -w; |
| 228 |
newX = 0; |
| 229 |
} |
| 230 |
}else{ |
| 231 |
if(newView){ |
| 232 |
newX = w; |
| 233 |
} |
| 234 |
} |
| 235 |
}else{ |
| 236 |
newView = this.previousView(this.domNode); |
| 237 |
if(pos.x > w/4){ |
| 238 |
if(newView){ |
| 239 |
to.x = w; |
| 240 |
newX = 0; |
| 241 |
} |
| 242 |
}else{ |
| 243 |
if(newView){ |
| 244 |
newX = -w; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if(newView){ |
| 250 |
newView._beingFlipped = true; |
| 251 |
newView.slideTo({x:newX}, duration, easing); |
| 252 |
newView._beingFlipped = false; |
| 253 |
newView.domNode._isShowing = (newView && newX === 0); |
| 254 |
} |
| 255 |
this.domNode._isShowing = !(newView && newX === 0); |
| 256 |
} |
| 257 |
this.inherited(arguments); |
| 258 |
}, |
| 259 |
|
| 260 |
onAnimationEnd: function(e){ |
| 261 |
|
| 262 |
|
| 263 |
if(e && e.target && domClass.contains(e.target, "mblScrollableScrollTo2")){ return; } |
| 264 |
this.inherited(arguments); |
| 265 |
}, |
| 266 |
|
| 267 |
onFlickAnimationEnd: function(e){ |
| 268 |
if(this._endOfTransitionTimeoutHandle){ |
| 269 |
this._endOfTransitionTimeoutHandle = this._endOfTransitionTimeoutHandle.remove(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
if(e && e.target && !domClass.contains(e.target, "mblScrollableScrollTo2")){ return; } |
| 274 |
this.inherited(arguments); |
| 275 |
|
| 276 |
if(this.domNode._isShowing){ |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
array.forEach(this.domNode.parentNode.childNodes, function(c){ |
| 281 |
if(this.isSwapView(c)){ |
| 282 |
domClass.remove(c, "mblIn"); |
| 283 |
if(!c._isShowing){ |
| 284 |
c.style.display = "none"; |
| 285 |
c.style[css3.name("transform")] = ""; |
| 286 |
c.style.left = "0px"; |
| 287 |
|
| 288 |
c.style.paddingTop = ""; |
| 289 |
} |
| 290 |
} |
| 291 |
}, this); |
| 292 |
connect.publish("/dojox/mobile/viewChanged", [this]); |
| 293 |
|
| 294 |
this.containerNode.style.paddingTop = ""; |
| 295 |
}else if(!has("css3-animations")){ |
| 296 |
this.containerNode.style.left = "0px"; |
| 297 |
} |
| 298 |
this._setSiblingViewsInMotion(false); |
| 299 |
}, |
| 300 |
|
| 301 |
_setSiblingViewsInMotion: function(inMotion){ |
| 302 |
var inMotionAttributeValue = inMotion ? "true" : false; |
| 303 |
var parent = this.domNode.parentNode; |
| 304 |
if(parent){ |
| 305 |
parent.setAttribute("data-dojox-mobile-swapview-inmotion", inMotionAttributeValue); |
| 306 |
} |
| 307 |
}, |
| 308 |
|
| 309 |
_siblingViewsInMotion: function(){ |
| 310 |
var parent = this.domNode.parentNode; |
| 311 |
if(parent){ |
| 312 |
return parent.getAttribute("data-dojox-mobile-swapview-inmotion") == "true"; |
| 313 |
}else{ |
| 314 |
return false; |
| 315 |
} |
| 316 |
} |
| 317 |
}); |
| 318 |
return has("dojo-bidi") ? declare("dojox.mobile.SwapView", [SwapView, BidiSwapView]) : SwapView; |
| 319 |
}); |