mirror of
https://gitee.com/ShopeX/OMS
synced 2026-04-09 07:55:34 +08:00
2 lines
10 KiB
JavaScript
2 lines
10 KiB
JavaScript
|
|
var Observer=new Class({Implements:[Options,Events],options:{periodical:!1,delay:1e3},initialize:function(e,t,i){this.element=$(e)||$$(e),this.addEvent("onFired",t),this.setOptions(i),this.bound=this.changed.bind(this),this.resume()},changed:function(){var e=this.element.get("value");$equals(this.value,e)||(this.clear(),this.value=e,this.timeout=this.onFired.delay(this.options.delay,this))},setValue:function(e){return this.value=e,this.element.set("value",e),this.clear()},onFired:function(){this.fireEvent("onFired",[this.value,this.element])},clear:function(){return $clear(this.timeout||null),this},pause:function(){return this.timer?$clear(this.timer):this.element.removeEvent("keyup",this.bound),this.clear()},resume:function(){return this.value=this.element.get("value"),this.options.periodical?this.timer=this.changed.periodical(this.options.periodical,this):this.element.addEvent("keyup",this.bound),this}}),$equals=function(e,t){return e==t||JSON.encode(e)==JSON.encode(t)},Autocompleter=new Class({Implements:[Options,Events],options:{minLength:1,markQuery:!0,width:"inherit",maxChoices:10,injectChoice:null,customChoices:null,emptyChoices:null,visibleChoices:!0,className:"autocompleter-choices",zIndex:65535,delay:1e3,observerOptions:{},fxOptions:{},autoSubmit:!1,overflow:!1,overflowMargin:25,selectFirst:!0,filter:null,filterCase:!1,filterSubset:!1,forceSelect:!1,selectMode:!0,choicesMatch:null,multiple:!1,separator:", ",separatorSplit:/\s*[,;]\s*/,autoTrim:!1,allowDupes:!1,cache:!0,relative:!0},initialize:function(e,t){this.element=$(e),this.setOptions(t),this.build(),this.observer=new Observer(this.element,this.prefetch.bind(this),$merge({delay:this.options.delay},this.options.observerOptions)),this.queryValue=null,this.options.filter&&(this.filter=this.options.filter.bind(this));t=this.options.selectMode;this.typeAhead="type-ahead"==t,this.selectMode=!0===t?"selection":t,this.cached=[]},build:function(){$(this.options.customChoices)?this.choices=this.options.customChoices:(this.choices=new Element("ul",{"class":this.options.className,styles:{display:"none",zIndex:this.options.zIndex}}).inject(document.body),this.relative=!1,this.options.relative&&(this.choices.inject(this.element,"after"),this.relative=this.element.getOffsetParent()),this.fix=new OverlayFix(this.choices)),this.options.separator.test(this.options.separatorSplit)||(this.options.separatorSplit=this.options.separator),this.fx=this.options.fxOptions?new Fx.Tween(this.choices,$merge({property:"opacity",link:"cancel",duration:200},this.options.fxOptions)).addEvent("onStart",Chain.prototype.clearChain).set(0):null,this.choices.addEvents({mouseover:function(e){this.store("focussed",!0)},mouseout:function(e){this.store("focussed",!1)},mousedown:function(){this.store("focussed",!0)}}),this.element.setProperty("autocomplete","off").addEvent(Browser.Engine.trident||Browser.Engine.webkit?"keydown":"keypress",this.onCommand.bind(this)).addEvent("click",this.onCommand.bind(this,[!1])).addEvent("focus",this.toggleFocus.create({bind:this,arguments:!0,delay:100})).addEvent("blur",this.toggleFocus.create({bind:this,arguments:!1,delay:100}))},destroy:function(){this.fix&&this.fix.destroy(),this.choices=this.selected=this.choices.destroy()},toggleFocus:function(e){(this.focussed=e)||!this.choices||this.choices.retrieve("focussed")||this.hideChoices(!0),this.fireEvent(e?"onFocus":"onBlur",[this.element])},onCommand:function(e){if(!e&&this.focussed)return this.prefetch();if(e&&e.key&&!e.shift)switch(e.key){case"enter":if(this.element.value!=this.opted)return!0;if(this.selected&&this.visible)return this.choiceSelect(this.selected),!!this.options.autoSubmit;break;case"up":case"down":var t;return this.prefetch()||null===this.queryValue||(t="up"==e.key,this.choiceOver((this.selected||this.choices)[this.selected?t?"getPrevious":"getNext":t?"getLast":"getFirst"](this.options.choicesMatch),!0)),!1;case"esc":case"tab":this.hideChoices(!0)}return!0},setSelection:function(e){var t,i,s=this.selected.inputValue,o=s,h=this.queryValue.length,n=s.length;s.substr(0,h).toLowerCase()!=this.queryValue.toLowerCase()&&(h=0),this.options.multiple&&(t=this.options.separatorSplit,o=this.element.value,h+=this.queryIndex,n+=this.queryIndex,i=o.substr(this.queryIndex).split(t,1)[0],o=o.substr(0,this.queryIndex)+s+o.substr(this.queryIndex+i.length),e&&(t=o.split(this.options.separatorSplit).filter(function(e){return this.test(e)},/[^\s,]+/),this.options.allowDupes||(t=[].combine(t)),i=this.options.separator,n=(o=t.join(i)+i).length)),this.observer.setValue(o),this.opted=o,!e&&"pick"!=this.selectMode||(h=n),this.element.selectRange(h,n),this.fireEvent("onSelection",[this.element,this.selected,o,s])},showChoices:function(){var e,t,i,s,o=this.options.choicesMatch,h=this.choices.getFirst(o);this.selected=this.selectedValue=null,this.fix&&(e=this.element.getCoordinates(this.relative),i=this.options.width||"auto",0<(t=document.getElementById("main").getScroll()).y&&(e.bottom=t.y+e.bottom),this.choices.setStyles({left:e.left,top:e.bottom,width:!0===i||"inherit"==i?e.width:i})),h&&(this.visible||(this.visible=!0,this.choices.setStyle("display",""),this.fx&&this.fx.start(1),this.fireEvent("onShow",[this.element,this.choices])),this.options.selectFirst&&(this.typeAhead=!0),(this.options.selectFirst||this.typeAhead||h.inputValue==this.queryValue)&&this.choiceOver(h,this.typeAhead),i=this.choices.getChildren(o),h=this.options.maxChoices,this.overflown=!(o={overflowY:"hidden",height:""}),i.length>h&&(s=i[h-1],o.overflowY="scroll",o.height=s.getCoordinates(this.choices).bottom,this.overflown=!0),this.choices.setStyles(o),this.fix.show(),this.options.visibleChoices&&(h=document.getScroll(),s=document.getSize(),(o=this.choices.getCoordinates()).right>h.x+s.x&&(h.x=o.right-s.x),o.bottom>h.y+s.y&&(h.y=o.bottom-s.y),window.scrollTo(Math.min(h.x,o.left),Math.min(h.y,o.top))))},hideChoices:function(e){var t;e&&(t=this.element.value,this.options.forceSelect&&(t=this.opted),this.options.autoTrim&&(t=t.split(this.options.separatorSplit).filter($arguments(0)).join(this.options.separator)),this.observer.setValue(t)),this.visible&&(this.visible=!1,this.selected&&this.selected.removeClass("autocompleter-selected"),this.observer.clear(),t=function(){this.choices.hide(),this.fix.hide()}.bind(this),this.fx?this.fx.start(0).chain(t):t(),this.fireEvent("onHide",[this.element,this.choices]))},prefetch:function(){var e,t,i,s=this.element.value,o=s;if(this.options.multiple&&(i=this.options.separatorSplit,e=s.split(i),t=this.element.getSelectedRange().start,t-=(i=s.substr(0,t).split(i))[i=i.length-1].length,o=e[i]),o.length<this.options.minLength)this.hideChoices();else if(o===this.queryValue||this.visible&&o==this.selectedValue){if(this.visible)return!1;this.showChoices()}else this.queryValue=o,this.queryIndex=t,this.fetchCached()||this.query();return!0},fetchCached:function(){return!1},update:function(e){this.choices.empty();var t=(this.cached=e)&&$type(e);!t||"array"==t&&!e.length||"hash"==t&&!e.getLength()?(this.options.emptyChoices||this.hideChoices).call(this):(this.options.maxChoices<e.length&&!this.options.overflow&&(e.length=this.options.maxChoices),e.each(this.options.injectChoice||function(e){var t=new Element("li",{html:this.markQueryValue(e)});t.inputValue=e,this.addChoiceEvents(t).inject(this.choices)},this),this.showChoices())},choiceOver:function(e,t){var i,s,o;e&&e!=this.selected&&(this.selected&&this.selected.removeClass("autocompleter-selected"),this.selected=e.addClass("autocompleter-selected"),this.fireEvent("onSelect",[this.element,this.selected,t]),this.selectMode||(this.opted=this.element.value),t&&(this.selectedValue=this.selected.inputValue,this.overflown&&(i=this.selected.getCoordinates(this.choices),s=this.options.overflowMargin,t=(o=this.choices.scrollTop)+(e=this.choices.offsetHeight),i.top-s<o&&o?this.choices.scrollTop=Math.max(i.top-s,0):i.bottom+s>t&&(this.choices.scrollTop=Math.min(i.bottom-e+s,t))),this.selectMode&&this.setSelection()))},choiceSelect:function(e){e&&this.choiceOver(e),this.setSelection(!0),this.queryValue=!1,this.fireEvent("submit"),this.hideChoices()},filter:function(e){return(e||this.tokens).filter(function(e){return this.test(e)},new RegExp((this.options.filterSubset?"":"^")+this.queryValue.escapeRegExp(),this.options.filterCase?"":"i"))},markQueryValue:function(e){return this.options.markQuery&&this.queryValue?e.replace(new RegExp("("+(this.options.filterSubset?"":"^")+this.queryValue.escapeRegExp()+")",this.options.filterCase?"":"i"),'<span class="autocompleter-queried">$1</span>'):e},addChoiceEvents:function(e){return e.addEvents({mouseover:this.choiceOver.bind(this,[e]),click:this.choiceSelect.bind(this,[e])})}}),OverlayFix=new Class({initialize:function(e){Browser.Engine.trident&&(this.element=$(e),this.relative=this.element.getOffsetParent(),this.fix=new Element("iframe",{frameborder:"0",scrolling:"no",src:"javascript:false;",styles:{position:"absolute",border:"none",display:"none",filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=0)"}}).inject(this.element,"after"))},show:function(){var e;return this.fix&&(delete(e=this.element.getCoordinates(this.relative)).right,delete e.bottom,this.fix.setStyles($extend(e,{display:"",zIndex:(this.element.getStyle("zIndex")||1)-1}))),this},hide:function(){return this.fix&&this.fix.setStyle("display","none"),this},destroy:function(){this.fix&&(this.fix=this.fix.destroy())}});Autocompleter.script=new Class({Extends:Autocompleter,options:{getData:{},callJSON:$empty,getVar:"value"},initialize:function(e,t,i){this.parent(e,i),this.url=t},query:function(){this.fireEvent("onLoad");var e=$unlink(this.options.getData)||{};e[this.options.getVar]=this.queryValue;var t=$(this.options.indicator);t&&t.setStyle("display","");t=this.options.indicatorClass;t&&this.element.addClass(t),this.fireEvent("onRequest",[this.element,e,this.queryValue]),this.requestScript(e)},requestScript:function(){var src=this.url+"&"+$H(arguments[0]).toQueryString(),load=this.queryResponse.bind(this);new Request({url:src,method:"get",onComplete:function(rs){-1!=rs.indexOf("autocompleter_json")&&eval(rs),load()}}).send()},queryResponse:function(){var e=$(this.options.indicator);e&&e.hide();e=this.options.indicatorClass;e&&this.element.removeClass(e);e=this.options.callJSON();this.update(e),this.fireEvent("onComplete",[this.element])}}); |