diff --git a/dist/jquery.autoSuggest.css b/dist/jquery.autoSuggest.css index f693adc..aa64cc6 100644 --- a/dist/jquery.autoSuggest.css +++ b/dist/jquery.autoSuggest.css @@ -27,7 +27,7 @@ ul.as-selections li.as-selection-item { color: #2b3840; font-size: 13px; font-family: "Lucida Grande", arial, sans-serif; - text-shadow: 0 1px 1px white; + text-shadow: 0 1px 1px #fff; background-color: #d4eaf6; background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #d4eaf6), color-stop(100%, #bfe0f1)); background: -webkit-linear-gradient(top, #d4eaf6, #bfe0f1); @@ -62,7 +62,7 @@ ul.as-selections li.as-selection-item a.as-close { font-size: 14px; font-weight: bold; text-shadow: 0 1px 1px white; - -webkit-transition: color 0.1s ease-in; + -webkit-transition: color .1s ease-in; } ul.as-selections li.as-selection-item.blur { color: #666666; diff --git a/dist/jquery.autoSuggest.js b/dist/jquery.autoSuggest.js index 1bd734b..d0fcb06 100644 --- a/dist/jquery.autoSuggest.js +++ b/dist/jquery.autoSuggest.js @@ -1,4 +1,4 @@ -/*! jQuery AutoSuggest - v2.3.0 - 2013-03-29 +/*! jQuery AutoSuggest - v2.3.0 - 2013-08-05 * URL: http://hlsolutions.github.com/jquery-autosuggest * Copyright (c) 2013 Jan Philipp * Licensed MIT, GPL */ @@ -22,8 +22,7 @@ Based on the 1.6er release dated in July, 2012 $ = jQuery; - /* A collection of utility functions. - */ + /* A collection of utility functions.*/ Utils = (function() { @@ -43,7 +42,6 @@ Based on the 1.6er release dated in July, 2012 Utils.setPlaceholderEnabled = function(input, enable) { var from, targets, to; - targets = ['placeholder', 'disabled-placeholder']; if (enable) { from = targets[1]; @@ -65,8 +63,7 @@ Based on the 1.6er release dated in July, 2012 })(); - /* A collection of configuration resolvers. - */ + /* A collection of configuration resolvers.*/ ConfigResolver = (function() { @@ -82,7 +79,6 @@ Based on the 1.6er release dated in July, 2012 ConfigResolver.getExtraParams = function(options) { var obj, pair, parts, result, _i, _len, _ref; - result = options.extraParams; if ($.isFunction(result)) { result = result(this); @@ -114,8 +110,7 @@ Based on the 1.6er release dated in July, 2012 })(); - /* The SelectionControl maintains and manage any selections. - */ + /* The SelectionControl maintains and manage any selections.*/ SelectionHolder = (function() { @@ -132,7 +127,6 @@ Based on the 1.6er release dated in July, 2012 SelectionHolder.prototype.syncToHiddenField = function() { var item, value, _i, _len, _ref; - if (!this.hiddenField) { return; } @@ -188,7 +182,6 @@ Based on the 1.6er release dated in July, 2012 Events.onSelectionAdd = function(scope, containerElement, detachedElement, options, item, selections) { var element; - element = options.onSelectionAdd.call(scope, containerElement, detachedElement, options); Utils.setPlaceholderEnabled(scope, selections.length === 0); if ($.isFunction(options.afterSelectionAdd)) { @@ -514,7 +507,6 @@ Based on the 1.6er release dated in July, 2012 pluginMethods = { init: function(dataSource, options) { var ajaxRequest, fetcher; - options = $.extend({}, defaults, options); ajaxRequest = null; if (options.remoteFilter === 'auto') { @@ -527,10 +519,8 @@ Based on the 1.6er release dated in July, 2012 case 'string': return function(query, callback) { var ajaxRequestConfig, extraParams, onDone, params; - params = {}; - /* ensures query is encoded - */ + /* ensures query is encoded*/ params["" + options.queryParam] = encodeURIComponent(decodeURIComponent(query)); if (options.retrieveLimit) { @@ -572,7 +562,6 @@ Based on the 1.6er release dated in July, 2012 return this.each(function() { var abortRequest, addSelection, clonePublicApi, currentSelection, element, elementId, hiddenInputField, hiddenInputFieldId, hiddenInputFieldName, i, input, inputWrapper, input_focus, interval, item, keyChange, lastKeyPressCode, lastKeyWasTab, moveResultSelection, new_value, num_count, prev, processData, processRequest, publicApi, resultsContainer, resultsList, selectionsContainer, timeout, value, _i, _j, _len, _len1, _ref, _ref1; - options.inputAttrs = $.extend(options.inputAttrs, {}); input_focus = false; input = $(this); @@ -635,7 +624,6 @@ Based on the 1.6er release dated in July, 2012 publicApi = { add: function(data) { var counted, item; - counted = $(selectionsContainer).find('li').length; item = addSelection(data, "u" + counted); if (item != null) { @@ -667,7 +655,6 @@ Based on the 1.6er release dated in July, 2012 addSelection = function(data, num) { var closeElement, item; - currentSelection.add(data[options.selectedValuesProp]); item = $("
  • "); item.on({ @@ -759,7 +746,6 @@ Based on the 1.6er release dated in July, 2012 */ var string, _k, _results; - if (lastKeyPressCode === 46 || __indexOf.call((function() { _results = []; for (_k = 9; _k < 31; _k++){ _results.push(_k); } @@ -790,7 +776,6 @@ Based on the 1.6er release dated in July, 2012 }; processData = function(data, query) { var formatted, forward, matchCount, name, num, regex, resultsContainerVisible, str, text, workingData, _k, _l, _len2, _len3, _ref2; - if (!options.matchCase) { query = query.toLowerCase(); } @@ -825,7 +810,6 @@ Based on the 1.6er release dated in July, 2012 formatted.on({ click: function() { var number, raw_data; - element = $(this); raw_data = element.data('data'); number = raw_data.num; @@ -857,8 +841,7 @@ Based on the 1.6er release dated in July, 2012 workingData = $.extend({}, data[num]); query = query.replace(/"/g, '\\"'); regex = !options.matchCase ? new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + Utils.escapeHtml(query) + ")(?![^<>]*>)(?![^&;]+;)", "gi") : new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + Utils.escapeHtml(query) + ")(?![^<>]*>)(?![^&;]+;)", "g"); - /* When this is a string, escape the value and process a regular replacement for highlighting. - */ + /* When this is a string, escape the value and process a regular replacement for highlighting.*/ if (typeof workingData[options.selectedItemProp] === 'string') { workingData[options.selectedItemProp] = Utils.escapeHtml(workingData[options.selectedItemProp]); @@ -904,7 +887,6 @@ Based on the 1.6er release dated in July, 2012 }; moveResultSelection = function(direction) { var active, lis, start; - if (resultsContainer.find(':visible').length) { lis = resultsContainer.find('li'); switch (direction) { @@ -982,11 +964,9 @@ Based on the 1.6er release dated in July, 2012 Utils.setPlaceholderEnabled(element, currentSelection.isEmpty()); }, keydown: function(event) { - /* track the last key pressed - */ + /* track the last key pressed*/ var active, first_focus, i_input, n_data, _selection, _selections; - lastKeyPressCode = event.keyCode; first_focus = false; switch (event.keyCode) { @@ -1041,22 +1021,21 @@ Based on the 1.6er release dated in July, 2012 break; case 9: case 188: + case 13: active = resultsContainer.find('li.active:visible:first'); if (options.canGenerateNewSelections) { lastKeyWasTab = true; i_input = input.val().replace(/(,)/g, ''); - /* Generate a new bubble with text when no suggestion selected - */ + /* Generate a new bubble with text when no suggestion selected*/ - if (i_input !== '' && !currentSelection.exist(i_input) && i_input.length >= options.minChars && active.length === 0) { + if (i_input !== '' && !currentSelection.exist(i_input) && i_input.length >= options.minChars && active.length === 0 && (options.neverSubmit || event.keyCode !== 13)) { event.preventDefault(); n_data = {}; n_data["" + options.selectedItemProp] = i_input; n_data["" + options.selectedValuesProp] = i_input; addSelection(n_data, "00" + (selectionsContainer.find('li').length + 1)); input.val(''); - /* Cancel previous ajaxRequest when new tag is added - */ + /* Cancel previous ajaxRequest when new tag is added*/ abortRequest(); } @@ -1098,7 +1077,6 @@ Based on the 1.6er release dated in July, 2012 }, add: function() { var element, item, items, _i, _len; - items = 1 <= arguments.length ? __slice.call(arguments, 0) : []; element = $(this); for (_i = 0, _len = items.length; _i < _len; _i++) { @@ -1108,7 +1086,6 @@ Based on the 1.6er release dated in July, 2012 }, remove: function() { var element, value, values, _i, _len; - values = 1 <= arguments.length ? __slice.call(arguments, 0) : []; element = $(this); for (_i = 0, _len = values.length; _i < _len; _i++) { diff --git a/dist/jquery.autoSuggest.min.js b/dist/jquery.autoSuggest.min.js index f9c6857..db134ee 100644 --- a/dist/jquery.autoSuggest.min.js +++ b/dist/jquery.autoSuggest.min.js @@ -1,6 +1,6 @@ -/*! jQuery AutoSuggest - v2.3.0 - 2013-03-29 +/*! jQuery AutoSuggest - v2.3.0 - 2013-08-05 * 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)},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 +!function(){var a,b,c,d,e,f,g,h=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},i=[].slice;a=jQuery,e=function(){function b(){}return b.prototype._=void 0,b.escapeQuotes=function(a){return a?a.replace(/"/g,'\\"'):void 0},b.escapeHtml=function(b){return a("").text(b).html()},b.setPlaceholderEnabled=function(a,b){var c,d,e;d=["placeholder","disabled-placeholder"],b?(c=d[1],e=d[0]):(c=d[0],e=d[1]),!a.attr(e)&&a.attr(c)&&(a.attr(e,function(){return a.attr(c)}),a.removeAttr(c))},b}(),b=function(){function b(){}return b.prototype._=void 0,b.getExtraParams=function(b){var c,d,e,f,g,h,i;if(f=b.extraParams,a.isFunction(f)&&(f=f(this)),"string"===a.type(f)){for(c={},i=f.split("&"),g=0,h=i.length;h>g;g++)d=i[g],""!==d&&(e=d.split("=",2),e.length&&(c[e[0]]=e[1]));f=c}return f},b}(),d=function(){function b(a,b){this.hiddenField=a,this.items=null!=b?b:[]}return b.prototype._=void 0,b.prototype.hiddenField=null,b.prototype.items=null,b.prototype.syncToHiddenField=function(){var a,b,c,d,e;if(this.hiddenField){for(b="",e=this.items,c=0,d=e.length;d>c;c++)a=e[c],b+=","+a;b&&(b+=","),this.hiddenField.val(b||",")}},b.prototype.add=function(a){this.exist(a)||this.items.push(a),this.syncToHiddenField()},b.prototype.remove=function(b){this.items=a.grep(this.items,function(a){return a!==b}),this.syncToHiddenField()},b.prototype.isEmpty=function(){return 0===this.items.length},b.prototype.exist=function(b){return-1!==a.inArray(b,this.items)},b.prototype.getAll=function(){return this.items.slice(0)},b.prototype.clear=function(){this.items=[],this.syncToHiddenField()},b}(),c=function(){function b(){}return b.onSelectionAdd=function(b,c,d,f,g,h){var i;i=f.onSelectionAdd.call(b,c,d,f),e.setPlaceholderEnabled(b,0===h.length),a.isFunction(f.afterSelectionAdd)&&f.afterSelectionAdd.call(b,i,g,h)},b.onSelectionRemove=function(b,c,d,f,g){a.isFunction(d.onSelectionRemove)&&d.onSelectionRemove.call(b,c,d),e.setPlaceholderEnabled(b,0===g.length),a.isFunction(d.afterSelectionRemove)&&d.afterSelectionRemove.call(b,c,f,g)},b.onSelectionClick=function(b,c,d,f,g){a.isFunction(d.afterSelectionClick)&&d.afterSelectionClick.call(b,c,f,g),e.setPlaceholderEnabled(b,0===g.length)},b.onAjaxRequestDone=function(b,c,d){a.isFunction(d.onAjaxRequestDone)&&c.done(d.onAjaxRequestDone)},b.onAjaxRequestFail=function(b,c,d){a.isFunction(d.onAjaxRequestFail)&&c.fail(d.onAjaxRequestFail)},b.onAjaxRequestAlways=function(b,c,d){a.isFunction(d.onAjaxRequestAlways)&&c.always(d.onAjaxRequestAlways)},b}(),f={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(a,b){return a.before(b),a.prev()},onSelectionRemove:function(a,b){return b.fadeOut?a.fadeOut(b.fadeOut,function(){return a.remove()}):a.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},g={init:function(g,i){var j,k;return i=a.extend({},f,i),j=null,"auto"===i.remoteFilter&&(i.remoteFilter="string"===a.type(g)),(k=function(){switch(a.type(g)){case"function":return g;case"string":return function(d,e){var f,h,k,l;l={},l[""+i.queryParam]=encodeURIComponent(decodeURIComponent(d)),i.retrieveLimit&&(l[i.limitParam]=encodeURIComponent(i.retrieveLimit)),h=b.getExtraParams(i),"object"===a.type(h)&&a.extend(l,h),f=a.extend({},i.ajaxOptions,{url:g,data:l}),k=function(b){return a.isFunction(i.afterRequest)&&(b=i.afterRequest.apply(this,[b])),e(b,d)},j=a.ajax(f).done(k),c.onAjaxRequestDone(this,j,i),c.onAjaxRequestFail(this,j,i),c.onAjaxRequestAlways(this,j,i)};case"array":case"object":return function(a,b){return b(g,a)}}}())?this.each(function(){var b,f,g,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R;switch(i.inputAttrs=a.extend(i.inputAttrs,{}),u=!1,s=a(this),m=null,n=null,o=null,p=null,q=null,i.asHtmlID?(m=i.asHtmlID,n=m,p="as-values-"+m,i.useOriginalInputName?(q=s.attr("name"),s.attr({name:"old_"+s.attr("name")})):q="as_values_"+m):(m=""+(m||"")+Math.floor(100*Math.random()),n="as-input-"+m,p="as-values-"+m,i.useOriginalInputName?(q=s.attr("name"),s.attr({name:"old_"+s.attr("name")})):q="as_values_"+m),i.inputAttrs.id=n,i.usePlaceholder||(i.inputAttrs.placeholder=i.startText),s.attr(i.inputAttrs),s.addClass("as-input"),i.usePlaceholder||s.val(i.startText),s.wrap('').wrap('
  • '),J=a("#as-selections-"+m),t=a("#as-original-"+m),H=a('
    '),I=a(''),o=a(''),l=new d(o),v=null,K=null,D="",z=!1,y=null,C=0,G={add:function(b){var c,d;c=a(J).find("li").length,d=f(b,"u"+c),null!=d&&d.addClass("blur")},remove:function(a){l.remove(a),J.find('li[data-value="'+e.escapeHtml(a)+'"]').remove()}},g=function(){return{add:G.add,remove:G.remove}},s.bind("addSelection",function(a,b){return G.add(b)}),s.bind("removeSelection",function(a,b){return G.remove(b)}),f=function(b,d){var f,g;return l.add(b[i.selectedValuesProp]),g=a('
  • '),g.on({click:function(){m=a(this),c.onSelectionClick(s,m,i,b[i.selectedValuesProp],l.getAll()),J.children().removeClass("selected"),m.addClass("selected")},mousedown:function(){u=!1}}),f=a('×'),f.click(function(){return l.remove(b[i.selectedValuesProp]),c.onSelectionRemove(s,g,i,null,l.getAll()),u=!0,s.focus(),!1}),"string"!=typeof b[i.selectedItemProp]?c.onSelectionAdd(s,t,g.append(b[i.selectedItemProp]).prepend(f),i,b,l.getAll()):c.onSelectionAdd(s,t,g.text(b[i.selectedItemProp]).prepend(f),i,b,l.getAll()),t.prev()},a.isFunction(i.start)&&i.start.call(this,g()),a.type(i.preFill)){case"string":for(Q=i.preFill.split(","),M=0,O=Q.length;O>M;M++)L=Q[M],w={},w[""+i.selectedValuesProp]=L,""!==L&&f(w,"000"+r);break;case"array":if(i.preFill.length)for(a.isFunction(i.afterRequest)&&(i.preFill=i.afterRequest.call(this,i.preFill)),R=i.preFill,r=N=0,P=R.length;P>N;r=++N)w=R[r],B=w[i.selectedValuesProp],"undefined"==typeof B&&(B=""),""!==B&&f(w,"000"+r)}return l.isEmpty()||(s.val(""),J.find("li.as-selection-item").addClass("blur").removeClass("selected"),e.setPlaceholderEnabled(s,!1)),s.after(o),J.on({click:function(){u=!0,s.focus()},mousedown:function(){J.children().removeClass("selected"),u=!1}}),J.after(H),x=function(){var a,b,c;return 46===y||h.call(function(){for(c=[],b=9;31>b;b++)c.push(b);return c}.apply(this),y)>=0?(H.hide(),void 0):(a=s.val().replace(/[\\]+|[\/]+/g,""),""===a||a!==D?(D=a,a.length>=i.minChars||0===i.minChars&&0===a.length?(J.addClass("loading"),F(a)):(J.removeClass("loading"),H.hide())):void 0)},F=function(c){return a.isFunction(i.beforeRequest)&&(c=i.beforeRequest.apply(this,[c,i])),b(),k(c,E)},E=function(b,c){var d,g,h,j,k,n,o,p,q,r,t,v,x,y,A;for(i.matchCase||(c=c.toLowerCase()),c=c.replace("(","(","g").replace(")",")","g"),h=0,H.hide().html(I.html("")),k=0,t=0,x=b.length;x>t;t++){if(w=b[t],C++,g=!1,"value"===i.searchObjProps)p=w.value;else for(p="",A=i.searchObjProps.split(","),v=0,y=A.length;y>v;v++)j=A[v],p+=""+w[a.trim(j)]+" ";if(p&&(i.matchCase||(p=p.toLowerCase()),i.searchActive&&(-1===p.indexOf(c)&&!i.remoteFilter||l.exist(w[i.selectedValuesProp]))||(g=!0)),g&&(d=a('
  • '),d.on({click:function(){var c,d;m=a(this),d=m.data("data"),c=d.num,J.find("#as-selection-"+c).length<=0&&!z&&(b=d.attributes,s.val("").focus(),D="",f(b,c),a.isFunction(i.onResultItemClick)&&i.onResultItemClick.call(this,d),H.hide()),z=!1},mousedown:function(){u=!1},mouseover:function(){m=a(this),I.find("li").removeClass("active"),m.addClass("active")}}),d.data("data",{attributes:b[k],num:C}),r=a.extend({},b[k]),c=c.replace(/"/g,'\\"'),n=i.matchCase?new RegExp("(?![^&;]+;)(?!<[^<>]*)("+e.escapeHtml(c)+")(?![^<>]*>)(?![^&;]+;)","g"):new RegExp("(?![^&;]+;)(?!<[^<>]*)("+e.escapeHtml(c)+")(?![^<>]*>)(?![^&;]+;)","gi"),"string"==typeof r[i.selectedItemProp]?(r[i.selectedItemProp]=e.escapeHtml(r[i.selectedItemProp]),i.resultsHighlight&&c.length>0&&(r[i.selectedItemProp]=r[i.selectedItemProp].replace(n,"$1"))):i.resultsHighlight&&c.length>0&&r[i.selectedItemProp].html(r[i.selectedItemProp].html().replace(n,"$1")),d=i.formatList?i.formatList.call(this,r,d):d.html(r[i.selectedItemProp]),I.append(d),h++,i.retrieveLimit&&i.retrieveLimit===h))break;k+=1}J.removeClass("loading"),0>=h&&(q=i.emptyText,"regexp"===a.type(i.emptyTextPlaceholder)&&(q=q.replace(i.emptyTextPlaceholder,c)),I.html('
  • '+q+"
  • ")),I.css({width:J.outerWidth()}),o=h>0||i.showResultListWhenNoMatch,o&&H.show(),a.isFunction(i.afterResultListShow)&&i.afterResultListShow.call(this,o)},A=function(a){var b,c,d;if(H.find(":visible").length){switch(c=H.find("li"),a){case"down":d=c.eq(0);break;default:d=c.filter(":last")}if(b=H.find("li.active:first"),b.length)switch(a){case"down":d=b.next();break;default:d=b.prev()}c.removeClass("active"),d.addClass("active")}},b=function(){j&&(j.abort(),j=null)},s.on({focus:function(){return m=a(this),!i.usePlaceholder&&m.val()===i.startText&&l.isEmpty()?m.val(""):u&&(J.find("li.as-selections-item").removeClass("blur"),""!==m.val()&&(I.css({width:J.outerWidth()}),H.show())),v&&clearInterval(v),v=setInterval(function(){i.showResultList&&(i.selectionLimit&&J.find("li.as-selection-item").length>=i.selectionLimit?(I.html('
  • '+i.limitText+"
  • "),H.show()):x())},i.keyDelay),u=!0,0===i.minChars&&F(m.val()),!0},blur:function(){m=a(this),!i.usePlaceholder&&""===m.val()&&l.isEmpty()&&i.minChars>0?m.val(i.startText):u&&(J.find("li.as-selection-item").addClass("blur").removeClass("selected"),H.hide()),v&&clearInterval(v),e.setPlaceholderEnabled(m,l.isEmpty())},keydown:function(d){var g,h,j,k,m,n;switch(y=d.keyCode,h=!1,d.keyCode){case 38:d.preventDefault(),A("up");break;case 40:d.preventDefault(),a(":visible",H).length>0?A("down"):(K&&clearTimeout(K),K=setTimeout(function(){x()},i.keyDelay));break;case 8:""===s.val()&&(n=l.getAll(),m=null,m=n.length?n[n.length-1]:null,J.children().not(t.prev()).removeClass("selected"),t.prev().hasClass("selected")?(l.remove(m),c.onSelectionRemove(s,t.prev(),i,null,l.getAll())):(c.onSelectionClick(s,t.prev(),i,null,l.getAll()),t.prev().addClass("selected"))),1===s.val().length&&(H.hide(),D="",b()),H.find(":visible").length&&(K&&clearTimeout(K),K=setTimeout(function(){x()},i.keyDelay));break;case 9:case 188:case 13:g=H.find("li.active:visible:first"),i.canGenerateNewSelections?(z=!0,j=s.val().replace(/(,)/g,""),""!==j&&!l.exist(j)&&j.length>=i.minChars&&0===g.length&&(i.neverSubmit||13!==d.keyCode)&&(d.preventDefault(),k={},k[""+i.selectedItemProp]=j,k[""+i.selectedValuesProp]=j,f(k,"00"+(J.find("li").length+1)),s.val(""),b())):s.val(""),g.length&&(z=!1,g.click(),H.hide(),d.preventDefault());break;case 13:z=!1,g=H.find("li.active:first"),g.length&&(g.click(),H.hide()),(i.neverSubmit||g.length)&&d.preventDefault();break;case 27:i.preventPropagationOnEscape&&H.find(":visible").length&&d.stopPropagation();break;case 16:case 20:b(),H.hide()}e.setPlaceholderEnabled(s,l.isEmpty())}})}):void 0},add:function(){var b,c,d,e,f;for(d=1<=arguments.length?i.call(arguments,0):[],b=a(this),e=0,f=d.length;f>e;e++)c=d[e],b.trigger("addSelection",c)},remove:function(){var b,c,d,e,f;for(d=1<=arguments.length?i.call(arguments,0):[],b=a(this),e=0,f=d.length;f>e;e++)c=d[e],b.trigger("removeSelection",c)},defaults:function(b,c){null==c&&(c=!1),c&&(f={}),a.extend(f,b)}},a.fn.autoSuggest=function(a){return g[a]?g[a].apply(this,Array.prototype.slice.call(arguments,1)):g.init.apply(this,arguments)}}.call(this); \ No newline at end of file diff --git a/src/jquery.autoSuggest.coffee b/src/jquery.autoSuggest.coffee index 2f69e51..3154736 100644 --- a/src/jquery.autoSuggest.coffee +++ b/src/jquery.autoSuggest.coffee @@ -865,14 +865,14 @@ pluginMethods = keyChange() return ), options.keyDelay - when 9, 188 # tab, comma + when 9, 188, 13 # tab, comma, return active = resultsContainer.find('li.active:visible:first') if options.canGenerateNewSelections lastKeyWasTab = true # remove all comma i_input = input.val().replace /(,)/g, '' ### Generate a new bubble with text when no suggestion selected ### - if i_input isnt '' && !currentSelection.exist(i_input) && i_input.length >= options.minChars && active.length is 0 + if i_input isnt '' && !currentSelection.exist(i_input) && i_input.length >= options.minChars && active.length is 0 && (options.neverSubmit || event.keyCode != 13) event.preventDefault() n_data = {} n_data["#{options.selectedItemProp}"] = i_input diff --git a/test/jquery.autoSuggest_test.js b/test/jquery.autoSuggest_test.js index d54352c..8d9f2d3 100644 --- a/test/jquery.autoSuggest_test.js +++ b/test/jquery.autoSuggest_test.js @@ -115,7 +115,7 @@ test('Type "Yao Ming" and select it by COMMA', 3, function () { el = create(); - // Type "Yap Ming" and "," + // Type "Yao Ming" and "," el.focus(); el.val("Yao Ming"); el.simulate("keydown", {"keyCode" : keyCode.COMMA}); @@ -129,7 +129,7 @@ test('Type "Yao Ming" and select it by TAB', 3, function () { el = create(); - // Type "Yap Ming" and "\t" + // Type "Yao Ming" and "\t" el.focus(); el.val("Yao Ming"); el.simulate("keydown", {"keyCode" : keyCode.TAB}); @@ -143,7 +143,7 @@ asyncTest('Select two values and remove both of them.', 5, function () { el = create(); - // Type "Yap Ming" and "," + // Type "Yao Ming" and "," el.focus(); el.val("Yao Ming"); el.simulate("keydown", {"keyCode" : keyCode.COMMA}); @@ -361,7 +361,7 @@ test('Type "Yao Ming" but press than ESC. No value should be selected.', 1, function () { el = create(); - // Type "Yap Ming" and "," + // Type "Yao Ming" and "," el.focus(); el.val("Yao Ming"); el.simulate("keydown", {"keyCode" : keyCode.ESC}); @@ -479,6 +479,28 @@ equal(selections().length, 0, "Should remove using a callback."); }); + module('Configuration: "options.neverSubmit"', { + teardown : function () { + remove(); + } + }); + + test('Type "Yao Ming" and select it by ENTER', 3, function () { + el = create(null, $.extend({}, options, { + neverSubmit : true + })); + // Type "Yao Ming" and "\t" + el.focus(); + el.val("Yao Ming"); + el.simulate("keydown", {"keyCode" : keyCode.ENTER}); + + sel = selections(); + equal(sel.length, 1, "Should have one name"); + equal($(sel[0]).text(), "×Yao Ming", "Should be Yao Ming"); + equal(value().val(), ",Yao Ming,", "Should be Yao Ming"); + remove(); + }); + module('Configuration: "onAjaxRequestAlways"', { teardown : function () { remove();