de10392a7f
Pas besoin de 12 sous-dossiers pour `autocomplete-light`.
9 lines
No EOL
20 KiB
JavaScript
9 lines
No EOL
20 KiB
JavaScript
/*
|
|
* jquery-autocomplete-light - v3.5.0
|
|
* Dead simple autocompletion and widgets for jQuery
|
|
* http://yourlabs.org
|
|
*
|
|
* Made by James Pic
|
|
* Under MIT License
|
|
*/
|
|
if(void 0===window.isOpera)var isOpera=navigator.userAgent.indexOf("Opera")>=0&&parseFloat(navigator.appVersion);if(void 0===window.isIE)var isIE=document.all&&!isOpera&&parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]);void 0===window.findPosX&&(window.findPosX=function(a){var b=0;if(a.offsetParent){for(;a.offsetParent;)b+=a.offsetLeft-(isOpera?0:a.scrollLeft),a=a.offsetParent;isIE&&a.parentElement&&(b+=a.offsetLeft-a.scrollLeft)}else a.x&&(b+=a.x);return b}),void 0===window.findPosY&&(window.findPosY=function(a){var b=0;if(a.offsetParent){for(;a.offsetParent;)b+=a.offsetTop-(isOpera?0:a.scrollTop),a=a.offsetParent;isIE&&a.parentElement&&(b+=a.offsetTop-a.scrollTop)}else a.y&&(b+=a.y);return b}),void 0===window.yourlabs&&(window.yourlabs={}),void 0!==window.yourlabs.Autocomplete&&console.log("WARNING ! You are loading autocomplete.js **again**."),yourlabs.getInternetExplorerVersion=function(){var a=-1;if("Microsoft Internet Explorer"===navigator.appName){var b=navigator.userAgent;null!==new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})").exec(b)&&(a=parseFloat(RegExp.$1))}return a},$.fn.yourlabsRegistry=function(a,b){var c=yourlabs.getInternetExplorerVersion();if(-1===c||c>8)return void 0===b?this.data(a):this.data(a,b);void 0===$.fn.yourlabsRegistry.data&&($.fn.yourlabsRegistry.data={}),void 0===$.fn.yourlabsRegistry.guid&&($.fn.yourlabsRegistry.guid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var b=16*Math.random()|0;return("x"===a?b:3&b|8).toString(16)})});var d="data-yourlabs-"+a+"-registry-id",e=this.attr(d);return void 0===e&&(e=$.fn.yourlabsRegistry.guid(),this.attr(d,e)),void 0!==b&&($.fn.yourlabsRegistry.data[e]=b),$.fn.yourlabsRegistry.data[e]},yourlabs.Autocomplete=function(a){this.input=a,this.value=null,this.minimumCharacters=2,this.hideAfter=200,this.alignRight=!1,this.url=!1,this.xhrWait=200,this.choiceSelector=".choice",this.hilightClass="hilight",this.autoHilightFirst=!1,this.bindMouseDown=!0,this.queryVariable="q",this.data={},this.xhr=!1,this.lastData={},this.box=$('<span class="yourlabs-autocomplete" data-input-id="'+this.input.attr("id")+'"></span>'),this.container=this.input.parents().filter(function(){var a=$(this).css("position");return"absolute"===a||"fixed"===a}).first(),this.container.length||(this.container=$("body"))},yourlabs.Autocomplete.prototype.initialize=function(){var a=yourlabs.getInternetExplorerVersion();if(this.input.on("blur.autocomplete",$.proxy(this.inputBlur,this)).on("focus.autocomplete",$.proxy(this.inputClick,this)).on("keydown.autocomplete",$.proxy(this.inputKeyup,this)),$(window).on("resize",$.proxy(function(){this.box.is(":visible")&&this.fixPosition()},this)),/Firefox/i.test(navigator.userAgent)&&$(window).on("scroll",$.proxy(this.hide,this)),-1===a||a>9)this.input.on("input.autocomplete",$.proxy(this.refresh,this));else{var b=["keyup.autocomplete","keypress.autocomplete","cut.autocomplete","paste.autocomplete"];this.input.on(b.join(" "),function(a){$.proxy(this.inputKeyup,this)})}this.box.on("mouseenter",this.choiceSelector,$.proxy(this.boxMouseenter,this)).on("mouseleave",this.choiceSelector,$.proxy(this.boxMouseleave,this)),this.bindMouseDown&&this.box.on("mousedown",this.choiceSelector,$.proxy(this.boxClick,this)),this.data[this.queryVariable]=""},yourlabs.Autocomplete.prototype.destroy=function(a){a.unbind("blur.autocomplete").unbind("focus.autocomplete").unbind("input.autocomplete").unbind("keydown.autocomplete").unbind("keypress.autocomplete").unbind("keyup.autocomplete")},yourlabs.Autocomplete.prototype.inputBlur=function(a){window.setTimeout($.proxy(this.hide,this),this.hideAfter)},yourlabs.Autocomplete.prototype.inputClick=function(a){null===this.value&&(this.value=this.getQuery()),this.value.length>=this.minimumCharacters&&this.show()},yourlabs.Autocomplete.prototype.boxMouseenter=function(a){var b=this.box.find("."+this.hilightClass);this.input.trigger("dehilightChoice",[b,this]),this.input.trigger("hilightChoice",[$(a.currentTarget),this])},yourlabs.Autocomplete.prototype.boxMouseleave=function(a){this.input.trigger("dehilightChoice",[this.box.find("."+this.hilightClass),this])},yourlabs.Autocomplete.prototype.boxClick=function(a){var b=this.box.find("."+this.hilightClass);this.input.trigger("selectChoice",[b,this])},yourlabs.Autocomplete.prototype.getQuery=function(){return this.input.val()},yourlabs.Autocomplete.prototype.inputKeyup=function(a){if(this.input.is(":visible"))switch(a.keyCode){case 40:case 38:case 16:case 17:case 18:this.move(a);break;case 9:case 13:if(!this.box.is(":visible"))return;var b=this.box.find("."+this.hilightClass);if(!b.length)return;a.preventDefault(),a.stopPropagation(),this.input.trigger("selectChoice",[b,this]);break;case 27:if(!this.box.is(":visible"))return;this.hide();break;default:this.refresh()}},yourlabs.Autocomplete.prototype.show=function(a){this.fixPosition();var b=0===$.trim(this.box.find(this.choiceSelector)).length;if((this.hasChanged()||b)&&!this.xhr)return void this.fetch();if(void 0!==a&&(this.box.html(a),this.fixPosition()),this.box.is(":empty"))return void(this.box.is(":visible")&&this.hide());var c=this.box.find("."+this.hilightClass),d=this.box.find(this.choiceSelector+":first");d&&!c.length&&this.autoHilightFirst&&d.addClass(this.hilightClass),this.box.is(":visible")||(this.box.css("display","block"),this.fixPosition())},yourlabs.Autocomplete.prototype.hide=function(){this.box.hide()},yourlabs.Autocomplete.prototype.move=function(a){if(null===this.value&&(this.value=this.getQuery()),this.value.length<this.minimumCharacters)return!0;var b=this.box.find("."+this.hilightClass);$.inArray(a.keyCode,[9,13])>-1&&b.length&&a.preventDefault();var c;if(38!==a.keyCode||a.charCode){if(40!==a.keyCode||a.charCode)return;c="down"}else c="up";var d,e=this.box.find(this.choiceSelector+":first"),f=this.box.find(this.choiceSelector+":last");this.show(),b.length?("up"===c?(d=b.prevAll(this.choiceSelector+":first"),d.length||(d=f)):(d=b.nextAll(this.choiceSelector+":first"),d.length||(d=e)),this.input.trigger("dehilightChoice",[b,this])):d="up"===c?f:e,a.preventDefault(),this.input.trigger("hilightChoice",[d,this])},yourlabs.Autocomplete.prototype.fixPosition=function(){var a=this.input.get(0),b=this.input.parents().filter(function(){return"auto"!==$(this).css("z-index")&&"0"!==$(this).css("z-index")}).first().css("z-index"),c=this.input.parents().filter(function(){return"absolute"===$(this).css("position")}).get(0),d=findPosY(a)+this.input.outerHeight()+"px",e=findPosX(a)+"px";if(void 0!==c){var f=findPosY(c),g=findPosX(c),h=findPosY(a)+this.input.outerHeight(),i=findPosX(a);d=h-f+"px",e=i-g+"px"}this.alignRight&&(e=findPosX(a)+a.scrollLeft-(this.box.outerWidth()-this.input.outerWidth())+"px"),this.box.appendTo(this.container).css({position:"absolute",minWidth:parseInt(this.input.outerWidth()),top:d,left:e,zIndex:b})},yourlabs.Autocomplete.prototype.refresh=function(){this.value=this.getQuery(),this.value.length<this.minimumCharacters?this.hide():this.fetch()},yourlabs.Autocomplete.prototype.hasChanged=function(){for(var a in this.data)if(!(a in this.lastData)||this.data[a]!==this.lastData[a])return!0;return!1},yourlabs.Autocomplete.prototype.fetch=function(){if(this.data[this.queryVariable]=this.value,!this.hasChanged())return void this.show();this.lastData={};for(var a in this.data)this.lastData[a]=this.data[a];this.xhr&&0===this.xhr.readyState&&this.xhr.abort(),this.timeoutId&&clearTimeout(this.timeoutId),this.timeoutId=setTimeout($.proxy(this.makeXhr,this),this.xhrWait)},yourlabs.Autocomplete.prototype.makeXhr=function(){this.input.addClass("xhr-pending"),this.xhr=$.ajax(this.url,{type:"GET",data:this.data,complete:$.proxy(this.fetchComplete,this)})},yourlabs.Autocomplete.prototype.fetchComplete=function(a,b){this.xhr===a&&(this.xhr=!1,this.input.removeClass("xhr-pending"),"abort"!==b&&this.show(a.responseText))},$.fn.yourlabsAutocomplete=function(a){if(!(this.length<1)){a=a||{};var b=this.yourlabsRegistry("autocomplete");if("destroy"===a)return void(b&&(b.destroy(this),this.removeData("autocomplete")));if(this.attr("autocomplete","off"),void 0===b){b=new yourlabs.Autocomplete(this);for(var c in this.data())if(c&&"autocomplete"===c.substr(0,12)&&"autocomplete"!==c){var d=c.replace("autocomplete","");d=d.charAt(0).toLowerCase()+d.slice(1),b[d]=this.data(c)}if(b=$.extend(b,a),!b.url)return void alert("Autocomplete needs a url !");this.yourlabsRegistry("autocomplete",b),b.initialize()}return b}},$(document).ready(function(){function a(a,b,c){b.removeClass(c.hilightClass)}$(document).bind("hilightChoice",function(a,b,c){b.addClass(c.hilightClass)}),$(document).bind("dehilightChoice",a),$(document).bind("selectChoice",a),$(document).bind("selectChoice",function(a,b,c){c.hide()})}),$(document).ready(function(){var a=function(a){function b(){i.closed?$("#yourlabs_overlay").remove():setTimeout(b,500)}var c=a.attr("id").replace(/^add_/,"");c=d(c),href=a.attr("href"),-1===href.indexOf("?")&&(href+="?"),href+="&winName="+c;var e=500,f=800,g=screen.width/2-f/2,h=screen.height/2-e/2,i=window.open(href,c,"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width="+f+", height="+e+", top="+h+", left="+g);return $("body").append('<div id="yourlabs_overlay"></div'),$("#yourlabs_overlay").click(function(){i.close(),$(this).remove()}),setTimeout(b,1500),i.focus(),!1},b=function(a,b,d){b=c(b),d=c(d);var f=e(a.name),g=document.getElementById(f);if(g){if($(g).is("select")){var h=new Option(d,b);g.options[g.options.length]=h,h.selected=!0}}else alert("Could not get input id for win "+f);a.close()};window.dismissAddAnotherPopup=b;var c=function(a){return a=a.replace(/</g,""),a=a.replace(/"/g,'"'),a=a.replace(/'/g,"'"),a=a.replace(/&/g,"&")},d=function(a){return a=a.replace(/\./g,"__dot__"),a=a.replace(/\-/g,"__dash__"),a=a.replace(/\[/g,"__braceleft__"),a=a.replace(/\]/g,"__braceright__")},e=function(a){return a=a.replace(/__dot__/g,"."),a=a.replace(/__dash__/g,"-"),a=a.replace(/__braceleft__/g,"["),a=a.replace(/__braceright__/g,"]")};$(".autocomplete-add-another").show().click(function(b){b.preventDefault(),a($(this))})}),void 0===window.yourlabs&&(window.yourlabs={}),yourlabs.RemoteAutocompleteWidget={getValue:function(a){var b=a.data("value");return"string"==typeof b&&isNaN(b)&&b.match(/^https?:/)&&($.ajax(this.autocompleteOptions.url,{async:!1,traditional:!0,type:"post",data:{value:b},success:function(a,c,d){b=a}}),a.data("value",b)),b}},$(document).bind("yourlabsWidgetReady",function(){$("body").on("initialize",".autocomplete-light-widget[data-bootstrap=rest_model]",function(){$(this).yourlabsWidget(yourlabs.RemoteAutocompleteWidget)})}),$.fn.getSelectionStart=function(){var a;if(0===this.lengh)return-1;input=this[0];var b=input.value.length;return input.createTextRange?(window.getSelection?a=window.getSelection():(a=document.selection.createRange().duplicate(),a.moveEnd("character",input.value.length)),""===a.text&&(b=input.value.length),b=input.value.lastIndexOf(a.text)):void 0!==typeof input.selectionStart&&(b=input.selectionStart),b},$.fn.getCursorPosition=function(){return 0===this.lengh?-1:$(this).getSelectionStart()},$.fn.getCursorWord=function(){var a=$(this).val(),b=$(this).getCursorWordPositions();return a.substring(b[0],b[1])},$.fn.getCursorWordPositions=function(){for(var a=$(this).getCursorPosition(),b=$(this).val(),c=a-1;c>=0&&","!==b[c];c--);c=c<0?0:c;for(var d=a;d<=b.length-1&&","!==b[d];d++);for(;","===b[c]||" "===b[c];)c++;for(;","===b[d]||" "===b[d];)d--;return[c,d+1]},yourlabs.TextWidget=function(a){this.input=a,this.autocompleteOptions={getQuery:function(){return this.input.getCursorWord()}}},yourlabs.TextWidget.prototype.initializeAutocomplete=function(){this.autocomplete=this.input.yourlabsAutocomplete(this.autocompleteOptions),this.autocomplete.box.addClass("autocomplete-light-text-widget")},yourlabs.TextWidget.prototype.bindSelectChoice=function(){this.input.bind("selectChoice",function(a,b){b.length&&$(this).yourlabsTextWidget().selectChoice(b)})},yourlabs.TextWidget.prototype.selectChoice=function(a){var b=this.input.val(),c=this.getValue(a),d=this.input.getCursorWordPositions(),e=b.substring(0,d[0]);e+=c,e+=b.substring(d[1]),this.input.val(e),this.input.focus()},yourlabs.TextWidget.prototype.getValue=function(a){return $.trim(a.text())},yourlabs.TextWidget.prototype.initialize=function(){this.initializeAutocomplete(),this.bindSelectChoice()},yourlabs.TextWidget.prototype.destroy=function(a){a.unbind("selectChoice").yourlabsAutocomplete("destroy")},$.fn.yourlabsTextWidget=function(a){var b;if("destroy"===(a=a||{}))return void((b=this.data("widget"))&&(b.destroy(this),this.removeData("widget")));if(void 0===this.data("widget")){b=new yourlabs.TextWidget(this);var c=this.data(),d={autocompleteOptions:{minimumCharacters:0,getQuery:function(){return this.input.getCursorWord()}}};for(var e in c)if(e)if("autocomplete"===e.substr(0,12)){if("autocomplete"===e)continue;var f=e.replace("autocomplete","");f=f.replace(f[0],f[0].toLowerCase()),d.autocompleteOptions[f]=c[e]}else d[e]=c[e];b=$.extend(b,d),b=$.extend(b,a),this.data("widget",b),b.initialize(),b.input.attr("data-widget-ready",1),b.input.trigger("widget-ready")}return this.data("widget")},$(document).ready(function(){$("body").on("initialize","input[data-widget-bootstrap=text]",function(){$(this).yourlabsTextWidget()}),$(document).trigger("yourlabsTextWidgetReady"),$('.autocomplete-light-text-widget:not([id*="__prefix__"])').each(function(){$(this).trigger("initialize")}),$(document).bind("DOMNodeInserted",function(a){var b=$(a.target).find(".autocomplete-light-text-widget");(b.length||(b=!!$(a.target).is(".autocomplete-light-text-widget")&&$(a.target)))&&(b.is(".yourlabs-autocomplete")||(b.yourlabsWidget("destroy"),b.find("input").yourlabsAutocomplete("destroy"),b.trigger("initialize")))})}),void 0===window.yourlabs&&(window.yourlabs={}),yourlabs.Widget=function(a){this.widget=a,this.input=this.widget.find("input[data-autocomplete-url]"),this.select=this.widget.find("select"),this.deck=this.widget.find(".deck"),this.choiceTemplate=this.widget.find(".choice-template .choice"),this.maximumValues=0,this.clearInputOnSelectChoice="1"},yourlabs.Widget.prototype.getValue=function(a){return a.attr("data-value")},yourlabs.Widget.prototype.initializeAutocomplete=function(){this.autocomplete=this.input.yourlabsAutocomplete(),this.autocomplete.box.addClass("autocomplete-light-widget")},yourlabs.Widget.prototype.bindSelectChoice=function(){this.input.bind("selectChoice",function(a,b){if(b.length){var c=$(this).parents(".autocomplete-light-widget").yourlabsWidget();c.selectChoice(b),c.widget.trigger("widgetSelectChoice",[b,c])}})},yourlabs.Widget.prototype.selectChoice=function(a){var b=this.getValue(a);if(!b)return void(window.console&&console.log("yourlabs.Widget.getValue failed"));this.freeDeck(),this.addToDeck(a,b),this.addToSelect(a,b);var c=$(":input:visible").index(this.input);if(this.resetDisplay(),"1"===this.clearInputOnSelectChoice&&(this.input.val(""),this.autocomplete.value=""),this.input.is(":visible"))this.input.focus();else{$(":input:visible:eq("+c+")").focus()}this.select.is("[multiple]")||this.input.prop("disabled",!0)},yourlabs.Widget.prototype.freeDeck=function(){var a=this.maximumValues-this.deck.children().length;if(this.maximumValues&&a<1){var b=$(this.deck.children()[0]);this.deselectChoice(b)}},yourlabs.Widget.prototype.resetDisplay=function(){var a=this.select.find("option:selected").length;this.maximumValues&&a===this.maximumValues?this.input.hide():this.input.show(),this.deck.show(),this.autocomplete.box.is(":visible")&&this.autocomplete.fixPosition()},yourlabs.Widget.prototype.deckChoiceHtml=function(a,b){var c=a.clone();return this.addRemove(c),c},yourlabs.Widget.prototype.optionChoice=function(a){var b=this.choiceTemplate.clone(),c=b.find(".append-option-html");return c.length?c.append(a.html()):b.html(a.html()),b},yourlabs.Widget.prototype.addRemove=function(a){var b=this.widget.find(".remove:last").clone().css("display","inline-block"),c=a.find(".prepend-remove");c.length?c.prepend(b):a.prepend(b)},yourlabs.Widget.prototype.addToDeck=function(a,b){if(!this.deck.find('[data-value="'+b+'"]').length){var c=this.deckChoiceHtml(a);c.attr("data-value",b),this.deck.append(c)}},yourlabs.Widget.prototype.addToSelect=function(a,b){var c=this.select.find('option[value="'+b+'"]');c.length||(this.select.append('<option selected="selected" value="'+b+'"></option>'),c=this.select.find('option[value="'+b+'"]')),c.attr("selected","selected"),this.select.trigger("change"),this.updateAutocompleteExclude()},yourlabs.Widget.prototype.deselectChoice=function(a){var b=this.getValue(a);this.select.find('option[value="'+b+'"]').remove(),this.select.trigger("change"),a.remove(),0===this.deck.children().length&&this.deck.hide(),this.updateAutocompleteExclude(),this.resetDisplay(),this.input.prop("disabled",!1),this.widget.trigger("widgetDeselectChoice",[a,this])},yourlabs.Widget.prototype.updateAutocompleteExclude=function(){var a=this,b=this.deck.find(this.autocomplete.choiceSelector);this.autocomplete.data.exclude=$.map(b,function(b){return a.getValue($(b))})},yourlabs.Widget.prototype.initialize=function(){this.initializeAutocomplete();var a=this;this.deck.find(this.autocomplete.choiceSelector).each(function(){var b=a.getValue($(this)),c=a.select.find('option[value="'+b+'"]');c.prop("selected")||c.prop("selected",!0)});var b=this.deck.find(this.input.yourlabsAutocomplete().choiceSelector);this.addRemove(b),this.resetDisplay(),a.select.val()&&!this.select.is("[multiple]")&&this.input.prop("disabled",!0),this.bindSelectChoice()},yourlabs.Widget.prototype.destroy=function(a){a.find("input").unbind("selectChoice").yourlabsAutocomplete("destroy")},$.fn.yourlabsWidget=function(a){a=a||{};var b=this.yourlabsRegistry("widget");if("destroy"===a)return void(b&&(b.destroy(this),this.removeData("widget")));if(void 0===b){b=new yourlabs.Widget(this);for(var c in this.data())if(c&&"widget"===c.substr(0,6)&&"widget"!==c){var d=c.replace("widget","");d=d.charAt(0).toLowerCase()+d.slice(1),b[d]=this.data(c)}b=$.extend(b,a),$(this).yourlabsRegistry("widget",b),b.initialize(),b.widget.attr("data-widget-ready",1),b.widget.trigger("widget-ready")}return b},$(document).ready(function(){$("body").on("initialize",".autocomplete-light-widget[data-widget-bootstrap=normal]",function(){$(this).yourlabsWidget()}),$("body").on("click",".autocomplete-light-widget .deck .remove",function(){var a=$(this).parents(".autocomplete-light-widget").yourlabsWidget(),b=a.input.yourlabsAutocomplete().choiceSelector,c=$(this).parents(b);a.deselectChoice(c)}),$(document).trigger("yourlabsWidgetReady"),$('.autocomplete-light-widget:not([id*="__prefix__"])').each(function(){$(this).trigger("initialize")}),$(document).bind("DOMNodeInserted",function(a){var b;if($(a.target).is("option")){if(b=$(a.target).parents(".autocomplete-light-widget"),!b.length)return;b=b.yourlabsWidget();var c=$(a.target),d=c.attr("value");if(!b.deck.find('[data-value="'+d+'"]').length){var e=b.optionChoice(c);e.attr("data-value",d),b.selectChoice(e)}}else{if(b=$(a.target).find(".autocomplete-light-widget:not([data-widget-ready])"),!b.length)return;if(b.is(".yourlabs-autocomplete"))return;b.yourlabsWidget("destroy"),b.find("input").yourlabsAutocomplete("destroy"),b.trigger("initialize")}});var a=yourlabs.getInternetExplorerVersion();if(-1!==a&&a<9){observe=[".autocomplete-light-widget:not([data-yourlabs-skip])",".autocomplete-light-widget option:not([data-yourlabs-skip])"].join(),$(observe).attr("data-yourlabs-skip",1);var b=function(){$(observe).each(function(){$(document).trigger(jQuery.Event("DOMNodeInserted",{target:$(this)})),$(this).attr("data-yourlabs-skip",1)}),setTimeout(b,500)};setTimeout(b,500)}}); |