view.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS206: Consider reworking classes to avoid initClass
  8. * DS207: Consider shorter variations of null checks
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  10. */
  11. const Cls = (app.View = class View {
  12. static initClass() {
  13. $.extend(this.prototype, Events);
  14. }
  15. constructor() {
  16. this.setupElement();
  17. if (this.el.className) { this.originalClassName = this.el.className; }
  18. if (this.constructor.className) { this.resetClass(); }
  19. this.refreshElements();
  20. if (typeof this.init === 'function') {
  21. this.init();
  22. }
  23. this.refreshElements();
  24. }
  25. setupElement() {
  26. if (this.el == null) { this.el = typeof this.constructor.el === 'string' ?
  27. $(this.constructor.el)
  28. : this.constructor.el ?
  29. this.constructor.el
  30. :
  31. document.createElement(this.constructor.tagName || 'div'); }
  32. if (this.constructor.attributes) {
  33. for (var key in this.constructor.attributes) {
  34. var value = this.constructor.attributes[key];
  35. this.el.setAttribute(key, value);
  36. }
  37. }
  38. }
  39. refreshElements() {
  40. if (this.constructor.elements) {
  41. for (var name in this.constructor.elements) { var selector = this.constructor.elements[name]; this[name] = this.find(selector); }
  42. }
  43. }
  44. addClass(name) {
  45. this.el.classList.add(name);
  46. }
  47. removeClass(name) {
  48. this.el.classList.remove(name);
  49. }
  50. toggleClass(name) {
  51. this.el.classList.toggle(name);
  52. }
  53. hasClass(name) {
  54. return this.el.classList.contains(name);
  55. }
  56. resetClass() {
  57. this.el.className = this.originalClassName || '';
  58. if (this.constructor.className) {
  59. for (var name of Array.from(this.constructor.className.split(' '))) { this.addClass(name); }
  60. }
  61. }
  62. find(selector) {
  63. return $(selector, this.el);
  64. }
  65. findAll(selector) {
  66. return $$(selector, this.el);
  67. }
  68. findByClass(name) {
  69. return this.findAllByClass(name)[0];
  70. }
  71. findLastByClass(name) {
  72. const all = this.findAllByClass(name)[0];
  73. return all[all.length - 1];
  74. }
  75. findAllByClass(name) {
  76. return this.el.getElementsByClassName(name);
  77. }
  78. findByTag(tag) {
  79. return this.findAllByTag(tag)[0];
  80. }
  81. findLastByTag(tag) {
  82. const all = this.findAllByTag(tag);
  83. return all[all.length - 1];
  84. }
  85. findAllByTag(tag) {
  86. return this.el.getElementsByTagName(tag);
  87. }
  88. append(value) {
  89. $.append(this.el, value.el || value);
  90. }
  91. appendTo(value) {
  92. $.append(value.el || value, this.el);
  93. }
  94. prepend(value) {
  95. $.prepend(this.el, value.el || value);
  96. }
  97. prependTo(value) {
  98. $.prepend(value.el || value, this.el);
  99. }
  100. before(value) {
  101. $.before(this.el, value.el || value);
  102. }
  103. after(value) {
  104. $.after(this.el, value.el || value);
  105. }
  106. remove(value) {
  107. $.remove(value.el || value);
  108. }
  109. empty() {
  110. $.empty(this.el);
  111. this.refreshElements();
  112. }
  113. html(value) {
  114. this.empty();
  115. this.append(value);
  116. }
  117. tmpl(...args) {
  118. return app.templates.render(...Array.from(args || []));
  119. }
  120. delay(fn, ...args) {
  121. const delay = typeof args[args.length - 1] === 'number' ? args.pop() : 0;
  122. return setTimeout(fn.bind(this, ...Array.from(args)), delay);
  123. }
  124. onDOM(event, callback) {
  125. $.on(this.el, event, callback);
  126. }
  127. offDOM(event, callback) {
  128. $.off(this.el, event, callback);
  129. }
  130. bindEvents() {
  131. let method, name;
  132. if (this.constructor.events) {
  133. for (name in this.constructor.events) { method = this.constructor.events[name]; this.onDOM(name, this[method]); }
  134. }
  135. if (this.constructor.routes) {
  136. for (name in this.constructor.routes) { method = this.constructor.routes[name]; app.router.on(name, this[method]); }
  137. }
  138. if (this.constructor.shortcuts) {
  139. for (name in this.constructor.shortcuts) { method = this.constructor.shortcuts[name]; app.shortcuts.on(name, this[method]); }
  140. }
  141. }
  142. unbindEvents() {
  143. let method, name;
  144. if (this.constructor.events) {
  145. for (name in this.constructor.events) { method = this.constructor.events[name]; this.offDOM(name, this[method]); }
  146. }
  147. if (this.constructor.routes) {
  148. for (name in this.constructor.routes) { method = this.constructor.routes[name]; app.router.off(name, this[method]); }
  149. }
  150. if (this.constructor.shortcuts) {
  151. for (name in this.constructor.shortcuts) { method = this.constructor.shortcuts[name]; app.shortcuts.off(name, this[method]); }
  152. }
  153. }
  154. addSubview(view) {
  155. return (this.subviews || (this.subviews = [])).push(view);
  156. }
  157. activate() {
  158. if (this.activated) { return; }
  159. this.bindEvents();
  160. if (this.subviews) { for (var view of Array.from(this.subviews)) { view.activate(); } }
  161. this.activated = true;
  162. return true;
  163. }
  164. deactivate() {
  165. if (!this.activated) { return; }
  166. this.unbindEvents();
  167. if (this.subviews) { for (var view of Array.from(this.subviews)) { view.deactivate(); } }
  168. this.activated = false;
  169. return true;
  170. }
  171. detach() {
  172. this.deactivate();
  173. $.remove(this.el);
  174. }
  175. });
  176. Cls.initClass();