Source: side-slider.js

  1. /**
  2. * Side-slider Component<BR>
  3. * <BR><BR><img src=/tk/lib/components/w/img/side-slider.png width=30% style="border:1px lime dashed;padding:20px">
  4. * <BR><BR><a href="/tk/lib/components/w/html/side-slider.html">DEMO</a>
  5. */
  6. class SideSlider extends HTMLElement {
  7. constructor() {
  8. wc.group("SideSlider.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("SideSlider.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("SideSlider.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.dom.content;
  36. let sp = this.querySelector(".scotch-panel");
  37. let dv = this.querySelector("div:first-child");
  38. $(".scotch-panel-wrapper, .scotch-panel-content").height(this.properties.height);
  39. $(sp).scotchPanel({
  40. clickSelector: ".toggle-panel",
  41. containerSelector: dv,
  42. direction: this.properties.side,
  43. duration: 300,
  44. transition: "ease-in-out",
  45. clickSelector: ".toggle-panel",
  46. distanceX: this.properties.width,
  47. enableEscapeKey: true
  48. });
  49. // ADD STATS AND OTHER FINAL STUFF
  50. this._finalize();
  51. wc.groupEnd();
  52. };
  53. /**
  54. * Called with .setAttribute(...) function call
  55. * @attributeChangedCallback
  56. */
  57. attributeChangedCallback(attr, oldval, newval) {
  58. wc.group("SideSlider.attributeChangedCallback:", attr, oldval, newval);
  59. this.properties = this.properties || [];
  60. let obs = SideSlider.observedAttributes;
  61. for (let i = 0; i < obs.length; i++) {
  62. if (newval) {
  63. this.properties[obs[i]] = newval;
  64. }
  65. }
  66. // YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
  67. try {
  68. switch(attr)
  69. {
  70. case "header":
  71. //this.querySelector("h1").innerHTML = newval;
  72. break;
  73. default:
  74. break;
  75. }
  76. } catch(e) {
  77. wc.warn(e.name + ' > ' + e.message);
  78. }
  79. wc.groupEnd();
  80. };
  81. /**
  82. * Stores DOM elements of interest for future use
  83. * @private
  84. * @_fetchElements
  85. */
  86. _fetchElements() {
  87. wc.group("SideSlider._fetchElements");
  88. this.dom = this.dom || [];
  89. this.dom.content = this.innerHTML;
  90. wc.groupEnd();
  91. };
  92. /**
  93. * Component attributes are _fetched and defaults are set if undefined
  94. * @private
  95. * @_fetchAttributes
  96. */
  97. _fetchAttributes() {
  98. wc.group("SideSlider._fetchAttributes");
  99. this.properties = {
  100. uparam : "",
  101. cname : "SideSlider",
  102. author : "Mel M. Heravi",
  103. version : "1.0"
  104. };
  105. // SAVE WIDGET SPECIFIC PROPERTIES
  106. this.propertiesW = [];
  107. // SAVE ALL OTHER PROPERTIES
  108. let attrs = wc.getAttributes(this)
  109. for (var key in attrs) {
  110. let attr = this.getAttribute(key) || this.properties.key;
  111. this.properties[key] = this.getAttribute(key);
  112. this.propertiesW[key] = this.getAttribute(key);
  113. wc.log(key + ": " + attrs[key]);
  114. }
  115. // SET ALL INITIAL ATTRIBUTES
  116. for (var key in this.properties) {
  117. switch(key)
  118. {
  119. case "header":
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. wc.log("ATTRIBUTES: ", this.properties);
  126. wc.groupEnd();
  127. };
  128. /**
  129. * configure the instance object and artifacts
  130. * @configure
  131. * @param {string} data use data if exist else use 'this.properties.cfg' parameter
  132. */
  133. configure(data) {
  134. wc.group("SideSlider.configure:", data);
  135. // IF JSON VARIABLE (data) IS PROVIDED
  136. if (data) {
  137. this._process(data);
  138. } else {
  139. let self = this;
  140. $.getJSON(this.properties.cfg, function(data) {
  141. self._process(data);
  142. }).fail(function(jqXHR, textStatus, errorThrown) {
  143. alert("ERROR: INCOMING TEXT " + jqXHR.responseText);
  144. });
  145. }
  146. wc.groupEnd();
  147. };
  148. /**
  149. * _process the instance object and artifacts
  150. * @private
  151. * @_process
  152. */
  153. _process(data) {
  154. wc.group("SideSlider._process:", data);
  155. for (var i = 0; i < data.length; i++) {
  156. //console.log(">>>>>>", data[i]);
  157. }
  158. wc.groupEnd();
  159. };
  160. /**
  161. * Initialize component
  162. * @private
  163. * @_initialize
  164. */
  165. _initialize() {
  166. wc.group("SideSlider._initialize:", this.id);
  167. // FETCH ALL INTERESTING ELEMENTS
  168. this._fetchElements();
  169. // FETCH ALL ATTRIBUTES
  170. this._fetchAttributes();
  171. wc.groupEnd();
  172. };
  173. /**
  174. * Save data for analytics and final wrap up
  175. * @private
  176. * @_finalize
  177. */
  178. _finalize() {
  179. wc.group("SideSlider._finalize:", this.id);
  180. this.classList.add("wc");
  181. // ADD ANALYTICS HERE
  182. wc.setStats(this, this.properties.cname, this.properties.version);
  183. // SHOW IT NOW (NO FLICKERS)
  184. this.style.visibility = "visible";
  185. wc.groupEnd();
  186. };
  187. /**
  188. * Invoked When component is removed. Usually with a .remove() function call
  189. * @disconnectedCallback
  190. */
  191. disconnectedCallback() {
  192. wc.group("SideSlider.disconnectedCallback")
  193. // FREE MEMORY AND CLEANUP
  194. wc.groupEnd();
  195. };
  196. /**
  197. * Destroy the instance object and artifacts
  198. * @destroy
  199. */
  200. destroy() {
  201. wc.group("SideSlider.destroy");
  202. // FREE ALL MEMORY
  203. // you should delete all created objects here
  204. // FREE POINTER
  205. delete this;
  206. // REMOVE ITEM FROM DOM
  207. this.parentNode.removeChild(this);
  208. wc.groupEnd();
  209. };
  210. }
  211. window.customElements.define('wc-side-slider', SideSlider);