Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aui-carousel-debug.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. AUI.add('aui-carousel', function(A) {
  2. var Lang = A.Lang,
  3. STR_BLANK = ' ',
  4. CAROUSEL = 'carousel',
  5. getCN = A.getClassName,
  6. CSS_ITEM = getCN(CAROUSEL, 'item'),
  7. CSS_ITEM_ACTIVE = getCN(CAROUSEL, 'item', 'active'),
  8. CSS_ITEM_TRANSITION = getCN(CAROUSEL, 'item', 'transition'),
  9. CSS_MENU_ACTIVE = getCN(CAROUSEL, 'menu', 'active'),
  10. CSS_MENU_INDEX = getCN(CAROUSEL, 'menu', 'index'),
  11. CSS_MENU_ITEM = getCN(CAROUSEL, 'menu', 'item'),
  12. CSS_MENU_NEXT = getCN(CAROUSEL, 'menu', 'next'),
  13. CSS_MENU_PLAY = getCN(CAROUSEL, 'menu', 'play'),
  14. CSS_MENU_PAUSE = getCN(CAROUSEL, 'menu', 'pause'),
  15. CSS_MENU_PREV = getCN(CAROUSEL, 'menu', 'prev'),
  16. CSS_MENU_ITEM_DEFAULT = [CSS_MENU_ITEM, CSS_MENU_INDEX].join(STR_BLANK),
  17. CSS_MENU_ITEM_ACTIVE = [CSS_MENU_ITEM, CSS_MENU_INDEX, CSS_MENU_ACTIVE].join(STR_BLANK),
  18. DOT = '.',
  19. SELECTOR_MENU_INDEX = DOT + CSS_MENU_INDEX,
  20. SELECTOR_MENU_NEXT = DOT + CSS_MENU_NEXT,
  21. SELECTOR_MENU_PAUSE = DOT + CSS_MENU_PAUSE,
  22. SELECTOR_MENU_PLAY = DOT + CSS_MENU_PLAY,
  23. SELECTOR_MENU_PLAY_OR_PAUSE = [SELECTOR_MENU_PLAY, SELECTOR_MENU_PAUSE].join(),
  24. SELECTOR_MENU_PREV = DOT + CSS_MENU_PREV,
  25. TPL_MENU = new A.Template(
  26. '<menu>',
  27. '<li><a class="', CSS_MENU_ITEM, ' ', CSS_MENU_PLAY, '"></a></li>',
  28. '<li><a class="', CSS_MENU_ITEM, ' ', CSS_MENU_PREV, '"></a></li>',
  29. '<tpl for="items">',
  30. '<li><a class="', CSS_MENU_ITEM, ' {[ $i == parent.activeIndex ? "', CSS_MENU_ITEM_ACTIVE, '" : "', CSS_MENU_ITEM_DEFAULT,'" ]}">{$index}</a></li>',
  31. '</tpl>',
  32. '<li><a class="', CSS_MENU_ITEM, ' ', CSS_MENU_NEXT, '"></a></li>',
  33. '</menu>'
  34. ),
  35. UI_SRC = A.Widget.UI_SRC,
  36. MAP_EVENT_INFO = {
  37. src: UI_SRC
  38. };
  39. var Carousel = A.Component.create(
  40. {
  41. NAME: CAROUSEL,
  42. ATTRS: {
  43. activeIndex: {
  44. value: 0,
  45. setter: '_setActiveIndex'
  46. },
  47. animationTime: {
  48. value: 0.5
  49. },
  50. intervalTime: {
  51. value: 0.75
  52. },
  53. itemSelector: {
  54. value: '>*'
  55. },
  56. nodeMenu: {
  57. value: null,
  58. setter: '_setNodeMenu'
  59. },
  60. nodeMenuItemSelector: {
  61. value: DOT + CSS_MENU_ITEM
  62. },
  63. playing: {
  64. value: true
  65. }
  66. },
  67. prototype: {
  68. animation: null,
  69. nodeSelection: null,
  70. nodeMenu: null,
  71. initializer: function() {
  72. var instance = this;
  73. instance.animation = new A.Anim(
  74. {
  75. duration: instance.get('animationTime'),
  76. to: {
  77. opacity: 1
  78. }
  79. }
  80. );
  81. },
  82. renderUI: function() {
  83. var instance = this;
  84. instance._updateNodeSelection();
  85. instance.nodeMenu = instance.get('nodeMenu');
  86. instance._updateMenuNodes();
  87. },
  88. bindUI: function() {
  89. var instance = this;
  90. instance.after(
  91. {
  92. activeIndexChange: instance._afterActiveIndexChange,
  93. animationTimeChange: instance._afterAnimationTimeChange,
  94. itemSelectorChange: instance._afterItemSelectorChange,
  95. intervalTimeChange: instance._afterIntervalTimeChange,
  96. nodeMenuItemSelector: instance._afterNodeMenuItemSelectorChange,
  97. playingChange: instance._afterPlayingChange
  98. }
  99. );
  100. instance._bindMenu();
  101. if (instance.get('playing') === true) {
  102. instance._afterPlayingChange(
  103. {
  104. prevVal: false,
  105. newVal: true
  106. }
  107. );
  108. }
  109. },
  110. syncUI: function() {
  111. var instance = this;
  112. instance._uiSetActiveIndex(instance.get('activeIndex'));
  113. },
  114. item: function(val) {
  115. var instance = this;
  116. instance.set('activeIndex', val);
  117. },
  118. next: function() {
  119. var instance = this;
  120. instance._updateIndexNext();
  121. },
  122. pause: function() {
  123. var instance = this;
  124. instance.set('playing', false);
  125. },
  126. play: function() {
  127. var instance = this;
  128. instance.set('playing', true);
  129. },
  130. prev: function() {
  131. var instance = this;
  132. instance._updateIndexPrev();
  133. },
  134. _afterActiveIndexChange: function(event) {
  135. var instance = this;
  136. instance._uiSetActiveIndex(
  137. event.newVal,
  138. {
  139. prevVal: event.prevVal,
  140. animate: instance.get('playing'),
  141. src: event.src
  142. }
  143. );
  144. },
  145. _afterAnimationTimeChange: function(event) {
  146. var instance = this;
  147. instance.animation.set('duration', event.newVal);
  148. },
  149. _afterItemSelectorChange: function(event) {
  150. var instance = this;
  151. instance._updateNodeSelection();
  152. },
  153. _afterNodeMenuItemSelectorChange: function(event) {
  154. var instance = this;
  155. instance.nodeMenuItemSelector = event.newVal;
  156. instance._updateMenuNodes();
  157. },
  158. _afterIntervalTimeChange: function(event) {
  159. var instance = this;
  160. instance._clearIntervalRotationTask();
  161. instance._createIntervalRotationTask();
  162. },
  163. _afterPlayingChange: function(event) {
  164. var instance = this;
  165. var menuPlayItem = instance.nodeMenu.one(SELECTOR_MENU_PLAY_OR_PAUSE);
  166. var playing = event.newVal;
  167. var fromClass = CSS_MENU_PAUSE;
  168. var toClass = CSS_MENU_PLAY;
  169. var rotationTaskMethod = '_clearIntervalRotationTask';
  170. if (playing) {
  171. fromClass = CSS_MENU_PLAY;
  172. toClass = CSS_MENU_PAUSE;
  173. rotationTaskMethod = '_createIntervalRotationTask';
  174. }
  175. instance[rotationTaskMethod]();
  176. if (menuPlayItem) {
  177. menuPlayItem.replaceClass(fromClass, toClass);
  178. }
  179. },
  180. _bindMenu: function() {
  181. var instance = this;
  182. var menu = instance.nodeMenu;
  183. var nodeMenuItemSelector = instance.get('nodeMenuItemSelector');
  184. menu.delegate('click', instance._onClickDelegate, nodeMenuItemSelector, instance);
  185. instance.nodeMenuItemSelector = nodeMenuItemSelector;
  186. },
  187. _clearIntervalRotationTask: function() {
  188. var instance = this;
  189. clearInterval(instance._intervalRotationTask);
  190. },
  191. _createIndexRandom: function() {
  192. var instance = this;
  193. return Math.ceil(Math.random() * instance.nodeSelection.size()) - 1;
  194. },
  195. _createIntervalRotationTask: function() {
  196. var instance = this;
  197. instance._clearIntervalRotationTask();
  198. instance._intervalRotationTask = setInterval(
  199. function() {
  200. instance._updateIndexNext(
  201. {
  202. animate: true
  203. }
  204. );
  205. },
  206. instance.get('intervalTime') * 1000
  207. );
  208. },
  209. _onAnimationEnd: function(event, newImage, oldImage, newMenuItem, oldMenuItem) {
  210. var instance = this;
  211. if (oldImage) {
  212. oldImage.removeClass(CSS_ITEM_TRANSITION);
  213. }
  214. newImage.setStyle('opacity', '1');
  215. },
  216. _onAnimationStart: function(event, newImage, oldImage, newMenuItem, oldMenuItem) {
  217. var instance = this;
  218. newImage.addClass(CSS_ITEM_ACTIVE);
  219. if (newMenuItem) {
  220. newMenuItem.addClass(CSS_MENU_ACTIVE);
  221. }
  222. if (oldImage) {
  223. oldImage.replaceClass(CSS_ITEM_ACTIVE, CSS_ITEM_TRANSITION);
  224. }
  225. if (oldMenuItem) {
  226. oldMenuItem.removeClass(CSS_MENU_ACTIVE);
  227. }
  228. },
  229. _onClickDelegate: function(event) {
  230. var instance = this;
  231. event.preventDefault();
  232. var currentTarget = event.currentTarget;
  233. var handler;
  234. if (currentTarget.hasClass(CSS_MENU_INDEX)) {
  235. handler = instance._onMenuItemClick;
  236. }
  237. else if (currentTarget.hasClass(CSS_MENU_PREV)) {
  238. handler = instance._updateIndexPrev;
  239. }
  240. else if (currentTarget.hasClass(CSS_MENU_NEXT)) {
  241. handler = instance._updateIndexNext;
  242. }
  243. else if (currentTarget.test(SELECTOR_MENU_PLAY_OR_PAUSE)) {
  244. handler = instance._onMenuPlayClick;
  245. }
  246. if (handler) {
  247. handler.apply(instance, arguments);
  248. }
  249. },
  250. _onMenuItemClick: function(event) {
  251. var instance = this;
  252. event.preventDefault();
  253. var newIndex = instance.menuNodes.indexOf(event.currentTarget);
  254. instance.set('activeIndex', newIndex, MAP_EVENT_INFO);
  255. },
  256. _onMenuPlayClick: function(event) {
  257. var instance = this;
  258. this.set('playing', !this.get('playing'));
  259. },
  260. _renderMenu: function() {
  261. var instance = this;
  262. var menu = TPL_MENU.render(
  263. {
  264. items: instance.nodeSelection.getDOM(),
  265. activeIndex: instance.get('activeIndex')
  266. }
  267. );
  268. instance.get('contentBox').appendChild(menu);
  269. return menu;
  270. },
  271. _setActiveIndex: function(val) {
  272. var instance = this;
  273. if (val == 'rand') {
  274. val = instance._createIndexRandom();
  275. }
  276. else {
  277. val = Math.max(Math.min(val, instance.nodeSelection.size()), -1);
  278. }
  279. return val;
  280. },
  281. _setNodeMenu: function(val) {
  282. var instance = this;
  283. return A.one(val) || instance._renderMenu();
  284. },
  285. _uiSetActiveIndex: function(newVal, objOptions) {
  286. var instance = this;
  287. var oldImage = null;
  288. var oldMenuItem = null;
  289. var onStart = null;
  290. var onEnd = null;
  291. var newImage = instance.nodeSelection.item(newVal);
  292. var menuNodes = instance.menuNodes;
  293. var newMenuItem = menuNodes.item(newVal);
  294. instance.animation.set('node', newImage);
  295. if (objOptions && !Lang.isUndefined(objOptions.prevVal)) {
  296. var prevVal = objOptions.prevVal;
  297. newImage.setStyle('opacity', '0');
  298. oldMenuItem = menuNodes.item(prevVal);
  299. oldImage = instance.nodeSelection.item(prevVal);
  300. oldImage.replaceClass(CSS_ITEM_ACTIVE, CSS_ITEM_TRANSITION);
  301. instance.animation.stop();
  302. }
  303. else {
  304. newImage.addClass(CSS_ITEM_ACTIVE);
  305. newImage.setStyle('opacity', '1');
  306. }
  307. onStart = instance.animation.on(
  308. 'start',
  309. function(event) {
  310. instance._onAnimationStart(event, newImage, oldImage, newMenuItem, oldMenuItem);
  311. onStart.detach();
  312. }
  313. );
  314. onEnd = instance.animation.on(
  315. 'end',
  316. function(event) {
  317. instance._onAnimationEnd(event, newImage, oldImage, newMenuItem, oldMenuItem);
  318. onEnd.detach();
  319. }
  320. );
  321. if (objOptions) {
  322. if (objOptions.animate) {
  323. instance.animation.run();
  324. }
  325. else {
  326. instance.animation.fire('start');
  327. instance.animation.fire('end');
  328. }
  329. if (objOptions.src == UI_SRC && objOptions.animate) {
  330. instance._createIntervalRotationTask();
  331. }
  332. }
  333. },
  334. _updateIndexNext: function(options) {
  335. var instance = this;
  336. var currentIndex = instance.get('activeIndex');
  337. var nodeSelectionSize = instance.nodeSelection.size();
  338. var newIndex = currentIndex + 1;
  339. if (newIndex > (nodeSelectionSize - 1)) {
  340. newIndex = 0;
  341. }
  342. if (options) {
  343. options.src = UI_SRC;
  344. }
  345. instance.set('activeIndex', newIndex, options);
  346. },
  347. _updateIndexPrev: function(options) {
  348. var instance = this;
  349. var currentIndex = instance.get('activeIndex');
  350. var newIndex = currentIndex - 1;
  351. if (newIndex < 0) {
  352. newIndex = instance.nodeSelection.size() - 1;
  353. }
  354. if (options) {
  355. options.src = UI_SRC;
  356. }
  357. instance.set('activeIndex', newIndex, options);
  358. },
  359. _updateMenuNodes: function() {
  360. var instance = this;
  361. instance.menuNodes = instance.nodeMenu.all(SELECTOR_MENU_INDEX);
  362. },
  363. _updateNodeSelection: function() {
  364. var instance = this;
  365. var itemSelector = instance.get('itemSelector');
  366. var nodeSelection = instance.get('contentBox').all(itemSelector);
  367. nodeSelection.addClass(CSS_ITEM);
  368. instance.nodeSelection = nodeSelection;
  369. },
  370. _intervalRotationTask: null
  371. }
  372. }
  373. );
  374. A.Carousel = Carousel;
  375. }, '@VERSION@' ,{skinnable:true, requires:['aui-base','aui-template','anim']});