Source: progressbar.js

  1. /**
  2. * Progressbar Component
  3. * <BR><BR><img src=../images/progressbar.png width=50% style="border:1px lime dashed";>
  4. * <BR><BR><a href="../html/progressbar.html">DEMO</a>
  5. */
  6. class Progressbar extends HTMLElement {
  7. constructor() {
  8. console.group("Progressbar.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("Progressbar.observedAttributes");
  18. this.observables = ["min", "max", "value", "width", "type"];
  19. console.log(this.observables);
  20. console.groupEnd();
  21. return this.observables;
  22. };
  23. /**
  24. * This function is called when this is attached to DOM
  25. * @connectedCallback.
  26. */
  27. connectedCallback() {
  28. console.group("Progressbar.connectedCallback")
  29. let self = this;
  30. // FETCH ALL INTERESTING ELEMENTS
  31. this._fetchElements();
  32. // FETCH ALL ATTRIBUTES
  33. this._fetchAttributes();
  34. // REPLACE CONTENT IF NECESSARY WITH NEW STUFF
  35. this.innerHTML = `
  36. <div class="progress" style=width:${this.properties.width}>
  37. <div class="progress-bar progress-bar-${this.properties.type}" role="progressbar"
  38. aria-valuenow="${this.properties.value}" aria-valuemin="${this.properties.min}"
  39. aria-valuemax="${this.properties.max}" style="width:${this.properties.value}%">${this.properties.value}%</div>
  40. </div>`;
  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("Progressbar.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("Progressbar.attributeChangedCallback:", attr, oldval, newval);
  61. this.properties = this.properties || [];
  62. // EXAMPLE ONLY. replace with observables
  63. switch(attr)
  64. {
  65. case "min":
  66. this.properties.min = newval;
  67. break;
  68. case "max":
  69. this.properties.max = newval;
  70. break;
  71. case "value":
  72. this.properties.value = newval;
  73. let pb = this.querySelector(".progress-bar");
  74. pb.innerHTML = this.properties.value + "%";
  75. pb.value = this.properties.value;
  76. pb.style.width = pb.value+'%';
  77. pb.setAttribute.attr('aria-valuenow', pb.value);
  78. break;
  79. case "width":
  80. this.properties.width = newval;
  81. this.style.width = this.properties.width;
  82. break;
  83. case "type":
  84. this.properties.type = newval;
  85. break;
  86. }
  87. console.groupEnd();
  88. };
  89. /**
  90. * Stores DOM elements of interest for future use
  91. * @_fetchElements
  92. */
  93. _fetchElements() {
  94. console.group("Progressbar._fetchElements");
  95. this.dom = this.dom || [];
  96. this.dom.content = this.innerHTML;
  97. console.groupEnd();
  98. };
  99. /**
  100. * Component attributes are _fetched and defaults are set if undefined
  101. * @_fetchAttributes
  102. * @param {string} [width=100%] progressbar width
  103. * @param {string} [min=0] progressbar width min value
  104. * @param {string} [max=100] progressbar width max value
  105. * @param {string} [value=0] progressbar current value
  106. * @param {string} [type=success] progressbar type
  107. */
  108. _fetchAttributes() {
  109. console.group("Progressbar._fetchAttributes");
  110. this.properties = {
  111. "cname" : "Progressbar",
  112. "author" : "Mel Heravi",
  113. "version" : "1.0",
  114. "min" : "0",
  115. "max" : "100",
  116. "width" : "100%",
  117. "value" : "0",
  118. "type" : "success"
  119. };
  120. // FETCH BACKGROUND
  121. if (this.hasAttribute("min")) {
  122. this.properties.min = this.getAttribute("min");
  123. console.log("min: ", this.properties.min);
  124. }
  125. if (this.hasAttribute("max")) {
  126. this.properties.max = this.getAttribute("max");
  127. console.log("max: ", this.properties.max);
  128. }
  129. if (this.hasAttribute("value")) {
  130. this.properties.value = this.getAttribute("value");
  131. console.log("value: ", this.properties.value);
  132. }
  133. if (this.hasAttribute("width")) {
  134. this.properties.width = this.getAttribute("width");
  135. console.log("width: ", this.properties.width);
  136. }
  137. if (this.hasAttribute("type")) {
  138. this.properties.type = this.getAttribute("type");
  139. console.log("type: ", this.properties.type);
  140. }
  141. console.groupEnd();
  142. };
  143. /**
  144. * A sample callback usage function - see connectedCallback()
  145. * @_onClick
  146. */
  147. _onClick() {
  148. console.group("Progressbar._onClick:", this.id);
  149. wc.publish(this, "wc-progressbar", {
  150. action: "click",
  151. id: this.id
  152. });
  153. console.groupEnd();
  154. };
  155. /**
  156. * Destroy the instance object and artifacts
  157. * @_destroy
  158. */
  159. destroy() {
  160. console.group("Progressbar.destroy:", this.id);
  161. // FREE POINTER
  162. delete this;
  163. // REMOVE ITEM FROM DOM
  164. this.parentNode.removeChild(this);
  165. console.groupEnd();
  166. };
  167. /**
  168. * SAVE DATA FOR ANALYTICS
  169. * @__finalize
  170. */
  171. _finalize() {
  172. console.group("Progressbar._finalize:", this.id);
  173. this.classList.add("wc");
  174. // ADD ANALYTICS HERE
  175. wc.getStats(this, this.properties.cname, this.properties.version);
  176. console.groupEnd();
  177. };
  178. /**
  179. * FOR TESTING PURPOSES
  180. * @test
  181. */
  182. static test() {
  183. console.group("Progressbar.test");
  184. console.log("testing results will be printed here...");
  185. console.groupEnd();
  186. return true;
  187. }
  188. }
  189. window.customElements.define('wc-progressbar', Progressbar);