Source: components.js

  1. /**
  2. * Components Component<BR>
  3. * <BR><BR><img src=/tk/lib/components/w/img/components.png width=30% style="border:1px lime dashed;padding:20px">
  4. * <BR><BR><a href="/tk/lib/components/w/html/components.html">DEMO</a>
  5. */
  6. class Components extends HTMLElement {
  7. constructor() {
  8. wc.group("Components.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("Components.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("Components.connectedCallback")
  28. let self = this;
  29. this.env = wc.getSearchParam("env") || "dev"
  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/components/main.html'></wc-include>"
  38. this.configure();
  39. wc.wait4(".btn-group-horizontal .btn", function() {
  40. $(".btn-group-horizontal .btn").on("click", function() {
  41. self.env = $(this).attr("env")
  42. self._env();
  43. });
  44. });
  45. wc.wait4(".wc-components-search", function() {
  46. $(".wc-components-search").focus();
  47. $(".wc-components-search").on('input', function() {
  48. self._search();
  49. });
  50. if (self.env == "prod") {
  51. $(".btn-group-horizontal .btn").hide();
  52. }
  53. });
  54. // ADD STATS AND OTHER FINAL STUFF
  55. this._finalize();
  56. ga('send', {'hitType': 'event','eventCategory': 'wc-connected','eventAction': 'connected','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  57. wc.groupEnd();
  58. };
  59. /**
  60. * Called with .setAttribute(...) function call
  61. * @attributeChangedCallback
  62. */
  63. attributeChangedCallback(attr, oldval, newval) {
  64. wc.group("Components.attributeChangedCallback:", attr, oldval, newval);
  65. this.properties = this.properties || [];
  66. let obs = Components.observedAttributes;
  67. for (let i = 0; i < obs.length; i++) {
  68. if (newval) {
  69. this.properties[obs[i]] = newval;
  70. }
  71. }
  72. // YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
  73. try {
  74. switch(attr)
  75. {
  76. case "header":
  77. //this.querySelector("h1").innerHTML = newval;
  78. break;
  79. default:
  80. break;
  81. }
  82. } catch(e) {
  83. wc.warn(e.name + ' > ' + e.message);
  84. }
  85. wc.groupEnd();
  86. };
  87. /**
  88. * Stores DOM elements of interest for future use
  89. * @private
  90. * @_fetchElements
  91. */
  92. _fetchElements() {
  93. wc.group("Components._fetchElements");
  94. this.dom = this.dom || [];
  95. this.dom.content = this.innerHTML;
  96. wc.groupEnd();
  97. };
  98. /**
  99. * Component attributes are _fetched and defaults are set if undefined
  100. * @private
  101. * @_fetchAttributes
  102. */
  103. _fetchAttributes() {
  104. wc.group("Components._fetchAttributes");
  105. this.properties = {
  106. uparam : "",
  107. cname : "Components",
  108. author : "Mel M. Heravi, Ph.D.",
  109. version : "1.0"
  110. };
  111. // SAVE WIDGET SPECIFIC PROPERTIES
  112. this.propertiesW = [];
  113. // SAVE ALL OTHER PROPERTIES
  114. let attrs = wc.getAttributes(this)
  115. for (var key in attrs) {
  116. let attr = this.getAttribute(key) || this.properties.key;
  117. this.properties[key] = this.getAttribute(key);
  118. this.propertiesW[key] = this.getAttribute(key);
  119. wc.log(key + ": " + attrs[key]);
  120. }
  121. // SET ALL INITIAL ATTRIBUTES
  122. for (var key in this.properties) {
  123. switch(key)
  124. {
  125. case "header":
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. wc.log("ATTRIBUTES: ", this.properties);
  132. wc.groupEnd();
  133. };
  134. /**
  135. * configure the instance object and artifacts
  136. * @configure
  137. * @param {string} data use data if exist else use 'this.properties.cfg' parameter
  138. */
  139. configure(data) {
  140. wc.group("Components.configure:", data);
  141. let self = this;
  142. // GET HTML FILES
  143. $.getJSON("/mtk/rest/os/dir?path=/Melify/mtk/dev/tk/lib/components/w/html&files=*", function(hfiles) {
  144. self.hfiles = hfiles;
  145. // GET IMG FILES
  146. $.getJSON("/mtk/rest/os/dir?path=/Melify/mtk/dev/tk/lib/components/w/img&files=*.png", function(ifiles) {
  147. self.ifiles = ifiles;
  148. // GET PROD FILES
  149. $.getJSON("/mtk/rest/os/dir?path=/Melify/mtk/dev/tk/lib/components/misc/webpack/prod&files=*", function(pfiles) {
  150. self.pfiles = pfiles;
  151. self._process();
  152. });
  153. });
  154. });
  155. wc.groupEnd();
  156. };
  157. /**
  158. * _process the instance object and artifacts
  159. * @private
  160. * @_process
  161. */
  162. _process() {
  163. wc.group("Components._process");
  164. if (0) {
  165. console.log("HFILES", this.hfiles)
  166. console.log("IFILES", this.ifiles)
  167. console.log("PFILES", this.pfiles)
  168. }
  169. for (var i = 0; i < this.ifiles.length; i++) {
  170. let name = this.ifiles[i].replace(".png","");
  171. this._tile(name);
  172. }
  173. this._env();
  174. wc.groupEnd();
  175. };
  176. /**
  177. * Initialize component
  178. * @private
  179. * @_initialize
  180. */
  181. _initialize() {
  182. wc.group("Components._initialize:", this.id);
  183. // FETCH ALL INTERESTING ELEMENTS
  184. this._fetchElements();
  185. // FETCH ALL ATTRIBUTES
  186. this._fetchAttributes();
  187. wc.groupEnd();
  188. };
  189. /**
  190. * Save data for analytics and final wrap up
  191. * @private
  192. * @_finalize
  193. */
  194. _finalize() {
  195. wc.group("Components._finalize:", this.id);
  196. this.classList.add("wc");
  197. // ADD ANALYTICS HERE
  198. wc.setStats(this, this.properties.cname, this.properties.version);
  199. // SHOW IT NOW (NO FLICKERS)
  200. this.style.visibility = "visible";
  201. wc.groupEnd();
  202. };
  203. /**
  204. * Invoked When component is removed. Usually with a .remove() function call
  205. * @disconnectedCallback
  206. */
  207. disconnectedCallback() {
  208. wc.group("Components.disconnectedCallback")
  209. ga('send', {'hitType': 'event','eventCategory': 'wc-disconnected','eventAction': 'disconnected','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  210. wc.groupEnd();
  211. };
  212. /**
  213. * Destroy the instance object and artifacts
  214. * @destroy
  215. */
  216. destroy() {
  217. wc.group("Components.destroy");
  218. // FREE ALL MEMORY
  219. // you should delete all created objects here
  220. // FREE POINTER
  221. delete this;
  222. // REMOVE ITEM FROM DOM
  223. this.parentNode.removeChild(this);
  224. ga('send', {'hitType': 'event','eventCategory': 'wc-destroyed','eventAction': 'distroy','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  225. wc.groupEnd();
  226. };
  227. /**
  228. * @_tile
  229. */
  230. _tile(name) {
  231. wc.group("Components._tile:", name);
  232. $(".wc-components-main").append(`
  233. <a href="/tk/lib/components/misc/webpack/src/w/html/${name}.html" target="_blank" id="${name}">
  234. <div class="wc-components-tile float-left col-lg-2">
  235. <div class="shadow border">
  236. <span class="wc-components-name">${name}</span>
  237. <img src="/tk/lib/components/misc/webpack/src/w/img/${name}.png">
  238. </div>
  239. </div>
  240. </a>`);
  241. wc.groupEnd();
  242. };
  243. /**
  244. * @_env
  245. */
  246. _env() {
  247. wc.group("Components._env");
  248. let self = this;
  249. switch(this.env)
  250. {
  251. case "prod":
  252. $(".wc-components-tile").parent().each(function() {
  253. let id = $(this).attr("id");
  254. $(this).attr("href","/tk/lib/components/misc/webpack/prod/" + id);
  255. if (!self.pfiles.includes(id)) {
  256. $("#" + id).css({"opacity":"0.2", "cursor":"not-allowed"}).attr("onclick","return false");
  257. }
  258. });
  259. break;
  260. case "dev":
  261. $(".wc-components-tile").parent().each(function() {
  262. let id = $(this).attr("id");
  263. $(this).attr("href",`/tk/lib/components/misc/webpack/src/w/html/${id}.html`);
  264. if (self.hfiles.includes(id + ".html")) {
  265. $("#" + id).css({"opacity":"1.0", "cursor":"hand"}).removeAttr("onclick")
  266. }
  267. });
  268. break;
  269. }
  270. // CLIP BUTTONS
  271. $(".btn-group-horizontal .btn").removeClass("active");
  272. $(`[env=${this.env}]`).addClass("active");
  273. // SET IT IN ADDRESSBAR
  274. wc.setSearchParam(`?env=${this.env}`);
  275. wc.groupEnd();
  276. };
  277. /**
  278. * @_search
  279. */
  280. _search() {
  281. wc.group("Components._search");
  282. let self = this;
  283. let val = $(".wc-components-search").val()
  284. $(".wc-components-main a").each(function() {
  285. let id = $(this).attr("id");
  286. if (!id.includes(val)) {
  287. $(this).hide();
  288. } else {
  289. $(this).show();
  290. }
  291. });
  292. wc.groupEnd();
  293. };
  294. }
  295. window.customElements.define('wc-components', Components);