view.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. app.View = class View extends Events {
  2. constructor(el) {
  3. super();
  4. if (el instanceof HTMLElement) {
  5. this.el = el;
  6. }
  7. this.setupElement();
  8. if (this.el.className) {
  9. this.originalClassName = this.el.className;
  10. }
  11. if (this.constructor.className) {
  12. this.resetClass();
  13. }
  14. this.refreshElements();
  15. if (typeof this.init === "function") {
  16. this.init();
  17. this.refreshElements();
  18. }
  19. }
  20. setupElement() {
  21. if (this.el == null) {
  22. this.el =
  23. typeof this.constructor.el === "string"
  24. ? $(this.constructor.el)
  25. : this.constructor.el
  26. ? this.constructor.el
  27. : document.createElement(this.constructor.tagName || "div");
  28. }
  29. if (this.constructor.attributes) {
  30. for (var key in this.constructor.attributes) {
  31. var value = this.constructor.attributes[key];
  32. this.el.setAttribute(key, value);
  33. }
  34. }
  35. }
  36. refreshElements() {
  37. if (this.constructor.elements) {
  38. for (var name in this.constructor.elements) {
  39. var selector = this.constructor.elements[name];
  40. this[name] = this.find(selector);
  41. }
  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(" "))) {
  60. this.addClass(name);
  61. }
  62. }
  63. }
  64. find(selector) {
  65. return $(selector, this.el);
  66. }
  67. findAll(selector) {
  68. return $$(selector, this.el);
  69. }
  70. findByClass(name) {
  71. return this.findAllByClass(name)[0];
  72. }
  73. findLastByClass(name) {
  74. const all = this.findAllByClass(name)[0];
  75. return all[all.length - 1];
  76. }
  77. findAllByClass(name) {
  78. return this.el.getElementsByClassName(name);
  79. }
  80. findByTag(tag) {
  81. return this.findAllByTag(tag)[0];
  82. }
  83. findLastByTag(tag) {
  84. const all = this.findAllByTag(tag);
  85. return all[all.length - 1];
  86. }
  87. findAllByTag(tag) {
  88. return this.el.getElementsByTagName(tag);
  89. }
  90. append(value) {
  91. $.append(this.el, value.el || value);
  92. }
  93. appendTo(value) {
  94. $.append(value.el || value, this.el);
  95. }
  96. prepend(value) {
  97. $.prepend(this.el, value.el || value);
  98. }
  99. prependTo(value) {
  100. $.prepend(value.el || value, this.el);
  101. }
  102. before(value) {
  103. $.before(this.el, value.el || value);
  104. }
  105. after(value) {
  106. $.after(this.el, value.el || value);
  107. }
  108. remove(value) {
  109. $.remove(value.el || value);
  110. }
  111. empty() {
  112. $.empty(this.el);
  113. this.refreshElements();
  114. }
  115. html(value) {
  116. this.empty();
  117. this.append(value);
  118. }
  119. tmpl(...args) {
  120. return app.templates.render(...args);
  121. }
  122. delay(fn, ...args) {
  123. const delay = typeof args[args.length - 1] === "number" ? args.pop() : 0;
  124. return setTimeout(fn.bind(this, ...args), delay);
  125. }
  126. onDOM(event, callback) {
  127. $.on(this.el, event, callback);
  128. }
  129. offDOM(event, callback) {
  130. $.off(this.el, event, callback);
  131. }
  132. bindEvents() {
  133. let method, name;
  134. if (this.constructor.events) {
  135. for (name in this.constructor.events) {
  136. method = this.constructor.events[name];
  137. this[method] = this[method].bind(this);
  138. this.onDOM(name, this[method]);
  139. }
  140. }
  141. if (this.constructor.routes) {
  142. for (name in this.constructor.routes) {
  143. method = this.constructor.routes[name];
  144. this[method] = this[method].bind(this);
  145. app.router.on(name, this[method]);
  146. }
  147. }
  148. if (this.constructor.shortcuts) {
  149. for (name in this.constructor.shortcuts) {
  150. method = this.constructor.shortcuts[name];
  151. this[method] = this[method].bind(this);
  152. app.shortcuts.on(name, this[method]);
  153. }
  154. }
  155. }
  156. unbindEvents() {
  157. let method, name;
  158. if (this.constructor.events) {
  159. for (name in this.constructor.events) {
  160. method = this.constructor.events[name];
  161. this.offDOM(name, this[method]);
  162. }
  163. }
  164. if (this.constructor.routes) {
  165. for (name in this.constructor.routes) {
  166. method = this.constructor.routes[name];
  167. app.router.off(name, this[method]);
  168. }
  169. }
  170. if (this.constructor.shortcuts) {
  171. for (name in this.constructor.shortcuts) {
  172. method = this.constructor.shortcuts[name];
  173. app.shortcuts.off(name, this[method]);
  174. }
  175. }
  176. }
  177. addSubview(view) {
  178. return (this.subviews || (this.subviews = [])).push(view);
  179. }
  180. activate() {
  181. if (this.activated) {
  182. return;
  183. }
  184. this.bindEvents();
  185. if (this.subviews) {
  186. for (var view of Array.from(this.subviews)) {
  187. view.activate();
  188. }
  189. }
  190. this.activated = true;
  191. return true;
  192. }
  193. deactivate() {
  194. if (!this.activated) {
  195. return;
  196. }
  197. this.unbindEvents();
  198. if (this.subviews) {
  199. for (var view of Array.from(this.subviews)) {
  200. view.deactivate();
  201. }
  202. }
  203. this.activated = false;
  204. return true;
  205. }
  206. detach() {
  207. this.deactivate();
  208. $.remove(this.el);
  209. }
  210. };