Source: dragndrop.js

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