| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- app.View = class View extends Events {
- constructor(el) {
- super();
- if (el instanceof HTMLElement) {
- this.el = el;
- }
- this.setupElement();
- if (this.el.className) {
- this.originalClassName = this.el.className;
- }
- if (this.constructor.className) {
- this.resetClass();
- }
- this.refreshElements();
- if (typeof this.init === "function") {
- this.init();
- this.refreshElements();
- }
- }
- setupElement() {
- if (this.el == null) {
- this.el =
- typeof this.constructor.el === "string"
- ? $(this.constructor.el)
- : this.constructor.el
- ? this.constructor.el
- : document.createElement(this.constructor.tagName || "div");
- }
- if (this.constructor.attributes) {
- for (var key in this.constructor.attributes) {
- var value = this.constructor.attributes[key];
- this.el.setAttribute(key, value);
- }
- }
- }
- refreshElements() {
- if (this.constructor.elements) {
- for (var name in this.constructor.elements) {
- var selector = this.constructor.elements[name];
- this[name] = this.find(selector);
- }
- }
- }
- addClass(name) {
- this.el.classList.add(name);
- }
- removeClass(name) {
- this.el.classList.remove(name);
- }
- toggleClass(name) {
- this.el.classList.toggle(name);
- }
- hasClass(name) {
- return this.el.classList.contains(name);
- }
- resetClass() {
- this.el.className = this.originalClassName || "";
- if (this.constructor.className) {
- for (var name of Array.from(this.constructor.className.split(" "))) {
- this.addClass(name);
- }
- }
- }
- find(selector) {
- return $(selector, this.el);
- }
- findAll(selector) {
- return $$(selector, this.el);
- }
- findByClass(name) {
- return this.findAllByClass(name)[0];
- }
- findLastByClass(name) {
- const all = this.findAllByClass(name)[0];
- return all[all.length - 1];
- }
- findAllByClass(name) {
- return this.el.getElementsByClassName(name);
- }
- findByTag(tag) {
- return this.findAllByTag(tag)[0];
- }
- findLastByTag(tag) {
- const all = this.findAllByTag(tag);
- return all[all.length - 1];
- }
- findAllByTag(tag) {
- return this.el.getElementsByTagName(tag);
- }
- append(value) {
- $.append(this.el, value.el || value);
- }
- appendTo(value) {
- $.append(value.el || value, this.el);
- }
- prepend(value) {
- $.prepend(this.el, value.el || value);
- }
- prependTo(value) {
- $.prepend(value.el || value, this.el);
- }
- before(value) {
- $.before(this.el, value.el || value);
- }
- after(value) {
- $.after(this.el, value.el || value);
- }
- remove(value) {
- $.remove(value.el || value);
- }
- empty() {
- $.empty(this.el);
- this.refreshElements();
- }
- html(value) {
- this.empty();
- this.append(value);
- }
- tmpl(...args) {
- return app.templates.render(...args);
- }
- delay(fn, ...args) {
- const delay = typeof args[args.length - 1] === "number" ? args.pop() : 0;
- return setTimeout(fn.bind(this, ...args), delay);
- }
- onDOM(event, callback) {
- $.on(this.el, event, callback);
- }
- offDOM(event, callback) {
- $.off(this.el, event, callback);
- }
- bindEvents() {
- let method, name;
- if (this.constructor.events) {
- for (name in this.constructor.events) {
- method = this.constructor.events[name];
- this[method] = this[method].bind(this);
- this.onDOM(name, this[method]);
- }
- }
- if (this.constructor.routes) {
- for (name in this.constructor.routes) {
- method = this.constructor.routes[name];
- this[method] = this[method].bind(this);
- app.router.on(name, this[method]);
- }
- }
- if (this.constructor.shortcuts) {
- for (name in this.constructor.shortcuts) {
- method = this.constructor.shortcuts[name];
- this[method] = this[method].bind(this);
- app.shortcuts.on(name, this[method]);
- }
- }
- }
- unbindEvents() {
- let method, name;
- if (this.constructor.events) {
- for (name in this.constructor.events) {
- method = this.constructor.events[name];
- this.offDOM(name, this[method]);
- }
- }
- if (this.constructor.routes) {
- for (name in this.constructor.routes) {
- method = this.constructor.routes[name];
- app.router.off(name, this[method]);
- }
- }
- if (this.constructor.shortcuts) {
- for (name in this.constructor.shortcuts) {
- method = this.constructor.shortcuts[name];
- app.shortcuts.off(name, this[method]);
- }
- }
- }
- addSubview(view) {
- return (this.subviews || (this.subviews = [])).push(view);
- }
- activate() {
- if (this.activated) {
- return;
- }
- this.bindEvents();
- if (this.subviews) {
- for (var view of Array.from(this.subviews)) {
- view.activate();
- }
- }
- this.activated = true;
- return true;
- }
- deactivate() {
- if (!this.activated) {
- return;
- }
- this.unbindEvents();
- if (this.subviews) {
- for (var view of Array.from(this.subviews)) {
- view.deactivate();
- }
- }
- this.activated = false;
- return true;
- }
- detach() {
- this.deactivate();
- $.remove(this.el);
- }
- };
|