sidebar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Generated by CoffeeScript 2.0.0-beta4
  2. (function() {
  3. // ------------------------------------------------------------------------
  4. // 變數與常數設置
  5. // ------------------------------------------------------------------------
  6. // 模組名稱。
  7. var Attribute, ClassName, Device, EVENT_NAMESPACE, Error, Event, MODULE_NAMESPACE, NAME, Selector, Settings, Transition, Visibility;
  8. NAME = 'sidebar';
  9. // 模組事件鍵名。
  10. EVENT_NAMESPACE = `.${NAME}`;
  11. // 模組命名空間。
  12. MODULE_NAMESPACE = `module-${NAME}`;
  13. // 模組設定。
  14. Settings = {
  15. // 消音所有提示,甚至是錯誤訊息。
  16. silent: false,
  17. // 顯示除錯訊息。
  18. debug: true,
  19. // 監聽 DOM 結構異動並自動重整快取。
  20. observeChanges: true,
  21. // 是否要在側邊欄出現的時候淡化頁面。
  22. dimPage: true,
  23. // 是否允許使用者點擊頁面關閉側邊欄。
  24. closable: true,
  25. // 是否要在側邊欄出現的時候所動頁面捲軸滾動。
  26. scrollLock: true,
  27. // 側邊欄的出場效果。(`overlay` 為覆蓋、`push` 為推出、`squeeze` 為擠壓)
  28. transition: 'overlay',
  29. // 預設的顯示模式,設為 `auto` 的時候出場效果會被覆蓋。(`auto` 為電腦常駐、行動裝置隱藏、`hidden` 為預設隱藏)
  30. visibility: 'hidden',
  31. // 當側邊欄剛出現時所會呼叫的回呼函式。
  32. onVisible: () => {},
  33. // 當側邊欄出現動畫結束時所會呼叫的回呼函式。
  34. onShow: () => {},
  35. // 當側邊欄可見狀態有所變更且動畫執行完畢時所會呼叫的回呼函式。
  36. onChange: () => {},
  37. // 當側邊欄關閉時所會呼叫的回呼函式。
  38. onHide: () => {},
  39. // 當側邊欄關閉動畫結束時所會呼叫的回呼函式。
  40. onHidden: () => {}
  41. };
  42. // 標籤。
  43. Attribute = {
  44. SCROLL_LOCK: 'data-scroll-lock'
  45. };
  46. // 過場動畫。
  47. Transition = {
  48. OVERLAY: 'overlay',
  49. PUSH: 'push',
  50. SQUEEZE: 'squeeze'
  51. };
  52. // 可見度。
  53. Visibility = {
  54. AUTO: 'auto',
  55. HIDDEN: 'hidden',
  56. STATIC: 'static'
  57. };
  58. // 事件名稱。
  59. Event = {
  60. VISIBLE: `visible${EVENT_NAMESPACE}`,
  61. SHOW: `show${EVENT_NAMESPACE}`,
  62. CHANGE: `change${EVENT_NAMESPACE}`,
  63. HIDE: `hide${EVENT_NAMESPACE}`,
  64. HIDDEN: `hidden${EVENT_NAMESPACE}`,
  65. CLICK: `click${EVENT_NAMESPACE}`,
  66. RESIZE: `resize${EVENT_NAMESPACE}`,
  67. TOUCHSTART: `touchstart${EVENT_NAMESPACE}`,
  68. ANIMATIONEND: 'animationend'
  69. };
  70. // 樣式名稱。
  71. ClassName = {
  72. VISIBLE: 'visible',
  73. ACTIVE: 'active',
  74. DIMMED: 'dimmed',
  75. ANIMATING: 'animating',
  76. OVERLAY: 'overlay',
  77. SQUEEZABLE: 'squeezable'
  78. };
  79. // 裝置。
  80. Device = {
  81. MOBILE: 'mobile'
  82. };
  83. // 選擇器名稱。
  84. Selector = {
  85. PUSHER: '.ts.pusher',
  86. BODY: 'body'
  87. };
  88. // 錯誤訊息。
  89. Error = {};
  90. // ------------------------------------------------------------------------
  91. // 模組註冊
  92. // ------------------------------------------------------------------------
  93. ts.register({NAME, MODULE_NAMESPACE, Error, Settings}, ({$allModules, $this, element, debug, settings}) => {
  94. var $pusher, device, duration, elementNamespace, module;
  95. // ------------------------------------------------------------------------
  96. // 區域變數
  97. // ------------------------------------------------------------------------
  98. $pusher = ts();
  99. device = '';
  100. duration = 450;
  101. elementNamespace = '';
  102. // ------------------------------------------------------------------------
  103. // 模組定義
  104. // ------------------------------------------------------------------------
  105. return module = {
  106. setting: (key, value) => {
  107. settings[key] = value;
  108. return $allModules;
  109. },
  110. set: {
  111. lock: (value) => {
  112. if (value) {
  113. return $pusher.attr(Attribute.SCROLL_LOCK, 'true');
  114. } else {
  115. return $pusher.removeAttr(Attribute.SCROLL_LOCK);
  116. }
  117. },
  118. animating: (value) => {
  119. if (value) {
  120. return $this.addClass(ClassName.ANIMATING);
  121. } else {
  122. return $this.removeClass(ClassName.ANIMATING);
  123. }
  124. },
  125. visible: (value) => {
  126. if (value) {
  127. return $this.addClass(ClassName.VISIBLE);
  128. } else {
  129. return $this.removeClass(ClassName.VISIBLE);
  130. }
  131. }
  132. },
  133. update: {
  134. device: () => {
  135. return device = ts.device().device;
  136. }
  137. },
  138. get: {
  139. transition: () => {
  140. return settings.transition;
  141. },
  142. visibility: () => {
  143. return settings.visibility;
  144. }
  145. },
  146. reset: {
  147. pusher: () => {
  148. return $pusher.removeClass(ClassName.SQUEEZABLE);
  149. },
  150. sidebar: () => {
  151. return $this.removeClass(ClassName.OVERLAY);
  152. }
  153. },
  154. dim: {
  155. page: (value) => {
  156. if (value) {
  157. return $pusher.addClass(ClassName.DIMMED);
  158. } else {
  159. return $pusher.removeClass(ClassName.DIMMED);
  160. }
  161. }
  162. },
  163. show: () => {
  164. if (module.is.visible()) {
  165. return;
  166. }
  167. module.trigger.visible();
  168. module.reset.pusher();
  169. module.reset.sidebar();
  170. if (module.get.transition() === Transition.SQUEEZE) {
  171. $pusher.addClass(ClassName.SQUEEZABLE);
  172. }
  173. if (module.get.transition() === Transition.OVERLAY) {
  174. $this.addClass(ClassName.OVERLAY);
  175. }
  176. if (module.get.visibility() === Visibility.AUTO) {
  177. if (device === Device.MOBILE) {
  178. module.dim.page(true);
  179. module.set.lock(true);
  180. } else {
  181. module.dim.page(false);
  182. module.set.lock(false);
  183. }
  184. }
  185. if (settings.scrollLock) {
  186. module.set.lock(true);
  187. }
  188. if (settings.dimPage && module.get.visibility() !== Visibility.AUTO) {
  189. module.dim.page(true);
  190. }
  191. module.animate.show(() => {
  192. module.trigger.show();
  193. return module.trigger.change();
  194. });
  195. return $allModules;
  196. },
  197. hide: () => {
  198. if (module.is.hidden()) {
  199. return;
  200. }
  201. module.trigger.hide();
  202. module.set.lock(false);
  203. if (settings.dimPage) {
  204. module.dim.page(false);
  205. }
  206. module.animate.hide(() => {
  207. module.trigger.hidden();
  208. module.reset.pusher();
  209. module.reset.sidebar();
  210. return module.trigger.change();
  211. });
  212. return $allModules;
  213. },
  214. toggle: () => {
  215. if (module.is.hidden()) {
  216. module.show();
  217. } else {
  218. module.hide();
  219. }
  220. return $allModules;
  221. },
  222. animate: {
  223. show: (callback) => {
  224. module.set.animating(true);
  225. module.set.visible(true);
  226. return $this.off(Event.ANIMATIONEND).one(Event.ANIMATIONEND, () => {
  227. module.set.animating(false);
  228. if (callback != null) {
  229. return callback.call();
  230. }
  231. }).emulate(Event.ANIMATIONEND, duration);
  232. },
  233. hide: (callback) => {
  234. module.set.animating(true);
  235. module.set.visible(false);
  236. return $this.off(Event.ANIMATIONEND).one(Event.ANIMATIONEND, () => {
  237. module.set.animating(false);
  238. if (callback != null) {
  239. return callback.call();
  240. }
  241. }).emulate(Event.ANIMATIONEND, duration);
  242. }
  243. },
  244. attach: {
  245. events: (selector) => {
  246. return ts(selector).on(Event.CLICK, () => {
  247. return module.toggle();
  248. });
  249. }
  250. },
  251. is: {
  252. visible: () => {
  253. return $this.hasClass(ClassName.VISIBLE);
  254. },
  255. hidden: () => {
  256. return !module.is.visible();
  257. },
  258. animating: () => {
  259. return $this.hasClass(ClassName.ANIMATING);
  260. },
  261. closable: () => {
  262. return settings.closable === true;
  263. },
  264. child: (target) => {
  265. return $this.contains(target);
  266. }
  267. },
  268. repaint: () => {
  269. module.update.device();
  270. if (device === Device.MOBILE) {
  271. settings.transition = Transition.OVERLAY;
  272. return module.hide();
  273. } else {
  274. settings.transition = Transition.SQUEEZE;
  275. return module.show();
  276. }
  277. },
  278. trigger: {
  279. visible: () => {
  280. return $this.trigger(Event.VISIBLE, element);
  281. },
  282. show: () => {
  283. return $this.trigger(Event.SHOW, element);
  284. },
  285. change: () => {
  286. return $this.trigger(Event.CHANGE, element);
  287. },
  288. hide: () => {
  289. return $this.trigger(Event.HIDE, element);
  290. },
  291. hidden: () => {
  292. return $this.trigger(Event.HIDDEN, element);
  293. }
  294. },
  295. create: {
  296. id: () => {
  297. var id;
  298. id = (Math.random().toString(16) + '000000000').substr(2, 8);
  299. elementNamespace = `.${id}`;
  300. return debug('已產生臨時編號', id);
  301. }
  302. },
  303. bind: {
  304. repaint: () => {
  305. return ts(window).on(Event.RESIZE, () => {
  306. return module.repaint();
  307. });
  308. },
  309. events: () => {
  310. ts(Selector.BODY).on(`click.${elementNamespace}`, (event, context) => {
  311. debug('發生 CLICK 事件', context);
  312. if (module.is.child(event.target)) {
  313. return;
  314. }
  315. if (device !== Device.MOBILE && module.get.visibility() === Visibility.AUTO) {
  316. return;
  317. }
  318. if (!module.is.animating() && module.is.closable()) {
  319. return module.hide();
  320. }
  321. });
  322. $this.on(Event.VISIBLE, (event, context) => {
  323. debug('發生 VISIBLE 事件', context);
  324. return settings.onVisible.call(context, event);
  325. });
  326. $this.on(Event.SHOW, (event, context) => {
  327. debug('發生 SHOW 事件', context);
  328. return settings.onShow.call(context, event);
  329. });
  330. $this.on(Event.CHANGE, (event, context) => {
  331. debug('發生 CHANGE 事件', context);
  332. return settings.onChange.call(context, event);
  333. });
  334. $this.on(Event.HIDE, (event, context) => {
  335. debug('發生 HIDE 事件', context);
  336. return settings.onHide.call(context, event);
  337. });
  338. return $this.on(Event.HIDDEN, (event, context) => {
  339. debug('發生 HIDDEN 事件', context);
  340. return settings.onHidden.call(context, event);
  341. });
  342. }
  343. },
  344. // ------------------------------------------------------------------------
  345. // 基礎方法
  346. // ------------------------------------------------------------------------
  347. initialize: () => {
  348. debug('初始化側邊欄', element);
  349. $pusher = ts(Selector.PUSHER);
  350. module.create.id();
  351. module.update.device();
  352. module.bind.events();
  353. if (module.get.visibility() !== Visibility.AUTO) {
  354. return;
  355. }
  356. module.bind.repaint();
  357. return module.repaint();
  358. },
  359. instantiate: () => {
  360. return debug('實例化側邊欄', element);
  361. },
  362. refresh: () => {
  363. return $allModules;
  364. },
  365. destroy: () => {
  366. debug('摧毀側邊欄', element);
  367. $this.removeData(MODULE_NAMESPACE).off(EVENT_NAMESPACE);
  368. ts(Selector.BODY).off(`click.${elementNamespace}`);
  369. return $allModules;
  370. }
  371. };
  372. });
  373. }).call(this);