forked from fabiomcosta/mootools-meio-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meio.Autocomplete.min.js
1 lines (1 loc) · 17.6 KB
/
Meio.Autocomplete.min.js
1
(function(b,c){var d=Browser.ie6;var a=new Class({Implements:Options,options:{top:"auto",left:"auto",width:"auto",height:"auto",opacity:true,src:"javascript:false;"},initialize:function(f,e){if(!d){return}this.setOptions(e);this.element=c(f);var g=this.element.getFirst();if(!(g&&g.hasClass("bgiframe"))){this.element.grab(document.createElement(this.render()),"top")}},toPx:function(e){return isFinite(e)?e+"px":e},render:function(){var e=this.options;return['<iframe class="bgiframe" frameborder="0" tabindex="-1" src="',e.src,'" ','style="display:block;position:absolute;z-index:-1;',(e.opacity!==false?"filter:alpha(opacity='0');":""),"top:",(e.top==="auto"?"expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+'px')":this.toPx(e.top)),";","left:",(e.left==="auto"?"expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+'px')":this.toPx(e.left)),";","width:",(e.width==="auto"?"expression(this.parentNode.offsetWidth+'px')":this.toPx(e.width)),";","height:",(e.height==="auto"?"expression(this.parentNode.offsetHeight+'px')":this.toPx(e.height)),";",'"/>'].join("")}});Element.implement("bgiframe",function(e){if(d){new a(this,e)}return this})})(this,document.id||this.$);(function(b,c){var a=Browser;Object.append(Element.NativeEvents,{paste:2,input:2});Element.Events.paste={base:(a.opera||(a.firefox&&a.version<3))?"input":"paste",condition:function(d){this.fireEvent("paste",d,1);return false}};Element.Events.keyrepeat={base:(a.firefox||a.opera)?"keypress":"keydown",condition:Function.from(true)};b.Meio=b.Meio||{}}(this,document.id||this.$));Meio.Widget=new Class({initialize:function(){this.elements={}},addElement:function(a,b){this.elements[a]=b},addEventToElement:function(b,a,c){this.elements[b].addEvent(a,c.bind(this))},addEventsToElement:function(b,c){for(var a in c){this.addEventToElement(b,a,c[a])}},attach:function(){for(var a in this.elements){this.elements[a].attach()}},detach:function(){for(var a in this.elements){this.elements[a].detach()}},destroy:function(){for(var a in this.elements){if(this.elements[a]){this.elements[a].destroy()}}}});(function(a,b){a.Meio.Element=new Class({Implements:[Events],initialize:function(c){this.setNode(c);this.createBoundEvents();this.attach()},setNode:function(c){this.node=c?b(c)||$$(c)[0]:this.render()},createBoundEvents:function(){this.bound={};this.boundEvents.each(function(c){this.bound[c]=function(d){this.fireEvent("before"+c.capitalize(),d);if(this[c]){this[c](d)}this.fireEvent(c,d);return true}.bind(this)},this)},attach:function(){for(var c in this.bound){this.node.addEvent(c,this.bound[c])}},detach:function(){for(var c in this.bound){this.node.removeEvent(c,this.bound[c])}},addClass:function(c){this.node.addClass(this.options.classes[c])},removeClass:function(c){this.node.removeClass(this.options.classes[c])},toElement:function(){return this.node},render:function(){}})}(this,document.id||this.$));(function(a,b){a.Meio.Element.Field=new Class({Extends:a.Meio.Element,Implements:[Options],options:{classes:{loading:"ma-loading",selected:"ma-selected"}},initialize:function(d,c){this.keyPressControl={};this.boundEvents=["paste","focus","blur","click","keyup","keyrepeat"];if(Browser.ie6){this.boundEvents.push("keypress")}this.setOptions(c);this.parent(d);b(a).addEvent("unload",function(){if(this.node){b(this.node).set("autocomplete","on")}}.bind(this))},setNode:function(c){this.parent(c);this.node.set("autocomplete","off")},keyrepeat:function(c){clearInterval(this.keyrepeatTimer);this.keyrepeatTimer=this._keyrepeat.delay(1,this,c)},_keyrepeat:function(c){this.fireEvent("delayedKeyrepeat",c)},destroy:function(){this.detach();this.node.removeAttribute("autocomplete")},keypress:function(c){if(c.key==="enter"){this.bound.keyrepeat(c)}}})}(this,document.id||this.$));(function(a,b){a.Meio.Element.List=new Class({Extends:a.Meio.Element,Implements:[Options],options:{width:"field",container:"body",classes:{container:"ma-container",hover:"ma-hover",odd:"ma-odd",even:"ma-even"}},initialize:function(c){this.boundEvents=["mousedown","mouseover"];this.setOptions(c);this.parent();this.focusedItem=null},applyMaxHeight:function(f){var c=this.list.childNodes;var e=c[f-1]||(c.length?c[c.length-1]:null);if(!e){return}e=b(e);for(var d=2;d--;){this.node.setStyle("height",e.getCoordinates(this.list).bottom)}},mouseover:function(d){var c=this.getItemFromEvent(d),f=this.options.classes.hover;if(!c){return true}if(this.focusedItem){this.focusedItem.removeClass(f)}c.addClass(f);this.focusedItem=c;this.fireEvent("focusItem",[this.focusedItem])},mousedown:function(c){c.preventDefault();this.shouldNotBlur=true;if(!(this.focusedItem=this.getItemFromEvent(c))){c.dontHide=true;return true}this.focusedItem.removeClass(this.options.classes.hover)},focusItem:function(d){var e=this.options.classes.hover,c;if(this.focusedItem){if((c=this.focusedItem[d=="up"?"getPrevious":"getNext"]())){this.focusedItem.removeClass(e);c.addClass(e);this.focusedItem=c;this.scrollFocusedItem(d)}}else{if((c=this.list.getFirst())){c.addClass(e);this.focusedItem=c}}},scrollFocusedItem:function(f){var c=this.focusedItem.getCoordinates(this.list),e=this.node.scrollTop;if(f=="down"){var g=c.bottom-this.node.getStyle("height").toInt();if((g-e)>0){this.node.scrollTop=g}}else{var d=c.top;if(e&&e>d){this.node.scrollTop=d}}},getItemFromEvent:function(d){var c=d.target;while(c&&c.tagName.toLowerCase()!="li"){if(c===this.node){return null}c=c.parentNode}return b(c)},render:function(){var c=new Element("div",{"class":this.options.classes.container});if(c.bgiframe){c.bgiframe({top:0,left:0})}this.list=new Element("ul").inject(c);$$(this.options.container)[0].grab(c);return c},positionNextTo:function(c){var d=this.options.width,e=this.node;var f=c.getCoordinates();e.setStyle("width",d=="field"?c.getWidth().toInt()-e.getStyle("border-left-width").toInt()-e.getStyle("border-right-width").toInt():d);e.setPosition({x:f.left,y:f.bottom})},show:function(){this.node.scrollTop=0;this.node.setStyle("visibility","visible");this.showing=true},hide:function(){this.showing=false;this.node.setStyle("visibility","hidden")}})}(this,document.id||this.$));(function(d,e){var a=d.Meio;var b={9:1,16:1,17:1,18:1,224:1,91:1,37:1,38:1,39:1,40:1};var c=function(f){return f.replace(/"/g,""").replace(/'/g,"'")};a.Autocomplete=new Class({Extends:a.Widget,Implements:[Options,Events],options:{delay:200,minChars:0,cacheLength:20,selectOnTab:true,autoFocus:false,maxVisibleItems:10,cacheType:"shared",filter:{},fieldOptions:{},listOptions:{},requestOptions:{},urlOptions:{}},initialize:function(f,i,g,h){this.parent();this.setOptions(g);this.active=0;this.filters=a.Autocomplete.Filter.get(this.options.filter);this.addElement("list",h||new a.Element.List(this.options.listOptions));this.addListEvents();this.addElement("field",new a.Element.Field(f,this.options.fieldOptions));this.addFieldEvents();this.addSelectEvents();this.attach();this.initCache();this.initData(i)},addFieldEvents:function(){this.addEventsToElement("field",{beforeKeyrepeat:function(h){this.active=1;var f=h.key,g=this.elements.list;if(f=="up"||f=="down"||(f=="enter"&&g.showing)){h.preventDefault()}},delayedKeyrepeat:function(h){var f=h.key,g=this.elements.field;g.keyPressControl[f]=true;switch(f){case"up":case"down":this.focusItem(f);break;case"enter":this.setInputValue();break;case"tab":if(this.options.selectOnTab){this.setInputValue()}g.keyPressControl[f]=false;break;case"esc":this.elements.list.hide();break;default:this.setupList()}this.oldInputedText=g.node.get("value")},keyup:function(g){var f=this.elements.field;if(!b[g.code]){if(!f.keyPressControl[g.key]){this.setupList()}f.keyPressControl[g.key]=false}},focus:function(){this.active=1;var f=this.elements.list;f.focusedItem=null;f.positionNextTo(this.elements.field.node)},click:function(){if(++this.active>2&&!this.elements.list.showing){this.forceSetupList()}},blur:function(g){this.active=0;var f=this.elements.list;if(f.shouldNotBlur){this.elements.field.node.setCaretPosition("end");f.shouldNotBlur=false;if(f.focusedItem){f.hide()}}else{f.hide()}},paste:function(){return this.setupList()}})},addListEvents:function(){this.addEventsToElement("list",{mousedown:function(f){if(this.active&&!f.dontHide){this.setInputValue()}}})},update:function(){var m=this.data,p=this.elements.list;var r=m.getKey(),j=this.cache.get(r),o;if(j){o=j.html;this.itemsData=j.data}else{m=m.get();var t=[],s=[],k=p.options.classes,u=this.inputedText;var g=this.filters.filter,f=this.filters.formatMatch,q=this.filters.formatItem;for(var v,l=0,h=0;(v=m[l++]);){if(g.call(this,u,v)){t.push('<li title="',c(f.call(this,u,v)),'" data-index="',h,'" class="',(h%2?k.even:k.odd),'">',q.call(this,u,v,h),"</li>");s.push(v);h++}}o=t.join("");this.cache.set(r,{html:o,data:s});this.itemsData=s}p.focusedItem=null;this.fireEvent("deselect",[this.elements]);p.list.set("html",o);if(this.options.maxVisibleItems){p.applyMaxHeight(this.options.maxVisibleItems)}},setupList:function(){this.inputedText=this.elements.field.node.get("value");if(this.inputedText!==this.oldInputedText){this.forceSetupList(this.inputedText)}else{this.elements.list.hide()}return true},forceSetupList:function(f){f=f||this.elements.field.node.get("value");if(f.length>=this.options.minChars){clearInterval(this.prepareTimer);this.prepareTimer=this.data.prepare.delay(this.options.delay,this.data,f)}},dataReady:function(){this.update();if(this.onUpdate){this.onUpdate();this.onUpdate=null}var f=this.elements.list;if(f.list.get("html")){this.fireEvent("itemToList",[this.elements]);if(this.options.autoFocus&&f.list.getChildren().length===1){this.focusItem()}if(this.active){f.show()}else{if(this.options.autoFocus&&f.list.getChildren().length===1){this.setInputValue()}}}else{this.fireEvent("noItemToList",[this.elements]);f.hide()}},setInputValue:function(){var g=this.elements.list;if(g.focusedItem){var h=g.focusedItem.get("title");this.elements.field.node.set("value",h);var f=g.focusedItem.get("data-index");this.fireEvent("select",[this.elements,this.itemsData[f],h,f])}g.hide()},focusItem:function(g){var f=this.elements.list;if(f.showing){f.focusItem(g)}else{this.forceSetupList();this.onUpdate=function(){f.focusItem(g)}}},addSelectEvents:function(){this.addEvents({select:function(f){f.field.addClass("selected")},deselect:function(f){f.field.removeClass("selected")}})},initData:function(f){this.data=(typeOf(f)=="string")?new a.Autocomplete.Data.Request(f,this.cache,this.elements.field,this.options.requestOptions,this.options.urlOptions):(typeOf(f)=="function")?new a.Autocomplete.Data.Source(f,this.cache,this.elements.field):new a.Autocomplete.Data(f,this.cache);this.data.addEvent("ready",this.dataReady.bind(this))},initCache:function(){var f=this.options.cacheLength;if(this.options.cacheType=="shared"){this.cache=a.Autocomplete.Cache.instance;this.cache.setMaxLength(f)}else{if(this.options.cacheType=="own"){this.cache=new a.Autocomplete.Cache(f)}else{this.cache=new a.Autocomplete.FakeCache()}}},refreshCache:function(f){this.cache.refresh();this.cache.setMaxLength(f||this.options.cacheLength)},refreshAll:function(g,f){this.refreshCache(g);this.data.refreshKey(f)}})}(this,document.id||this.$));(function(a,b){a.Meio.Autocomplete.Select=new Class({Extends:a.Meio.Autocomplete,options:{syncName:"id",valueField:null,valueFilter:function(c){return c.id}},initialize:function(c,f,d,e){this.parent(c,f,d,e);this.valueField=b(this.options.valueField);if(!this.valueField){return}this.syncWithValueField(f)},syncWithValueField:function(d){var c=this.getValueFromValueField();if(c&&this.options.syncName){this.addParameter(d);this.addDataReadyEvent(c);this.data.prepare(this.elements.field.node.get("value"))}else{this.addValueFieldEvents()}},addValueFieldEvents:function(){this.addEvents({select:function(d,c){this.valueField.set("value",this.options.valueFilter.call(this,c))},deselect:function(c){this.valueField.set("value","")}})},addParameter:function(c){this.parameter={name:this.options.syncName,value:function(){return this.valueField.value}.bind(this)};if(this.data.url){this.data.url.addParameter(this.parameter)}},addDataReadyEvent:function(e){var d=this;var c=function(){d.addValueFieldEvents();var f=this.get();for(var g=f.length;g--;){if(d.options.valueFilter.call(d,f[g])==e){var h=d.filters.formatMatch.call(d,"",f[g],0);d.elements.field.node.set("value",h);d.fireEvent("select",[d.elements,f[g],h,g]);break}}if(this.url){this.url.removeParameter(d.parameter)}this.removeEvent("ready",c)};this.data.addEvent("ready",c)},getValueFromValueField:function(){return this.valueField.get("value")}})}(this,document.id||this.$));(function(a,b){Meio.Autocomplete.Select.One=new Class({Extends:Meio.Autocomplete.Select,options:{filter:{path:"text"}},initialize:function(c,d,e){this.select=b(c);this.replaceSelect();this.parent(this.field,this.createDataArray(),Object.merge(d||{},{valueField:this.select,valueFilter:function(f){return f.value}}),e)},replaceSelect:function(){var c=this.select.getSelected()[0];this.field=new Element("input",{type:"text"});var d=c.get("value");if(d||d===0){this.field.set("value",c.get("html"))}this.select.setStyle("display","none");this.field.inject(this.select,"after")},createDataArray:function(){var d=this.select.options,f=[];for(var e=0,c,g;c=d[e++];){g=c.value;if(g||g===0){f.push({value:g,text:c.innerHTML})}}return f},addValueFieldEvents:function(){this.addEvents({select:function(f,e,g,c){var d=this.valueField.getElement('option[value="'+this.options.valueFilter.call(this,e)+'"]');if(d){d.selected=true}},deselect:function(d){var c=this.valueField.getSelected()[0];if(c){c.selected=false}}})},getValueFromValueField:function(){return this.valueField.getSelected()[0].get("value")}})}(this,document.id||this.$));Meio.Autocomplete.Filter={filters:{},get:function(a){var b=a.type,d=(a.path||"").split(".");var c=(b&&this.filters[b])?this.filters[b](this,d):a;return Object.merge(this.defaults(d),c)},define:function(b,a){this.filters[b]=a},defaults:function(b){var a=this;return{filter:function(d,c){return d?a._getValueFromKeys(c,b).test(new RegExp(d.escapeRegExp(),"i")):true},formatMatch:function(d,c){return a._getValueFromKeys(c,b)},formatItem:function(e,d,c){return e?a._getValueFromKeys(d,b).replace(new RegExp("("+e.escapeRegExp()+")","gi"),"<strong>$1</strong>"):a._getValueFromKeys(d,b)}}},_getValueFromKeys:function(e,c){var b,d=e;for(var a=0;(b=c[a++]);){d=d[b]}return d}};Meio.Autocomplete.Filter.define("contains",function(a,b){return{}});Meio.Autocomplete.Filter.define("startswith",function(a,b){return{filter:function(d,c){return d?a._getValueFromKeys(c,b).test(new RegExp("^"+d.escapeRegExp(),"i")):true}}});Meio.Autocomplete.Data=new Class({Implements:[Options,Events],initialize:function(b,a){this.data=b;this._cache=a;this.dataString=JSON.encode(this.data)},get:function(){return this.data},getKey:function(){return this.cachedKey},prepare:function(a){this.cachedKey=this.dataString+(a||"");this.fireEvent("ready")},cache:function(a,b){this._cache.set(a,b)},refreshKey:function(){}});Meio.Autocomplete.Data.Source=new Class({Extends:Meio.Autocomplete.Data,initialize:function(c,a,b){this.source=c;this._cache=a;this.element=b},prepare:function(a){this.element.addClass("loading");this.source(a,this.done.bind(this))},done:function(a){this.data=a;this.element.removeClass("loading");this.fireEvent("ready")}});Meio.Autocomplete.Data.Request=new Class({Extends:Meio.Autocomplete.Data,options:{noCache:true,formatResponse:function(a){return a},link:"cancel"},initialize:function(d,b,e,c,a){this.setOptions(c);this.rawUrl=d;this._cache=b;this.element=e;this.urlOptions=a;this.refreshKey();this.createRequest()},prepare:function(a){this.cachedKey=this.url.evaluate(a);if(this._cache.has(this.cachedKey)){this.fireEvent("ready")}else{this.request.send({url:this.cachedKey})}},createRequest:function(){var a=this;this.request=new Request.JSON(this.options);this.request.addEvents({request:function(){a.element.addClass("loading")},complete:function(){a.element.removeClass("loading")},success:function(b){a.data=a.options.formatResponse(b);a.fireEvent("ready")}})},refreshKey:function(a){a=Object.merge(this.urlOptions,{url:this.rawUrl},a||{});this.url=new Meio.Autocomplete.Data.Request.URL(a.url,a)}});Meio.Autocomplete.Data.Request.URL=new Class({Implements:[Options],options:{queryVarName:"q",extraParams:null,max:20},initialize:function(b,a){this.setOptions(a);this.rawUrl=b;this.url=b;this.url+=this.url.contains("?")?"&":"?";this.dynamicExtraParams=[];var d=Array.from(this.options.extraParams);for(var c=d.length;c--;){this.addParameter(d[c])}if(this.options.max){this.addParameter("limit="+this.options.max)}},evaluate:function(d){d=d||"";var c=this.dynamicExtraParams,a=[];a.push(this.options.queryVarName+"="+encodeURIComponent(d));for(var b=c.length;b--;){a.push(encodeURIComponent(c[b].name)+"="+encodeURIComponent(Function.from(c[b].value)()))}return this.url+a.join("&")},addParameter:function(a){if(a.nodeType==1||typeOf(a.value)=="function"){this.dynamicExtraParams.push(a)}else{this.url+=((typeOf(a)=="string")?a:encodeURIComponent(a.name)+"="+encodeURIComponent(a.value))+"&"}},removeParameter:function(a){this.dynamicExtraParams.erase(a)}});(function(a){a.Meio.Autocomplete=a.Meio.Autocomplete||{};var b=a.Meio.Autocomplete.Cache=new Class({initialize:function(c){this.refresh();this.setMaxLength(c)},set:function(c,d){if(!this.cache[c]){if(this.getLength()>=this.maxLength){var e=this.pos.shift();this.cache[e]=null;delete this.cache[e]}this.cache[c]=d;this.pos.push(c)}return this},get:function(c){return this.cache[c||""]||null},has:function(c){return !!this.get(c)},getLength:function(){return this.pos.length},refresh:function(){this.cache={};this.pos=[]},setMaxLength:function(c){this.maxLength=Math.max(c,1)}});a.Meio.Autocomplete.FakeCache=new Class({Extends:b,get:function(){return null},set:function(){return this}});a.Meio.Autocomplete.Cache.instance=new Meio.Autocomplete.Cache()}(this));