Source: avatar.js

  1. /**
  2. * Avatar Component<BR>
  3. * <BR><BR><img src=/tk/lib/components/w/img/avatar.png width=30% style="border:1px lime dashed;padding:20px">
  4. * <BR><BR><a href="/tk/lib/components/w/html/avatar.html">DEMO</a>
  5. */
  6. class Avatar extends HTMLElement {
  7. constructor() {
  8. wc.group("Avatar.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("Avatar.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("Avatar.connectedCallback")
  28. // ADD A RANDOM ID IF NONE EXIST
  29. if (!this.id) {
  30. this.id = this.constructor.name.toLowerCase() + "-" + wc.uid();
  31. }
  32. // GET PROPERTIES AND INTERESTING ELEMENTS
  33. this._initialize();
  34. // ADD COMPONENT MARKTOP
  35. this.innerHTML = this._template()
  36. // TRANSFER ALL ATTRIBUTES NOW (below is an example)
  37. // -------------------------------------------------
  38. // let widget = this.querySelector("input[type=text]");
  39. // for (var key in this.propertiesW) {
  40. // if (key != "class" && key != "id") {
  41. // this.removeAttribute(key);
  42. // widget.setAttribute(key, this.properties[key]);
  43. // }
  44. // }
  45. // PUBLISH ALL EVENTS OF INTEREST
  46. this._publish();
  47. // ADD STATS AND OTHER FINAL STUFF
  48. this._finalize();
  49. ga('send', {'hitType': 'event','eventCategory': 'wc-connected','eventAction': 'connected','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  50. wc.groupEnd();
  51. };
  52. /**
  53. * Initial Markup
  54. * @private
  55. * @_template
  56. */
  57. _template() {
  58. wc.group("Avatar.template");
  59. let self = this;
  60. var temp = `
  61. <div class="wc-avatar-container" style="width:${this.properties.width};height:${this.properties.height};background:${this.properties.background}">
  62. <div class="wc-avatar-image">
  63. <img src="${this.properties.img}" width="100%">
  64. </div>
  65. <div class="wc-avatar-label">
  66. ${this.properties.title}
  67. </div>
  68. </div>`;
  69. wc.groupEnd();
  70. return temp;
  71. };
  72. /**
  73. * Publish all events of interest
  74. * @private
  75. * @_publish
  76. */
  77. _publish() {
  78. wc.group("Avatar.publish");
  79. let self = this;
  80. this.addEventListener("click", e => {
  81. ga('send', {'hitType': 'event','eventCategory': 'wc-publish','eventAction': 'click','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  82. wc.publish("wc-avatar", {
  83. time: new Date().getTime(),
  84. action: "click",
  85. id: this.id,
  86. uparam: this.getAttribute("uparam")
  87. });
  88. });
  89. wc.groupEnd();
  90. }
  91. /**
  92. * Called with .setAttribute(...) function call
  93. * @attributeChangedCallback
  94. */
  95. attributeChangedCallback(attr, oldval, newval) {
  96. wc.group("Avatar.attributeChangedCallback:", attr, oldval, newval);
  97. this.properties = this.properties || [];
  98. let obs = Avatar.observedAttributes;
  99. for (let i = 0; i < obs.length; i++) {
  100. if (newval) {
  101. this.properties[obs[i]] = newval;
  102. }
  103. }
  104. // YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
  105. try {
  106. switch(attr)
  107. {
  108. case "header":
  109. //this.querySelector("h1").innerHTML = newval;
  110. break;
  111. default:
  112. break;
  113. }
  114. } catch(e) {
  115. wc.warn(e.name + ' > ' + e.message);
  116. }
  117. wc.groupEnd();
  118. };
  119. /**
  120. * Stores DOM elements of interest for future use
  121. * @private
  122. * @_fetchElements
  123. */
  124. _fetchElements() {
  125. wc.group("Avatar._fetchElements");
  126. this.dom = this.dom || [];
  127. this.dom.content = this.innerHTML;
  128. wc.groupEnd();
  129. };
  130. /**
  131. * Component attributes are _fetched and defaults are set if undefined
  132. * @private
  133. * @_fetchAttributes
  134. */
  135. _fetchAttributes() {
  136. wc.group("Avatar._fetchAttributes");
  137. this.properties = {
  138. uparam : "",
  139. cname : "Avatar",
  140. author : "Mel M. Heravi, Ph.D.",
  141. version : "1.0"
  142. };
  143. // SAVE WIDGET SPECIFIC PROPERTIES
  144. this.propertiesW = [];
  145. // SAVE ALL OTHER PROPERTIES
  146. let attrs = wc.getAttributes(this)
  147. for (var key in attrs) {
  148. let attr = this.getAttribute(key) || this.properties.key;
  149. this.properties[key] = this.getAttribute(key);
  150. this.propertiesW[key] = this.getAttribute(key);
  151. wc.log(key + ": " + attrs[key]);
  152. }
  153. // SET ALL INITIAL ATTRIBUTES
  154. for (var key in this.properties) {
  155. switch(key)
  156. {
  157. case "header":
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. wc.log("ATTRIBUTES: ", this.properties);
  164. wc.groupEnd();
  165. };
  166. /**
  167. * configure the instance object and artifacts
  168. * @configure
  169. * @param {string} data use data if exist else use 'this.properties.cfg' parameter
  170. */
  171. configure(data) {
  172. wc.group("Avatar.configure:", data);
  173. // IF JSON VARIABLE (data) IS PROVIDED
  174. if (data) {
  175. this._process(data);
  176. } else {
  177. let self = this;
  178. $.getJSON(this.properties.cfg, function(data) {
  179. self._process(data);
  180. }).fail(function(jqXHR, textStatus, errorThrown) {
  181. alert("ERROR: INCOMING TEXT " + jqXHR.responseText);
  182. });
  183. }
  184. wc.groupEnd();
  185. };
  186. /**
  187. * _process the instance object and artifacts
  188. * @private
  189. * @_process
  190. */
  191. _process(data) {
  192. wc.group("Avatar._process:", data);
  193. // DO WHATEVER WITH THE DATA
  194. wc.groupEnd();
  195. };
  196. /**
  197. * Initialize component
  198. * @private
  199. * @_initialize
  200. */
  201. _initialize() {
  202. wc.group("Avatar._initialize:", this.id);
  203. // FETCH ALL INTERESTING ELEMENTS
  204. this._fetchElements();
  205. // FETCH ALL ATTRIBUTES
  206. this._fetchAttributes();
  207. wc.groupEnd();
  208. };
  209. /**
  210. * Save data for analytics and final wrap up
  211. * @private
  212. * @_finalize
  213. */
  214. _finalize() {
  215. wc.group("Avatar._finalize:", this.id);
  216. this.classList.add("wc");
  217. // ADD ANALYTICS HERE
  218. wc.setStats(this, this.properties.cname, this.properties.version);
  219. // SHOW IT NOW (NO FLICKERS)
  220. this.style.visibility = "visible";
  221. wc.groupEnd();
  222. };
  223. /**
  224. * Invoked When component is removed. Usually with a .remove() function call
  225. * @disconnectedCallback
  226. */
  227. disconnectedCallback() {
  228. wc.group("Avatar.disconnectedCallback")
  229. ga('send', {'hitType': 'event','eventCategory': 'wc-disconnected','eventAction': 'disconnected','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  230. wc.groupEnd();
  231. };
  232. /**
  233. * Destroy the instance object and artifacts
  234. * @destroy
  235. */
  236. destroy() {
  237. wc.group("Avatar.destroy");
  238. // FREE ALL MEMORY
  239. // you should delete all created objects here
  240. // FREE POINTER
  241. delete this;
  242. // REMOVE ITEM FROM DOM
  243. this.parentNode.removeChild(this);
  244. ga('send', {'hitType': 'event','eventCategory': 'wc-destroyed','eventAction': 'distroy','eventLabel': this.properties.cname, 'eventValue':JSON.stringify({'env':wcENV,'app':wcAPP,'url':wcURL})});
  245. wc.groupEnd();
  246. };
  247. }
  248. window.customElements.define('wc-avatar', Avatar);