Source: header.js

  1. /**
  2. * Header Component <BR>
  3. * This component will replace both thick and thin components moving forward<BR>
  4. * <BR><BR><img src=../images/header.png width=100% style="border:1px lime dashed";>
  5. * <BR><BR><a href="../html/header.html">DEMO</a>
  6. */
  7. class Header extends HTMLElement {
  8. constructor() {
  9. console.group("Header.constructor")
  10. super();
  11. console.groupEnd();
  12. };
  13. /**
  14. * Set observable values here. When Changed, attributeChangedCallback is invoked
  15. * @observedAttributes
  16. */
  17. static get observedAttributes() {
  18. console.group("Header.observedAttributes");
  19. this.observables = ["uname", "aname", "sname", "env"];
  20. console.log(this.observables);
  21. console.groupEnd();
  22. return this.observables;
  23. };
  24. /**
  25. * This function is called when this is attached to DOM
  26. * @connectedCallback.
  27. */
  28. connectedCallback() {
  29. console.group("Header.connectedCallback")
  30. let self = this;
  31. // FETCH ALL INTERESTING ELEMENTS
  32. this._fetchElements();
  33. // FETCH ALL ATTRIBUTES
  34. this._fetchAttributes();
  35. // REPLACE CONTENT IF NECESSARY WITH NEW STUFF
  36. this.innerHTML = `
  37. <div class="wc-header-container ${this.properties.lob} lob-border-bottom">
  38. <div class="wc-header-inner clearfix">
  39. <div class='container'>
  40. <div class='row'>
  41. <div class='col-md-12'>
  42. <div class="wc-header-lhs pull-left">
  43. <div class="clearfix">
  44. <div class="wc-header-logo pull-left"><img src="/Melify/mtk/dev/tk/lib/components/w3c/assets/dtcc.png"></div>
  45. <div class="wc-header-sep pull-left"></div>
  46. <div class="wc-header-app pull-left">
  47. <div class="wc-header-app-sname clearfix">${this.properties.sname}</div>
  48. <div class="wc-header-app-aname ${this.properties.lob} lob-color clearfix">
  49. <div class="wc-color">${this.properties.aname} <font color=red><small>${this.properties.env}</font></small></div>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="wc-header-rhs pull-right">
  55. <div class="wc-header-top-menus">
  56. ${this.dom.topmenus}
  57. </div>
  58. <div class="wc-header-user">
  59. <div class="clearfix">
  60. <div class="wc-header-cog pull-right">
  61. ${this.dom.cogmenus}
  62. </div>
  63. <div class="wc-header-logout pull-right">
  64. <a href="#" id="header-logout">Logout</a>
  65. </div>
  66. <div class="wc-header-uname pull-right">${this.properties.uname}</div>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="wc-header-network clearfix ${this.properties.lob} lob-pattern">
  75. <div class="wc-header-network-inner clearfix">
  76. <div class='container'>
  77. <div class='row'>
  78. <div class='col-md-12'>
  79. <div class="wc-header-network-lhs pull-left">
  80. <div class="wc-header-bot-menus">
  81. ${this.dom.botmenus}
  82. </div>
  83. </div>
  84. <div class="wc-header-network-rhs pull-right">
  85. <div class="clearfix">
  86. <div class="pull-right"><i class="fa fa-search"></i></div>
  87. <div class="pull-right"><input name="search" value="" class="form-control" style=background:#333!important;border:0!important></div>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </div>`;
  95. // DO DROPDOWN BOTTOM BORDERS NOW
  96. let dropdown = __(".dropdown-menu");
  97. $(dropdown).addClass(this.properties.lob);
  98. $(dropdown).addClass("lob-border-bottom");
  99. if (this.properties.search === "false") {
  100. let search = this.querySelector(".wc-header-network-rhs .clearfix");
  101. $(search).hide();
  102. }
  103. let links = this.querySelectorAll("a");
  104. for (let i=0; i<links.length; i++) {
  105. let link = links[i];
  106. if (link.id != "") {
  107. link.addEventListener("click", function() {
  108. self._onClick(link);
  109. });
  110. }
  111. }
  112. this._finalize();
  113. // SHOW IT NOW (NO FLICKERS)
  114. this.style.visibility = "visible";
  115. console.groupEnd();
  116. };
  117. /**
  118. * Invoked When component is removed. Usually with a .remove() function call
  119. * @disconnectedCallback
  120. */
  121. disconnectedCallback() {
  122. console.group("Header.disconnectedCallback")
  123. /* CLEAN UP NOW */
  124. console.groupEnd();
  125. };
  126. /**
  127. * Called with .setAttribute(...) function call
  128. * @attributeChangedCallback
  129. */
  130. attributeChangedCallback(attr, oldval, newval) {
  131. console.group("Header.attributeChangedCallback:", attr, oldval, newval);
  132. this.properties = this.properties || [];
  133. console.log("attributes: ", this.properties);
  134. // EXAMPLE ONLY. replace with observables
  135. switch(attr)
  136. {
  137. case "uname":
  138. this.properties.uname = newval;
  139. this.style.uname = this.properties.uname;
  140. break;
  141. case "aname":
  142. this.properties.aname = newval;
  143. this.style.aname = this.properties.aname;
  144. break;
  145. case "sname":
  146. this.properties.sname = newval;
  147. this.style.sname = this.properties.sname;
  148. break;
  149. case "env":
  150. this.properties.env = newval;
  151. this.style.env = this.properties.env;
  152. break;
  153. }
  154. console.groupEnd();
  155. };
  156. /**
  157. * Stores DOM elements of interest for future use
  158. * @_fetchElements
  159. */
  160. _fetchElements() {
  161. console.group("Header._fetchElements");
  162. this.dom = this.dom || [];
  163. this.dom.topmenus = this.querySelector("wc-header-topmenus").innerHTML;
  164. this.dom.botmenus = this.querySelector("wc-header-botmenus").innerHTML;
  165. this.dom.cogmenus = this.querySelector("wc-header-cogmenus").innerHTML;
  166. console.groupEnd();
  167. };
  168. /**
  169. * Component attributes are _fetched and defaults are set if undefined
  170. * @_fetchAttributes
  171. * @param {string} [uname=NOTSET] user name
  172. * @param {string} [sname=NOTSET] service name
  173. * @param {string} [aname=NOTSET] application name
  174. * @param {string} env environment
  175. */
  176. _fetchAttributes() {
  177. console.group("Header._fetchAttributes");
  178. this.properties = {
  179. "cname" : "Header",
  180. "author" : "Mel Heravi",
  181. "version" : "1.0",
  182. "uname" : "NOTSET",
  183. "aname" : "NOTSET",
  184. "sname" : "NOTSET",
  185. "env" : "",
  186. };
  187. // SAVE WIDGET SPECIFIC PROPERTIES
  188. this.propertiesW = [];
  189. // SAVE ALL OTHER PROPERTIES
  190. let attrs = wc.getAttributes(this)
  191. for (var key in attrs) {
  192. this.properties[key] = this.getAttribute(key);
  193. this.propertiesW[key] = this.getAttribute(key);
  194. console.log(key + ": " + attrs[key]);
  195. }
  196. console.groupEnd();
  197. };
  198. /**
  199. * A sample callback usage function - see connectedCallback()
  200. * @_onClick
  201. */
  202. _onClick(link) {
  203. console.group("Header._onClick:", link);
  204. wc.publish(this, "wc-header", {
  205. action: "click",
  206. id: link.id
  207. });
  208. console.groupEnd();
  209. };
  210. /**
  211. * Destroy the instance object and artifacts
  212. * @_destroy
  213. */
  214. destroy() {
  215. console.group("Header.destroy:", this.id);
  216. // FREE POINTER
  217. delete this;
  218. // REMOVE ITEM FROM DOM
  219. this.parentNode.removeChild(this);
  220. console.groupEnd();
  221. };
  222. /**
  223. * SAVE DATA FOR ANALYTICS
  224. * @__finalize
  225. */
  226. _finalize() {
  227. console.group("Header._finalize:", this.id);
  228. this.classList.add("wc");
  229. // ADD ANALYTICS HERE
  230. wc.getStats(this, this.properties.cname, this.properties.version);
  231. console.groupEnd();
  232. };
  233. /**
  234. * FOR TESTING PURPOSES
  235. * @test
  236. */
  237. static test() {
  238. console.group("Header.test");
  239. console.log("testing results will be printed here...");
  240. console.groupEnd();
  241. return true;
  242. }
  243. }
  244. window.customElements.define('wc-header', Header);