| 1 |
define([ |
| 2 |
"dojo/aspect", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/Deferred", |
| 5 |
"dojo/_base/lang", |
| 6 |
"dojo/_base/array", |
| 7 |
"dojo/_base/fx", |
| 8 |
"dojo/dom", |
| 9 |
"dojo/dom-attr", |
| 10 |
"dojo/dom-construct", |
| 11 |
"dojo/dom-geometry", |
| 12 |
"dojo/dom-style", |
| 13 |
"dojo/topic", |
| 14 |
"dojo/on", |
| 15 |
"dojo/parser", |
| 16 |
"dojo/query", |
| 17 |
"dojo/fx/easing", |
| 18 |
"dojo/NodeList-dom" |
| 19 |
], function(aspect, declare, Deferred, lang, array, fx, dom, domAttr, domConstruct, domGeometry, domStyle, topic, |
| 20 |
on, parser, query){ |
| 21 |
|
| 22 |
|
| 23 |
var _defaultTransition = "dojox.widget.rotator.swap", |
| 24 |
_defaultTransitionDuration = 500, |
| 25 |
_displayStr = "display", |
| 26 |
_noneStr = "none", |
| 27 |
_zIndex = "zIndex"; |
| 28 |
|
| 29 |
var Rotator = declare("dojox.widget.Rotator", null, { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
transition: _defaultTransition, |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
transitionParams: "duration:" + _defaultTransitionDuration, |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
panes: null, |
| 104 |
|
| 105 |
constructor: function(params, node){ |
| 106 |
|
| 107 |
|
| 108 |
lang.mixin(this, params); |
| 109 |
|
| 110 |
var _t = this, |
| 111 |
t = _t.transition, |
| 112 |
tt = _t._transitions = {}, |
| 113 |
idm = _t._idMap = {}, |
| 114 |
tp = _t.transitionParams = eval("({ " + _t.transitionParams + " })"), |
| 115 |
node = _t._domNode = dom.byId(node), |
| 116 |
cb = _t._domNodeContentBox = domGeometry.getContentBox(node); |
| 117 |
|
| 118 |
|
| 119 |
_t.id = node.id || (new Date()).getTime(); |
| 120 |
|
| 121 |
|
| 122 |
if(domStyle.get(node, "position") == "static"){ |
| 123 |
domStyle.set(node, "position", "relative"); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
tt[t] = lang.getObject(t); |
| 128 |
if(!tt[t]){ |
| 129 |
this._transitionWarn(t, _defaultTransition); |
| 130 |
tt[_t.transition = _defaultTransition] = lang.getObject(_defaultTransition); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if(!tp.duration){ |
| 135 |
tp.duration = _defaultTransitionDuration; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
array.forEach(_t.panes, function(p){ |
| 140 |
domConstruct.create("div", p, node); |
| 141 |
}); |
| 142 |
|
| 143 |
|
| 144 |
_t.panes = []; |
| 145 |
|
| 146 |
|
| 147 |
query("> *", node).forEach(function(n, i){ |
| 148 |
_t._initializePane(n, i); |
| 149 |
}); |
| 150 |
|
| 151 |
_t._controlSub = topic.subscribe(_t.id + "/rotator/control", lang.hitch(_t, this.control)); |
| 152 |
}, |
| 153 |
|
| 154 |
insert: function(node, index){ |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
var pane, |
| 159 |
panes = this.panes, |
| 160 |
paneNode; |
| 161 |
|
| 162 |
if (index == null) { |
| 163 |
index = panes.length; |
| 164 |
} |
| 165 |
|
| 166 |
if (index < panes.length) { |
| 167 |
pane = panes[index]; |
| 168 |
domConstruct.place(node, pane.node, 'before'); |
| 169 |
} |
| 170 |
else { |
| 171 |
domConstruct.place(node, this._domNode, 'last'); |
| 172 |
} |
| 173 |
|
| 174 |
this._initializePane(node, index); |
| 175 |
}, |
| 176 |
|
| 177 |
remove: function(nodeOrIndex){ |
| 178 |
|
| 179 |
|
| 180 |
function removeFromPanes(idx) { |
| 181 |
var removed = panes.splice(idx, 1)[0]; |
| 182 |
if (removed) { |
| 183 |
if (removed.id) { |
| 184 |
_t._idMap[removed.id] = undefined; |
| 185 |
} |
| 186 |
_t._domNode.removeChild(removed.node); |
| 187 |
} |
| 188 |
if (_t.idx > idx) { |
| 189 |
_t.idx--; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
var index, |
| 194 |
_t = this, |
| 195 |
panes = this.panes; |
| 196 |
|
| 197 |
if (typeof nodeOrIndex === "number") { |
| 198 |
index = nodeOrIndex; |
| 199 |
} |
| 200 |
else { |
| 201 |
for (var i = 0; i < panes.length; i++) { |
| 202 |
if (panes[i].node === nodeOrIndex) { |
| 203 |
index = i; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if (index == null) { |
| 209 |
return; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
if (index === this.idx) { |
| 214 |
var def = this.go(this.idx - 1); |
| 215 |
if (def) { |
| 216 |
return def.then(function () { |
| 217 |
removeFromPanes(index); |
| 218 |
}); |
| 219 |
} |
| 220 |
else { |
| 221 |
removeFromPanes(index); |
| 222 |
} |
| 223 |
} |
| 224 |
else { |
| 225 |
removeFromPanes(index); |
| 226 |
} |
| 227 |
}, |
| 228 |
|
| 229 |
destroy: function(){ |
| 230 |
|
| 231 |
|
| 232 |
array.forEach([this._controlSub, this.wfe], function(wfe) { wfe && wfe.remove() }); |
| 233 |
domConstruct.destroy(this._domNode); |
| 234 |
this.panes = []; |
| 235 |
}, |
| 236 |
|
| 237 |
next: function(){ |
| 238 |
|
| 239 |
|
| 240 |
return this.go(this.idx + 1); |
| 241 |
}, |
| 242 |
|
| 243 |
prev: function(){ |
| 244 |
|
| 245 |
|
| 246 |
return this.go(this.idx - 1); |
| 247 |
}, |
| 248 |
|
| 249 |
go: function(p){ |
| 250 |
|
| 251 |
|
| 252 |
var _t = this, |
| 253 |
i = _t.idx, |
| 254 |
pp = _t.panes, |
| 255 |
len = pp.length, |
| 256 |
idm = _t._idMap[p]; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
_t._resetWaitForEvent(); |
| 261 |
|
| 262 |
|
| 263 |
p = idm != null ? idm : (p || 0); |
| 264 |
p = p < len ? (p < 0 ? len-1 : p) : 0; |
| 265 |
|
| 266 |
|
| 267 |
if(p == i || _t.anim){ |
| 268 |
return null; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
var current = pp[i], |
| 273 |
next = pp[p]; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
domStyle.set(current.node, _zIndex, 2); |
| 278 |
domStyle.set(next.node, _zIndex, 1); |
| 279 |
|
| 280 |
|
| 281 |
var info = { |
| 282 |
current: current, |
| 283 |
next: next, |
| 284 |
rotator: _t |
| 285 |
}, |
| 286 |
|
| 287 |
|
| 288 |
anim = _t.anim = _t._transitions[next.trans](lang.mixin({ |
| 289 |
rotatorBox: _t._domNodeContentBox |
| 290 |
}, info, next.params)); |
| 291 |
|
| 292 |
if(anim){ |
| 293 |
|
| 294 |
var def = new Deferred(), |
| 295 |
ev = next.waitForEvent, |
| 296 |
|
| 297 |
h = aspect.after(anim, "onEnd", function(){ |
| 298 |
|
| 299 |
domStyle.set(current.node, { |
| 300 |
display: _noneStr, |
| 301 |
left: 0, |
| 302 |
opacity: 1, |
| 303 |
top: 0, |
| 304 |
zIndex: 0 |
| 305 |
}); |
| 306 |
|
| 307 |
h.remove(); |
| 308 |
_t.anim = null; |
| 309 |
_t.idx = p; |
| 310 |
|
| 311 |
|
| 312 |
if(current.onAfterOut){ current.onAfterOut(info); } |
| 313 |
if(next.onAfterIn){ next.onAfterIn(info); } |
| 314 |
|
| 315 |
_t.onUpdate("onAfterTransition"); |
| 316 |
|
| 317 |
if(!ev){ |
| 318 |
|
| 319 |
|
| 320 |
_t._resetWaitForEvent(); |
| 321 |
|
| 322 |
|
| 323 |
def.callback(); |
| 324 |
} |
| 325 |
}, true); |
| 326 |
|
| 327 |
|
| 328 |
_t.wfe = ev ? topic.subscribe(ev, function(){ |
| 329 |
_t._resetWaitForEvent(); |
| 330 |
def.callback(true); |
| 331 |
}) : null; |
| 332 |
|
| 333 |
_t.onUpdate("onBeforeTransition"); |
| 334 |
|
| 335 |
|
| 336 |
if(current.onBeforeOut){ current.onBeforeOut(info); } |
| 337 |
if(next.onBeforeIn){ next.onBeforeIn(info); } |
| 338 |
|
| 339 |
|
| 340 |
anim.play(); |
| 341 |
|
| 342 |
|
| 343 |
return def; |
| 344 |
} |
| 345 |
}, |
| 346 |
|
| 347 |
onUpdate: function(type, params){ |
| 348 |
|
| 349 |
|
| 350 |
topic.publish(this.id + "/rotator/update", type, this, params || {}); |
| 351 |
}, |
| 352 |
|
| 353 |
_initializePane: function(node, index) { |
| 354 |
|
| 355 |
|
| 356 |
var tp = this.transitionParams, |
| 357 |
q = { node: node, idx: index, params: lang.mixin({}, tp, eval("({ " + (domAttr.get(node, "transitionParams") || "") + " })")) }, |
| 358 |
r = q.trans = domAttr.get(node, "transition") || this.transition, |
| 359 |
tt = this._transitions, |
| 360 |
panes = this.panes, |
| 361 |
|
| 362 |
p = { |
| 363 |
left: 0, |
| 364 |
top: 0, |
| 365 |
position: "absolute", |
| 366 |
display: _noneStr |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
array.forEach(["id", "title", "duration", "waitForEvent"], function(a){ |
| 371 |
q[a] = domAttr.get(node, a); |
| 372 |
}); |
| 373 |
|
| 374 |
if(q.id){ |
| 375 |
this._idMap[q.id] = index; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
if(!tt[r] && !(tt[r] = lang.getObject(r))){ |
| 380 |
this._transitionWarn(r, q.trans = this.transition); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
if(this.idx == null || domAttr.get(node, "selected")){ |
| 385 |
if(this.idx != null){ |
| 386 |
domStyle.set(panes[this.idx].node, _displayStr, _noneStr); |
| 387 |
} |
| 388 |
this.idx = index; |
| 389 |
p.display = ""; |
| 390 |
} |
| 391 |
domStyle.set(node, p); |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
query("> script[type^='dojo/method']", node).orphan().forEach(function(s){ |
| 396 |
var e = domAttr.get(s, "event"); |
| 397 |
if(e){ |
| 398 |
q[e] = parser._functionFromScript(s); |
| 399 |
} |
| 400 |
}); |
| 401 |
|
| 402 |
|
| 403 |
panes.splice(index, 0, q); |
| 404 |
}, |
| 405 |
|
| 406 |
_resetWaitForEvent: function(){ |
| 407 |
|
| 408 |
|
| 409 |
if(this.wfe){ |
| 410 |
this.wfe.remove(); |
| 411 |
delete this.wfe; |
| 412 |
} |
| 413 |
}, |
| 414 |
|
| 415 |
control: function(action){ |
| 416 |
|
| 417 |
|
| 418 |
var args = lang._toArray(arguments), |
| 419 |
_t = this; |
| 420 |
args.shift(); |
| 421 |
|
| 422 |
_t._resetWaitForEvent(); |
| 423 |
|
| 424 |
if(_t[action]){ |
| 425 |
|
| 426 |
var def = _t[action].apply(_t, args); |
| 427 |
if(def){ |
| 428 |
def.addCallback(function(){ |
| 429 |
_t.onUpdate(action); |
| 430 |
}); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
_t.onManualChange(action); |
| 436 |
}else{ |
| 437 |
console.warn(_t.declaredClass, ' - Unsupported action "', action, '".'); |
| 438 |
} |
| 439 |
}, |
| 440 |
|
| 441 |
resize: function(width, height){ |
| 442 |
var b = this._domNodeContentBox = { w: width, h: height }; |
| 443 |
domGeometry.setContentSize(this._domNode, b); |
| 444 |
array.forEach(this.panes, function(p){ domGeometry.setContentSize(p.node, b); }); |
| 445 |
}, |
| 446 |
|
| 447 |
onManualChange: function(){ |
| 448 |
|
| 449 |
|
| 450 |
}, |
| 451 |
|
| 452 |
_transitionWarn: function(bt, dt){ |
| 453 |
console.warn(this.declaredClass, ' - Unable to find transition "', bt, '", defaulting to "', dt, '".'); |
| 454 |
} |
| 455 |
}); |
| 456 |
|
| 457 |
lang.setObject(_defaultTransition, function(args){ |
| 458 |
|
| 459 |
|
| 460 |
return new fx.Animation({ |
| 461 |
play: function(){ |
| 462 |
domStyle.set(args.current.node, _displayStr, _noneStr); |
| 463 |
domStyle.set(args.next.node, _displayStr, ""); |
| 464 |
this._fire("onEnd"); |
| 465 |
} |
| 466 |
}); |
| 467 |
}); |
| 468 |
|
| 469 |
return Rotator; |
| 470 |
}); |