Source: table.js

  1. /**
  2. * Table Component<BR>
  3. * <BR><BR><img src=/tk/lib/components/w/img/table.png width=70% style="border:1px lime dashed;padding:20px">
  4. * <BR><BR><a href="/tk/lib/components/w/html/table.html">DEMO</a>
  5. */
  6. class Table extends HTMLElement {
  7. constructor() {
  8. console.group("Table.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("Table.observedAttributes");
  18. this.observables = [];
  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("Table.connectedCallback")
  28. // GET PROPERTIES AND INTERESTING ELEMENTS
  29. this._initialize();
  30. // REPLACE CONTENT IF NECESSARY WITH NEW STUFF
  31. this.innerHTML = `
  32. <div class="table-responsive">
  33. <table class="table" width="100%">
  34. <span id="${this.id}-loading" class="wc-loading-img"></span>
  35. </table>
  36. </div>`
  37. // ADD STATS AND OTHER FINAL STUFF
  38. this._finalize();
  39. // PUBLISH INTERESTING EVENTS
  40. //this._publish();
  41. console.groupEnd();
  42. };
  43. /**
  44. * @_trasferAttr
  45. * @param {string} elem1
  46. * @param {string} elem2
  47. */
  48. _trasferAttr(elem1,elem2) {
  49. console.group("Table._trasferAttr:", elem1, elem2);
  50. // TRANSFER
  51. $.each($(elem1).prop("attributes"), function() {
  52. if (this.name != "id") { // DO NOT COPY ID
  53. console.log("copied:", this.name, this.value);
  54. $(elem2).attr(this.name, this.value);
  55. }
  56. });
  57. console.groupEnd();
  58. };
  59. /**
  60. * Publish all events
  61. * @_publish
  62. */
  63. _publish() {
  64. console.group("Table.publish");
  65. this.addEventListener("click", e => {
  66. this._click(this);
  67. });
  68. console.groupEnd();
  69. return true;
  70. }
  71. /**
  72. * @_click
  73. */
  74. _click() {
  75. console.group("Table._click:", this.id);
  76. wc.publish("wc-table", {
  77. time: new Date().getTime(),
  78. action: "click",
  79. id: this.id,
  80. uparam: this.properties.uparam
  81. });
  82. console.groupEnd();
  83. return true;
  84. };
  85. /**
  86. * Invoked When component is removed. Usually with a .remove() function call
  87. * @disconnectedCallback
  88. */
  89. disconnectedCallback() {
  90. console.group("Table.disconnectedCallback")
  91. /* CLEAN UP NOW */
  92. console.groupEnd();
  93. return true;
  94. };
  95. /**
  96. * Called with .setAttribute(...) function call
  97. * @attributeChangedCallback
  98. */
  99. attributeChangedCallback(attr, oldval, newval) {
  100. console.group("Table.attributeChangedCallback:", attr, oldval, newval);
  101. this.properties = this.properties || [];
  102. let obs = Table.observedAttributes;
  103. for (let i = 0; i < obs.length; i++) {
  104. this.properties[obs[i]] = newval;
  105. }
  106. // YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
  107. try {
  108. switch(attr)
  109. {
  110. case "background":
  111. this.style.background = newval;
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. catch(e) {
  118. console.warn(e.name + ' > ' + e.message);
  119. }
  120. console.groupEnd();
  121. return true;
  122. };
  123. /**
  124. * Stores DOM elements of interest for future use
  125. * @_fetchElements
  126. */
  127. _fetchElements() {
  128. console.group("Table._fetchElements");
  129. this.dom = this.dom || [];
  130. this.dom.content = this.innerHTML;
  131. console.groupEnd();
  132. return true;
  133. };
  134. /**
  135. * Component attributes are _fetched and defaults are set if undefined
  136. * @_fetchAttributes
  137. * @param {string} [modal=true] mode for our table
  138. */
  139. _fetchAttributes() {
  140. console.group("Table._fetchAttributes");
  141. this.properties = {
  142. cname : "table",
  143. author : "Mel Heravi",
  144. version : "1.0",
  145. };
  146. // SAVE WIDGET SPECIFIC PROPERTIES
  147. this.propertiesW = [];
  148. // SAVE ALL OTHER PROPERTIES
  149. let attrs = wc.getAttributes(this)
  150. for (var key in attrs) {
  151. let attr = this.getAttribute(key) || this.properties.key;
  152. this.properties[key] = this.getAttribute(key);
  153. this.propertiesW[key] = this.getAttribute(key);
  154. console.log(key + ": " + attrs[key]);
  155. }
  156. // SET ALL INITIAL ATTRIBUTES
  157. for (var key in this.properties) {
  158. switch(key)
  159. {
  160. case "background":
  161. this.style.background = this.properties.background;
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. console.log("ATTRIBUTES: ", this.properties);
  168. console.groupEnd();
  169. return true;
  170. };
  171. /**
  172. * Destroy the instance object and artifacts
  173. * @_destroy
  174. */
  175. destroy() {
  176. console.group("Table.destroy:", this.id);
  177. // FREE ALL MEMORY
  178. // you should delete all created objects here
  179. // FREE POINTER
  180. delete this;
  181. // REMOVE ITEM FROM DOM
  182. this.parentNode.removeChild(this);
  183. console.groupEnd();
  184. return true;
  185. };
  186. /**
  187. * configure the instance object and artifacts
  188. * @_configure
  189. */
  190. configure(tdata) {
  191. console.group("Table.configure");
  192. let table = this.querySelector("table");
  193. $(table).addClass(tdata.class);
  194. // ADD TABLE HEADER
  195. let tstr = "<thead><tr>"
  196. for (var key in tdata.columns) {
  197. let h = tdata.columns[key];
  198. tstr += `<th id="${h.id}">${h.title}</th>`
  199. }
  200. tstr += "</tr></thead>"
  201. // ADD BODY HEADER
  202. tstr += "<tbody>"
  203. for (var key in tdata.data) {
  204. tstr += "<tr>";
  205. let r = tdata.data[key];
  206. $.each(r, function(i, item) {
  207. tstr += "<td>" + item + "</td>";
  208. });
  209. tstr += "</tr>";
  210. }
  211. tstr += "</tbody>"
  212. $(table).append(tstr);
  213. // ASSIGN A CLASS TO EACH COLUMN
  214. for (let i=1; i < tdata.columns.length + 1; i++) {
  215. $(table).find("td:nth-child(" + i + "), th:nth-child(" + i + ")").addClass("wc-table-col-" + i);
  216. }
  217. // REMOVE LOADING
  218. $("#" + this.id + "-loading").remove();
  219. console.groupEnd();
  220. return true;
  221. };
  222. /**
  223. * SAVE DATA FOR ANALYTICS
  224. * @__initialize
  225. */
  226. _initialize() {
  227. console.group("Table._initialize:", this.id);
  228. // FETCH ALL INTERESTING ELEMENTS
  229. this._fetchElements(this);
  230. // FETCH ALL ATTRIBUTES
  231. this._fetchAttributes(this);
  232. console.groupEnd();
  233. return true;
  234. };
  235. /**
  236. * SAVE DATA FOR ANALYTICS
  237. * @__finalize
  238. */
  239. _finalize() {
  240. console.group("Table._finalize:", this.id);
  241. this.classList.add("wc");
  242. // ADD ANALYTICS HERE
  243. wc.setStats(this, this.properties.cname, this.properties.version);
  244. // SHOW IT NOW (NO FLICKERS)
  245. this.style.visibility = "visible";
  246. console.groupEnd();
  247. return true;
  248. };
  249. }
  250. window.customElements.define('wc-table', Table);