Source: pubsub.js

  1. /**
  2. * Pubsub Component<BR>
  3. * <BR><BR><img src=/tk/lib/components/w/img/pubsub.png width=30% style="border:1px lime dashed;padding:20px">
  4. * <BR><BR><a href="/tk/lib/components/w/html/pubsub.html">DEMO</a>
  5. */
  6. class Pubsub extends HTMLElement {
  7. constructor() {
  8. wc.group("Pubsub.constructor")
  9. super();
  10. wc.groupEnd();
  11. };
  12. /**
  13. * Set observable values here. When Changed, attributeChangedCallback is invoked
  14. * @observedAttributes
  15. */
  16. static get observedAttributes() {
  17. wc.group("Pubsub.observedAttributes");
  18. this.observables = [];
  19. wc.groupEnd();
  20. return this.observables;
  21. };
  22. /**
  23. * Called when this is attached to DOM
  24. * @connectedCallback.
  25. */
  26. connectedCallback() {
  27. wc.group("Pubsub.connectedCallback")
  28. let self = this;
  29. tkloading.show();
  30. // ADD A RANDOM ID IF NONE EXIST
  31. if (!this.id) {
  32. this.id = this.constructor.name.toLowerCase() + "-" + wc.uid();
  33. }
  34. // GET PROPERTIES AND INTERESTING ELEMENTS
  35. this._initialize();
  36. // ADD COMPONENT MARKTOP
  37. this.innerHTML = `<wc-include href='/tk/lib/components/misc/webpack/src/w/html/parts/pubsub/main.html'></wc-include>`;
  38. this._finalize();
  39. ga('send', {'hitType': 'event','eventCategory': 'wc-connected','eventAction': 'connected','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  40. wc.groupEnd();
  41. };
  42. /**
  43. * Called with .setAttribute(...) function call
  44. * @attributeChangedCallback
  45. */
  46. attributeChangedCallback(attr, oldval, newval) {
  47. wc.group("Pubsub.attributeChangedCallback:", attr, oldval, newval);
  48. this.properties = this.properties || [];
  49. let obs = Pubsub.observedAttributes;
  50. for (let i = 0; i < obs.length; i++) {
  51. if (newval) {
  52. this.properties[obs[i]] = newval;
  53. }
  54. }
  55. // YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
  56. try {
  57. switch(attr)
  58. {
  59. case "header":
  60. //this.querySelector("h1").innerHTML = newval;
  61. break;
  62. default:
  63. break;
  64. }
  65. } catch(e) {
  66. wc.warn(e.name + ' > ' + e.message);
  67. }
  68. wc.groupEnd();
  69. };
  70. /**
  71. * Stores DOM elements of interest for future use
  72. * @private
  73. * @_fetchElements
  74. */
  75. _fetchElements() {
  76. wc.group("Pubsub._fetchElements");
  77. this.dom = this.dom || [];
  78. this.dom.content = this.innerHTML;
  79. wc.groupEnd();
  80. };
  81. /**
  82. * Component attributes are _fetched and defaults are set if undefined
  83. * @private
  84. * @_fetchAttributes
  85. */
  86. _fetchAttributes() {
  87. wc.group("Pubsub._fetchAttributes");
  88. this.properties = {
  89. uparam : "",
  90. cname : "Pubsub",
  91. author : "Mel M. Heravi, Ph.D.",
  92. version : "1.0"
  93. };
  94. // SAVE WIDGET SPECIFIC PROPERTIES
  95. this.propertiesW = [];
  96. // SAVE ALL OTHER PROPERTIES
  97. let attrs = wc.getAttributes(this)
  98. for (var key in attrs) {
  99. let attr = this.getAttribute(key) || this.properties.key;
  100. this.properties[key] = this.getAttribute(key);
  101. this.propertiesW[key] = this.getAttribute(key);
  102. wc.log(key + ": " + attrs[key]);
  103. }
  104. // SET ALL INITIAL ATTRIBUTES
  105. for (var key in this.properties) {
  106. switch(key)
  107. {
  108. case "header":
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. wc.log("ATTRIBUTES: ", this.properties);
  115. wc.groupEnd();
  116. };
  117. /**
  118. * configure the instance object and artifacts
  119. * @configure
  120. * @param {string} data use data if exist else use 'this.properties.cfg' parameter
  121. */
  122. configure(data) {
  123. wc.group("Pubsub.configure:", data);
  124. // IF JSON VARIABLE (data) IS PROVIDED
  125. if (data) {
  126. this._process(data);
  127. } else {
  128. let self = this;
  129. $.getJSON(this.properties.cfg, function(data) {
  130. self._process(data);
  131. }).fail(function(jqXHR, textStatus, errorThrown) {
  132. alert("ERROR: INCOMING TEXT " + jqXHR.responseText);
  133. });
  134. }
  135. wc.groupEnd();
  136. };
  137. /**
  138. * _process the instance object and artifacts
  139. * @private
  140. * @_process
  141. */
  142. _process(data) {
  143. wc.group("Pubsub._process:", data);
  144. // DO WHATEVER WITH THE DATA
  145. wc.groupEnd();
  146. };
  147. /**
  148. * Initialize component
  149. * @private
  150. * @_initialize
  151. */
  152. _initialize() {
  153. wc.group("Pubsub._initialize:", this.id);
  154. // FETCH ALL INTERESTING ELEMENTS
  155. this._fetchElements();
  156. // FETCH ALL ATTRIBUTES
  157. this._fetchAttributes();
  158. wc.groupEnd();
  159. };
  160. /**
  161. * Save data for analytics and final wrap up
  162. * @private
  163. * @_finalize
  164. */
  165. _finalize() {
  166. wc.group("Pubsub._finalize:", this.id);
  167. this.classList.add("wc");
  168. // ADD ANALYTICS HERE
  169. wc.setStats(this, this.properties.cname, this.properties.version);
  170. // SHOW IT NOW (NO FLICKERS)
  171. this.style.visibility = "visible";
  172. wc.groupEnd();
  173. };
  174. /**
  175. * Invoked When component is removed. Usually with a .remove() function call
  176. * @disconnectedCallback
  177. */
  178. disconnectedCallback() {
  179. wc.group("Pubsub.disconnectedCallback")
  180. ga('send', {'hitType': 'event','eventCategory': 'wc-disconnected','eventAction': 'disconnected','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  181. wc.groupEnd();
  182. };
  183. /**
  184. * Destroy the instance object and artifacts
  185. * @destroy
  186. */
  187. destroy() {
  188. wc.group("Pubsub.destroy");
  189. // FREE ALL MEMORY
  190. // you should delete all created objects here
  191. // FREE POINTER
  192. delete this;
  193. // REMOVE ITEM FROM DOM
  194. this.parentNode.removeChild(this);
  195. ga('send', {'hitType': 'event','eventCategory': 'wc-destroyed','eventAction': 'distroy','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  196. wc.groupEnd();
  197. };
  198. }
  199. window.customElements.define('wc-pubsub', Pubsub);