30

View

Objective
  • Responder
    • View
  1. function View() {
  2.     Responder.call(this);
  3.  
  4.     this._parent = null;
  5.     this._widget = null;
  6. }
  7.  
  8. View.prototype = Object.create(Responder.prototype);
  9.  
  10. Object.defineProperty(View.prototype, 'constructor', { value: View, enumerable: false, writable: true });
  11.  
  12. Object.defineProperty(View.prototype, 'widget', {
  13.     get:    function() {
  14.         return this._widget;
  15.     }
  16. });
  17.  
  18. Object.defineProperty(View.prototype, 'parent', {
  19.     get:    function() {
  20.         return this._parent;
  21.     }
  22. });
  23.  
  24. View.prototype.interfaced = function() {
  25.     return this._widget ? true : false;
  26. };
  27.  
  28. View.prototype.isManaged = function() {
  29.     return this._widget && this._parent && this._widget.parentElement === this._parent ? true : false;
  30. };
  31.  
  32. View.prototype.isHidden = function() {
  33.     return this._widget && this._widget.hidden === true;
  34. };
  35.  
  36. View.prototype.isDisabled = function() {
  37.     return this._widget && this._widget.disabled === true;
  38. };
  39.  
  40. View.prototype.isEnabled = function() {
  41.     return this._widget && this._widget.disabled === false;
  42. };
  43.  
  44. View.prototype.hide = function() {
  45.     if (this._widget)
  46.         this._widget.hidden = true;
  47.  
  48.     return this;
  49. };
  50.  
  51. View.prototype.show = function() {
  52.     if (this._widget)
  53.         this._widget.hidden = false;
  54.  
  55.     return this;
  56. };
  57.  
  58. View.prototype.disable = function() {
  59.     if (this._widget)
  60.         this._widget.disabled = true;
  61.  
  62.     return this;
  63. };
  64.  
  65. View.prototype.enable = function() {
  66.     if (this._widget)
  67.         this._widget.disabled = false;
  68.  
  69.     return this;
  70. };
  71.  
  72. View.prototype.setStyle = function(prop, val) {
  73.     if (this._widget)
  74.         this._widget.style[prop] = val;
  75.  
  76.     return this;
  77. };
  78.  
  79. View.prototype.setAttribute = function(attr, val) {
  80.     if (this._widget)
  81.         this._widget.setAttribute(attr, val);
  82.  
  83.     return this;
  84. };
  85.  
  86. View.prototype.removeAttribute = function(attr) {
  87.     if (this._widget)
  88.         this._widget.removeAttribute(attr);
  89.  
  90.     return this;
  91. };
  92.  
  93. View.prototype.addFont = function(font) {
  94.     let link = document.createElement('link');
  95.  
  96.     link.setAttribute('rel', 'stylesheet');
  97.     link.setAttribute('href', `https://fonts.googleapis.com/css?family=${font.replace(/ +/, '+')}`);
  98.  
  99.     document.head.appendChild(link);
  100.  
  101.     return this;
  102. };
  103.  
  104. View.prototype.addClass = function(c) {
  105.     if (this._widget)
  106.         this._widget.classList.add(c);
  107.  
  108.     return this;
  109. };
  110.  
  111. View.prototype.removeClass = function(c) {
  112.     if (this._widget)
  113.         this._widget.classList.remove(c);
  114.  
  115.     return this;
  116. };
  117.  
  118. View.prototype.toggleClass = function(c) {
  119.     if (this._widget)
  120.         this._widget.classList.toggle(c);
  121.  
  122.     return this;
  123. };
  124.  
  125. View.prototype.hasClass = function(c) {
  126.     return this._widget ? this._widget.classList.contains(c) : false;
  127. };
  128.  
  129. View.prototype.addEventListener = function(type, listener, ...args) {
  130.     if (this._widget)
  131.         this._widget.addEventListener(type, listener, ...args);
  132.  
  133.     return this;
  134. };
  135.  
  136. View.prototype.removeEventListener = function(type, listener, ...args) {
  137.     if (this._widget)
  138.         this._widget.removeEventListener(type, listener, ...args);
  139.  
  140.     return this;
  141. };
  142.  
  143. View.prototype.dispatchEvent = function(event) {
  144.     if (this._widget)
  145.         this._widget.dispatchEvent(event);
  146.  
  147.     return this;
  148. };
  149.  
  150. View.prototype.resetWidget = function() {
  151.     return this;
  152. };
  153.  
  154. View.prototype.manageWidget = function(parent = null) {
  155.     if (parent)
  156.         this._parent = parent;
  157.  
  158.     if (this._parent && this._widget)
  159.         this._parent.appendChild(this._widget);
  160.  
  161.     return this;
  162. };
  163.  
  164. View.prototype.unmanageWidget = function() {
  165.     if (this._parent && this._widget)
  166.         this._parent.removeChild(this._widget);
  167.  
  168.     return this;
  169. };
  170.  
  171. View.prototype.setWidget = function(w) {
  172.     this._widget = w;
  173.  
  174.     return this;
  175. };
  176.  
  177. View.prototype.setManagedWidget = function(w) {
  178.     this._parent = w.parentElement;
  179.  
  180.     this.setWidget(w);
  181.  
  182.     return this;
  183. };
  184.  
  185. View.prototype.cloneWidget = function() {
  186.     return this._widget ? this._widget.cloneNode(true) : null;
  187. };
  188.  
  189. View.prototype.createWidget = function() {
  190.     return this;
  191. };
  192.  
  193. View.prototype.createManagedWidget = function(parent, ...args) {
  194.     this.createWidget(...args);
  195.  
  196.     this.manageWidget(parent);
  197.  
  198.     return this;
  199. };
  200.  
  201. View.prototype.destroyWidget = function() {
  202.     this.unmanageWidget();
  203.  
  204.     this._widget = null;
  205.  
  206.     return this;
  207. };
SEE ALSO

Responder, Inspector, Clip, Draggable

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].