diff --git a/README.md b/README.md index c6d08da..be1eb5b 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ > Simple autocomplete pure vanilla Javascript library. :rocket: Live Demo **v10** -autoComplete.js is a simple pure vanilla Javascript library that's progressively designed for speed, high versatility and seamless integration with a wide range of projects & systems. (Made for a better developer experience) +autoComplete.js is a simple, pure vanilla Javascript library progressively designed for speed, high versatility, and seamless integration with a wide range of projects & systems. (Made for a better developer experience) ## Features @@ -47,13 +47,13 @@ autoComplete.js is a simple pure vanilla Javascript library that's progressively `JS` ```html - + ``` `CSS` ```html - + ``` #### Package Manager diff --git a/dist/autoComplete.js b/dist/autoComplete.js index edde55f..8d151db 100644 --- a/dist/autoComplete.js +++ b/dist/autoComplete.js @@ -186,22 +186,6 @@ ctx.input = typeof ctx.selector === "string" ? document.querySelector(ctx.selector) : ctx.selector(); }); - var preInit = (function (ctx) { - var callback = function callback(mutations, observer) { - mutations.forEach(function (mutation) { - if (ctx.input) { - observer.disconnect(); - ctx.init(); - } - }); - }; - var observer = new MutationObserver(callback); - observer.observe(document, { - childList: true, - subtree: true - }); - }); - var select$1 = function select(element) { return typeof element === "string" ? document.querySelector(element) : element; }; @@ -641,9 +625,6 @@ function extend (autoComplete) { var prototype = autoComplete.prototype; - prototype.preInit = function () { - preInit(this); - }; prototype.init = function () { init(this); }; @@ -651,6 +632,11 @@ start(this); }; prototype.unInit = function () { + if (this.wrapper) { + var parentNode = this.wrapper.parentNode; + parentNode.insertBefore(this.input, this.wrapper); + parentNode.removeChild(this.wrapper); + } removeEvents(this); }; prototype.open = function () { @@ -693,8 +679,7 @@ }; configure(this); extend.call(this, autoComplete); - var run = this.observe ? preInit : init; - run(this); + init(this); } return autoComplete; diff --git a/dist/autoComplete.js.gz b/dist/autoComplete.js.gz index 8add9d6..b885ba1 100644 Binary files a/dist/autoComplete.js.gz and b/dist/autoComplete.js.gz differ diff --git a/dist/autoComplete.min.js b/dist/autoComplete.min.js index 1a98b0e..ca6a951 100644 --- a/dist/autoComplete.min.js +++ b/dist/autoComplete.min.js @@ -1 +1 @@ -var t,e;t=this,e=function(){"use strict";function t(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function e(e){for(var n=1;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,u=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return u=t.done,t},e:function(t){a=!0,s=t},f:function(){try{u||null==n.return||n.return()}finally{if(a)throw s}}}}(n.keys);try{for(f.s();!(l=f.n()).done;)c(l.value)}catch(t){f.e(t)}finally{f.f()}}else c()})),n.filter&&(a=n.filter(a));var c=a.slice(0,s.maxResults);e.feedback={query:t,matches:a,results:c},d("results",e)},b="aria-expanded",y="aria-activedescendant",g="aria-selected",w=function(t,n){t.feedback.selection=e({index:n},t.feedback.results[n])},O=function(t){t.isOpen||((t.wrapper||t.input).setAttribute(b,!0),t.list.removeAttribute("hidden"),t.isOpen=!0,d("open",t))},A=function(t){t.isOpen&&((t.wrapper||t.input).setAttribute(b,!1),t.input.setAttribute(y,""),t.list.setAttribute("hidden",""),t.isOpen=!1,d("close",t))},S=function(t,e){var n=e.list.getElementsByTagName(e.resultItem.tag);if(e.isOpen&&n.length){var r,o,s=e.cursor;t>=n.length&&(t=0),t<0&&(t=n.length-1),e.cursor=t,s>-1&&(n[s].removeAttribute(g),u&&(o=n[s].classList).remove.apply(o,i(u))),n[t].setAttribute(g,!0),u&&(r=n[t].classList).add.apply(r,i(u)),e.input.setAttribute(y,n[e.cursor].id),e.list.scrollTop=n[t].offsetTop-e.list.clientHeight+n[t].clientHeight+5,e.feedback.cursor=e.cursor,w(e,t),d("navigate",e)}},L=function(t){var e=t.cursor+1;S(e,t)},j=function(t){var e=t.cursor-1;S(e,t)},k=function(t,e,n){(n=n>=0?n:t.cursor)<0||(t.feedback.event=e,w(t,n),d("selection",t),A(t))};function E(t){var n=this;return new Promise((function(r,i){var o,s,u,a,c,f,p;return o=t.input,s=t.query,u=t.trigger,a=t.threshold,c=t.resultsList,f=(p=o)instanceof HTMLInputElement||p instanceof HTMLTextAreaElement?p.value:p.innerHTML,function(t,e,n){return e?e(t):t.length>=n}(f=s?s(f):f,u,a)?m(t).then((function(o){try{return t.feedback instanceof Error?r():(v(f,t),c&&function(t){var n=t.resultsList,r=t.list,i=t.resultItem,o=t.feedback;o.query;var s=o.matches,u=o.results;if(t.cursor=-1,r.innerHTML="",s.length||n.noResults){var a=document.createDocumentFragment();u.forEach((function(t,n){var r=l(i.tag,e({id:"".concat(i.id,"_").concat(n),role:"option",innerHTML:t.match,inside:a},i.class&&{class:i.class}));i.element&&i.element(r,t)})),r.append(a),n.element&&n.element(r,o),O(t)}else A(t)}(t),d.call(n))}catch(t){return i(t)}}),i):(A(t),d.call(n));function d(){return r()}}))}var T=function(t,e){for(var n in t)for(var r in t[n])e(r,n)},I=function(t){var n=t.events;t.trigger;var r=t.debounce,i=t.resultsList,o=function(t,e){var n;return function(){clearTimeout(n),n=setTimeout((function(){return t()}),e)}}((function(){return E(t)}),r),s=t.events=e({input:e({},n&&n.input)},i&&{list:n?e({},n.list):{}}),a={input:{input:function(){o()},keydown:function(e){!function(t,e){var n=t.keyCode,r=e.resultItem.selected;switch(r&&(u=r.split(" ")),n){case 40:case 38:t.preventDefault(),40===n?L(e):j(e);break;case 13:t.preventDefault(),e.cursor>=0&&k(e,t);break;case 9:e.resultsList.tabSelect&&e.cursor>=0?(t.preventDefault(),k(e,t)):A(e);break;case 27:t.preventDefault(),e.input.value="",A(e)}}(e,t)},blur:function(){A(t)}},list:{mousedown:function(t){t.preventDefault()},click:function(e){!function(t,e){var n=e.resultItem.tag.toUpperCase(),r=Array.from(e.list.querySelectorAll(n)),i=t.target.closest(n);if(i&&i.nodeName===n){t.preventDefault();var o=r.indexOf(i);k(e,t,o)}}(e,t)}}};T(a,(function(t,e){(i||"list"!==e)&&(s[e][t]||(s[e][t]=a[e][t]))})),T(s,(function(e,n){t[n].addEventListener(e,s[n][e])}))};function x(t){var n=this;return new Promise((function(r,i){var o,s,u,a,c,f;if(o=t.name,s=t.input,u=t.placeHolder,a=t.resultsList,c=t.data,f={role:"combobox","aria-owns":a.id,"aria-haspopup":!0,"aria-expanded":!1},l(s,e(e({"aria-controls":a.id,"aria-autocomplete":"both"},u&&{placeholder:u}),!t.wrapper&&e({},f))),t.wrapper&&(t.wrapper=l("div",e({around:s,class:o+"_wrapper"},f))),a&&(t.list=l(a.tag,e({dest:["string"==typeof a.destination?document.querySelector(a.destination):a.destination(),a.position],id:a.id,role:"listbox",hidden:"hidden"},a.class&&{class:a.class}))),c.cache)return m(t).then((function(t){try{return p.call(n)}catch(t){return i(t)}}),i);function p(){return I(t),d("init",t),r()}return p.call(n)}))}function P(t){var e=t.prototype;e.preInit=function(){a(this)},e.init=function(){x(this)},e.start=function(){E(this)},e.unInit=function(){var t;T((t=this).events,(function(e,n){t[n].removeEventListener(e,t.events[n][e])}))},e.open=function(){O(this)},e.close=function(){A(this)},e.goTo=function(t){S(t,this)},e.next=function(){L(this)},e.previous=function(){j(this)},e.select=function(t){k(this,null,t)},t.search=e.search=function(t,e,n){h(t,e,n)}}return function t(e){this.options=e,this.id=t.instances=(t.instances||0)+1,this.name="autoComplete",this.wrapper=1,this.threshold=1,this.debounce=0,this.resultsList={position:"afterend",tag:"ul",maxResults:5},this.resultItem={tag:"li"},function(t){var e=t.id,r=t.name,i=t.options,o=t.resultsList,s=t.resultItem;for(var u in i)if("object"===n(i[u]))for(var a in t[u]||(t[u]={}),i[u])t[u][a]=i[u][a];else t[u]=i[u];t.selector=t.selector||"#"+r,o.destination=o.destination||t.selector,o.id=o.id||r+"_list_"+e,s.id=s.id||r+"_result",t.input="string"==typeof t.selector?document.querySelector(t.selector):t.selector()}(this),P.call(this,t),(this.observe?a:x)(this)}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).autoComplete=e(); +var e,t;e=this,t=function(){"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function t(t){for(var n=1;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,a=!0,u=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return a=e.done,e},e:function(e){u=!0,s=e},f:function(){try{a||null==n.return||n.return()}finally{if(u)throw s}}}}(n.keys);try{for(f.s();!(l=f.n()).done;)c(l.value)}catch(e){f.e(e)}finally{f.f()}}else c()})),n.filter&&(u=n.filter(u));var c=u.slice(0,s.maxResults);t.feedback={query:e,matches:u,results:c},p("results",t)},v="aria-expanded",y="aria-activedescendant",b="aria-selected",g=function(e,n){e.feedback.selection=t({index:n},e.feedback.results[n])},w=function(e){e.isOpen||((e.wrapper||e.input).setAttribute(v,!0),e.list.removeAttribute("hidden"),e.isOpen=!0,p("open",e))},O=function(e){e.isOpen&&((e.wrapper||e.input).setAttribute(v,!1),e.input.setAttribute(y,""),e.list.setAttribute("hidden",""),e.isOpen=!1,p("close",e))},A=function(e,t){var n=t.list.getElementsByTagName(t.resultItem.tag);if(t.isOpen&&n.length){var r,o,s=t.cursor;e>=n.length&&(e=0),e<0&&(e=n.length-1),t.cursor=e,s>-1&&(n[s].removeAttribute(b),a&&(o=n[s].classList).remove.apply(o,i(a))),n[e].setAttribute(b,!0),a&&(r=n[e].classList).add.apply(r,i(a)),t.input.setAttribute(y,n[t.cursor].id),t.list.scrollTop=n[e].offsetTop-t.list.clientHeight+n[e].clientHeight+5,t.feedback.cursor=t.cursor,g(t,e),p("navigate",t)}},S=function(e){var t=e.cursor+1;A(t,e)},j=function(e){var t=e.cursor-1;A(t,e)},k=function(e,t,n){(n=n>=0?n:e.cursor)<0||(e.feedback.event=t,g(e,n),p("selection",e),O(e))};function L(e){var n=this;return new Promise((function(r,i){var o,s,a,u,l,f,p;return o=e.input,s=e.query,a=e.trigger,u=e.threshold,l=e.resultsList,f=(p=o)instanceof HTMLInputElement||p instanceof HTMLTextAreaElement?p.value:p.innerHTML,function(e,t,n){return t?t(e):e.length>=n}(f=s?s(f):f,a,u)?h(e).then((function(o){try{return e.feedback instanceof Error?r():(m(f,e),l&&function(e){var n=e.resultsList,r=e.list,i=e.resultItem,o=e.feedback;o.query;var s=o.matches,a=o.results;if(e.cursor=-1,r.innerHTML="",s.length||n.noResults){var u=document.createDocumentFragment();a.forEach((function(e,n){var r=c(i.tag,t({id:"".concat(i.id,"_").concat(n),role:"option",innerHTML:e.match,inside:u},i.class&&{class:i.class}));i.element&&i.element(r,e)})),r.append(u),n.element&&n.element(r,o),w(e)}else O(e)}(e),d.call(n))}catch(e){return i(e)}}),i):(O(e),d.call(n));function d(){return r()}}))}var T=function(e,t){for(var n in e)for(var r in e[n])t(r,n)},E=function(e){var n=e.events;e.trigger;var r=e.debounce,i=e.resultsList,o=function(e,t){var n;return function(){clearTimeout(n),n=setTimeout((function(){return e()}),t)}}((function(){return L(e)}),r),s=e.events=t({input:t({},n&&n.input)},i&&{list:n?t({},n.list):{}}),u={input:{input:function(){o()},keydown:function(t){!function(e,t){var n=e.keyCode,r=t.resultItem.selected;switch(r&&(a=r.split(" ")),n){case 40:case 38:e.preventDefault(),40===n?S(t):j(t);break;case 13:e.preventDefault(),t.cursor>=0&&k(t,e);break;case 9:t.resultsList.tabSelect&&t.cursor>=0?(e.preventDefault(),k(t,e)):O(t);break;case 27:e.preventDefault(),t.input.value="",O(t)}}(t,e)},blur:function(){O(e)}},list:{mousedown:function(e){e.preventDefault()},click:function(t){!function(e,t){var n=t.resultItem.tag.toUpperCase(),r=Array.from(t.list.querySelectorAll(n)),i=e.target.closest(n);if(i&&i.nodeName===n){e.preventDefault();var o=r.indexOf(i);k(t,e,o)}}(t,e)}}};T(u,(function(e,t){(i||"list"!==t)&&(s[t][e]||(s[t][e]=u[t][e]))})),T(s,(function(t,n){e[n].addEventListener(t,s[n][t])}))};function x(e){var n=this;return new Promise((function(r,i){var o,s,a,u,l,f;if(o=e.name,s=e.input,a=e.placeHolder,u=e.resultsList,l=e.data,f={role:"combobox","aria-owns":u.id,"aria-haspopup":!0,"aria-expanded":!1},c(s,t(t({"aria-controls":u.id,"aria-autocomplete":"both"},a&&{placeholder:a}),!e.wrapper&&t({},f))),e.wrapper&&(e.wrapper=c("div",t({around:s,class:o+"_wrapper"},f))),u&&(e.list=c(u.tag,t({dest:["string"==typeof u.destination?document.querySelector(u.destination):u.destination(),u.position],id:u.id,role:"listbox",hidden:"hidden"},u.class&&{class:u.class}))),l.cache)return h(e).then((function(e){try{return d.call(n)}catch(e){return i(e)}}),i);function d(){return E(e),p("init",e),r()}return d.call(n)}))}function I(e){var t=e.prototype;t.init=function(){x(this)},t.start=function(){L(this)},t.unInit=function(){if(this.wrapper){var e=this.wrapper.parentNode;e.insertBefore(this.input,this.wrapper),e.removeChild(this.wrapper)}var t;T((t=this).events,(function(e,n){t[n].removeEventListener(e,t.events[n][e])}))},t.open=function(){w(this)},t.close=function(){O(this)},t.goTo=function(e){A(e,this)},t.next=function(){S(this)},t.previous=function(){j(this)},t.select=function(e){k(this,null,e)},e.search=t.search=function(e,t,n){d(e,t,n)}}return function e(t){this.options=t,this.id=e.instances=(e.instances||0)+1,this.name="autoComplete",this.wrapper=1,this.threshold=1,this.debounce=0,this.resultsList={position:"afterend",tag:"ul",maxResults:5},this.resultItem={tag:"li"},function(e){var t=e.id,r=e.name,i=e.options,o=e.resultsList,s=e.resultItem;for(var a in i)if("object"===n(i[a]))for(var u in e[a]||(e[a]={}),i[a])e[a][u]=i[a][u];else e[a]=i[a];e.selector=e.selector||"#"+r,o.destination=o.destination||e.selector,o.id=o.id||r+"_list_"+t,s.id=s.id||r+"_result",e.input="string"==typeof e.selector?document.querySelector(e.selector):e.selector()}(this),I.call(this,e),x(this)}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).autoComplete=t(); diff --git a/dist/autoComplete.min.js.gz b/dist/autoComplete.min.js.gz index 72934e9..278cd34 100644 Binary files a/dist/autoComplete.min.js.gz and b/dist/autoComplete.min.js.gz differ diff --git a/docs/demo/index.html b/docs/demo/index.html index f0efe86..d5d5f7b 100644 --- a/docs/demo/index.html +++ b/docs/demo/index.html @@ -72,7 +72,7 @@ + href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.1.5/dist/css/autoComplete.min.css"> @@ -150,7 +150,7 @@

mode

- + diff --git a/docs/demo/js/autoComplete.js b/docs/demo/js/autoComplete.js index edde55f..8d151db 100644 --- a/docs/demo/js/autoComplete.js +++ b/docs/demo/js/autoComplete.js @@ -186,22 +186,6 @@ ctx.input = typeof ctx.selector === "string" ? document.querySelector(ctx.selector) : ctx.selector(); }); - var preInit = (function (ctx) { - var callback = function callback(mutations, observer) { - mutations.forEach(function (mutation) { - if (ctx.input) { - observer.disconnect(); - ctx.init(); - } - }); - }; - var observer = new MutationObserver(callback); - observer.observe(document, { - childList: true, - subtree: true - }); - }); - var select$1 = function select(element) { return typeof element === "string" ? document.querySelector(element) : element; }; @@ -641,9 +625,6 @@ function extend (autoComplete) { var prototype = autoComplete.prototype; - prototype.preInit = function () { - preInit(this); - }; prototype.init = function () { init(this); }; @@ -651,6 +632,11 @@ start(this); }; prototype.unInit = function () { + if (this.wrapper) { + var parentNode = this.wrapper.parentNode; + parentNode.insertBefore(this.input, this.wrapper); + parentNode.removeChild(this.wrapper); + } removeEvents(this); }; prototype.open = function () { @@ -693,8 +679,7 @@ }; configure(this); extend.call(this, autoComplete); - var run = this.observe ? preInit : init; - run(this); + init(this); } return autoComplete; diff --git a/docs/demo/js/autoComplete.js.gz b/docs/demo/js/autoComplete.js.gz index 8add9d6..b885ba1 100644 Binary files a/docs/demo/js/autoComplete.js.gz and b/docs/demo/js/autoComplete.js.gz differ diff --git a/docs/demo/js/autoComplete.min.js b/docs/demo/js/autoComplete.min.js index 1a98b0e..ca6a951 100644 --- a/docs/demo/js/autoComplete.min.js +++ b/docs/demo/js/autoComplete.min.js @@ -1 +1 @@ -var t,e;t=this,e=function(){"use strict";function t(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function e(e){for(var n=1;nt.length)&&(e=t.length);for(var n=0,r=new Array(e);n=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,u=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return u=t.done,t},e:function(t){a=!0,s=t},f:function(){try{u||null==n.return||n.return()}finally{if(a)throw s}}}}(n.keys);try{for(f.s();!(l=f.n()).done;)c(l.value)}catch(t){f.e(t)}finally{f.f()}}else c()})),n.filter&&(a=n.filter(a));var c=a.slice(0,s.maxResults);e.feedback={query:t,matches:a,results:c},d("results",e)},b="aria-expanded",y="aria-activedescendant",g="aria-selected",w=function(t,n){t.feedback.selection=e({index:n},t.feedback.results[n])},O=function(t){t.isOpen||((t.wrapper||t.input).setAttribute(b,!0),t.list.removeAttribute("hidden"),t.isOpen=!0,d("open",t))},A=function(t){t.isOpen&&((t.wrapper||t.input).setAttribute(b,!1),t.input.setAttribute(y,""),t.list.setAttribute("hidden",""),t.isOpen=!1,d("close",t))},S=function(t,e){var n=e.list.getElementsByTagName(e.resultItem.tag);if(e.isOpen&&n.length){var r,o,s=e.cursor;t>=n.length&&(t=0),t<0&&(t=n.length-1),e.cursor=t,s>-1&&(n[s].removeAttribute(g),u&&(o=n[s].classList).remove.apply(o,i(u))),n[t].setAttribute(g,!0),u&&(r=n[t].classList).add.apply(r,i(u)),e.input.setAttribute(y,n[e.cursor].id),e.list.scrollTop=n[t].offsetTop-e.list.clientHeight+n[t].clientHeight+5,e.feedback.cursor=e.cursor,w(e,t),d("navigate",e)}},L=function(t){var e=t.cursor+1;S(e,t)},j=function(t){var e=t.cursor-1;S(e,t)},k=function(t,e,n){(n=n>=0?n:t.cursor)<0||(t.feedback.event=e,w(t,n),d("selection",t),A(t))};function E(t){var n=this;return new Promise((function(r,i){var o,s,u,a,c,f,p;return o=t.input,s=t.query,u=t.trigger,a=t.threshold,c=t.resultsList,f=(p=o)instanceof HTMLInputElement||p instanceof HTMLTextAreaElement?p.value:p.innerHTML,function(t,e,n){return e?e(t):t.length>=n}(f=s?s(f):f,u,a)?m(t).then((function(o){try{return t.feedback instanceof Error?r():(v(f,t),c&&function(t){var n=t.resultsList,r=t.list,i=t.resultItem,o=t.feedback;o.query;var s=o.matches,u=o.results;if(t.cursor=-1,r.innerHTML="",s.length||n.noResults){var a=document.createDocumentFragment();u.forEach((function(t,n){var r=l(i.tag,e({id:"".concat(i.id,"_").concat(n),role:"option",innerHTML:t.match,inside:a},i.class&&{class:i.class}));i.element&&i.element(r,t)})),r.append(a),n.element&&n.element(r,o),O(t)}else A(t)}(t),d.call(n))}catch(t){return i(t)}}),i):(A(t),d.call(n));function d(){return r()}}))}var T=function(t,e){for(var n in t)for(var r in t[n])e(r,n)},I=function(t){var n=t.events;t.trigger;var r=t.debounce,i=t.resultsList,o=function(t,e){var n;return function(){clearTimeout(n),n=setTimeout((function(){return t()}),e)}}((function(){return E(t)}),r),s=t.events=e({input:e({},n&&n.input)},i&&{list:n?e({},n.list):{}}),a={input:{input:function(){o()},keydown:function(e){!function(t,e){var n=t.keyCode,r=e.resultItem.selected;switch(r&&(u=r.split(" ")),n){case 40:case 38:t.preventDefault(),40===n?L(e):j(e);break;case 13:t.preventDefault(),e.cursor>=0&&k(e,t);break;case 9:e.resultsList.tabSelect&&e.cursor>=0?(t.preventDefault(),k(e,t)):A(e);break;case 27:t.preventDefault(),e.input.value="",A(e)}}(e,t)},blur:function(){A(t)}},list:{mousedown:function(t){t.preventDefault()},click:function(e){!function(t,e){var n=e.resultItem.tag.toUpperCase(),r=Array.from(e.list.querySelectorAll(n)),i=t.target.closest(n);if(i&&i.nodeName===n){t.preventDefault();var o=r.indexOf(i);k(e,t,o)}}(e,t)}}};T(a,(function(t,e){(i||"list"!==e)&&(s[e][t]||(s[e][t]=a[e][t]))})),T(s,(function(e,n){t[n].addEventListener(e,s[n][e])}))};function x(t){var n=this;return new Promise((function(r,i){var o,s,u,a,c,f;if(o=t.name,s=t.input,u=t.placeHolder,a=t.resultsList,c=t.data,f={role:"combobox","aria-owns":a.id,"aria-haspopup":!0,"aria-expanded":!1},l(s,e(e({"aria-controls":a.id,"aria-autocomplete":"both"},u&&{placeholder:u}),!t.wrapper&&e({},f))),t.wrapper&&(t.wrapper=l("div",e({around:s,class:o+"_wrapper"},f))),a&&(t.list=l(a.tag,e({dest:["string"==typeof a.destination?document.querySelector(a.destination):a.destination(),a.position],id:a.id,role:"listbox",hidden:"hidden"},a.class&&{class:a.class}))),c.cache)return m(t).then((function(t){try{return p.call(n)}catch(t){return i(t)}}),i);function p(){return I(t),d("init",t),r()}return p.call(n)}))}function P(t){var e=t.prototype;e.preInit=function(){a(this)},e.init=function(){x(this)},e.start=function(){E(this)},e.unInit=function(){var t;T((t=this).events,(function(e,n){t[n].removeEventListener(e,t.events[n][e])}))},e.open=function(){O(this)},e.close=function(){A(this)},e.goTo=function(t){S(t,this)},e.next=function(){L(this)},e.previous=function(){j(this)},e.select=function(t){k(this,null,t)},t.search=e.search=function(t,e,n){h(t,e,n)}}return function t(e){this.options=e,this.id=t.instances=(t.instances||0)+1,this.name="autoComplete",this.wrapper=1,this.threshold=1,this.debounce=0,this.resultsList={position:"afterend",tag:"ul",maxResults:5},this.resultItem={tag:"li"},function(t){var e=t.id,r=t.name,i=t.options,o=t.resultsList,s=t.resultItem;for(var u in i)if("object"===n(i[u]))for(var a in t[u]||(t[u]={}),i[u])t[u][a]=i[u][a];else t[u]=i[u];t.selector=t.selector||"#"+r,o.destination=o.destination||t.selector,o.id=o.id||r+"_list_"+e,s.id=s.id||r+"_result",t.input="string"==typeof t.selector?document.querySelector(t.selector):t.selector()}(this),P.call(this,t),(this.observe?a:x)(this)}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).autoComplete=e(); +var e,t;e=this,t=function(){"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function t(t){for(var n=1;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,a=!0,u=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return a=e.done,e},e:function(e){u=!0,s=e},f:function(){try{a||null==n.return||n.return()}finally{if(u)throw s}}}}(n.keys);try{for(f.s();!(l=f.n()).done;)c(l.value)}catch(e){f.e(e)}finally{f.f()}}else c()})),n.filter&&(u=n.filter(u));var c=u.slice(0,s.maxResults);t.feedback={query:e,matches:u,results:c},p("results",t)},v="aria-expanded",y="aria-activedescendant",b="aria-selected",g=function(e,n){e.feedback.selection=t({index:n},e.feedback.results[n])},w=function(e){e.isOpen||((e.wrapper||e.input).setAttribute(v,!0),e.list.removeAttribute("hidden"),e.isOpen=!0,p("open",e))},O=function(e){e.isOpen&&((e.wrapper||e.input).setAttribute(v,!1),e.input.setAttribute(y,""),e.list.setAttribute("hidden",""),e.isOpen=!1,p("close",e))},A=function(e,t){var n=t.list.getElementsByTagName(t.resultItem.tag);if(t.isOpen&&n.length){var r,o,s=t.cursor;e>=n.length&&(e=0),e<0&&(e=n.length-1),t.cursor=e,s>-1&&(n[s].removeAttribute(b),a&&(o=n[s].classList).remove.apply(o,i(a))),n[e].setAttribute(b,!0),a&&(r=n[e].classList).add.apply(r,i(a)),t.input.setAttribute(y,n[t.cursor].id),t.list.scrollTop=n[e].offsetTop-t.list.clientHeight+n[e].clientHeight+5,t.feedback.cursor=t.cursor,g(t,e),p("navigate",t)}},S=function(e){var t=e.cursor+1;A(t,e)},j=function(e){var t=e.cursor-1;A(t,e)},k=function(e,t,n){(n=n>=0?n:e.cursor)<0||(e.feedback.event=t,g(e,n),p("selection",e),O(e))};function L(e){var n=this;return new Promise((function(r,i){var o,s,a,u,l,f,p;return o=e.input,s=e.query,a=e.trigger,u=e.threshold,l=e.resultsList,f=(p=o)instanceof HTMLInputElement||p instanceof HTMLTextAreaElement?p.value:p.innerHTML,function(e,t,n){return t?t(e):e.length>=n}(f=s?s(f):f,a,u)?h(e).then((function(o){try{return e.feedback instanceof Error?r():(m(f,e),l&&function(e){var n=e.resultsList,r=e.list,i=e.resultItem,o=e.feedback;o.query;var s=o.matches,a=o.results;if(e.cursor=-1,r.innerHTML="",s.length||n.noResults){var u=document.createDocumentFragment();a.forEach((function(e,n){var r=c(i.tag,t({id:"".concat(i.id,"_").concat(n),role:"option",innerHTML:e.match,inside:u},i.class&&{class:i.class}));i.element&&i.element(r,e)})),r.append(u),n.element&&n.element(r,o),w(e)}else O(e)}(e),d.call(n))}catch(e){return i(e)}}),i):(O(e),d.call(n));function d(){return r()}}))}var T=function(e,t){for(var n in e)for(var r in e[n])t(r,n)},E=function(e){var n=e.events;e.trigger;var r=e.debounce,i=e.resultsList,o=function(e,t){var n;return function(){clearTimeout(n),n=setTimeout((function(){return e()}),t)}}((function(){return L(e)}),r),s=e.events=t({input:t({},n&&n.input)},i&&{list:n?t({},n.list):{}}),u={input:{input:function(){o()},keydown:function(t){!function(e,t){var n=e.keyCode,r=t.resultItem.selected;switch(r&&(a=r.split(" ")),n){case 40:case 38:e.preventDefault(),40===n?S(t):j(t);break;case 13:e.preventDefault(),t.cursor>=0&&k(t,e);break;case 9:t.resultsList.tabSelect&&t.cursor>=0?(e.preventDefault(),k(t,e)):O(t);break;case 27:e.preventDefault(),t.input.value="",O(t)}}(t,e)},blur:function(){O(e)}},list:{mousedown:function(e){e.preventDefault()},click:function(t){!function(e,t){var n=t.resultItem.tag.toUpperCase(),r=Array.from(t.list.querySelectorAll(n)),i=e.target.closest(n);if(i&&i.nodeName===n){e.preventDefault();var o=r.indexOf(i);k(t,e,o)}}(t,e)}}};T(u,(function(e,t){(i||"list"!==t)&&(s[t][e]||(s[t][e]=u[t][e]))})),T(s,(function(t,n){e[n].addEventListener(t,s[n][t])}))};function x(e){var n=this;return new Promise((function(r,i){var o,s,a,u,l,f;if(o=e.name,s=e.input,a=e.placeHolder,u=e.resultsList,l=e.data,f={role:"combobox","aria-owns":u.id,"aria-haspopup":!0,"aria-expanded":!1},c(s,t(t({"aria-controls":u.id,"aria-autocomplete":"both"},a&&{placeholder:a}),!e.wrapper&&t({},f))),e.wrapper&&(e.wrapper=c("div",t({around:s,class:o+"_wrapper"},f))),u&&(e.list=c(u.tag,t({dest:["string"==typeof u.destination?document.querySelector(u.destination):u.destination(),u.position],id:u.id,role:"listbox",hidden:"hidden"},u.class&&{class:u.class}))),l.cache)return h(e).then((function(e){try{return d.call(n)}catch(e){return i(e)}}),i);function d(){return E(e),p("init",e),r()}return d.call(n)}))}function I(e){var t=e.prototype;t.init=function(){x(this)},t.start=function(){L(this)},t.unInit=function(){if(this.wrapper){var e=this.wrapper.parentNode;e.insertBefore(this.input,this.wrapper),e.removeChild(this.wrapper)}var t;T((t=this).events,(function(e,n){t[n].removeEventListener(e,t.events[n][e])}))},t.open=function(){w(this)},t.close=function(){O(this)},t.goTo=function(e){A(e,this)},t.next=function(){S(this)},t.previous=function(){j(this)},t.select=function(e){k(this,null,e)},e.search=t.search=function(e,t,n){d(e,t,n)}}return function e(t){this.options=t,this.id=e.instances=(e.instances||0)+1,this.name="autoComplete",this.wrapper=1,this.threshold=1,this.debounce=0,this.resultsList={position:"afterend",tag:"ul",maxResults:5},this.resultItem={tag:"li"},function(e){var t=e.id,r=e.name,i=e.options,o=e.resultsList,s=e.resultItem;for(var a in i)if("object"===n(i[a]))for(var u in e[a]||(e[a]={}),i[a])e[a][u]=i[a][u];else e[a]=i[a];e.selector=e.selector||"#"+r,o.destination=o.destination||e.selector,o.id=o.id||r+"_list_"+t,s.id=s.id||r+"_result",e.input="string"==typeof e.selector?document.querySelector(e.selector):e.selector()}(this),I.call(this,e),x(this)}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).autoComplete=t(); diff --git a/docs/demo/js/autoComplete.min.js.gz b/docs/demo/js/autoComplete.min.js.gz index 72934e9..278cd34 100644 Binary files a/docs/demo/js/autoComplete.min.js.gz and b/docs/demo/js/autoComplete.min.js.gz differ diff --git a/docs/index.html b/docs/index.html index 495d8bb..416839d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -158,7 +158,7 @@ src="//platform-api.sharethis.com/js/sharethis.js#property=5c213660c276020011d38212&product=inline-share-buttons" async="async"> + href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.1.5/dist/css/autoComplete.min.css"> @@ -209,7 +209,7 @@ mustache: { data: ["../package.json", { minVersion: "10.1", - version: "10.1.4" + version: "10.1.5" }] } } @@ -224,7 +224,7 @@ - + \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index b789ea2..57b87b6 100644 --- a/docs/index.md +++ b/docs/index.md @@ -32,8 +32,7 @@ ## What it is? -autoComplete.js is a simple pure vanilla Javascript library that's progressively designed for speed, -high versatility and seamless integration with a wide range of projects & systems. +autoComplete.js is a simple, pure vanilla Javascript library progressively designed for speed, high versatility, and seamless integration with a wide range of projects & systems. (Made for a better developer experience) ## Features diff --git a/docs/release-notes.md b/docs/release-notes.md index 597368b..59cb328 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -35,8 +35,12 @@ For more information on semantic versioning, please visit . *** -### v10.1.4 โœจ -- ๐Ÿ”ง Fixed: Unresolved dependencies when building a svelte app (Thanks ๐Ÿ‘ @sunshineplan) #243 +### v10.1.5 โœจ +- ๐Ÿงน Removed: `preInit` stage (Thanks ๐Ÿ‘ @folknor) #229 +- ๐Ÿ”ง Fixed: `unInit` to remove the `wrapper` element (Thanks ๐Ÿ‘ @deniseismo) #245 + +### v10.1.4 +- ๐Ÿ”ง Fixed: Unresolved dependencies when building a Svelte app (Thanks ๐Ÿ‘ @sunshineplan) #243 ### v10.1.3 - ๐Ÿ”ง Fixed: `mark` tag's invalid "classes" to "class" attribute (Thanks ๐Ÿ‘ @50kudos) #242 diff --git a/package.json b/package.json index 87803fd..c075039 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "10.1.4", + "version": "10.1.5", "name": "@tarekraafat/autocomplete.js", "description": "Simple autocomplete pure vanilla Javascript library.", "keywords": [ diff --git a/src/autoComplete.js b/src/autoComplete.js index 25baa2b..656209d 100644 --- a/src/autoComplete.js +++ b/src/autoComplete.js @@ -1,6 +1,5 @@ import configure from "./services/configure"; import extend from "./services/extend"; -import preInit from "./services/preInit"; import init from "./services/init"; /** @@ -66,8 +65,6 @@ export default function autoComplete(config) { configure(this); // Stage API methods extend.call(this, autoComplete); - // Set to run "preInit" if "observer" enabled else "init" - const run = this.observe ? preInit : init; - // Run autoComplete.js - run(this); + // Initialize autoComplete.js + init(this); } diff --git a/src/controllers/listController.js b/src/controllers/listController.js index c236565..26dab9c 100644 --- a/src/controllers/listController.js +++ b/src/controllers/listController.js @@ -273,7 +273,7 @@ const navigate = function (event, ctx) { case 27: event.preventDefault(); - // Clear "inputField" value + // Clear "input" value ctx.input.value = ""; close(ctx); diff --git a/src/helpers/eventEmitter.js b/src/helpers/eventEmitter.js index 8c6d5c3..05767e2 100644 --- a/src/helpers/eventEmitter.js +++ b/src/helpers/eventEmitter.js @@ -5,6 +5,6 @@ * @param {Object} ctx - autoComplete.js context */ export default (name, ctx) => { - // Dispatch event on "inputField" + // Dispatch event on "input" ctx.input.dispatchEvent(new CustomEvent(name, { bubbles: true, detail: ctx.feedback, cancelable: true })); }; diff --git a/src/helpers/io.js b/src/helpers/io.js index 55800af..5408481 100644 --- a/src/helpers/io.js +++ b/src/helpers/io.js @@ -44,7 +44,7 @@ const create = (tag, options) => { }; /** - * Get the "inputField" query value + * Get the "input" query value * * @param {Element} field - input or textarea element * @@ -59,7 +59,7 @@ const getQuery = (field) => * @param {String} value - user's raw search query value * @param {Object} diacritics - formatting on/off * - * @returns {String} - Raw "inputField" value as a string + * @returns {String} - Raw "input" value as a string */ const format = (value, diacritics) => { value = value.toString().toLowerCase(); diff --git a/src/services/extend.js b/src/services/extend.js index d597d9f..5028733 100644 --- a/src/services/extend.js +++ b/src/services/extend.js @@ -1,4 +1,3 @@ -import preInit from "./preInit"; import init from "./init"; import start from "./start"; import { removeEvents } from "../controllers/eventController"; @@ -13,11 +12,6 @@ import search from "../controllers/searchController"; export default function (autoComplete) { const { prototype } = autoComplete; - // Pre-Initialize autoComplete.js engine - prototype.preInit = function () { - preInit(this); - }; - // Initialize autoComplete.js engine prototype.init = function () { init(this); @@ -30,6 +24,13 @@ export default function (autoComplete) { // Un-Initialize autoComplete.js engine prototype.unInit = function () { + if (this.wrapper) { + const parentNode = this.wrapper.parentNode; + + parentNode.insertBefore(this.input, this.wrapper); + parentNode.removeChild(this.wrapper); + } + removeEvents(this); }; diff --git a/src/services/init.js b/src/services/init.js index 014d129..7f042c9 100644 --- a/src/services/init.js +++ b/src/services/init.js @@ -18,7 +18,7 @@ export default async function (ctx) { "aria-expanded": false, }; - // Set "inputField" attributes + // Set "input" attributes create(input, { "aria-controls": resultsList.id, "aria-autocomplete": "both", diff --git a/src/services/preInit.js b/src/services/preInit.js deleted file mode 100644 index 5bf06ce..0000000 --- a/src/services/preInit.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Pre-Initialization stage - * - * @param {Object} ctx - autoComplete.js context - */ -export default (ctx) => { - // Callback function to execute when mutations are observed - const callback = (mutations, observer) => { - mutations.forEach((mutation) => { - // Check if "inputField" added to the DOM - if (ctx.input) { - // If yes disconnect the observer - observer.disconnect(); - // Initialize autoComplete.js - ctx.init(); - } - }); - }; - // Create mutation observer instance - const observer = new MutationObserver(callback); - // Start observing the entire DOM until "inputField" is present - // The entire document will be observed for all changes - observer.observe(document, { childList: true, subtree: true }); -}; diff --git a/src/services/start.js b/src/services/start.js index f451493..8a47653 100644 --- a/src/services/start.js +++ b/src/services/start.js @@ -10,7 +10,7 @@ import { render, close } from "../controllers/listController"; export default async function (ctx) { const { input, query, trigger, threshold, resultsList } = ctx; - // Get "inputField" query value + // Get "input" query value let queryVal = getQuery(input); queryVal = query ? query(queryVal) : queryVal; // Get trigger decision