Rotator.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.widget.Rotator"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.widget.Rotator"] = true;
  8. dojo.provide("dojox.widget.Rotator");
  9. dojo.require("dojo.parser");
  10. (function(d){
  11. // build friendly strings
  12. var _defaultTransition = "dojox.widget.rotator.swap", // please do NOT change
  13. _defaultTransitionDuration = 500,
  14. _displayStr = "display",
  15. _noneStr = "none",
  16. _zIndex = "zIndex";
  17. d.declare("dojox.widget.Rotator", null, {
  18. // summary:
  19. // A widget for rotating through child nodes using transitions.
  20. //
  21. // description:
  22. // A small, fast, extensible, awesome rotator that cycles, with transitions,
  23. // through panes (child nodes) displaying only one at a time and ties into
  24. // controllers used to change state.
  25. //
  26. // The Rotator does not rely on dijit. It is designed to be as lightweight
  27. // as possible. Controllers and transitions have been externalized
  28. // so builds can be as optimized with only the components you want to use.
  29. //
  30. // For best results, each rotator pane should be the same height and width as
  31. // the Rotator container node and consider setting overflow to hidden.
  32. // While the Rotator will accept any DOM node for a rotator pane, a block
  33. // element or element with display:block is recommended.
  34. //
  35. // Note: When the Rotator begins, it does not transition the first pane.
  36. //
  37. // subscribed topics:
  38. // [id]/rotator/control - Controls the Rotator
  39. // Parameters:
  40. // /*string*/ action - The name of a method of the Rotator to run
  41. // /*anything?*/ args - One or more arguments to pass to the action
  42. //
  43. // published topics:
  44. // [id]/rotator/update - Notifies controllers that a pane or state has changed.
  45. // Parameters:
  46. // /*string*/ type - the type of notification
  47. // /*dojox.widget.Rotator*/ rotator
  48. // - the rotator instance
  49. // /*object?*/ params - params
  50. //
  51. // declarative dojo/method events (per pane):
  52. // onBeforeIn - Fired before the transition in starts.
  53. // onAfterIn - Fired after the transition in ends.
  54. // onBeforeOut - Fired before the transition out starts.
  55. // onAfterOut - Fired after the transition out ends.
  56. //
  57. // example:
  58. // | <div dojoType="dojox.widget.Rotator">
  59. // | <div>Pane 1!</div>
  60. // | <div>Pane 2!</div>
  61. // | </div>
  62. //
  63. // example:
  64. // | <script type="text/javascript">
  65. // | dojo.require("dojox.widget.rotator.Fade");
  66. // | </script>
  67. // | <div dojoType="dojox.widget.Rotator" transition="dojox.widget.rotator.crossFade">
  68. // | <div>Pane 1!</div>
  69. // | <div>Pane 2!</div>
  70. // | </div>
  71. // transition: string
  72. // The name of a function that is passed two panes nodes and a duration,
  73. // then returns a dojo.Animation object. The default value is
  74. // "dojox.widget.rotator.swap".
  75. transition: _defaultTransition,
  76. // transitionParams: string
  77. // Parameters for the transition. The string is read in and eval'd as an
  78. // object. If the duration is absent, the default value will be used.
  79. transitionParams: "duration:" + _defaultTransitionDuration,
  80. // panes: array
  81. // Array of panes to be created in the Rotator. Each array element
  82. // will be passed as attributes to a dojo.create() call.
  83. panes: null,
  84. constructor: function(/*Object*/params, /*DomNode|string*/node){
  85. // summary:
  86. // Initializes the panes and events.
  87. d.mixin(this, params);
  88. var _t = this,
  89. t = _t.transition,
  90. tt = _t._transitions = {},
  91. idm = _t._idMap = {},
  92. tp = _t.transitionParams = eval("({ " + _t.transitionParams + " })"),
  93. node = _t._domNode = dojo.byId(node),
  94. cb = _t._domNodeContentBox = d.contentBox(node), // we are going to assume the rotator will not be changing size
  95. // default styles to apply to all the container node and rotator's panes
  96. p = {
  97. left: 0,
  98. top: 0
  99. },
  100. warn = function(bt, dt){
  101. console.warn(_t.declaredClass, ' - Unable to find transition "', bt, '", defaulting to "', dt, '".');
  102. };
  103. // if we don't have an id, then generate one
  104. _t.id = node.id || (new Date()).getTime();
  105. // force the rotator DOM node to a relative position and attach the container node to it
  106. if(d.style(node, "position") == "static"){
  107. d.style(node, "position", "relative");
  108. }
  109. // create our object for caching transition objects
  110. tt[t] = d.getObject(t);
  111. if(!tt[t]){
  112. warn(t, _defaultTransition);
  113. tt[_t.transition = _defaultTransition] = d.getObject(_defaultTransition);
  114. }
  115. // clean up the transition params
  116. if(!tp.duration){
  117. tp.duration = _defaultTransitionDuration;
  118. }
  119. // if there are any panes being passed in, add them to this node
  120. d.forEach(_t.panes, function(p){
  121. d.create("div", p, node);
  122. });
  123. // zero out our panes array to store the real pane instance
  124. var pp = _t.panes = [];
  125. // find and initialize the panes
  126. d.query(">", node).forEach(function(n, i){
  127. var q = { node: n, idx: i, params: d.mixin({}, tp, eval("({ " + (d.attr(n, "transitionParams") || "") + " })")) },
  128. r = q.trans = d.attr(n, "transition") || _t.transition;
  129. // cache each pane's title, duration, and waitForEvent attributes
  130. d.forEach(["id", "title", "duration", "waitForEvent"], function(a){
  131. q[a] = d.attr(n, a);
  132. });
  133. if(q.id){
  134. idm[q.id] = i;
  135. }
  136. // cache the transition function
  137. if(!tt[r] && !(tt[r] = d.getObject(r))){
  138. warn(r, q.trans = _t.transition);
  139. }
  140. p.position = "absolute";
  141. p.display = _noneStr;
  142. // find the selected pane and initialize styles
  143. if(_t.idx == null || d.attr(n, "selected")){
  144. if(_t.idx != null){
  145. d.style(pp[_t.idx].node, _displayStr, _noneStr);
  146. }
  147. _t.idx = i;
  148. p.display = "";
  149. }
  150. d.style(n, p);
  151. // check for any declarative script blocks
  152. d.query("> script[type^='dojo/method']", n).orphan().forEach(function(s){
  153. var e = d.attr(s, "event");
  154. if(e){
  155. q[e] = d.parser._functionFromScript(s);
  156. }
  157. });
  158. // add this pane to the array of panes
  159. pp.push(q);
  160. });
  161. _t._controlSub = d.subscribe(_t.id + "/rotator/control", _t, "control");
  162. },
  163. destroy: function(){
  164. // summary:
  165. // Destroys the Rotator and its DOM node.
  166. d.forEach([this._controlSub, this.wfe], d.unsubscribe);
  167. d.destroy(this._domNode);
  168. },
  169. next: function(){
  170. // summary:
  171. // Transitions the Rotator to the next pane.
  172. return this.go(this.idx + 1);
  173. },
  174. prev: function(){
  175. // summary:
  176. // Transitions the Rotator to the previous pane.
  177. return this.go(this.idx - 1);
  178. },
  179. go: function(/*int|string?*/p){
  180. // summary:
  181. // Transitions the Rotator to the specified pane index.
  182. var _t = this,
  183. i = _t.idx,
  184. pp = _t.panes,
  185. len = pp.length,
  186. idm = _t._idMap[p];
  187. // we gotta move on, so if the current pane is waiting for an event, just
  188. // ignore it and clean up
  189. _t._resetWaitForEvent();
  190. // determine the next index and set it to idx for the next go to
  191. p = idm != null ? idm : (p || 0);
  192. p = p < len ? (p < 0 ? len-1 : p) : 0;
  193. // if we're already on the requested pane or still transitioning, then return
  194. if(p == i || _t.anim){
  195. return null;
  196. }
  197. // get the current and next panes
  198. var current = pp[i],
  199. next = pp[p];
  200. // adjust the zIndexes so our animations look good... this must be done before
  201. // the animation is created so the animation could override it if necessary
  202. d.style(current.node, _zIndex, 2);
  203. d.style(next.node, _zIndex, 1);
  204. // info object passed to animations and onIn/Out events
  205. var info = {
  206. current: current,
  207. next: next,
  208. rotator: _t
  209. },
  210. // get the transition
  211. anim = _t.anim = _t._transitions[next.trans](d.mixin({
  212. rotatorBox: _t._domNodeContentBox
  213. }, info, next.params));
  214. if(anim){
  215. // create the deferred that we'll be returning
  216. var def = new d.Deferred(),
  217. ev = next.waitForEvent,
  218. h = d.connect(anim, "onEnd", function(){
  219. // reset the node styles
  220. d.style(current.node, {
  221. display: _noneStr,
  222. left: 0,
  223. opacity: 1,
  224. top: 0,
  225. zIndex: 0
  226. });
  227. d.disconnect(h);
  228. _t.anim = null;
  229. _t.idx = p;
  230. // fire end events
  231. if(current.onAfterOut){ current.onAfterOut(info); }
  232. if(next.onAfterIn){ next.onAfterIn(info); }
  233. _t.onUpdate("onAfterTransition");
  234. if(!ev){
  235. // if there is a previous waitForEvent, then we need to make
  236. // sure it gets unsubscribed
  237. _t._resetWaitForEvent();
  238. // animation is all done, fire the deferred callback.
  239. def.callback();
  240. }
  241. });
  242. // if we're waiting for an event, subscribe to it so we know when to continue
  243. _t.wfe = ev ? d.subscribe(ev, function(){
  244. _t._resetWaitForEvent();
  245. def.callback(true);
  246. }) : null;
  247. _t.onUpdate("onBeforeTransition");
  248. // fire start events
  249. if(current.onBeforeOut){ current.onBeforeOut(info); }
  250. if(next.onBeforeIn){ next.onBeforeIn(info); }
  251. // play the animation
  252. anim.play();
  253. // return the deferred
  254. return def; /*Deferred*/
  255. }
  256. },
  257. onUpdate: function(/*string*/type, /*object?*/params){
  258. // summary:
  259. // Send a notification to all controllers with the state of the rotator.
  260. d.publish(this.id + "/rotator/update", [type, this, params || {}]);
  261. },
  262. _resetWaitForEvent: function(){
  263. // summary:
  264. // If there is a waitForEvent pending, kill it.
  265. if(this.wfe){
  266. d.unsubscribe(this.wfe);
  267. this.wfe = null;
  268. }
  269. },
  270. control: function(/*string*/action){
  271. // summary:
  272. // Dispatches an action, first to this engine, then to the Rotator.
  273. var args = d._toArray(arguments),
  274. _t = this;
  275. args.shift();
  276. _t._resetWaitForEvent();
  277. if(_t[action]){
  278. // action exists, so call it and fire deferred if applicable
  279. var def = _t[action].apply(_t, args);
  280. if(def){
  281. def.addCallback(function(){
  282. _t.onUpdate(action);
  283. });
  284. }
  285. // since this action was triggered by a controller, we assume this was a
  286. // manual action, so check if we should pause
  287. _t.onManualChange(action);
  288. }else{
  289. console.warn(_t.declaredClass, ' - Unsupported action "', action, '".');
  290. }
  291. },
  292. resize: function(/*int*/width, /*int*/height){
  293. var b = this._domNodeContentBox = { w: width, h: height };
  294. d.contentBox(this._domNode, b);
  295. d.forEach(this.panes, function(p){ d.contentBox(p.node, b); });
  296. },
  297. onManualChange: function(){
  298. // summary:
  299. // Stub function that can be overriden or connected to.
  300. }
  301. });
  302. d.setObject(_defaultTransition, function(/*Object*/args){
  303. // summary:
  304. // The default rotator transition which swaps two panes.
  305. return new d._Animation({ // dojo.Animation
  306. play: function(){
  307. d.style(args.current.node, _displayStr, _noneStr);
  308. d.style(args.next.node, _displayStr, "");
  309. this._fire("onEnd");
  310. }
  311. });
  312. });
  313. })(dojo);
  314. }