Source: documentation.js

  1. /**
  2. * Documentation Component - used by "Portal Dashboard"
  3. * <BR><BR><img src=../images/documentation.png width=60%>
  4. * <BR><BR><a href="../html/documentation.html">DEMO</a>
  5. */
  6. class Documentation extends HTMLElement {
  7. constructor() {
  8. console.group("Documentation.constructor")
  9. super();
  10. console.groupEnd();
  11. };
  12. /**
  13. * Set observable values here. When Changed, attributeChangedCallback is invoked
  14. * @observedAttributes
  15. */
  16. static get observedAttributes() {
  17. console.group("Documentation.observedAttributes");
  18. this.observables = ["background"];
  19. console.groupEnd();
  20. return this.observables;
  21. };
  22. /**
  23. * This function is called when this is attached to DOM
  24. * @connectedCallback.
  25. */
  26. connectedCallback() {
  27. console.group("Documentation.connectedCallback")
  28. let self = this;
  29. // GET PROPERTIES AND INTERESTING ELEMENTS
  30. this._initialize();
  31. let items = __(".wc-documentation-item");
  32. for (var i=0; i<items.length; i++) {
  33. let obj = items[i]
  34. obj.addEventListener("click", function() {
  35. self._onClick(this.id);
  36. });
  37. }
  38. // REPLACE CONTENT IF NECESSARY WITH NEW STUFF
  39. //this.innerHTML = this.dom.content;
  40. // ADD STATS AND OTHER FINAL STUFF
  41. this._finalize();
  42. // SHOW IT NOW (NO FLICKERS)
  43. this.style.visibility = "visible";
  44. console.groupEnd();
  45. };
  46. /**
  47. * Invoked When component is removed. Usually with a .remove() function call
  48. * @disconnectedCallback
  49. */
  50. disconnectedCallback() {
  51. console.group("Documentation.disconnectedCallback")
  52. /* CLEAN UP NOW */
  53. console.groupEnd();
  54. };
  55. /**
  56. * Called with .setAttribute(...) function call
  57. * @attributeChangedCallback
  58. */
  59. attributeChangedCallback(attr, oldval, newval) {
  60. console.group("Documentation.attributeChangedCallback:", attr, oldval, newval);
  61. this.properties = this.properties || [];
  62. let obs = Documentation.observedAttributes;
  63. for (let i = 0; i < obs.length; i++) {
  64. this.properties[obs[i]] = newval;
  65. // YOUR CODE FOR CHANGES GO HERE
  66. }
  67. console.groupEnd();
  68. };
  69. /**
  70. * Stores DOM elements of interest for future use
  71. * @_fetchElements
  72. */
  73. _fetchElements() {
  74. console.group("Documentation._fetchElements");
  75. this.dom = this.dom || [];
  76. this.dom.content = this.innerHTML;
  77. console.groupEnd();
  78. };
  79. /**
  80. * Component attributes are _fetched and defaults are set if undefined
  81. * @_fetchAttributes
  82. * @param {string} [background=indianred] background color
  83. */
  84. _fetchAttributes() {
  85. console.group("Documentation._fetchAttributes");
  86. this.properties = {
  87. "cname" : "Documentation",
  88. "author" : "Mel Heravi",
  89. "version" : "1.0"
  90. };
  91. // SAVE WIDGET SPECIFIC PROPERTIES
  92. this.propertiesW = [];
  93. // SAVE ALL OTHER PROPERTIES
  94. let attrs = wc.getAttributes(this)
  95. for (var key in attrs) {
  96. this.properties[key] = this.getAttribute(key);
  97. this.propertiesW[key] = this.getAttribute(key);
  98. console.log(key + ": " + attrs[key]);
  99. }
  100. console.log("attributes: ", this.properties);
  101. // PROCESS ALL PROPERTIES (example below);
  102. // this.style.background = this.properties.background;
  103. console.groupEnd();
  104. };
  105. /**
  106. * A sample callback usage function - see connectedCallback()
  107. * @_onClick
  108. */
  109. _onClick(id) {
  110. console.group("Documentation._onClick:", id);
  111. let uparam = $("#" + id).attr("uparam");
  112. wc.publish(this, "wc-documentation", {
  113. action: "click",
  114. id: id,
  115. uparam: uparam
  116. });
  117. console.groupEnd();
  118. };
  119. /**
  120. * Destroy the instance object and artifacts
  121. * @_destroy
  122. */
  123. destroy() {
  124. console.group("Documentation.destroy:", this.id);
  125. // FREE POINTER
  126. delete this;
  127. // REMOVE ITEM FROM DOM
  128. this.parentNode.removeChild(this);
  129. console.groupEnd();
  130. };
  131. /**
  132. * SAVE DATA FOR ANALYTICS
  133. * @__initialize
  134. */
  135. _initialize() {
  136. console.group("Documentation._initialize:", this.id);
  137. // FETCH ALL INTERESTING ELEMENTS
  138. this._fetchElements();
  139. // FETCH ALL ATTRIBUTES
  140. this._fetchAttributes();
  141. console.groupEnd();
  142. };
  143. /**
  144. * SAVE DATA FOR ANALYTICS
  145. * @__finalize
  146. */
  147. _finalize() {
  148. console.group("Documentation._finalize:", this.id);
  149. this.classList.add("wc");
  150. // ADD ANALYTICS HERE
  151. wc.getStats(this, this.properties.cname, this.properties.version);
  152. console.groupEnd();
  153. };
  154. /**
  155. * FOR TESTING PURPOSES
  156. * @test
  157. */
  158. static test() {
  159. console.group("Documentation.test");
  160. console.log("testing results will be printed here...");
  161. console.groupEnd();
  162. return true;
  163. }
  164. }
  165. window.customElements.define('wc-documentation', Documentation);