Source: profile.js

  1. /**
  2. * Profile Component - used by "Portal Dashboard"
  3. * <BR><BR><img src=../images/profile.png width=40%>
  4. * <BR><BR><a href="../html/profile.html">DEMO</a>
  5. */
  6. class Profile extends HTMLElement {
  7. constructor() {
  8. console.group("Profile.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("Profile.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("Profile.connectedCallback")
  28. let self = this;
  29. // GET PROPERTIES AND INTERESTING ELEMENTS
  30. this._initialize();
  31. // REPLACE CONTENT IF NECESSARY WITH NEW STUFF
  32. this.innerHTML = `
  33. <div class="wc-profile-lhs">
  34. <div class="wc-profile-header">
  35. <div>
  36. ${this.properties.name}
  37. </div>
  38. </div>
  39. <div class="wc-profile-body">
  40. <table>
  41. <tbody>
  42. <tr>
  43. <td>Phone:</td>
  44. <td>${this.properties.phone}</td>
  45. </tr>
  46. <tr>
  47. <td>Email:</td>
  48. <td><a href=mailto:${this.properties.email}>${this.properties.email}</a></td>
  49. </tr>
  50. <tr>
  51. <td colspan="2"><a href="#" class="profile-link" id="${this.id}-email">Update Email ID</a></td>
  52. </tr>
  53. </tbody>
  54. </table>
  55. </div>
  56. </div>
  57. <div class="wc-profile-rhs">
  58. <i class="fa fa-lock fa-2x"></i>
  59. <div style=line-height:18px;margin-top:10px;>
  60. <a href="#" class="profile-link" id="${this.id}-password">Change your<BR>password online!</a>
  61. </div>
  62. </div>`
  63. let links = this.querySelectorAll(".profile-link");
  64. for (var i=0; i<links.length; i++) {
  65. let link = links[i]
  66. link.addEventListener("click", function() {
  67. self._onClick(link.id);
  68. });
  69. }
  70. // ADD STATS AND OTHER FINAL STUFF
  71. this._finalize();
  72. // SHOW IT NOW (NO FLICKERS)
  73. this.style.visibility = "visible";
  74. console.groupEnd();
  75. };
  76. /**
  77. * Invoked When component is removed. Usually with a .remove() function call
  78. * @disconnectedCallback
  79. */
  80. disconnectedCallback() {
  81. console.group("Profile.disconnectedCallback")
  82. /* CLEAN UP NOW */
  83. console.groupEnd();
  84. };
  85. /**
  86. * Called with .setAttribute(...) function call
  87. * @attributeChangedCallback
  88. */
  89. attributeChangedCallback(attr, oldval, newval) {
  90. console.group("Profile.attributeChangedCallback:", attr, oldval, newval);
  91. this.properties = this.properties || [];
  92. let obs = Profile.observedAttributes;
  93. for (let i = 0; i < obs.length; i++) {
  94. this.properties[obs[i]] = newval;
  95. // YOUR CODE FOR CHANGES GO HERE
  96. }
  97. console.groupEnd();
  98. };
  99. /**
  100. * Stores DOM elements of interest for future use
  101. * @_fetchElements
  102. */
  103. _fetchElements() {
  104. console.group("Profile._fetchElements");
  105. this.dom = this.dom || [];
  106. this.dom.content = this.innerHTML;
  107. console.groupEnd();
  108. };
  109. /**
  110. * Component attributes are _fetched and defaults are set if undefined
  111. * @_fetchAttributes
  112. * @param {string} [name=NOTSET] user name
  113. * @param {string} [phone=NOTSET] user phone number
  114. * @param {string} [email=NOTSET] user email
  115. */
  116. _fetchAttributes() {
  117. console.group("Profile._fetchAttributes");
  118. this.properties = {
  119. "cname" : "Profile",
  120. "author" : "Mel Heravi",
  121. "version" : "1.0",
  122. "name" : "NOTSET",
  123. "phone" : "NOTSET",
  124. "email" : "NOTSET"
  125. };
  126. // SAVE WIDGET SPECIFIC PROPERTIES
  127. this.propertiesW = [];
  128. // SAVE ALL OTHER PROPERTIES
  129. let attrs = wc.getAttributes(this)
  130. for (var key in attrs) {
  131. this.properties[key] = this.getAttribute(key);
  132. this.propertiesW[key] = this.getAttribute(key);
  133. console.log(key + ": " + attrs[key]);
  134. }
  135. console.log("attributes: ", this.properties);
  136. // PROCESS ALL PROPERTIES (example below);
  137. // this.style.background = this.properties.background;
  138. console.groupEnd();
  139. };
  140. /**
  141. * A sample callback usage function - see connectedCallback()
  142. * @_onClick
  143. */
  144. _onClick(id) {
  145. console.group("Profile._onClick:", id);
  146. wc.publish(this, "wc-profile", {
  147. id: id,
  148. action: "click",
  149. uparam: this.properties.uparam
  150. });
  151. console.groupEnd();
  152. };
  153. /**
  154. * Destroy the instance object and artifacts
  155. * @_destroy
  156. */
  157. destroy() {
  158. console.group("Profile.destroy:", this.id);
  159. // FREE POINTER
  160. delete this;
  161. // REMOVE ITEM FROM DOM
  162. this.parentNode.removeChild(this);
  163. console.groupEnd();
  164. };
  165. /**
  166. * SAVE DATA FOR ANALYTICS
  167. * @__initialize
  168. */
  169. _initialize() {
  170. console.group("Profile._initialize:", this.id);
  171. // FETCH ALL INTERESTING ELEMENTS
  172. this._fetchElements();
  173. // FETCH ALL ATTRIBUTES
  174. this._fetchAttributes();
  175. console.groupEnd();
  176. };
  177. /**
  178. * SAVE DATA FOR ANALYTICS
  179. * @__finalize
  180. */
  181. _finalize() {
  182. console.group("Profile._finalize:", this.id);
  183. this.classList.add("wc");
  184. // ADD ANALYTICS HERE
  185. wc.getStats(this, this.properties.cname, this.properties.version);
  186. console.groupEnd();
  187. };
  188. /**
  189. * FOR TESTING PURPOSES
  190. * @test
  191. */
  192. static test() {
  193. console.group("Profile.test");
  194. console.log("testing results will be printed here...");
  195. console.groupEnd();
  196. return true;
  197. }
  198. }
  199. window.customElements.define('wc-profile', Profile);