autocomplete-js
Advanced tools
Comparing version 2.0.2 to 2.0.3
/* | ||
* Autocomplete.js v2.0.2 | ||
* Autocomplete.js v2.0.3 | ||
* Developed by Baptiste Donaux | ||
@@ -40,3 +40,3 @@ * http://autocomplete-js.com | ||
AutoComplete.prototype.create(AutoComplete.merge(AutoComplete.defaults, params, { | ||
DOMResults: document.createElement("div"), | ||
DOMResults: document.createElement("div") | ||
}), selector); | ||
@@ -109,3 +109,3 @@ } | ||
} | ||
var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders), method = params._HttpMethod(), url = params._Url(), queryParams = params.QueryArg + "=" + params._Pre(); | ||
var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders), method = params._HttpMethod(), url = params._Url(), queryParams = params._QueryArg() + "=" + params._Pre(); | ||
if (method.match(/^GET$/i)) { | ||
@@ -406,8 +406,6 @@ url += "?" + queryParams; | ||
$AjaxTimer: null, | ||
$Listeners: {}, | ||
$Listeners: {} | ||
}; | ||
return AutoComplete; | ||
}()); | ||
//# sourceMappingURL=autocomplete.js.map | ||
; | ||
//# sourceMappingURL=autocomplete.js.map |
@@ -1,1 +0,1 @@ | ||
"use strict";var ConditionOperator;!function(t){t[t.AND=0]="AND",t[t.OR=1]="OR"}(ConditionOperator||(ConditionOperator={}));var AutoComplete=function(){function t(e,s){if(void 0===e&&(e={}),void 0===s&&(s="[data-autocomplete]"),Array.isArray(s))s.forEach(function(s){new t(e,s)});else if("string"==typeof s){var o=document.querySelectorAll(s);Array.prototype.forEach.call(o,function(s){new t(e,s)})}else t.prototype.create(t.merge(t.defaults,e,{DOMResults:document.createElement("div")}),s)}return t.prototype.create=function(e,s){if(e.Input=s,e.Input.nodeName.match(/^INPUT$/i)&&(e.Input.hasAttribute("type")===!1||e.Input.getAttribute("type").match(/^TEXT|SEARCH$/i))){e.Input.setAttribute("autocomplete","off"),e._Position(e),e.Input.parentNode.appendChild(e.DOMResults),e.$Listeners.focus=e._Focus.bind(e),e.$Listeners.keydown=t.prototype.event.bind(null,e),e.$Listeners.blur=e._Blur.bind(e),e.$Listeners.position=e._Position.bind(e),e.$Listeners.destroy=t.prototype.destroy.bind(null,e);for(var o in e.$Listeners)e.Input.addEventListener(o,e.$Listeners[o])}},t.prototype.event=function(e,s){for(var o in e.KeyboardMappings){var i=t.merge({Operator:ConditionOperator.AND},e.KeyboardMappings[o]),r=ConditionOperator.AND==i.Operator;i.Conditions.forEach(function(e){(r===!0&&i.Operator==ConditionOperator.AND||r===!1&&ConditionOperator.OR)&&(e=t.merge({Not:!1},e),e.hasOwnProperty("Is")?r=e.Is==s.keyCode?!e.Not:e.Not:e.hasOwnProperty("From")&&e.hasOwnProperty("To")&&(r=s.keyCode>=e.From&&s.keyCode<=e.To?!e.Not:e.Not))}),r===!0&&i.Callback.bind(e,s)()}},t.prototype.ajax=function(e,s,o){if(void 0===o&&(o=!0),e.$AjaxTimer&&window.clearTimeout(e.$AjaxTimer),o===!0)e.$AjaxTimer=window.setTimeout(t.prototype.ajax.bind(null,e,s,!1),e.Delay);else{e.Request&&e.Request.abort();var i=Object.getOwnPropertyNames(e.HttpHeaders),r=e._HttpMethod(),n=e._Url(),a=e.QueryArg+"="+e._Pre();r.match(/^GET$/i)&&(n+="?"+a),e.Request=new XMLHttpRequest,e.Request.open(r,n,!0);for(var u=i.length-1;u>=0;u--)e.Request.setRequestHeader(i[u],e.HttpHeaders[i[u]]);e.Request.onreadystatechange=s,e.Request.send(a)}},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={},s=0;s<arguments.length;s++)for(t in arguments[s])e[t]=arguments[s][t];return e},t.defaults={Delay:150,EmptyMessage:"No result here",HttpHeaders:{"Content-type":"application/x-www-form-urlencoded"},Limit: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&&(this._Select(e),this.DOMResults.setAttribute("class","autocomplete")),t.preventDefault()}},Operator:ConditionOperator.AND},KeyUpAndDown:{Conditions:[{Is:38,Not:!1},{Is:40,Not:!1}],Callback:function(t){var e=this.DOMResults.querySelector("li:first-child:not(.locked)"),s=this.DOMResults.querySelector("li.active");if(s){var o=Array.prototype.indexOf.call(s.parentNode.children,s),i=o+(t.keyCode-39),r=this.DOMResults.getElementsByTagName("li").length;0>i?i=r-1:i>=r&&(i=0),s.setAttribute("class",""),s.parentElement.childNodes.item(i).setAttribute("class","active")}else e&&e.setAttribute("class","active")},Operator:ConditionOperator.OR},AlphaNum:{Conditions:[{Is:13,Not:!0},{From:35,To:40,Not:!0}],Callback:function(e){var s=this.Input.getAttribute("data-autocomplete-old-value"),o=this._Pre();""!==o&&(s&&o==s||this.DOMResults.setAttribute("class","autocomplete open"),t.prototype.ajax(this,function(){4==this.Request.readyState&&200==this.Request.status&&(this._Render(this._Post(this.Request.response)),this._Open())}.bind(this)))},Operator:ConditionOperator.AND}},DOMResults:null,Request:null,Input:null,_EmptyMessage:function(){var t="";return t=this.Input.hasAttribute("data-autocomplete-empty-message")?this.Input.getAttribute("data-autocomplete-empty-message"):this.EmptyMessage,t===!1&&(t=""),t},_Limit:function(){var t=this.Input.getAttribute("data-autocomplete-limit");return isNaN(t)?this.Limit:parseInt(t)},_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.DOMResults.setAttribute("class","autocomplete");else{var e=this;setTimeout(function(){e._Blur(!0)},150)}},_Focus:function(){var t=this.Input.getAttribute("data-autocomplete-old-value");t&&this.Input.value==t||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(s){t._Select(e)})})},_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=document.createElement("ul"),s=document.createElement("li");if("string"==typeof t)if(t.length>0)this.DOMResults.innerHTML=t;else{var o=this._EmptyMessage();""!==o&&(s.innerHTML=o,s.setAttribute("class","locked"),e.appendChild(s))}else{this._Limit()<0&&(t=t.reverse());for(var i=0;i<t.length;i++)s.innerHTML=t[i].Label,s.setAttribute("data-autocomplete-value",t[i].Value),e.appendChild(s),s=document.createElement("li")}this.DOMResults.hasChildNodes()&&this.DOMResults.removeChild(this.DOMResults.childNodes[0]),this.DOMResults.appendChild(e)},_Post:function(t){try{var e=[],s=JSON.parse(t);if(0==Object.keys(s).length)return"";if(Array.isArray(s))for(var o=0;o<Object.keys(s).length;o++)e[e.length]={Value:s[o],Label:s[o]};else for(var i in s)e.push({Value:i,Label:s[i]});return e}catch(r){return t}},_Pre:function(){return this.Input.value},_Select:function(t){t.hasAttribute("data-autocomplete-value")?this.Input.value=t.getAttribute("data-autocomplete-value"):this.Input.value=t.innerHTML,this.Input.setAttribute("data-autocomplete-old-value",this.Input.value)},$AjaxTimer:null,$Listeners:{}},t}(); | ||
"use strict";var ConditionOperator;(function(ConditionOperator){ConditionOperator[ConditionOperator["AND"]=0]="AND";ConditionOperator[ConditionOperator["OR"]=1]="OR"})(ConditionOperator||(ConditionOperator={}));var AutoComplete=function(){function AutoComplete(params,selector){if(params===void 0){params={}}if(selector===void 0){selector="[data-autocomplete]"}if(Array.isArray(selector)){selector.forEach(function(s){new AutoComplete(params,s)})}else if(typeof selector=="string"){var elements=document.querySelectorAll(selector);Array.prototype.forEach.call(elements,function(input){new AutoComplete(params,input)})}else{AutoComplete.prototype.create(AutoComplete.merge(AutoComplete.defaults,params,{DOMResults:document.createElement("div")}),selector)}}AutoComplete.prototype.create=function(params,element){params.Input=element;if(params.Input.nodeName.match(/^INPUT$/i)&&(params.Input.hasAttribute("type")===false||params.Input.getAttribute("type").match(/^TEXT|SEARCH$/i))){params.Input.setAttribute("autocomplete","off");params._Position(params);params.Input.parentNode.appendChild(params.DOMResults);params.$Listeners["focus"]=params._Focus.bind(params);params.$Listeners["keydown"]=AutoComplete.prototype.event.bind(null,params);params.$Listeners["blur"]=params._Blur.bind(params);params.$Listeners["position"]=params._Position.bind(params);params.$Listeners["destroy"]=AutoComplete.prototype.destroy.bind(null,params);for(var event in params.$Listeners){params.Input.addEventListener(event,params.$Listeners[event])}}};AutoComplete.prototype.event=function(params,event){for(var name in params.KeyboardMappings){var mapping=AutoComplete.merge({Operator:ConditionOperator.AND},params.KeyboardMappings[name]),match=ConditionOperator.AND==mapping.Operator;mapping.Conditions.forEach(function(condition){if(match===true&&mapping.Operator==ConditionOperator.AND||match===false&&ConditionOperator.OR){condition=AutoComplete.merge({Not:false},condition);if(condition.hasOwnProperty("Is")){if(condition.Is==event.keyCode){match=!condition.Not}else{match=condition.Not}}else if(condition.hasOwnProperty("From")&&condition.hasOwnProperty("To")){if(event.keyCode>=condition.From&&event.keyCode<=condition.To){match=!condition.Not}else{match=condition.Not}}}});if(match===true){mapping.Callback.bind(params,event)()}}};AutoComplete.prototype.ajax=function(params,callback,timeout){if(timeout===void 0){timeout=true}if(params.$AjaxTimer){window.clearTimeout(params.$AjaxTimer)}if(timeout===true){params.$AjaxTimer=window.setTimeout(AutoComplete.prototype.ajax.bind(null,params,callback,false),params.Delay)}else{if(params.Request){params.Request.abort()}var propertyHttpHeaders=Object.getOwnPropertyNames(params.HttpHeaders),method=params._HttpMethod(),url=params._Url(),queryParams=params._QueryArg()+"="+params._Pre();if(method.match(/^GET$/i)){url+="?"+queryParams}params.Request=new XMLHttpRequest;params.Request.open(method,url,true);for(var i=propertyHttpHeaders.length-1;i>=0;i--){params.Request.setRequestHeader(propertyHttpHeaders[i],params.HttpHeaders[propertyHttpHeaders[i]])}params.Request.onreadystatechange=callback;params.Request.send(queryParams)}};AutoComplete.prototype.destroy=function(params){for(var event in params.$Listeners){params.Input.removeEventListener(event,params.$Listeners[event])}params.DOMResults.parentNode.removeChild(params.DOMResults)};AutoComplete.merge=function(){var merge={},tmp;for(var i=0;i<arguments.length;i++){for(tmp in arguments[i]){merge[tmp]=arguments[i][tmp]}}return merge};AutoComplete.defaults={Delay:150,EmptyMessage:"No result here",HttpHeaders:{"Content-type":"application/x-www-form-urlencoded"},Limit:0,HttpMethod:"GET",QueryArg:"q",Url:null,KeyboardMappings:{Enter:{Conditions:[{Is:13,Not:false}],Callback:function(event){if(this.DOMResults.getAttribute("class").indexOf("open")!=-1){var liActive=this.DOMResults.querySelector("li.active");if(liActive!==null){this._Select(liActive);this.DOMResults.setAttribute("class","autocomplete")}event.preventDefault()}},Operator:ConditionOperator.AND},KeyUpAndDown:{Conditions:[{Is:38,Not:false},{Is:40,Not:false}],Callback:function(event){var first=this.DOMResults.querySelector("li:first-child:not(.locked)"),active=this.DOMResults.querySelector("li.active");if(active){var currentIndex=Array.prototype.indexOf.call(active.parentNode.children,active),position=currentIndex+(event.keyCode-39),lisCount=this.DOMResults.getElementsByTagName("li").length;if(position<0){position=lisCount-1}else if(position>=lisCount){position=0}active.setAttribute("class","");active.parentElement.childNodes.item(position).setAttribute("class","active")}else if(first){first.setAttribute("class","active")}},Operator:ConditionOperator.OR},AlphaNum:{Conditions:[{Is:13,Not:true},{From:35,To:40,Not:true}],Callback:function(event){var oldValue=this.Input.getAttribute("data-autocomplete-old-value"),currentValue=this._Pre();if(currentValue!==""){if(!oldValue||currentValue!=oldValue){this.DOMResults.setAttribute("class","autocomplete open")}AutoComplete.prototype.ajax(this,function(){if(this.Request.readyState==4&&this.Request.status==200){this._Render(this._Post(this.Request.response));this._Open()}}.bind(this))}},Operator:ConditionOperator.AND}},DOMResults:null,Request:null,Input:null,_EmptyMessage:function(){var emptyMessage="";if(this.Input.hasAttribute("data-autocomplete-empty-message")){emptyMessage=this.Input.getAttribute("data-autocomplete-empty-message")}else{emptyMessage=this.EmptyMessage}if(emptyMessage===false){emptyMessage=""}return emptyMessage},_Limit:function(){var limit=this.Input.getAttribute("data-autocomplete-limit");if(isNaN(limit)){return this.Limit}return parseInt(limit)},_HttpMethod:function(){if(this.Input.hasAttribute("data-autocomplete-method")){return this.Input.getAttribute("data-autocomplete-method")}return this.HttpMethod},_QueryArg:function(){if(this.Input.hasAttribute("data-autocomplete-param-name")){return this.Input.getAttribute("data-autocomplete-param-name")}return this.QueryArg},_Url:function(){if(this.Input.hasAttribute("data-autocomplete")){return this.Input.getAttribute("data-autocomplete")}return this.Url},_Blur:function(now){if(now===void 0){now=false}if(now){this.DOMResults.setAttribute("class","autocomplete")}else{var params=this;setTimeout(function(){params._Blur(true)},150)}},_Focus:function(){var oldValue=this.Input.getAttribute("data-autocomplete-old-value");if(!oldValue||this.Input.value!=oldValue){this.DOMResults.setAttribute("class","autocomplete open")}},_Open:function(){var params=this;Array.prototype.forEach.call(this.DOMResults.getElementsByTagName("li"),function(li){if(li.getAttribute("class")!="locked"){li.onclick=function(event){params._Select(li)}}})},_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(response){var ul=document.createElement("ul"),li=document.createElement("li");if(typeof response=="string"){if(response.length>0){this.DOMResults.innerHTML=response}else{var emptyMessage=this._EmptyMessage();if(emptyMessage!==""){li.innerHTML=emptyMessage;li.setAttribute("class","locked");ul.appendChild(li)}}}else{if(this._Limit()<0){response=response.reverse()}for(var item=0;item<response.length;item++){li.innerHTML=response[item].Label;li.setAttribute("data-autocomplete-value",response[item].Value);ul.appendChild(li);li=document.createElement("li")}}if(this.DOMResults.hasChildNodes()){this.DOMResults.removeChild(this.DOMResults.childNodes[0])}this.DOMResults.appendChild(ul)},_Post:function(response){try{var returnResponse=[];var json=JSON.parse(response);if(Object.keys(json).length==0){return""}if(Array.isArray(json)){for(var i=0;i<Object.keys(json).length;i++){returnResponse[returnResponse.length]={Value:json[i],Label:json[i]}}}else{for(var value in json){returnResponse.push({Value:value,Label:json[value]})}}return returnResponse}catch(event){return response}},_Pre:function(){return this.Input.value},_Select:function(item){if(item.hasAttribute("data-autocomplete-value")){this.Input.value=item.getAttribute("data-autocomplete-value")}else{this.Input.value=item.innerHTML}this.Input.setAttribute("data-autocomplete-old-value",this.Input.value)},$AjaxTimer:null,$Listeners:{}};return AutoComplete}(); |
{ | ||
"name": "autocomplete-js", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"author": "support@autocomplete-js.com", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46958
547