Debian Codegraph @

dojox/widget/Rotator.js

raw symbols SCIP
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 // build friendly strings
23 var _defaultTransition = "dojox.widget.rotator.swap", // please do NOT change
24 _defaultTransitionDuration = 500,
25 _displayStr = "display",
26 _noneStr = "none",
27 _zIndex = "zIndex";
28
29 var Rotator = declare("dojox.widget.Rotator", null, {
30 // summary:
31 // A widget for rotating through child nodes using transitions.
32 //
33 // description:
34 // A small, fast, extensible, awesome rotator that cycles, with transitions,
35 // through panes (child nodes) displaying only one at a time and ties into
36 // controllers used to change state.
37 //
38 // The Rotator does not rely on dijit. It is designed to be as lightweight
39 // as possible. Controllers and transitions have been externalized
40 // so builds can be as optimized with only the components you want to use.
41 //
42 // For best results, each rotator pane should be the same height and width as
43 // the Rotator container node and consider setting overflow to hidden.
44 // While the Rotator will accept any DOM node for a rotator pane, a block
45 // element or element with display:block is recommended.
46 //
47 // Note: When the Rotator begins, it does not transition the first pane.
48 //
49 // subscribed topics:
50 //
51 // [id]/rotator/control - Controls the Rotator
52 // Parameters:
53 //
54 // - /*string*/ action - The name of a method of the Rotator to run
55 // - /*anything?*/ args - One or more arguments to pass to the action
56 //
57 // published topics:
58 //
59 // [id]/rotator/update - Notifies controllers that a pane or state has changed.
60 // Parameters:
61 //
62 // - /*string*/ type - the type of notification
63 // - /*dojox.widget.Rotator*/ rotator - the rotator instance
64 // - /*object?*/ params - params
65 //
66 // declarative dojo/method events (per pane):
67 //
68 // - onBeforeIn - Fired before the transition in starts.
69 // - onAfterIn - Fired after the transition in ends.
70 // - onBeforeOut - Fired before the transition out starts.
71 // - onAfterOut - Fired after the transition out ends.
72 //
73 // example:
74 // | <div dojoType="dojox.widget.Rotator">
75 // | <div>Pane 1!</div>
76 // | <div>Pane 2!</div>
77 // | </div>
78 //
79 // example:
80 // | <script type="text/javascript">
81 // | require("dojo/parser", "dojo/domReady!", "dojox/widget/Rotator", "dojox/widget/rotator/Fade"],
82 // | function(parser) { parser.parse(); });
83 // | </script>
84 // | <div dojoType="dojox/widget/Rotator" transition="dojox/widget/rotator/crossFade">
85 // | <div>Pane 1!</div>
86 // | <div>Pane 2!</div>
87 // | </div>
88
89 // transition: string
90 // The name of a function that is passed two panes nodes and a duration,
91 // then returns a dojo.Animation object. The default value is
92 // "dojox.widget.rotator.swap".
93 transition: _defaultTransition,
94
95 // transitionParams: string
96 // Parameters for the transition. The string is read in and eval'd as an
97 // object. If the duration is absent, the default value will be used.
98 transitionParams: "duration:" + _defaultTransitionDuration,
99
100 // panes: array
101 // Array of panes to be created in the Rotator. Each array element
102 // will be passed as attributes to a html.create() call.
103 panes: null,
104
105 constructor: function(/*Object*/params, /*DomNode|string*/node){
106 // summary:
107 // Initializes the panes and events.
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); // we are going to assume the rotator will not be changing size
117
118 // if we don't have an id, then generate one
119 _t.id = node.id || (new Date()).getTime();
120
121 // force the rotator DOM node to a relative position and attach the container node to it
122 if(domStyle.get(node, "position") == "static"){
123 domStyle.set(node, "position", "relative");
124 }
125
126 // create our object for caching transition objects
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 // clean up the transition params
134 if(!tp.duration){
135 tp.duration = _defaultTransitionDuration;
136 }
137
138 // if there are any panes being passed in, add them to this node
139 array.forEach(_t.panes, function(p){
140 domConstruct.create("div", p, node);
141 });
142
143 // zero out our panes array to store the real pane instance
144 _t.panes = [];
145
146 // find and initialize the panes
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(/*DomNode*/node, /*number?*/index){
155 // summary:
156 // Inserts a new pane into the rotator at a given index. If no index is
157 // given, the new pane is inserted at the end of the pane list.
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(/*DomNode|number*/nodeOrIndex){
178 // summary:
179 // Removes a pane from the rotator.
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 // summary:
231 // Destroys the Rotator and its DOM node.
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 // summary:
239 // Transitions the Rotator to the next pane.
240 return this.go(this.idx + 1);
241 },
242
243 prev: function(){
244 // summary:
245 // Transitions the Rotator to the previous pane.
246 return this.go(this.idx - 1);
247 },
248
249 go: function(/*int|string?*/p){
250 // summary:
251 // Transitions the Rotator to the specified pane index.
252 var _t = this,
253 i = _t.idx,
254 pp = _t.panes,
255 len = pp.length,
256 idm = _t._idMap[p];
257
258 // we gotta move on, so if the current pane is waiting for an event, just
259 // ignore it and clean up
260 _t._resetWaitForEvent();
261
262 // determine the next index and set it to idx for the next go to
263 p = idm != null ? idm : (p || 0);
264 p = p < len ? (p < 0 ? len-1 : p) : 0;
265
266 // if we're already on the requested pane or still transitioning, then return
267 if(p == i || _t.anim){
268 return null;
269 }
270
271 // get the current and next panes
272 var current = pp[i],
273 next = pp[p];
274
275 // adjust the zIndexes so our animations look good... this must be done before
276 // the animation is created so the animation could override it if necessary
277 domStyle.set(current.node, _zIndex, 2);
278 domStyle.set(next.node, _zIndex, 1);
279
280 // info object passed to animations and onIn/Out events
281 var info = {
282 current: current,
283 next: next,
284 rotator: _t
285 },
286
287 // get the transition
288 anim = _t.anim = _t._transitions[next.trans](lang.mixin({
289 rotatorBox: _t._domNodeContentBox
290 }, info, next.params));
291
292 if(anim){
293 // create the deferred that we'll be returning
294 var def = new Deferred(),
295 ev = next.waitForEvent,
296
297 h = aspect.after(anim, "onEnd", function(){
298 // reset the node styles
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 // fire end events
312 if(current.onAfterOut){ current.onAfterOut(info); }
313 if(next.onAfterIn){ next.onAfterIn(info); }
314
315 _t.onUpdate("onAfterTransition");
316
317 if(!ev){
318 // if there is a previous waitForEvent, then we need to make
319 // sure it gets unsubscribed
320 _t._resetWaitForEvent();
321
322 // animation is all done, fire the deferred callback.
323 def.callback();
324 }
325 }, true);
326
327 // if we're waiting for an event, subscribe to it so we know when to continue
328 _t.wfe = ev ? topic.subscribe(ev, function(){
329 _t._resetWaitForEvent();
330 def.callback(true);
331 }) : null;
332
333 _t.onUpdate("onBeforeTransition");
334
335 // fire start events
336 if(current.onBeforeOut){ current.onBeforeOut(info); }
337 if(next.onBeforeIn){ next.onBeforeIn(info); }
338
339 // play the animation
340 anim.play();
341
342 // return the deferred
343 return def; /*Deferred*/
344 }
345 },
346
347 onUpdate: function(/*string*/type, /*object?*/params){
348 // summary:
349 // Send a notification to all controllers with the state of the rotator.
350 topic.publish(this.id + "/rotator/update", type, this, params || {});
351 },
352
353 _initializePane: function(/*node*/node, /*number*/index) {
354 // summary:
355 // Initializes a new pane node.
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 // default styles to apply to all the container node and rotator's panes
362 p = {
363 left: 0,
364 top: 0,
365 position: "absolute",
366 display: _noneStr
367 };
368
369 // cache each pane's title, duration, and waitForEvent attributes
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 // cache the transition function
379 if(!tt[r] && !(tt[r] = lang.getObject(r))){
380 this._transitionWarn(r, q.trans = this.transition);
381 }
382
383 // find the selected pane and initialize styles
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 // check for any declarative script blocks
394 // TODO: The way declarative scripts are handled here needs to be updated to no conflict with dojo/parser
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 // add this pane to the array of panes
403 panes.splice(index, 0, q);
404 },
405
406 _resetWaitForEvent: function(){
407 // summary:
408 // If there is a waitForEvent pending, kill it.
409 if(this.wfe){
410 this.wfe.remove();
411 delete this.wfe;
412 }
413 },
414
415 control: function(/*string*/action){
416 // summary:
417 // Dispatches an action, first to this engine, then to the Rotator.
418 var args = lang._toArray(arguments),
419 _t = this;
420 args.shift();
421
422 _t._resetWaitForEvent();
423
424 if(_t[action]){
425 // action exists, so call it and fire deferred if applicable
426 var def = _t[action].apply(_t, args);
427 if(def){
428 def.addCallback(function(){
429 _t.onUpdate(action);
430 });
431 }
432
433 // since this action was triggered by a controller, we assume this was a
434 // manual action, so check if we should pause
435 _t.onManualChange(action);
436 }else{
437 console.warn(_t.declaredClass, ' - Unsupported action "', action, '".');
438 }
439 },
440
441 resize: function(/*int*/width, /*int*/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 // summary:
449 // Stub function that can be overriden or connected to.
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(/*Object*/args){
458 // summary:
459 // The default rotator transition which swaps two panes.
460 return new fx.Animation({ // dojo.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 });