diff --git a/dist/autocomplete.js b/dist/autocomplete.js index 42a4c96..a542232 100644 --- a/dist/autocomplete.js +++ b/dist/autocomplete.js @@ -2,7 +2,7 @@ /* * @license MIT * - * Autocomplete.js v2.7.1 + * Autocomplete.js v2.7.2 * Developed by Baptiste Donaux * http://autocomplete-js.com * @@ -182,6 +182,7 @@ var AutoComplete = /** @class */ (function () { return merge; }; AutoComplete.defaults = { + Classname: "autocomplete", Delay: 150, EmptyMessage: "No result here", Highlight: { @@ -210,9 +211,10 @@ var AutoComplete = /** @class */ (function () { if (this.DOMResults.getAttribute("class").indexOf("open") != -1) { var liActive = this.DOMResults.querySelector("li.active"); if (liActive !== null) { + var params = this; event.preventDefault(); this._Select(liActive); - this.DOMResults.setAttribute("class", "autocomplete"); + this.DOMResults.setAttribute("class", params.Classname); } } }, @@ -277,10 +279,11 @@ var AutoComplete = /** @class */ (function () { Not: true }], Callback: function () { + var params = this; var oldValue = this.Input.getAttribute("data-autocomplete-old-value"), currentValue = this._Pre(); if (currentValue !== "" && currentValue.length >= this._MinChars()) { if (!oldValue || currentValue != oldValue) { - this.DOMResults.setAttribute("class", "autocomplete open"); + this.DOMResults.setAttribute("class", params.Classname + " open"); } AutoComplete.prototype.cache(this, function (response) { this._Render(this._Post(response)); @@ -392,9 +395,10 @@ var AutoComplete = /** @class */ (function () { * Manage the open */ _Focus: function () { + var params = this; var oldValue = this.Input.getAttribute("data-autocomplete-old-value"); if ((!oldValue || this.Input.value != oldValue) && this._MinChars() <= this.Input.value.length) { - this.DOMResults.setAttribute("class", "autocomplete open"); + this.DOMResults.setAttribute("class", params.Classname + " open"); } }, /** @@ -411,13 +415,15 @@ var AutoComplete = /** @class */ (function () { }); }, _Close: function () { - this.DOMResults.setAttribute("class", "autocomplete"); + var params = this; + this.DOMResults.setAttribute("class", params.Classname); }, /** * Position the results HTML element */ _Position: function () { - this.DOMResults.setAttribute("class", "autocomplete"); + var params = this; + this.DOMResults.setAttribute("class", params.Classname); this.DOMResults.setAttribute("style", "top:" + (this.Input.offsetTop + this.Input.offsetHeight) + "px;left:" + this.Input.offsetLeft + "px;width:" + this.Input.clientWidth + "px;"); }, /** diff --git a/dist/autocomplete.min.js b/dist/autocomplete.min.js index f1c941b..e7e0d5d 100644 --- a/dist/autocomplete.min.js +++ b/dist/autocomplete.min.js @@ -1,7 +1,7 @@ /* * @license MIT * - * Autocomplete.js v2.7.1 + * Autocomplete.js v2.7.2 * Developed by Baptiste Donaux * http://autocomplete-js.com * @@ -9,6 +9,6 @@ */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.AutoComplete = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i=e.From&&o.keyCode<=e.To?!e.Not:e.Not))};for(var i in t.prototype.getEventsByType(e,n)){var s=t.merge({Operator:ConditionOperator.AND},e.KeyboardMappings[i]),a=ConditionOperator.AND==s.Operator;s.Conditions.forEach(r),!0===a&&s.Callback.call(e,o)}},t.prototype.makeRequest=function(t,e,n){var o=Object.getOwnPropertyNames(t.HttpHeaders),r=new XMLHttpRequest,i=t._HttpMethod(),s=t._Url(),a=t._Pre(),u=encodeURIComponent(t._QueryArg())+"="+encodeURIComponent(a);i.match(/^GET$/i)&&(-1!==s.indexOf("?")?s+="&"+u:s+="?"+u),r.open(i,s,!0);for(var l=o.length-1;l>=0;l--)r.setRequestHeader(o[l],t.HttpHeaders[o[l]]);return r.onreadystatechange=function(){4==r.readyState&&200==r.status?(t.$Cache[a]=r.response,e(r.response)):r.status>=400&&n()},r},t.prototype.ajax=function(e,n,o){void 0===o&&(o=!0),e.$AjaxTimer&&window.clearTimeout(e.$AjaxTimer),!0===o?e.$AjaxTimer=window.setTimeout(t.prototype.ajax.bind(null,e,n,!1),e.Delay):(e.Request&&e.Request.abort(),e.Request=n,e.Request.send(e._QueryArg()+"="+e._Pre()))},t.prototype.cache=function(e,n,o){var r=e._Cache(e._Pre());if(void 0===r){var i=t.prototype.makeRequest(e,n,o);t.prototype.ajax(e,i)}else n(r)},t.prototype.destroy=function(t){for(var e in t.$Listeners)t.Input.removeEventListener(e,t.$Listeners[e]);t.DOMResults.parentNode.removeChild(t.DOMResults)},t.merge=function(){for(var t,e={},n=0;n"+t+""}},HttpHeaders:{"Content-type":"application/x-www-form-urlencoded"},Limit:0,MinChars:0,HttpMethod:"GET",QueryArg:"q",Url:null,KeyboardMappings:{Enter:{Conditions:[{Is:13,Not:!1}],Callback:function(t){if(-1!=this.DOMResults.getAttribute("class").indexOf("open")){var e=this.DOMResults.querySelector("li.active");null!==e&&(t.preventDefault(),this._Select(e),this.DOMResults.setAttribute("class","autocomplete"))}},Operator:ConditionOperator.AND,Event:EventType.KEYDOWN},KeyUpAndDown_down:{Conditions:[{Is:38,Not:!1},{Is:40,Not:!1}],Callback:function(t){t.preventDefault()},Operator:ConditionOperator.OR,Event:EventType.KEYDOWN},KeyUpAndDown_up:{Conditions:[{Is:38,Not:!1},{Is:40,Not:!1}],Callback:function(t){t.preventDefault();var e=this.DOMResults.querySelector("li:first-child:not(.locked)"),n=this.DOMResults.querySelector("li:last-child:not(.locked)"),o=this.DOMResults.querySelector("li.active");if(o){var r=Array.prototype.indexOf.call(o.parentNode.children,o),i=r+(t.keyCode-39),s=this.DOMResults.getElementsByTagName("li").length;i<0?i=s-1:i>=s&&(i=0),o.classList.remove("active"),o.parentElement.children.item(i).classList.add("active")}else n&&38==t.keyCode?n.classList.add("active"):e&&e.classList.add("active")},Operator:ConditionOperator.OR,Event:EventType.KEYUP},AlphaNum:{Conditions:[{Is:13,Not:!0},{From:35,To:40,Not:!0}],Callback:function(){var e=this.Input.getAttribute("data-autocomplete-old-value"),n=this._Pre();""!==n&&n.length>=this._MinChars()?(e&&n==e||this.DOMResults.setAttribute("class","autocomplete open"),t.prototype.cache(this,function(t){this._Render(this._Post(t)),this._Open()}.bind(this),this._Error)):this._Close()},Operator:ConditionOperator.AND,Event:EventType.KEYUP}},DOMResults:null,Request:null,Input:null,_EmptyMessage:function(){return this.Input.hasAttribute("data-autocomplete-empty-message")?this.Input.getAttribute("data-autocomplete-empty-message"):!1!==this.EmptyMessage?this.EmptyMessage:""},_Limit:function(){var t=this.Input.getAttribute("data-autocomplete-limit");return isNaN(t)||null===t?this.Limit:parseInt(t,10)},_MinChars:function(){var t=this.Input.getAttribute("data-autocomplete-minchars");return isNaN(t)||null===t?this.MinChars:parseInt(t,10)},_Highlight:function(t){return t.replace(this.Highlight.getRegex(this._Pre()),this.Highlight.transform)},_HttpMethod:function(){return this.Input.hasAttribute("data-autocomplete-method")?this.Input.getAttribute("data-autocomplete-method"):this.HttpMethod},_QueryArg:function(){return this.Input.hasAttribute("data-autocomplete-param-name")?this.Input.getAttribute("data-autocomplete-param-name"):this.QueryArg},_Url:function(){return this.Input.hasAttribute("data-autocomplete")?this.Input.getAttribute("data-autocomplete"):this.Url},_Blur:function(t){if(void 0===t&&(t=!1),t)this._Close();else{var e=this;setTimeout(function(){e._Blur(!0)},150)}},_Cache:function(t){return this.$Cache[t]},_Focus:function(){var t=this.Input.getAttribute("data-autocomplete-old-value");(!t||this.Input.value!=t)&&this._MinChars()<=this.Input.value.length&&this.DOMResults.setAttribute("class","autocomplete open")},_Open:function(){var t=this;Array.prototype.forEach.call(this.DOMResults.getElementsByTagName("li"),function(e){"locked"!=e.getAttribute("class")&&(e.onclick=function(){t._Select(e)})})},_Close:function(){this.DOMResults.setAttribute("class","autocomplete")},_Position:function(){this.DOMResults.setAttribute("class","autocomplete"),this.DOMResults.setAttribute("style","top:"+(this.Input.offsetTop+this.Input.offsetHeight)+"px;left:"+this.Input.offsetLeft+"px;width:"+this.Input.clientWidth+"px;")},_Render:function(t){var e;e="string"==typeof t?this._RenderRaw(t):this._RenderResponseItems(t),this.DOMResults.hasChildNodes()&&this.DOMResults.removeChild(this.DOMResults.childNodes[0]),this.DOMResults.appendChild(e)},_RenderResponseItems:function(t){var e=document.createElement("ul"),n=document.createElement("li"),o=this._Limit();o<0?t=t.reverse():0===o&&(o=t.length);for(var r=0;r0)this.DOMResults.innerHTML=t;else{var o=this._EmptyMessage();""!==o&&(n.innerHTML=o,n.setAttribute("class","locked"),e.appendChild(n))}return e},_Post:function(t){try{var e=[],n=JSON.parse(t);if(0===Object.keys(n).length)return"";if(Array.isArray(n))for(var o=0;o=e.From&&r.keyCode<=e.To?!e.Not:e.Not))};for(var o in t.prototype.getEventsByType(e,n)){var s=t.merge({Operator:ConditionOperator.AND},e.KeyboardMappings[o]),a=ConditionOperator.AND==s.Operator;s.Conditions.forEach(i),!0===a&&s.Callback.call(e,r)}},t.prototype.makeRequest=function(t,e,n){var r=Object.getOwnPropertyNames(t.HttpHeaders),i=new XMLHttpRequest,o=t._HttpMethod(),s=t._Url(),a=t._Pre(),u=encodeURIComponent(t._QueryArg())+"="+encodeURIComponent(a);o.match(/^GET$/i)&&(-1!==s.indexOf("?")?s+="&"+u:s+="?"+u),i.open(o,s,!0);for(var l=r.length-1;l>=0;l--)i.setRequestHeader(r[l],t.HttpHeaders[r[l]]);return i.onreadystatechange=function(){4==i.readyState&&200==i.status?(t.$Cache[a]=i.response,e(i.response)):i.status>=400&&n()},i},t.prototype.ajax=function(e,n,r){void 0===r&&(r=!0),e.$AjaxTimer&&window.clearTimeout(e.$AjaxTimer),!0===r?e.$AjaxTimer=window.setTimeout(t.prototype.ajax.bind(null,e,n,!1),e.Delay):(e.Request&&e.Request.abort(),e.Request=n,e.Request.send(e._QueryArg()+"="+e._Pre()))},t.prototype.cache=function(e,n,r){var i=e._Cache(e._Pre());if(void 0===i){var o=t.prototype.makeRequest(e,n,r);t.prototype.ajax(e,o)}else n(i)},t.prototype.destroy=function(t){for(var e in t.$Listeners)t.Input.removeEventListener(e,t.$Listeners[e]);t.DOMResults.parentNode.removeChild(t.DOMResults)},t.merge=function(){for(var t,e={},n=0;n"+t+""}},HttpHeaders:{"Content-type":"application/x-www-form-urlencoded"},Limit:0,MinChars:0,HttpMethod:"GET",QueryArg:"q",Url:null,KeyboardMappings:{Enter:{Conditions:[{Is:13,Not:!1}],Callback:function(t){if(-1!=this.DOMResults.getAttribute("class").indexOf("open")){var e=this.DOMResults.querySelector("li.active");if(null!==e){var n=this;t.preventDefault(),this._Select(e),this.DOMResults.setAttribute("class",n.Classname)}}},Operator:ConditionOperator.AND,Event:EventType.KEYDOWN},KeyUpAndDown_down:{Conditions:[{Is:38,Not:!1},{Is:40,Not:!1}],Callback:function(t){t.preventDefault()},Operator:ConditionOperator.OR,Event:EventType.KEYDOWN},KeyUpAndDown_up:{Conditions:[{Is:38,Not:!1},{Is:40,Not:!1}],Callback:function(t){t.preventDefault();var e=this.DOMResults.querySelector("li:first-child:not(.locked)"),n=this.DOMResults.querySelector("li:last-child:not(.locked)"),r=this.DOMResults.querySelector("li.active");if(r){var i=Array.prototype.indexOf.call(r.parentNode.children,r),o=i+(t.keyCode-39),s=this.DOMResults.getElementsByTagName("li").length;o<0?o=s-1:o>=s&&(o=0),r.classList.remove("active"),r.parentElement.children.item(o).classList.add("active")}else n&&38==t.keyCode?n.classList.add("active"):e&&e.classList.add("active")},Operator:ConditionOperator.OR,Event:EventType.KEYUP},AlphaNum:{Conditions:[{Is:13,Not:!0},{From:35,To:40,Not:!0}],Callback:function(){var e=this,n=this.Input.getAttribute("data-autocomplete-old-value"),r=this._Pre();""!==r&&r.length>=this._MinChars()?(n&&r==n||this.DOMResults.setAttribute("class",e.Classname+" open"),t.prototype.cache(this,function(t){this._Render(this._Post(t)),this._Open()}.bind(this),this._Error)):this._Close()},Operator:ConditionOperator.AND,Event:EventType.KEYUP}},DOMResults:null,Request:null,Input:null,_EmptyMessage:function(){return this.Input.hasAttribute("data-autocomplete-empty-message")?this.Input.getAttribute("data-autocomplete-empty-message"):!1!==this.EmptyMessage?this.EmptyMessage:""},_Limit:function(){var t=this.Input.getAttribute("data-autocomplete-limit");return isNaN(t)||null===t?this.Limit:parseInt(t,10)},_MinChars:function(){var t=this.Input.getAttribute("data-autocomplete-minchars");return isNaN(t)||null===t?this.MinChars:parseInt(t,10)},_Highlight:function(t){return t.replace(this.Highlight.getRegex(this._Pre()),this.Highlight.transform)},_HttpMethod:function(){return this.Input.hasAttribute("data-autocomplete-method")?this.Input.getAttribute("data-autocomplete-method"):this.HttpMethod},_QueryArg:function(){return this.Input.hasAttribute("data-autocomplete-param-name")?this.Input.getAttribute("data-autocomplete-param-name"):this.QueryArg},_Url:function(){return this.Input.hasAttribute("data-autocomplete")?this.Input.getAttribute("data-autocomplete"):this.Url},_Blur:function(t){if(void 0===t&&(t=!1),t)this._Close();else{var e=this;setTimeout(function(){e._Blur(!0)},150)}},_Cache:function(t){return this.$Cache[t]},_Focus:function(){var t=this,e=this.Input.getAttribute("data-autocomplete-old-value");(!e||this.Input.value!=e)&&this._MinChars()<=this.Input.value.length&&this.DOMResults.setAttribute("class",t.Classname+" open")},_Open:function(){var t=this;Array.prototype.forEach.call(this.DOMResults.getElementsByTagName("li"),function(e){"locked"!=e.getAttribute("class")&&(e.onclick=function(){t._Select(e)})})},_Close:function(){var t=this;this.DOMResults.setAttribute("class",t.Classname)},_Position:function(){var t=this;this.DOMResults.setAttribute("class",t.Classname),this.DOMResults.setAttribute("style","top:"+(this.Input.offsetTop+this.Input.offsetHeight)+"px;left:"+this.Input.offsetLeft+"px;width:"+this.Input.clientWidth+"px;")},_Render:function(t){var e;e="string"==typeof t?this._RenderRaw(t):this._RenderResponseItems(t),this.DOMResults.hasChildNodes()&&this.DOMResults.removeChild(this.DOMResults.childNodes[0]),this.DOMResults.appendChild(e)},_RenderResponseItems:function(t){var e=document.createElement("ul"),n=document.createElement("li"),r=this._Limit();r<0?t=t.reverse():0===r&&(r=t.length);for(var i=0;i0)this.DOMResults.innerHTML=t;else{var r=this._EmptyMessage();""!==r&&(n.innerHTML=r,n.setAttribute("class","locked"),e.appendChild(n))}return e},_Post:function(t){try{var e=[],n=JSON.parse(t);if(0===Object.keys(n).length)return"";if(Array.isArray(n))for(var r=0;r= this._MinChars()) { if (!oldValue || currentValue != oldValue) { - this.DOMResults.setAttribute("class", "autocomplete open"); + this.DOMResults.setAttribute("class", `${params.Classname} open`); } AutoComplete.prototype.cache( @@ -350,10 +354,11 @@ class AutoComplete { * Manage the open */ _Focus: function(): void { + var params = this; var oldValue: string = this.Input.getAttribute("data-autocomplete-old-value"); if ((!oldValue || this.Input.value != oldValue) && this._MinChars() <= this.Input.value.length){ - this.DOMResults.setAttribute("class", "autocomplete open"); + this.DOMResults.setAttribute("class", `${params.Classname} open`); } }, @@ -372,14 +377,16 @@ class AutoComplete { }, _Close: function(): void { - this.DOMResults.setAttribute("class", "autocomplete"); + var params = this; + this.DOMResults.setAttribute("class", params.Classname); }, /** * Position the results HTML element */ _Position:function(): void { - this.DOMResults.setAttribute("class", "autocomplete"); + var params = this; + this.DOMResults.setAttribute("class", params.Classname); this.DOMResults.setAttribute("style", "top:" + (this.Input.offsetTop + this.Input.offsetHeight) + "px;left:" + this.Input.offsetLeft + "px;width:" + this.Input.clientWidth + "px;"); },