forked from hernantz/jSuggest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.jSuggest.min.js
executable file
·12 lines (12 loc) · 5.64 KB
/
jquery.jSuggest.min.js
1
2
3
4
5
6
7
8
9
10
11
12
(function(c){c.fn.jSuggest=function(r){var a=c.extend({source:{},uniqID:!1,startText:"Enter a Value",emptyText:"No Results Found",repeatText:"Inserting the same value twice is not allowed",loadingText:"Loading...",preFill:{},limitText:"No More Values Are Allowed",newItem:!1,newText:"Adding New Values Is Not Allowed",selectedItemProp:"value",selectProp:"value",seekVal:"value",queryParam:"q",queryLimit:!1,extraParams:"",matchCase:!1,minChars:1,keyDelay:400,resultsHighlight:!0,selectionLimit:!1,showResultList:!0,
selectionClick:function(){},selectionAdded:function(){},selectionRemoved:function(a){a.remove()},spotFirst:!0,formatList:!1,beforeRetrieve:function(a){return a},retrieveComplete:function(a){return a},resultClick:function(){},resultsComplete:function(){}},r),x=typeof a.source;return this.each(function(b){function v(){if(46==lastKey||9<lastKey&&32>lastKey)return e.hide();var f=c.trim(d.val()).replace(/[\\]+|[\/]+/g,"").replace(/\s+/g," ");if(f.length>=a.minChars)if(a.beforeRetrieve&&(f=a.beforeRetrieve.call(this,
f)),g.addClass("loading"),k.html('<li class="as-message">'+a.loadingText+"</li>").show(),e.show(),"string"===x){var b=q?"&limit="+encodeURIComponent(q):"";c.getJSON(a.source+"?"+a.queryParam+"="+encodeURIComponent(f)+b+a.extraParams,function(a){w(a,f)})}else w(a.source,f);else g.removeClass("loading"),e.hide()}function r(f,b,y){var p=0,h,l,i;e.html(k.html("")).hide();for(var m=0;m<y;m++){h=m;l="";for(var j=a.seekVal.split(","),n=0;n<j.length;n++)i=c.trim(j[n]),l+=f[h][i];a.matchCase||(l=l.toLowerCase(),
b=b.toLowerCase());if(-1!==l.search(b)&&-1===o.val().search(f[h][a.selectProp]+",")&&(l=c('<li class="as-result-item" id="as-result-item-'+h+'"></li>').click(function(){var f=c(this).data("data"),b=f.num,h=f.attributes;d.val("").focus();s(h,b);a.resultClick.call(this,f);e.hide()}).mouseover(function(){c("li",k).removeClass("active");c(this).addClass("active")}).data("data",{attributes:f[h],num:h}),h=c.extend({},f[h]),i=RegExp("(?![^&;]+;)(?!<[^<>]*)("+b+")(?![^<>]*>)(?![^&;]+;)",""+(!a.matchCase?
"gi":"g")+""),a.resultsHighlight&&(h[a.selectedItemProp]=h[a.selectedItemProp].replace(i,"<em>$1</em>")),l=!a.formatList?l.html(h[a.selectedItemProp]):a.formatList.call(this,h,l),k.append(l),p++,q&&q==p))break}g.removeClass("loading");t=!0;0>=p&&k.html('<li class="as-message">'+a.emptyText+"</li>");e.show();a.spotFirst&&u("down");a.resultsComplete.call(this)}function s(f,b){o.val(o.val()+f[a.selectProp]+",");var e=c('<li class="as-selection-item" id="as-selection-'+b+'"></li>').click(function(){a.selectionClick.call(this,
c(this));g.children().removeClass("selected");c(this).addClass("selected")}),p=c('<a class="as-close">x</a>').click(function(){o.val(o.val().replace(f[a.selectProp]+",",""));a.selectionRemoved.call(this,e,f);d.focus();return!1});i.before(e.html(f[a.selectedItemProp]).prepend(p));a.selectionAdded.call(this,i.prev(),f)}function u(a){if(0<c(":visible",e).length){var b=c("li",e),d="down"===a?b.eq(0):b.filter(":last"),g=c("li.active:first",e);0<g.length&&(d="down"===a?g.next():g.prev());b.removeClass("active");
d.addClass("active")}}function w(c,b){var d=0,e=a.retrieveComplete.call(this,c),g;for(g in e)e.hasOwnProperty(g)&&d++;r(e,b,d)}var b=!a.uniqID?b+""+Math.floor(100*Math.random()):b=a.uniqID,z=!a.uniqID?"as-input-"+b:b,d=c(this);d.attr("autocomplete","off").addClass("as-input").attr("id",z).val(a.startText);d.wrap('<ul class="as-selections" id="as-selections-'+b+'"></ul>').wrap('<li class="as-original" id="as-original-'+b+'"></li>');var g=c("#as-selections-"+b),i=c("#as-original-"+b),e=c('<div class="as-results" id="as-results-'+
b+'"></div>').hide(),k=c('<ul class="as-list"></ul>').css("width",g.outerWidth()).appendTo(e),o=c('<input type="hidden" class="as-values" name="as_values_'+b+'" id="as-values-'+b+'" />'),q=a.queryLimit,m=a.selectionLimit,t=!1,n=null;if("object"===typeof a.preFill){var b=0,j;for(j in a.preFill)a.preFill.hasOwnProperty(j)&&b++;if(0<b){m&&b>=m&&(b=m);for(j=0;j<b;j++)s(a.preFill[j],"000"+j);c("li.as-selection-item",g).addClass("blur").removeClass("selected");d.val("").width(30)}}d.after(o);g.click(function(){d.focus()}).after(e);
d.focus(function(){d.val()===a.startText&&""===o.val()&&d.val("");c("li.as-selection-item",g).removeClass("blur");""!==c.trim(d.val())&&e.show()}).blur(function(){""===d.val()&&""===o.val()&&d.val(a.startText);d.width(8*d.val().length+30);e.is(":hover")||(c("li.as-selection-item",g).addClass("blur").removeClass("selected"),e.hide())}).keydown(function(b){lastKey=b.keyCode;switch(lastKey){case 38:case 40:b.preventDefault();38===lastKey?u("up"):u("down");break;case 8:""===d.val()&&(g.children().not(i.prev()).removeClass("selected"),
i.prev().hasClass("selected")?i.prev().find(".as-close").click():(a.selectionClick.call(this,i.prev()),i.prev().addClass("selected")));1===d.val().length&&e.hide();n&&clearTimeout(n);n=setTimeout(function(){v()},a.keyDelay);break;case 9:case 188:case 13:b.preventDefault();b=c.trim(d.val()).replace(/(,)/g,"");if(""!==b&&0>o.val().search(b+",")&&b.length>=a.minChars)if((9===lastKey||13===lastKey)&&0<c("li.as-result-item:visible",e).length&&0<c("li.active:first",k).length)c("li.active:first",k).click();
else if(a.newItem){if(t)if(t=!1,m&&c("li",g).length<=m){var b=a.newItem.call(this,b),j=c("li",g).length;s(b,"00"+(j+1));e.hide();d.val("")}else k.html('<li class="as-message">'+a.limitText+"</li>").show()}else k.html('<li class="as-message">'+a.newText+"</li>").show();else k.html('<li class="as-message">'+a.repeatText+"</li>").show();break;default:m&&c("li.as-selection-item",g).length>=m?(k.html('<li class="as-message">'+a.limitText+"</li>"),e.show()):(n&&clearTimeout(n),n=setTimeout(function(){v()},
a.keyDelay))}}).keyup(function(){d.width(8*d.val().length+30)})})}})(jQuery);