Source: autocomplete.js

  1. /**
  2. * Autocomplete Component<BR>
  3. * <BR><BR><img src=/tk/lib/components/w/img/autocomplete.png width=30% style="border:1px lime dashed;padding:20px">
  4. * <BR><BR><a href="/tk/lib/components/w/html/autocomplete.html">DEMO</a>
  5. */
  6. class Autocomplete extends HTMLElement {
  7. constructor() {
  8. wc.group("Autocomplete.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("Autocomplete.observedAttributes");
  18. this.observables = [];
  19. wc.groupEnd();
  20. return this.observables;
  21. };
  22. /**
  23. * Initial Markup
  24. * @private
  25. * @_template
  26. */
  27. _template() {
  28. wc.group("Autocomplete.template");
  29. var temp = this.dom.content;
  30. wc.groupEnd();
  31. return temp;
  32. };
  33. /**
  34. * Called when this is attached to DOM
  35. * @connectedCallback.
  36. */
  37. connectedCallback() {
  38. wc.group("Autocomplete.connectedCallback")
  39. let self = this;
  40. // GET PROPERTIES AND INTERESTING ELEMENTS
  41. this._initialize();
  42. // ADD COMPONENT MARKTOP
  43. this.innerHTML = `<input class="form-control form-control-lg" autofocus type="text" name="q" placeholder="ready ...">`
  44. this.cache = {};
  45. var demo1 = new autoComplete({
  46. selector: 'input[name="q"]',
  47. minChars: 1,
  48. source: function(term, response){
  49. // EMPTY CACHE
  50. self.cache = {};
  51. $.getJSON(self.properties.url, {q:term}, function (data) {
  52. let d = [];
  53. for (var i = 0; i < data.length; i++) {
  54. self.cache[data[i].short] = data[i];
  55. d.push(data[i].long);
  56. }
  57. let input = self.querySelector("input");
  58. // if(d.length == 0) {
  59. // wc.log(">>>>>>>>>", d.length);
  60. // $(input).css({
  61. // "border-color":"red",
  62. // "color": "red"
  63. // })
  64. // wc.timeout(function(){
  65. // $(input).css({
  66. // "border-color":"steelblue",
  67. // "color": "steelblue"
  68. // })
  69. // }, 2000);
  70. // }
  71. response(d);
  72. }).fail(function(jqxhr, textStatus, error){
  73. var err = textStatus + ', ' + error;
  74. alert("REQUEST FAILED: more info in developer console " + err);
  75. });
  76. },
  77. onSelect: function(e, term, item){
  78. self._publish($(item).text());
  79. }
  80. });
  81. // ADD STATS AND OTHER FINAL STUFF
  82. this._finalize();
  83. wc.groupEnd();
  84. };
  85. /**
  86. * Publish all events of interest
  87. * @private
  88. * @_publish
  89. */
  90. _publish(name) {
  91. wc.group("Autocomplete.publish:", name);
  92. wc.publish("wc-autocomplete", {
  93. time: new Date().getTime(),
  94. action: "selected",
  95. uparam: this.cache[name]
  96. });
  97. wc.groupEnd();
  98. }
  99. /**
  100. * Called with .setAttribute(...) function call
  101. * @attributeChangedCallback
  102. */
  103. attributeChangedCallback(attr, oldval, newval) {
  104. wc.group("Autocomplete.attributeChangedCallback:", attr, oldval, newval);
  105. this.properties = this.properties || [];
  106. let obs = Autocomplete.observedAttributes;
  107. for (let i = 0; i < obs.length; i++) {
  108. if (newval) {
  109. this.properties[obs[i]] = newval;
  110. }
  111. }
  112. // YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
  113. try {
  114. switch(attr)
  115. {
  116. case "header":
  117. break;
  118. default:
  119. break;
  120. }
  121. } catch(e) {
  122. wc.warn(e.name + ' > ' + e.message);
  123. }
  124. wc.groupEnd();
  125. };
  126. /**
  127. * Stores DOM elements of interest for future use
  128. * @private
  129. * @_fetchElements
  130. */
  131. _fetchElements() {
  132. wc.group("Autocomplete._fetchElements");
  133. this.dom = this.dom || [];
  134. this.dom.content = this.innerHTML;
  135. wc.groupEnd();
  136. };
  137. /**
  138. * Component attributes are _fetched and defaults are set if undefined
  139. * @private
  140. * @_fetchAttributes
  141. */
  142. _fetchAttributes() {
  143. wc.group("Autocomplete._fetchAttributes");
  144. this.properties = {
  145. cname : "Autocomplete",
  146. author : "Mel M. Heravi",
  147. version : "1.0"
  148. };
  149. // SAVE WIDGET SPECIFIC PROPERTIES
  150. this.propertiesW = [];
  151. // SAVE ALL OTHER PROPERTIES
  152. let attrs = wc.getAttributes(this)
  153. for (var key in attrs) {
  154. let attr = this.getAttribute(key) || this.properties.key;
  155. this.properties[key] = this.getAttribute(key);
  156. this.propertiesW[key] = this.getAttribute(key);
  157. wc.log(key + ": " + attrs[key]);
  158. }
  159. // SET ALL INITIAL ATTRIBUTES
  160. for (var key in this.properties) {
  161. switch(key)
  162. {
  163. case "header":
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. wc.log("ATTRIBUTES: ", this.properties);
  170. wc.groupEnd();
  171. };
  172. /**
  173. * configure the instance object and artifacts
  174. * @configure
  175. */
  176. configure(aname) {
  177. wc.group("Autocomplete.configure:", aname);
  178. // IF JSON VARIABLE (aname) IS PROVIDED
  179. if (aname) {
  180. this._process(aname);
  181. } else {
  182. let self = this;
  183. $.getJSON(this.properties.cfg, function(data) {
  184. self._process(data);
  185. }).fail(function(jqXHR, textStatus, errorThrown) {
  186. alert("ERROR: INCOMING TEXT " + jqXHR.responseText);
  187. });
  188. }
  189. wc.groupEnd();
  190. };
  191. /**
  192. * _process the instance object and artifacts
  193. * @private
  194. * @_process
  195. */
  196. _process(data) {
  197. wc.group("Autocomplete._process:", data);
  198. // DO WHATEVER WITH THE DATA
  199. wc.groupEnd();
  200. };
  201. /**
  202. * Initialize component
  203. * @private
  204. * @_initialize
  205. */
  206. _initialize() {
  207. wc.group("Autocomplete._initialize:", this.id);
  208. // FETCH ALL INTERESTING ELEMENTS
  209. this._fetchElements();
  210. // FETCH ALL ATTRIBUTES
  211. this._fetchAttributes();
  212. wc.groupEnd();
  213. };
  214. /**
  215. * Save data for analytics and final wrap up
  216. * @private
  217. * @_finalize
  218. */
  219. _finalize() {
  220. wc.group("Autocomplete._finalize:", this.id);
  221. this.classList.add("wc");
  222. // ADD ANALYTICS HERE
  223. wc.setStats(this, this.properties.cname, this.properties.version);
  224. // SHOW IT NOW (NO FLICKERS)
  225. this.style.visibility = "visible";
  226. wc.groupEnd();
  227. };
  228. /**
  229. * Invoked When component is removed. Usually with a .remove() function call
  230. * @disconnectedCallback
  231. */
  232. disconnectedCallback() {
  233. wc.group("Autocomplete.disconnectedCallback")
  234. // FREE MEMORY AND CLEANUP
  235. wc.groupEnd();
  236. };
  237. }
  238. window.customElements.define('wc-autocomplete', Autocomplete);