From a75433ad127a1bd7b3e78e781d1a13387f6b4f32 Mon Sep 17 00:00:00 2001 From: Jan Philipp Date: Fri, 29 Mar 2013 17:58:01 +0100 Subject: [PATCH] Introduce `defaults` command #15. Apply additional options (merged in existing defaults) with `$.fn.autoSuggest('defaults', additionalOtions);` Replace the existing defaults for the *next* new autosuggest instance with `$.fn.autoSuggest('defaults', replaceOptions, true);` --- README.md | 8 ++++++++ dist/jquery.autoSuggest.js | 11 ++++++++++- dist/jquery.autoSuggest.min.js | 4 ++-- package.json | 2 +- src/jquery.autoSuggest.coffee | 5 +++++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e8726fc..2b65112 100644 --- a/README.md +++ b/README.md @@ -508,6 +508,14 @@ echo json_encode($data); ## Release History +## 2013-03-29 Version 2.3.0 + +* Add method `defaults` allowing extending or overriding the defaults: i.e. `$.fn.autoSuggest('defaults', {searchObjProps: 'title'});` + +## 2013-03-29 Version 2.2.0 + +* Change: Switch to Grunt 0.4 + ## 2013-03-03 Version 2.1.0 * Fix: Travis builds weren't be possible. Instead of using hooks in `travis.yml` this will be now managed by a `Gemfile` and `bundler`. diff --git a/dist/jquery.autoSuggest.js b/dist/jquery.autoSuggest.js index 71c0a70..1bd734b 100644 --- a/dist/jquery.autoSuggest.js +++ b/dist/jquery.autoSuggest.js @@ -1,4 +1,4 @@ -/*! jQuery AutoSuggest - v2.2.0 - 2013-03-29 +/*! jQuery AutoSuggest - v2.3.0 - 2013-03-29 * URL: http://hlsolutions.github.com/jquery-autosuggest * Copyright (c) 2013 Jan Philipp * Licensed MIT, GPL */ @@ -1115,6 +1115,15 @@ Based on the 1.6er release dated in July, 2012 value = values[_i]; element.trigger('removeSelection', value); } + }, + defaults: function(options, replace) { + if (replace == null) { + replace = false; + } + if (replace) { + defaults = {}; + } + $.extend(defaults, options); } }; diff --git a/dist/jquery.autoSuggest.min.js b/dist/jquery.autoSuggest.min.js index 98c4ea6..f9c6857 100644 --- a/dist/jquery.autoSuggest.min.js +++ b/dist/jquery.autoSuggest.min.js @@ -1,6 +1,6 @@ -/*! jQuery AutoSuggest - v2.2.0 - 2013-03-29 +/*! jQuery AutoSuggest - v2.3.0 - 2013-03-29 * URL: http://hlsolutions.github.com/jquery-autosuggest * Copyright (c) 2013 Jan Philipp * Licensed MIT, GPL */ -(function(){var e,t,l,n,i,s,a,r=[].indexOf||function(e){for(var t=0,l=this.length;l>t;t++)if(t in this&&this[t]===e)return t;return-1},o=[].slice;e=jQuery,i=function(){function t(){}return t.prototype._=void 0,t.escapeQuotes=function(e){return e?e.replace(/"/g,'\\"'):void 0},t.escapeHtml=function(t){return e("").text(t).html()},t.setPlaceholderEnabled=function(e,t){var l,n,i;n=["placeholder","disabled-placeholder"],t?(l=n[1],i=n[0]):(l=n[0],i=n[1]),!e.attr(i)&&e.attr(l)&&(e.attr(i,function(){return e.attr(l)}),e.removeAttr(l))},t}(),t=function(){function t(){}return t.prototype._=void 0,t.getExtraParams=function(t){var l,n,i,s,a,r,o;if(s=t.extraParams,e.isFunction(s)&&(s=s(this)),"string"===e.type(s)){for(l={},o=s.split("&"),a=0,r=o.length;r>a;a++)n=o[a],""!==n&&(i=n.split("=",2),i.length&&(l[i[0]]=i[1]));s=l}return s},t}(),n=function(){function t(e,t){this.hiddenField=e,this.items=null!=t?t:[]}return t.prototype._=void 0,t.prototype.hiddenField=null,t.prototype.items=null,t.prototype.syncToHiddenField=function(){var e,t,l,n,i;if(this.hiddenField){for(t="",i=this.items,l=0,n=i.length;n>l;l++)e=i[l],t+=","+e;t&&(t+=","),this.hiddenField.val(t||",")}},t.prototype.add=function(e){this.exist(e)||this.items.push(e),this.syncToHiddenField()},t.prototype.remove=function(t){this.items=e.grep(this.items,function(e){return e!==t}),this.syncToHiddenField()},t.prototype.isEmpty=function(){return 0===this.items.length},t.prototype.exist=function(t){return-1!==e.inArray(t,this.items)},t.prototype.getAll=function(){return this.items.slice(0)},t.prototype.clear=function(){this.items=[],this.syncToHiddenField()},t}(),l=function(){function t(){}return t.onSelectionAdd=function(t,l,n,s,a,r){var o;o=s.onSelectionAdd.call(t,l,n,s),i.setPlaceholderEnabled(t,0===r.length),e.isFunction(s.afterSelectionAdd)&&s.afterSelectionAdd.call(t,o,a,r)},t.onSelectionRemove=function(t,l,n,s,a){e.isFunction(n.onSelectionRemove)&&n.onSelectionRemove.call(t,l,n),i.setPlaceholderEnabled(t,0===a.length),e.isFunction(n.afterSelectionRemove)&&n.afterSelectionRemove.call(t,l,s,a)},t.onSelectionClick=function(t,l,n,s,a){e.isFunction(n.afterSelectionClick)&&n.afterSelectionClick.call(t,l,s,a),i.setPlaceholderEnabled(t,0===a.length)},t.onAjaxRequestDone=function(t,l,n){e.isFunction(n.onAjaxRequestDone)&&l.done(n.onAjaxRequestDone)},t.onAjaxRequestFail=function(t,l,n){e.isFunction(n.onAjaxRequestFail)&&l.fail(n.onAjaxRequestFail)},t.onAjaxRequestAlways=function(t,l,n){e.isFunction(n.onAjaxRequestAlways)&&l.always(n.onAjaxRequestAlways)},t}(),s={asHtmlID:!1,useOriginalInputName:!1,usePlaceholder:!1,preFill:null,startText:"Enter Name Here",emptyText:"No Results Found",emptyTextPlaceholder:/\{\d+\}/,limitText:"No More Selections Are Allowed",selectedItemProp:"value",selectedValuesProp:"value",searchActive:!0,searchObjProps:"value",queryParam:"q",limitParam:"limit",retrieveLimit:null,extraParams:null,matchCase:!1,minChars:1,keyDelay:400,resultsHighlight:!0,selectionLimit:!1,showResultList:!0,showResultListWhenNoMatch:!1,canGenerateNewSelections:!0,start:null,afterSelectionClick:null,afterSelectionAdd:null,afterSelectionRemove:null,onSelectionAdd:function(e,t){return e.before(t),e.prev()},onSelectionRemove:function(e,t){return t.fadeOut?e.fadeOut(t.fadeOut,function(){return e.remove()}):e.remove()},formatList:null,beforeRequest:null,afterRequest:null,onAjaxRequestDone:null,onAjaxRequestFail:null,onAjaxRequestAlways:null,onResultItemClick:null,afterResultListShow:null,neverSubmit:!1,preventPropagationOnEscape:!1,ajaxOptions:{type:"get",dataType:"json"},inputAttrs:{autocomplete:"off"},fadeOut:!1,remoteFilter:!1},a={init:function(a,o){var c,u;return o=e.extend({},s,o),c=null,"auto"===o.remoteFilter&&(o.remoteFilter="string"===e.type(a)),(u=function(){switch(e.type(a)){case"function":return a;case"string":return function(n,i){var s,r,u,d;d={},d[""+o.queryParam]=encodeURIComponent(decodeURIComponent(n)),o.retrieveLimit&&(d[o.limitParam]=encodeURIComponent(o.retrieveLimit)),r=t.getExtraParams(o),"object"===e.type(r)&&e.extend(d,r),s=e.extend({},o.ajaxOptions,{url:a,data:d}),u=function(t){return e.isFunction(o.afterRequest)&&(t=o.afterRequest.apply(this,[t])),i(t,n)},c=e.ajax(s).done(u),l.onAjaxRequestDone(this,c,o),l.onAjaxRequestFail(this,c,o),l.onAjaxRequestAlways(this,c,o)};case"array":case"object":return function(e,t){return t(a,e)}}}())?this.each(function(){var t,s,a,d,m,h,p,f,v,g,y,x,P,A,R,C,b,F,w,S,k,I,q,j,T,E,L,D,H,O,N,V,_,M,W,Q;switch(o.inputAttrs=e.extend(o.inputAttrs,{}),P=!1,y=e(this),m=null,h=null,p=null,f=null,v=null,o.asHtmlID?(m=o.asHtmlID,h=m,f="as-values-"+m,o.useOriginalInputName?(v=y.attr("name"),y.attr({name:"old_"+y.attr("name")})):v="as_values_"+m):(m=""+(m||"")+Math.floor(100*Math.random()),h="as-input-"+m,f="as-values-"+m,o.useOriginalInputName?(v=y.attr("name"),y.attr({name:"old_"+y.attr("name")})):v="as_values_"+m),o.inputAttrs.id=h,o.usePlaceholder||(o.inputAttrs.placeholder=o.startText),y.attr(o.inputAttrs),y.addClass("as-input"),o.usePlaceholder||y.val(o.startText),y.wrap('').wrap('
  • '),D=e("#as-selections-"+m),x=e("#as-original-"+m),E=e('
    '),L=e(''),p=e(''),d=new n(p),A=null,H=null,I="",F=!1,b=null,k=0,T={add:function(t){var l,n;l=e(D).find("li").length,n=s(t,"u"+l),null!=n&&n.addClass("blur")},remove:function(e){d.remove(e),D.find('li[data-value="'+i.escapeHtml(e)+'"]').remove()}},a=function(){return{add:T.add,remove:T.remove}},y.bind("addSelection",function(e,t){return T.add(t)}),y.bind("removeSelection",function(e,t){return T.remove(t)}),s=function(t,n){var s,a;return d.add(t[o.selectedValuesProp]),a=e('
  • '),a.on({click:function(){m=e(this),l.onSelectionClick(y,m,o,t[o.selectedValuesProp],d.getAll()),D.children().removeClass("selected"),m.addClass("selected")},mousedown:function(){P=!1}}),s=e('×'),s.click(function(){return d.remove(t[o.selectedValuesProp]),l.onSelectionRemove(y,a,o,null,d.getAll()),P=!0,y.focus(),!1}),"string"!=typeof t[o.selectedItemProp]?l.onSelectionAdd(y,x,a.append(t[o.selectedItemProp]).prepend(s),o,t,d.getAll()):l.onSelectionAdd(y,x,a.text(t[o.selectedItemProp]).prepend(s),o,t,d.getAll()),x.prev()},e.isFunction(o.start)&&o.start.call(this,a()),e.type(o.preFill)){case"string":for(W=o.preFill.split(","),N=0,_=W.length;_>N;N++)O=W[N],R={},R[""+o.selectedValuesProp]=O,""!==O&&s(R,"000"+g);break;case"array":if(o.preFill.length)for(e.isFunction(o.afterRequest)&&(o.preFill=o.afterRequest.call(this,o.preFill)),Q=o.preFill,g=V=0,M=Q.length;M>V;g=++V)R=Q[g],S=R[o.selectedValuesProp],S===void 0&&(S=""),""!==S&&s(R,"000"+g)}return d.isEmpty()||(y.val(""),D.find("li.as-selection-item").addClass("blur").removeClass("selected"),i.setPlaceholderEnabled(y,!1)),y.after(p),D.on({click:function(){P=!0,y.focus()},mousedown:function(){D.children().removeClass("selected"),P=!1}}),D.after(E),C=function(){var e,t,l;return 46===b||r.call(function(){for(l=[],t=9;31>t;t++)l.push(t);return l}.apply(this),b)>=0?(E.hide(),void 0):(e=y.val().replace(/[\\]+|[\/]+/g,""),""===e||e!==I?(I=e,e.length>=o.minChars||0===o.minChars&&0===e.length?(D.addClass("loading"),j(e)):(D.removeClass("loading"),E.hide())):void 0)},j=function(l){return e.isFunction(o.beforeRequest)&&(l=o.beforeRequest.apply(this,[l,o])),t(),u(l,q)},q=function(t,l){var n,a,r,c,u,h,p,f,v,g,x,A,C,b,w;for(o.matchCase||(l=l.toLowerCase()),l=l.replace("(","(","g").replace(")",")","g"),r=0,E.hide().html(L.html("")),u=0,x=0,C=t.length;C>x;x++){if(R=t[x],k++,a=!1,"value"===o.searchObjProps)f=R.value;else for(f="",w=o.searchObjProps.split(","),A=0,b=w.length;b>A;A++)c=w[A],f+=""+R[e.trim(c)]+" ";if(f&&(o.matchCase||(f=f.toLowerCase()),o.searchActive&&(-1===f.indexOf(l)&&!o.remoteFilter||d.exist(R[o.selectedValuesProp]))||(a=!0)),a&&(n=e('
  • '),n.on({click:function(){var l,n;m=e(this),n=m.data("data"),l=n.num,0>=D.find("#as-selection-"+l).length&&!F&&(t=n.attributes,y.val("").focus(),I="",s(t,l),e.isFunction(o.onResultItemClick)&&o.onResultItemClick.call(this,n),E.hide()),F=!1},mousedown:function(){P=!1},mouseover:function(){m=e(this),L.find("li").removeClass("active"),m.addClass("active")}}),n.data("data",{attributes:t[u],num:k}),g=e.extend({},t[u]),l=l.replace(/"/g,'\\"'),h=o.matchCase?RegExp("(?![^&;]+;)(?!<[^<>]*)("+i.escapeHtml(l)+")(?![^<>]*>)(?![^&;]+;)","g"):RegExp("(?![^&;]+;)(?!<[^<>]*)("+i.escapeHtml(l)+")(?![^<>]*>)(?![^&;]+;)","gi"),"string"==typeof g[o.selectedItemProp]?(g[o.selectedItemProp]=i.escapeHtml(g[o.selectedItemProp]),o.resultsHighlight&&l.length>0&&(g[o.selectedItemProp]=g[o.selectedItemProp].replace(h,"$1"))):o.resultsHighlight&&l.length>0&&g[o.selectedItemProp].html(g[o.selectedItemProp].html().replace(h,"$1")),n=o.formatList?o.formatList.call(this,g,n):n.html(g[o.selectedItemProp]),L.append(n),r++,o.retrieveLimit&&o.retrieveLimit===r))break;u+=1}D.removeClass("loading"),0>=r&&(v=o.emptyText,"regexp"===e.type(o.emptyTextPlaceholder)&&(v=v.replace(o.emptyTextPlaceholder,l)),L.html('
  • '+v+"
  • ")),L.css({width:D.outerWidth()}),p=r>0||o.showResultListWhenNoMatch,p&&E.show(),e.isFunction(o.afterResultListShow)&&o.afterResultListShow.call(this,p)},w=function(e){var t,l,n;if(E.find(":visible").length){switch(l=E.find("li"),e){case"down":n=l.eq(0);break;default:n=l.filter(":last")}if(t=E.find("li.active:first"),t.length)switch(e){case"down":n=t.next();break;default:n=t.prev()}l.removeClass("active"),n.addClass("active")}},t=function(){c&&(c.abort(),c=null)},y.on({focus:function(){return m=e(this),!o.usePlaceholder&&m.val()===o.startText&&d.isEmpty()?m.val(""):P&&(D.find("li.as-selections-item").removeClass("blur"),""!==m.val()&&(L.css({width:D.outerWidth()}),E.show())),A&&clearInterval(A),A=setInterval(function(){o.showResultList&&(o.selectionLimit&&D.find("li.as-selection-item").length>=o.selectionLimit?(L.html('
  • '+o.limitText+"
  • "),E.show()):C())},o.keyDelay),P=!0,0===o.minChars&&j(m.val()),!0},blur:function(){m=e(this),!o.usePlaceholder&&""===m.val()&&d.isEmpty()&&o.minChars>0?m.val(o.startText):P&&(D.find("li.as-selection-item").addClass("blur").removeClass("selected"),E.hide()),A&&clearInterval(A),i.setPlaceholderEnabled(m,d.isEmpty())},keydown:function(n){var a,r,c,u,m,h;switch(b=n.keyCode,r=!1,n.keyCode){case 38:n.preventDefault(),w("up");break;case 40:n.preventDefault(),e(":visible",E).length>0?w("down"):(H&&clearTimeout(H),H=setTimeout(function(){C()},o.keyDelay));break;case 8:""===y.val()&&(h=d.getAll(),m=null,m=h.length?h[h.length-1]:null,D.children().not(x.prev()).removeClass("selected"),x.prev().hasClass("selected")?(d.remove(m),l.onSelectionRemove(y,x.prev(),o,null,d.getAll())):(l.onSelectionClick(y,x.prev(),o,null,d.getAll()),x.prev().addClass("selected"))),1===y.val().length&&(E.hide(),I="",t()),E.find(":visible").length&&(H&&clearTimeout(H),H=setTimeout(function(){C()},o.keyDelay));break;case 9:case 188:a=E.find("li.active:visible:first"),o.canGenerateNewSelections?(F=!0,c=y.val().replace(/(,)/g,""),""!==c&&!d.exist(c)&&c.length>=o.minChars&&0===a.length&&(n.preventDefault(),u={},u[""+o.selectedItemProp]=c,u[""+o.selectedValuesProp]=c,s(u,"00"+(D.find("li").length+1)),y.val(""),t())):y.val(""),a.length&&(F=!1,a.click(),E.hide(),n.preventDefault());break;case 13:F=!1,a=E.find("li.active:first"),a.length&&(a.click(),E.hide()),(o.neverSubmit||a.length)&&n.preventDefault();break;case 27:o.preventPropagationOnEscape&&E.find(":visible").length&&n.stopPropagation();break;case 16:case 20:t(),E.hide()}i.setPlaceholderEnabled(y,d.isEmpty())}})}):void 0},add:function(){var t,l,n,i,s;for(n=arguments.length>=1?o.call(arguments,0):[],t=e(this),i=0,s=n.length;s>i;i++)l=n[i],t.trigger("addSelection",l)},remove:function(){var t,l,n,i,s;for(n=arguments.length>=1?o.call(arguments,0):[],t=e(this),i=0,s=n.length;s>i;i++)l=n[i],t.trigger("removeSelection",l)}},e.fn.autoSuggest=function(e){return a[e]?a[e].apply(this,Array.prototype.slice.call(arguments,1)):a.init.apply(this,arguments)}}).call(this); \ No newline at end of file +(function(){var e,t,l,n,i,s,a,r=[].indexOf||function(e){for(var t=0,l=this.length;l>t;t++)if(t in this&&this[t]===e)return t;return-1},o=[].slice;e=jQuery,i=function(){function t(){}return t.prototype._=void 0,t.escapeQuotes=function(e){return e?e.replace(/"/g,'\\"'):void 0},t.escapeHtml=function(t){return e("").text(t).html()},t.setPlaceholderEnabled=function(e,t){var l,n,i;n=["placeholder","disabled-placeholder"],t?(l=n[1],i=n[0]):(l=n[0],i=n[1]),!e.attr(i)&&e.attr(l)&&(e.attr(i,function(){return e.attr(l)}),e.removeAttr(l))},t}(),t=function(){function t(){}return t.prototype._=void 0,t.getExtraParams=function(t){var l,n,i,s,a,r,o;if(s=t.extraParams,e.isFunction(s)&&(s=s(this)),"string"===e.type(s)){for(l={},o=s.split("&"),a=0,r=o.length;r>a;a++)n=o[a],""!==n&&(i=n.split("=",2),i.length&&(l[i[0]]=i[1]));s=l}return s},t}(),n=function(){function t(e,t){this.hiddenField=e,this.items=null!=t?t:[]}return t.prototype._=void 0,t.prototype.hiddenField=null,t.prototype.items=null,t.prototype.syncToHiddenField=function(){var e,t,l,n,i;if(this.hiddenField){for(t="",i=this.items,l=0,n=i.length;n>l;l++)e=i[l],t+=","+e;t&&(t+=","),this.hiddenField.val(t||",")}},t.prototype.add=function(e){this.exist(e)||this.items.push(e),this.syncToHiddenField()},t.prototype.remove=function(t){this.items=e.grep(this.items,function(e){return e!==t}),this.syncToHiddenField()},t.prototype.isEmpty=function(){return 0===this.items.length},t.prototype.exist=function(t){return-1!==e.inArray(t,this.items)},t.prototype.getAll=function(){return this.items.slice(0)},t.prototype.clear=function(){this.items=[],this.syncToHiddenField()},t}(),l=function(){function t(){}return t.onSelectionAdd=function(t,l,n,s,a,r){var o;o=s.onSelectionAdd.call(t,l,n,s),i.setPlaceholderEnabled(t,0===r.length),e.isFunction(s.afterSelectionAdd)&&s.afterSelectionAdd.call(t,o,a,r)},t.onSelectionRemove=function(t,l,n,s,a){e.isFunction(n.onSelectionRemove)&&n.onSelectionRemove.call(t,l,n),i.setPlaceholderEnabled(t,0===a.length),e.isFunction(n.afterSelectionRemove)&&n.afterSelectionRemove.call(t,l,s,a)},t.onSelectionClick=function(t,l,n,s,a){e.isFunction(n.afterSelectionClick)&&n.afterSelectionClick.call(t,l,s,a),i.setPlaceholderEnabled(t,0===a.length)},t.onAjaxRequestDone=function(t,l,n){e.isFunction(n.onAjaxRequestDone)&&l.done(n.onAjaxRequestDone)},t.onAjaxRequestFail=function(t,l,n){e.isFunction(n.onAjaxRequestFail)&&l.fail(n.onAjaxRequestFail)},t.onAjaxRequestAlways=function(t,l,n){e.isFunction(n.onAjaxRequestAlways)&&l.always(n.onAjaxRequestAlways)},t}(),s={asHtmlID:!1,useOriginalInputName:!1,usePlaceholder:!1,preFill:null,startText:"Enter Name Here",emptyText:"No Results Found",emptyTextPlaceholder:/\{\d+\}/,limitText:"No More Selections Are Allowed",selectedItemProp:"value",selectedValuesProp:"value",searchActive:!0,searchObjProps:"value",queryParam:"q",limitParam:"limit",retrieveLimit:null,extraParams:null,matchCase:!1,minChars:1,keyDelay:400,resultsHighlight:!0,selectionLimit:!1,showResultList:!0,showResultListWhenNoMatch:!1,canGenerateNewSelections:!0,start:null,afterSelectionClick:null,afterSelectionAdd:null,afterSelectionRemove:null,onSelectionAdd:function(e,t){return e.before(t),e.prev()},onSelectionRemove:function(e,t){return t.fadeOut?e.fadeOut(t.fadeOut,function(){return e.remove()}):e.remove()},formatList:null,beforeRequest:null,afterRequest:null,onAjaxRequestDone:null,onAjaxRequestFail:null,onAjaxRequestAlways:null,onResultItemClick:null,afterResultListShow:null,neverSubmit:!1,preventPropagationOnEscape:!1,ajaxOptions:{type:"get",dataType:"json"},inputAttrs:{autocomplete:"off"},fadeOut:!1,remoteFilter:!1},a={init:function(a,o){var c,u;return o=e.extend({},s,o),c=null,"auto"===o.remoteFilter&&(o.remoteFilter="string"===e.type(a)),(u=function(){switch(e.type(a)){case"function":return a;case"string":return function(n,i){var s,r,u,d;d={},d[""+o.queryParam]=encodeURIComponent(decodeURIComponent(n)),o.retrieveLimit&&(d[o.limitParam]=encodeURIComponent(o.retrieveLimit)),r=t.getExtraParams(o),"object"===e.type(r)&&e.extend(d,r),s=e.extend({},o.ajaxOptions,{url:a,data:d}),u=function(t){return e.isFunction(o.afterRequest)&&(t=o.afterRequest.apply(this,[t])),i(t,n)},c=e.ajax(s).done(u),l.onAjaxRequestDone(this,c,o),l.onAjaxRequestFail(this,c,o),l.onAjaxRequestAlways(this,c,o)};case"array":case"object":return function(e,t){return t(a,e)}}}())?this.each(function(){var t,s,a,d,m,h,p,f,v,g,y,x,P,A,R,C,b,F,w,S,k,I,q,j,T,E,L,D,H,O,N,V,_,M,W,Q;switch(o.inputAttrs=e.extend(o.inputAttrs,{}),P=!1,y=e(this),m=null,h=null,p=null,f=null,v=null,o.asHtmlID?(m=o.asHtmlID,h=m,f="as-values-"+m,o.useOriginalInputName?(v=y.attr("name"),y.attr({name:"old_"+y.attr("name")})):v="as_values_"+m):(m=""+(m||"")+Math.floor(100*Math.random()),h="as-input-"+m,f="as-values-"+m,o.useOriginalInputName?(v=y.attr("name"),y.attr({name:"old_"+y.attr("name")})):v="as_values_"+m),o.inputAttrs.id=h,o.usePlaceholder||(o.inputAttrs.placeholder=o.startText),y.attr(o.inputAttrs),y.addClass("as-input"),o.usePlaceholder||y.val(o.startText),y.wrap('
      ').wrap('
    • '),D=e("#as-selections-"+m),x=e("#as-original-"+m),E=e('
      '),L=e('
        '),p=e(''),d=new n(p),A=null,H=null,I="",F=!1,b=null,k=0,T={add:function(t){var l,n;l=e(D).find("li").length,n=s(t,"u"+l),null!=n&&n.addClass("blur")},remove:function(e){d.remove(e),D.find('li[data-value="'+i.escapeHtml(e)+'"]').remove()}},a=function(){return{add:T.add,remove:T.remove}},y.bind("addSelection",function(e,t){return T.add(t)}),y.bind("removeSelection",function(e,t){return T.remove(t)}),s=function(t,n){var s,a;return d.add(t[o.selectedValuesProp]),a=e('
      • '),a.on({click:function(){m=e(this),l.onSelectionClick(y,m,o,t[o.selectedValuesProp],d.getAll()),D.children().removeClass("selected"),m.addClass("selected")},mousedown:function(){P=!1}}),s=e('×'),s.click(function(){return d.remove(t[o.selectedValuesProp]),l.onSelectionRemove(y,a,o,null,d.getAll()),P=!0,y.focus(),!1}),"string"!=typeof t[o.selectedItemProp]?l.onSelectionAdd(y,x,a.append(t[o.selectedItemProp]).prepend(s),o,t,d.getAll()):l.onSelectionAdd(y,x,a.text(t[o.selectedItemProp]).prepend(s),o,t,d.getAll()),x.prev()},e.isFunction(o.start)&&o.start.call(this,a()),e.type(o.preFill)){case"string":for(W=o.preFill.split(","),N=0,_=W.length;_>N;N++)O=W[N],R={},R[""+o.selectedValuesProp]=O,""!==O&&s(R,"000"+g);break;case"array":if(o.preFill.length)for(e.isFunction(o.afterRequest)&&(o.preFill=o.afterRequest.call(this,o.preFill)),Q=o.preFill,g=V=0,M=Q.length;M>V;g=++V)R=Q[g],S=R[o.selectedValuesProp],S===void 0&&(S=""),""!==S&&s(R,"000"+g)}return d.isEmpty()||(y.val(""),D.find("li.as-selection-item").addClass("blur").removeClass("selected"),i.setPlaceholderEnabled(y,!1)),y.after(p),D.on({click:function(){P=!0,y.focus()},mousedown:function(){D.children().removeClass("selected"),P=!1}}),D.after(E),C=function(){var e,t,l;return 46===b||r.call(function(){for(l=[],t=9;31>t;t++)l.push(t);return l}.apply(this),b)>=0?(E.hide(),void 0):(e=y.val().replace(/[\\]+|[\/]+/g,""),""===e||e!==I?(I=e,e.length>=o.minChars||0===o.minChars&&0===e.length?(D.addClass("loading"),j(e)):(D.removeClass("loading"),E.hide())):void 0)},j=function(l){return e.isFunction(o.beforeRequest)&&(l=o.beforeRequest.apply(this,[l,o])),t(),u(l,q)},q=function(t,l){var n,a,r,c,u,h,p,f,v,g,x,A,C,b,w;for(o.matchCase||(l=l.toLowerCase()),l=l.replace("(","(","g").replace(")",")","g"),r=0,E.hide().html(L.html("")),u=0,x=0,C=t.length;C>x;x++){if(R=t[x],k++,a=!1,"value"===o.searchObjProps)f=R.value;else for(f="",w=o.searchObjProps.split(","),A=0,b=w.length;b>A;A++)c=w[A],f+=""+R[e.trim(c)]+" ";if(f&&(o.matchCase||(f=f.toLowerCase()),o.searchActive&&(-1===f.indexOf(l)&&!o.remoteFilter||d.exist(R[o.selectedValuesProp]))||(a=!0)),a&&(n=e('
      • '),n.on({click:function(){var l,n;m=e(this),n=m.data("data"),l=n.num,0>=D.find("#as-selection-"+l).length&&!F&&(t=n.attributes,y.val("").focus(),I="",s(t,l),e.isFunction(o.onResultItemClick)&&o.onResultItemClick.call(this,n),E.hide()),F=!1},mousedown:function(){P=!1},mouseover:function(){m=e(this),L.find("li").removeClass("active"),m.addClass("active")}}),n.data("data",{attributes:t[u],num:k}),g=e.extend({},t[u]),l=l.replace(/"/g,'\\"'),h=o.matchCase?RegExp("(?![^&;]+;)(?!<[^<>]*)("+i.escapeHtml(l)+")(?![^<>]*>)(?![^&;]+;)","g"):RegExp("(?![^&;]+;)(?!<[^<>]*)("+i.escapeHtml(l)+")(?![^<>]*>)(?![^&;]+;)","gi"),"string"==typeof g[o.selectedItemProp]?(g[o.selectedItemProp]=i.escapeHtml(g[o.selectedItemProp]),o.resultsHighlight&&l.length>0&&(g[o.selectedItemProp]=g[o.selectedItemProp].replace(h,"$1"))):o.resultsHighlight&&l.length>0&&g[o.selectedItemProp].html(g[o.selectedItemProp].html().replace(h,"$1")),n=o.formatList?o.formatList.call(this,g,n):n.html(g[o.selectedItemProp]),L.append(n),r++,o.retrieveLimit&&o.retrieveLimit===r))break;u+=1}D.removeClass("loading"),0>=r&&(v=o.emptyText,"regexp"===e.type(o.emptyTextPlaceholder)&&(v=v.replace(o.emptyTextPlaceholder,l)),L.html('
      • '+v+"
      • ")),L.css({width:D.outerWidth()}),p=r>0||o.showResultListWhenNoMatch,p&&E.show(),e.isFunction(o.afterResultListShow)&&o.afterResultListShow.call(this,p)},w=function(e){var t,l,n;if(E.find(":visible").length){switch(l=E.find("li"),e){case"down":n=l.eq(0);break;default:n=l.filter(":last")}if(t=E.find("li.active:first"),t.length)switch(e){case"down":n=t.next();break;default:n=t.prev()}l.removeClass("active"),n.addClass("active")}},t=function(){c&&(c.abort(),c=null)},y.on({focus:function(){return m=e(this),!o.usePlaceholder&&m.val()===o.startText&&d.isEmpty()?m.val(""):P&&(D.find("li.as-selections-item").removeClass("blur"),""!==m.val()&&(L.css({width:D.outerWidth()}),E.show())),A&&clearInterval(A),A=setInterval(function(){o.showResultList&&(o.selectionLimit&&D.find("li.as-selection-item").length>=o.selectionLimit?(L.html('
      • '+o.limitText+"
      • "),E.show()):C())},o.keyDelay),P=!0,0===o.minChars&&j(m.val()),!0},blur:function(){m=e(this),!o.usePlaceholder&&""===m.val()&&d.isEmpty()&&o.minChars>0?m.val(o.startText):P&&(D.find("li.as-selection-item").addClass("blur").removeClass("selected"),E.hide()),A&&clearInterval(A),i.setPlaceholderEnabled(m,d.isEmpty())},keydown:function(n){var a,r,c,u,m,h;switch(b=n.keyCode,r=!1,n.keyCode){case 38:n.preventDefault(),w("up");break;case 40:n.preventDefault(),e(":visible",E).length>0?w("down"):(H&&clearTimeout(H),H=setTimeout(function(){C()},o.keyDelay));break;case 8:""===y.val()&&(h=d.getAll(),m=null,m=h.length?h[h.length-1]:null,D.children().not(x.prev()).removeClass("selected"),x.prev().hasClass("selected")?(d.remove(m),l.onSelectionRemove(y,x.prev(),o,null,d.getAll())):(l.onSelectionClick(y,x.prev(),o,null,d.getAll()),x.prev().addClass("selected"))),1===y.val().length&&(E.hide(),I="",t()),E.find(":visible").length&&(H&&clearTimeout(H),H=setTimeout(function(){C()},o.keyDelay));break;case 9:case 188:a=E.find("li.active:visible:first"),o.canGenerateNewSelections?(F=!0,c=y.val().replace(/(,)/g,""),""!==c&&!d.exist(c)&&c.length>=o.minChars&&0===a.length&&(n.preventDefault(),u={},u[""+o.selectedItemProp]=c,u[""+o.selectedValuesProp]=c,s(u,"00"+(D.find("li").length+1)),y.val(""),t())):y.val(""),a.length&&(F=!1,a.click(),E.hide(),n.preventDefault());break;case 13:F=!1,a=E.find("li.active:first"),a.length&&(a.click(),E.hide()),(o.neverSubmit||a.length)&&n.preventDefault();break;case 27:o.preventPropagationOnEscape&&E.find(":visible").length&&n.stopPropagation();break;case 16:case 20:t(),E.hide()}i.setPlaceholderEnabled(y,d.isEmpty())}})}):void 0},add:function(){var t,l,n,i,s;for(n=arguments.length>=1?o.call(arguments,0):[],t=e(this),i=0,s=n.length;s>i;i++)l=n[i],t.trigger("addSelection",l)},remove:function(){var t,l,n,i,s;for(n=arguments.length>=1?o.call(arguments,0):[],t=e(this),i=0,s=n.length;s>i;i++)l=n[i],t.trigger("removeSelection",l)},defaults:function(t,l){null==l&&(l=!1),l&&(s={}),e.extend(s,t)}},e.fn.autoSuggest=function(e){return a[e]?a[e].apply(this,Array.prototype.slice.call(arguments,1)):a.init.apply(this,arguments)}}).call(this); \ No newline at end of file diff --git a/package.json b/package.json index 187ecc0..9af9262 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jquery.autoSuggest", "title": "jQuery AutoSuggest", "description": "An autosuggest and autocomplete plugin for jQuery. With CoffeeScript and SASS flavour.", - "version": "2.2.0", + "version": "2.3.0", "homepage": "http://hlsolutions.github.com/jquery-autosuggest", "author": { "name": "Jan Philipp", diff --git a/src/jquery.autoSuggest.coffee b/src/jquery.autoSuggest.coffee index edeca0d..2f69e51 100644 --- a/src/jquery.autoSuggest.coffee +++ b/src/jquery.autoSuggest.coffee @@ -920,6 +920,11 @@ pluginMethods = element.trigger 'removeSelection', value return + # plugin method to reset the defaults (optionally reset) + defaults : (options, replace = false) -> + defaults = {} if replace + $.extend defaults, options + return # Define the actual jQuery plugin constructor. $.fn.autoSuggest = (method) ->