Rotator.js 11 KB

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