accordion.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Generated by CoffeeScript 2.3.1
  2. (function() {
  3. // --------------------------------------------------------------
  4. // 模組註冊
  5. // --------------------------------------------------------------
  6. var Accordion;
  7. ts.register(Accordion = (function() {
  8. class Accordion {
  9. // --------------------------------------------------------------
  10. // 建構子
  11. // --------------------------------------------------------------
  12. constructor({
  13. settings,
  14. $allModules,
  15. $module,
  16. element: element1,
  17. listener,
  18. id
  19. }) {
  20. // --------------------------------------------------------------
  21. // 公開方法
  22. // --------------------------------------------------------------
  23. // Open 能夠開啟指定的手風琴項目。
  24. this.Open = this.Open.bind(this);
  25. // Close 能夠關閉指定的手風琴項目。
  26. this.Close = this.Close.bind(this);
  27. // Toggle 能夠切換指定的手風琴項目,當項目是關閉的時候開啟,反之亦然。
  28. this.Toggle = this.Toggle.bind(this);
  29. // CloseChildren 能夠關閉指定項目下的所有手風琴項目。
  30. this.CloseChildren = this.CloseChildren.bind(this);
  31. // CloseOthers 能夠關閉指定項目以外的所有項目。
  32. this.CloseOthers = this.CloseOthers.bind(this);
  33. // CloseAll 會關閉所有項目。
  34. this.CloseAll = this.CloseAll.bind(this);
  35. // --------------------------------------------------------------
  36. // 內部方法
  37. // --------------------------------------------------------------
  38. // open 會開啟指定的分頁籤。
  39. this.open = this.open.bind(this);
  40. // close 會關閉指定的分頁籤。
  41. this.close = this.close.bind(this);
  42. // closeOthers 會關閉指定頁籤以外的所有分頁籤。
  43. this.closeOthers = this.closeOthers.bind(this);
  44. // Trigger
  45. // triggerOpen 會呼叫開啟分頁籤時的回呼函式。
  46. this.triggerOpen = this.triggerOpen.bind(this);
  47. // triggerClose 會呼叫關閉分頁籤時的回呼函式。
  48. this.triggerClose = this.triggerClose.bind(this);
  49. // Is
  50. // isActive 會回傳一個分頁籤是否為開啟的布林值。
  51. this.isActive = this.isActive.bind(this);
  52. // Set
  53. // setActive 會將指定的分頁籤設置為 Active 啟用狀態。
  54. this.setActive = this.setActive.bind(this);
  55. // setHidden 會將指定的分頁籤設置為 Hidden 隱藏狀態。
  56. this.setHidden = this.setHidden.bind(this);
  57. // $Get
  58. // $getContent 會回傳一個 Tocas 包裹的指定分頁籤內容元素。
  59. this.$getContent = this.$getContent.bind(this);
  60. // Bind
  61. // bindEvents 會綁定元素事件。
  62. this.bindEvents = this.bindEvents.bind(this);
  63. // Event
  64. // eventClick 會監聽手風琴標題的點擊事件,並且切換該手風琴頁籤的展示。
  65. this.eventClick = this.eventClick.bind(this);
  66. // --------------------------------------------------------------
  67. // 生命週期
  68. // --------------------------------------------------------------
  69. // beforeCreate 會在模組被初始化之前呼叫。
  70. this.beforeCreate = this.beforeCreate.bind(this);
  71. // created 會在模組被初始化之後呼叫。
  72. this.created = this.created.bind(this);
  73. // beforeUpdate 會在 DOM 元素有異動時被呼叫。
  74. this.updated = this.updated.bind(this);
  75. // beforeDestroy 會在模組被摧毀之前呼叫。
  76. this.beforeDestroy = this.beforeDestroy.bind(this);
  77. // destroyed 會在模組被摧毀之後呼叫。
  78. this.destroyed = this.destroyed.bind(this);
  79. this.settings = settings;
  80. this.$allModules = $allModules;
  81. this.$module = $module;
  82. this.element = element1;
  83. this.listener = listener;
  84. this.id = id;
  85. // --------------------------------------------------------------
  86. // 區域變數
  87. // --------------------------------------------------------------
  88. this.data = {
  89. $title: this.$module.find(this.selectors.title),
  90. $content: this.$module.find(this.selectors.content)
  91. };
  92. }
  93. Open(index) {
  94. this.open(index);
  95. return this;
  96. }
  97. Close(index) {
  98. this.close(index);
  99. return this;
  100. }
  101. Toggle(index) {
  102. if (this.isActive(index)) {
  103. this.close(index);
  104. } else {
  105. this.open(index);
  106. }
  107. return this;
  108. }
  109. CloseChildren() {
  110. this.$module.find(this.selectors.accordion).accordion('close all');
  111. return this;
  112. }
  113. CloseOthers(index) {
  114. this.closeOthers(index);
  115. return this;
  116. }
  117. CloseAll() {
  118. var i, j, ref;
  119. for (i = j = 0, ref = this.data.$title.length - 1; (0 <= ref ? j <= ref : j >= ref); i = 0 <= ref ? ++j : --j) {
  120. this.close(i);
  121. }
  122. return this;
  123. }
  124. open(index) {
  125. this.setActive(index);
  126. this.triggerOpen(index);
  127. if (this.settings.exclusive) {
  128. return this.closeOthers(index);
  129. }
  130. }
  131. close(index) {
  132. this.setHidden(index);
  133. return this.triggerClose(index);
  134. }
  135. closeOthers(index) {
  136. var i, j, ref;
  137. for (i = j = 0, ref = this.data.$title.length - 1; (0 <= ref ? j <= ref : j >= ref); i = 0 <= ref ? ++j : --j) {
  138. if (i !== index) {
  139. this.close(i);
  140. }
  141. }
  142. return this;
  143. }
  144. triggerOpen(index) {
  145. var $content;
  146. $content = this.$getContent(index);
  147. this.listener.trigger(this.events.open, $content);
  148. return this.listener.trigger(this.events.change, $content);
  149. }
  150. triggerClose(index) {
  151. var $content;
  152. $content = this.$getContent(index);
  153. this.listener.trigger(this.events.close, $content);
  154. return this.listener.trigger(this.events.change, $content);
  155. }
  156. isActive(index) {
  157. return this.data.$title.eq(index).hasClass(this.classNames.active);
  158. }
  159. setActive(index) {
  160. this.data.$title.eq(index).addClass(this.classNames.active);
  161. return this.data.$content.eq(index).addClass(this.classNames.active);
  162. }
  163. setHidden(index) {
  164. this.data.$title.eq(index).removeClass(this.classNames.active);
  165. return this.data.$content.eq(index).removeClass(this.classNames.active);
  166. }
  167. $getContent(index) {
  168. return this.data.$content.eq(index);
  169. }
  170. bindEvents() {
  171. return this.listener.on(this.events.click, this.selectors.title, this.eventClick);
  172. }
  173. eventClick(element) {
  174. var index;
  175. index = this.data.$title.indexOf(element);
  176. if (this.isActive(index)) {
  177. if (this.settings.collapsible) {
  178. return this.close(index);
  179. }
  180. } else {
  181. return this.open(index);
  182. }
  183. }
  184. beforeCreate() {}
  185. created() {
  186. return this.bindEvents();
  187. }
  188. updated() {}
  189. beforeDestroy() {}
  190. destroyed() {}
  191. };
  192. // --------------------------------------------------------------
  193. // 變數與常數設置
  194. // --------------------------------------------------------------
  195. // 模組設定。
  196. Accordion.prototype.settings = {
  197. // 是否僅允許單個手風琴只有一個分頁能被打開。
  198. exclusive: true,
  199. // 展開的手風琴是否可以被關閉。
  200. collapsible: true,
  201. // 當手風琴被關閉時,是否一同閉合子手風琴。
  202. closeNested: true,
  203. // 當手風琴正在展開時所會呼叫的函式。
  204. onOpening: () => {},
  205. // 當手風琴展開時所會呼叫的函式。
  206. onOpen: () => {},
  207. // 當手風琴正在關閉時所會呼叫的函式。
  208. onClosing: () => {},
  209. // 當手風琴關閉時所會呼叫的函式。
  210. onClose: () => {},
  211. // 當手風琴被切換開關時所會呼叫的函式。
  212. onChange: () => {}
  213. };
  214. // 事件名稱。
  215. Accordion.prototype.events = {
  216. opening: 'opening',
  217. open: 'open',
  218. closing: 'closing',
  219. close: 'close',
  220. change: 'change',
  221. click: 'click'
  222. };
  223. // 樣式名稱。
  224. Accordion.prototype.classNames = {
  225. active: 'active'
  226. };
  227. // 選擇器名稱。
  228. Accordion.prototype.selectors = {
  229. title: '.title',
  230. content: '.content',
  231. accordion: '.ts.accordion',
  232. activeContent: '.active.content',
  233. active: '.active'
  234. };
  235. return Accordion;
  236. }).call(this));
  237. }).call(this);