<NME> jquery.flexdatalist.min.js
<BEF> jQuery.fn.flexdatalist=function(e,t){"use strict";var i=function(e){e.each(function(){var e=$(this),t=e.data("flexdatalist");e.removeClass("flexdatalist-set").attr("type","text").val(t&&t.originalValue?t.originalValue:"").removeData("flexdatalist").next(".flexdatalist-alias, ul.flexdatalist-multiple").remove()})};if("string"==typeof e&&"reset"!==e){var a=this[0];if("destroy"===e)i($(this));else if("value"===e){if("undefined"==typeof t)return a.fvalue.get();a.fvalue.set(t)}else if("add"===e){if("undefined"==typeof t)return a.debug("Missing value to add!");a.fvalue.add(t)}else if("toggle"===e){if("undefined"==typeof t)return a.debug("Missing value to toggle!");a.fvalue.toggle(t)}else if("remove"===e){if("undefined"==typeof t)return a.debug("Missing value to remove!");a.fvalue.remove(t)}else if("disabled"===e){if("undefined"==typeof t)return a.fdisabled();a.fdisabled(t)}else if("string"==typeof e){if("undefined"==typeof t)return a.options.get(e);a.options.set(e,t)}return this}this&&"undefined"!=typeof this[0]&&"undefined"!=typeof this[0].fvalue&&i($(this));var r=$.extend({url:null,data:[],params:{},relatives:null,chainedRelatives:!1,cache:!0,cacheLifetime:60,minLength:2,groupBy:!1,selectionRequired:!1,focusFirstResult:!1,textProperty:null,valueProperty:null,visibleProperties:[],iconProperty:"thumb",searchIn:["label"],searchContain:!1,searchEqual:!1,searchByWord:!1,searchDisabled:!1,searchDelay:300,normalizeString:null,multiple:null,disabled:null,maxShownResults:100,removeOnBackspace:!0,noResultsText:'No results found for "{keyword}"',toggleSelected:!1,allowDuplicateValues:!1,requestType:"get",requestContentType:"x-www-form-urlencoded",resultsProperty:"results",keywordParamName:"keyword",limitOfValues:0,valuesSeparator:",",debug:!0},e);return this.each(function(e){var t=$(this),i=this,a=null,n=[],s="flex"+e,l=null,o=null;this.init=function(){var e=this.options.init();this.set.up(),this.fvalue.clear()._load(e.originalValue,function(){i.fdisabled(e.disabled)}),l.on("focusin",function(e){i.action.redoSearchFocus(e),i.action.showAllResults(e),o&&o.addClass("focus")}).on("input keydown",function(e){9===i.keyNum(e)&&i.results.remove(),i.action.keypressValue(e,188),i.action.backSpaceKeyRemove(e)}).on("input keyup",function(e){i.action.keypressValue(e,13),i.action.keypressSearch(e),i.action.copyValue(e),i.action.backSpaceKeyRemove(e)}).on("focusout",function(){o&&o.removeClass("focus")}),window.onresize=function(){i.position()}},this.action={keypressValue:function(e,t){var a=i.keyNum(e),r=l[0].value,n=i.options.get();if(r.length>0&&a===t&&!n.selectionRequired&&n.multiple){var r=l[0].value;e.preventDefault(),i.fvalue.extract(r),i.results.remove()}},keypressSearch:function(e){var t=i.keyNum(e),r=l.val(),n=r.length,s=i.options.get();clearTimeout(a),(0!==s.minLength||0!==n||9!==t&&16!==t)&&(0===n&&s.minLength>0||n<s.minLength?i.results.remove():(!t||13!==t&&(37>t||t>40))&&(i.results.remove(),a=setTimeout(function(){0===s.minLength&&n>0||s.minLength>0&&n>=s.minLength?i.data.load(function(e){i.search.get(r,e,function(e){i.results.show(e)})}):i.results.remove()},s.searchDelay)))},redoSearchFocus:function(e){var t=i.fvalue.get(),a=i.options.get(),r=l.val();(r.length>0&&a.multiple||r.length>0&&0===t.length)&&this.keypressSearch(e)},copyValue:function(e){if(13!==i.keyNum(e)){var t=l.val(),a=i.options.get();a.multiple||(a.selectionRequired?i.fvalue.clear():(i.fvalue.extract(t),0===t.length&&(n=[])))}},backSpaceKeyRemove:function(e){var t=i.options.get();if(t.removeOnBackspace){var a=l.val(),r=l.data("_remove");r?(i.fvalue.remove(r),l.data("_remove",null)):0===a.length&&t.multiple&&8===i.keyNum(e)&&l.data("_remove",l.parents("li:eq(0)").prev())}},showAllResults:function(){var e=l.val();e=$.trim(e),""===e&&0===i.options.get("minLength")&&i.data.load(function(e){i.results.show(e)})}},this.set={up:function(){l=this.alias(),i.options.get("multiple")?o=this.multipleInput(l):l.insertAfter(t),l.attr("autofocus")&&l.focus(),this.chained()},alias:function(){var e=t.clone(!1).attr({list:null,name:t.attr("name")?"flexdatalist-"+t.attr("name"):null,id:t.attr("id")?t.attr("id")+"-flexdatalist":null,value:""}).addClass("flexdatalist-alias").removeClass("flexdatalist").attr("autocomplete","off");return t.addClass("flexdatalist flexdatalist-set").prop("type","hidden"),e},multipleInput:function(e){return o=$('<ul tabindex="1">').addClass("flexdatalist-multiple").css({"border-color":t.css("border-left-color"),"border-width":t.css("border-left-width"),"border-style":t.css("border-left-style"),"border-radius":t.css("border-top-left-radius"),"background-color":t.css("background-color")}).insertAfter(t).click(function(){$(this).find("input").focus()}),$('<li class="input-container">').addClass("flexdatalist-multiple-value").append(e).appendTo(o),o},chained:function(){var e=i.options.get();if(e.relatives&&e.chainedRelatives){var a=function(){e.relatives.each(function(){var e=i.isEmpty($(this).val()),a=i.isEmpty(t[0].value);!e&&a||i.fvalue.clear(),i.fdisabled(e)})};e.relatives.on("change",function(){a()}),a(!0)}}},this.fvalue={get:function(e){var t=i.value,a=i.options.get();return!a.multiple&&!this.isJSON()||e?t:this.toObj(t)},set:function(e,a){return i.fdisabled()||(a||this.clear(!0),this._load(e)),t},add:function(e){return i.options.get("multiple")&&this.set(e,!0),this},toggle:function(e){return i.fdisabled()||this.multiple.toggle(e),this},remove:function(e){if(!i.fdisabled()){e=this.toObj(e),t.trigger("before:flexdatalist.remove",[e]);var a=[];if($.isArray(e))$.each(e,function(e,t){var r=i.fvalue.multiple.remove(t);r&&a.push(r)});else{var r=this.multiple.remove(e);r&&a.push(r)}t.trigger("after:flexdatalist.remove",[e,a]).trigger("change:flexdatalist",[a,i.options.get()]).trigger("change")}return this},_load:function(e,t){var a=i.options.get(),r=this.toStr(e),n=this.get(!0);if(t=t?t:$.noop,0==r.length&&0==n.length)return void t(e);if(e=this.toObj(e),!i.isEmpty(e)&&"string"==typeof a.valueProperty&&!this.isJSON(e)){var s=a.searchIn,l=a.searchEqual;return a.searchIn=a.valueProperty.split(","),a.searchEqual=!0,void i.data.load(function(r){i.search.get(e,r,function(r){i.isEmpty(r)||i.fvalue.extract(r),t(e),a.searchIn=s,a.searchEqual=l})},e)}i.fvalue.extract(e),t(e)},extract:function(e){var a=i.options.get(),r=[];t.trigger("before:flexdatalist.value",[e,a]),$.isArray(e)?$.each(e,function(e,t){r.push(i.fvalue._extract(t))}):r=i.fvalue._extract(e),t.trigger("after:flexdatalist.value",[r,a]).trigger("change:flexdatalist",[r,a]).trigger("change")},_extract:function(e){var a=this.text(e),r=this.value(e),s=(t[0].value,i.options.get());if(s.multiple){if(!i.isEmpty(a)){if(i.isDup(a))return;n.push(a),this.multiple.add(r,a)}}else this.single(r,a);return{value:r,text:a}},single:function(e,i){i&&i!==l.val()&&l.val(i,!0),t[0].value=e},multiple:{add:function(e,t){var a=this,r=this.li(e,t);i.options.get();r.click(function(){a.toggle($(this))}).find(".fdl-remove").click(function(){a.remove($(this).parent())}),this.push(e),l.val("",!0),this.checkLimit()},push:function(e){var a=i.fvalue.get();e=i.fvalue.toObj(e),a.push(e),e=i.fvalue.toStr(a),t[0].value=e},toggle:function(e){var a=i.options.get();if(a.toggleSelected){var r=this.findLi(e);if(r){var n=r.index(),s=r.data(),l=r.hasClass("disabled")?"enable":"disable",o=i.fvalue.get(),u=[{value:s.value,text:s.text,action:l},a];if(t.trigger("before:flexdatalist.toggle",u),"enable"===l){var f=r.data("value");o.splice(n,0,f),r.removeClass("disabled")}else o.splice(n,1),r.addClass("disabled");o=i.fvalue.toStr(o),i.value=o,t.trigger("after:flexdatalist.toggle",u).trigger("change:flexdatalist",u).trigger("change")}}},remove:function(e){var a=this.findLi(e);if(a){var r=i.fvalue.get(),s=a.index(),l=a.data(),o=(i.options.get(),{value:l.value,text:l.text});return r.splice(s,1),r=i.fvalue.toStr(r),t[0].value=r,a.remove(),i.fvalue.multiple.checkLimit(),n.splice(s,1),o}},removeAll:function(){var e=i.fvalue.get(),a=i.options.get();t.trigger("before:flexdatalist.remove.all",[e,a]),o.find("li:not(.input-container)").remove(),t[0].value="",n=[],t.trigger("after:flexdatalist.remove.all",[e,a])},li:function(e,t){var a=o.find("li.input-container");return $("<li>").addClass("value"+(i.options.get("toggleSelected")?" toggle":"")).append('<span class="text">'+t+"</span>").append('<span class="fdl-remove">&times;</span>').data({text:t,value:e}).insertBefore(a)},checkLimit:function(){var e=i.options.get("limitOfValues");if(e>0){var t=o.find("li.input-container"),a=n.length;e==a?t.hide():t.show()}},findLi:function(e){if(e instanceof jQuery)0===e.length&&(e=null);else{var t=e;e=null,o.find("li:not(.input-container)").each(function(){var i=$(this);return i.data("value")===t?(e=i,!1):void 0})}return e},isEmpty:function(){return this.get().length>0}},value:function(e){var t=e,a=i.options.get();return i.isObject(e)&&(this.isJSON()||this.isMixed()?(delete e.name_highlight,t=this.toStr(e)):t=i.isDefined(e,a.valueProperty)?e[a.valueProperty]:i.isDefined(e,a.searchIn[0])?e[a.searchIn[0]]:null),t},text:function(e){var t=e,a=i.options.get();return i.isObject(e)&&(t=e[a.searchIn[0]],t=i.isDefined(e,a.textProperty)?e[a.textProperty]:this.placeholders.replace(e,a.textProperty,t)),$("<div>").html(t).text()},placeholders:{replace:function(e,t,a){if(i.isObject(e)&&"string"==typeof t){var r=this.parse(t);if(!i.isEmpty(e)&&r)return $.each(r,function(a,r){i.isDefined(e,r)&&(t=t.replace(a,e[r]))}),t}return a},parse:function(e){var t=e.match(/\{.+?\}/g);if(t){var i={};return t.map(function(e){i[e]=e.slice(1,-1)}),i}return!1}},clear:function(e){var a=t[0].value,r=i.options.get();return r.multiple&&this.multiple.removeAll(),t[0].value="",""!==a&&t.trigger("change:flexdatalist",[null,r]).trigger("change"),e&&l.val("",!0),n=[],this},toObj:function(e){if("object"!=typeof e){var t=i.options.get();i.isEmpty(e)||!i.isDefined(e)?e=t.multiple?[]:this.isJSON()?{}:"":this.isCSV()?(e=e.toString().split(t.valuesSeparator),e=$.map(e,function(e){return $.trim(e)})):(this.isJSON()||this.isMixed()&&this.isJSON(e))&&(e=JSON.parse(e))}return e},toStr:function(e){return"string"!=typeof e&&(i.isEmpty(e)||!i.isDefined(e)?e="":"number"==typeof e?e=e.toString():this.isCSV()?e=e.join(i.options.get("valuesSeparator")):(this.isJSON()||this.isMixed())&&(e=JSON.stringify(e))),$.trim(e)},isJSON:function(e){if("undefined"!=typeof e){if(i.isObject(e))e=JSON.stringify(e);else if("string"!=typeof e)return!1;return 0===e.indexOf("{")||0===e.indexOf("[{")}var t=i.options.get(),a=t.valueProperty;return t.selectionRequired&&(i.isObject(a)||"*"===a)},isMixed:function(){var e=i.options.get();return!e.selectionRequired&&"*"===e.valueProperty},isCSV:function(){return!this.isJSON()&&i.options.get("multiple")}},this.data={load:function(e,i){var a=this,r=[];t.trigger("before:flexdatalist.data"),this.url(function(i){r=r.concat(i),a["static"](function(i){r=r.concat(i),a.datalist(function(i){r=r.concat(i),t.trigger("after:flexdatalist.data",[r]),e(r)})})},i)},"static":function(e){var t=i.options.get();if("string"==typeof t.data){var a=t.data,r=i.cache.read(a,!0);if(r)return void e(r);this.remote({url:a,success:function(r){t.data=r,e(r),i.cache.write(a,r,t.cacheLifetime,!0)}})}else"object"!=typeof t.data&&(t.data=[]),e(t.data)},datalist:function(e){var a=t.attr("list"),r=[];i.isEmpty(a)||$("#"+a).find("option").each(function(){var e=$(this),t=e.val(),i=e.text();r.push({label:i.length>0?i:t,value:t})}),e(r)},url:function(e,t){var a=l.val(),r=i.options.get(),n=r.keywordParamName,s=i.fvalue.get(),o=this.relativesData();if(i.isEmpty(r.url))return e([]);var u={};"post"===r.requestType&&($.each(r,function(e,t){0!=e.indexOf("_")&&"data"!=e&&(u[e]=t)}),delete u.relatives);var f=i.cache.keyGen({relative:o,load:t,keyword:a,contain:r.searchContain},r.url),c=i.cache.read(f);if(c)return void e(c);var d=$.extend(o,r.params,{load:t,contain:r.searchContain,selected:s,original:r.originalValue,options:u});d[n]=a,this.remote({url:r.url,data:d,success:function(t){var n=l.val();n.length>=a.length&&e(t),i.cache.write(f,t,r.cacheLifetime)}})},remote:function(e){var a=this,r=i.options.get();t.hasClass("flexdatalist-loading")||(t.addClass("flexdatalist-loading"),"json"===r.requestContentType&&(e.data=JSON.stringify(data)),$.ajax($.extend({type:r.requestType,dataType:"json",contentType:"application/"+r.requestContentType+"; charset=UTF-8",complete:function(){t.removeClass("flexdatalist-loading")}},e,{success:function(t){t=a.extractRemoteData(t),e.success(t)}})))},extractRemoteData:function(e){var t=i.options.get(),a=i.isDefined(e,t.resultsProperty)?e[t.resultsProperty]:e;return"string"==typeof a&&0===a.indexOf("[{")&&(a=JSON.parse(a)),a&&a.options&&i.options.set($.extend({},t,a.options)),i.isObject(a)?a:[]},relativesData:function(){var e=i.options.get("relatives"),t={};return e&&(t.relatives={},e.each(function(){var e=$(this),i=e.attr("name").split("][").join("-").split("]").join("-").split("[").join("-").replace(/^\|+|\-+$/g,"");t.relatives[i]=e.val()})),t}},this.search={get:function(e,a,r){var n=this,s=i.options.get();if(s.searchDisabled)return void i.debug("Search is disabled!");var l=i.cache.read(e);if(!l){if(t.trigger("before:flexdatalist.search",[e,a]),!i.isEmpty(e)){l=[];for(var o=n.split(e),u=0;u<a.length;u++){var f=a[u];i.isDup(f)||(f=n.matches(f,o),f&&l.push(f))}}i.cache.write(e,l,2),t.trigger("after:flexdatalist.search",[e,a,l])}r(l)},matches:function(e,t){var a=$.extend({},e),r=[],n=i.options.get(),s=n.searchIn;if(t.length>0)for(var l=0;l<s.length;l++){var o=s[l];if(i.isDefined(e,o)&&e[o]){for(var u=e[o].toString(),f=u,c=this.split(u),d=0;d<t.length;d++){var h=t[d];this.find(h,c)&&(r.push(h),f=this.highlight(h,f))}f!==u&&(a[o+"_highlight"]=this.highlight(f))}}return 0===r.length||n.searchByWord&&r.length<t.length-1?!1:a},highlight:function(e,t){return t?t.replace(new RegExp(e,i.options.get("searchContain")?"ig":"i"),"|:|$&|::|"):(e=e.split("|:|").join('<span class="highlight">'),e.split("|::|").join("</span>"))},find:function(e,t){for(var a=i.options.get(),r=0;r<t.length;r++){var n=t[r];if(n=this.normalizeString(n),e=this.normalizeString(e),a.searchEqual)return n==e;if(a.searchContain?n.indexOf(e)>=0:0===n.indexOf(e))return!0}return!1},split:function(e){if("string"==typeof e&&(e=[$.trim(e)]),i.options.get("searchByWord"))for(var t=0;t<e.length;t++){var a=$.trim(e[t]);if(a.indexOf(" ")>0){var r=a.split(" ");$.merge(e,r)}}return e},normalizeString:function(e){if("string"==typeof e){var t=i.options.get("normalizeString");return"function"==typeof t&&(e=t(e)),e.toUpperCase()}return e}},this.results={show:function(e){var a=this,r=i.options.get();if(this.remove(!0),e){if(0===e.length)return void this.empty(r.noResultsText);var n=this.container();r.groupBy?(e=this.group(e),Object.keys(e).forEach(function(t){var s=e[t],l=r.groupBy,o=i.results.highlight(s[0],l,t);$("<li>").addClass("group").append($("<span>").addClass("group-name").html(o)).append($("<span>").addClass("group-item-count").text(" "+s.length)).appendTo(n);a.items(s,n)})):this.items(e,n);var s=n.find("li:not(.group)");s.on("click",function(){var e=$(this).data("item");e&&(i.fvalue.extract(e),a.remove(),t.trigger("select:flexdatalist",[e,r]))}).hover(function(){s.removeClass("active"),$(this).addClass("active")},function(){$(this).removeClass("active")}),r.focusFirstResult&&s.filter(":first").addClass("active")}},empty:function(e){if(!i.isEmpty(e)){var t=this.container(),a=l.val();e=e.split("{keyword}").join(a),$("<li>").addClass("item no-results").append(e).appendTo(t)}},items:function(e,a){var r=i.options.get("maxShownResults");t.trigger("show:flexdatalist.results",[e]);for(var n=0;n<e.length&&!(r>0&&r===n);n++)this.item(e[n]).appendTo(a);t.trigger("shown:flexdatalist.results",[e])},item:function(e){for(var t=$("<li>").data("item",e).addClass("item"),a=i.options.get(),r=a.visibleProperties,n=0;n<r.length;n++){var s=r[n];if((!a.groupBy||a.groupBy!==s)&&i.isDefined(e,s)){var l={};if(s===a.iconProperty)l=$("<img>").addClass("item item-"+s).attr("src",e[s]);else{var o=i.results.highlight(e,s);l=$("<span>").addClass("item item-"+s).html(o+" ")}l.appendTo(t)}}return t},container:function(){var e=t;o&&(e=o);var a=$("ul.flexdatalist-results");return 0===a.length&&(a=$("<ul>").addClass("flexdatalist-results").appendTo("body").css({"border-color":e.css("border-left-color"),"border-width":"1px","border-bottom-left-radius":e.css("border-bottom-left-radius"),"border-bottom-right-radius":e.css("border-bottom-right-radius")}).data({target:l,input:t}),i.position(l)),a},group:function(e){for(var t=[],a=i.options.get("groupBy"),r=0;r<e.length;r++){var n=e[r];if(i.isDefined(n,a)){var s=n[a];i.isDefined(t,s)||(t[s]=[]),t[s].push(n)}}return t},highlight:function(e,t,a){return i.isDefined(e,t+"_highlight")?e[t+"_highlight"]:i.isDefined(e,t)?e[t]:a},remove:function(e){var i="ul.flexdatalist-results";e&&(i="ul.flexdatalist-results li"),$(i).remove(),t.trigger("removed:flexdatalist.results")}},this.cache={write:function(e,t,a,r){if(i.cache.isSupported()){e=this.keyGen(e,void 0,r);var n={value:t,timestamp:i.unixtime(),lifetime:a?a:!1};localStorage.setItem(e,JSON.stringify(n))}},read:function(e,t){if(i.cache.isSupported()){e=this.keyGen(e,void 0,t);var a=localStorage.getItem(e);if(a){var r=JSON.parse(a);if(r.lifetime){var n=i.unixtime()-r.timestamp;return r.lifetime>n?r.value:(i.cache["delete"](e),null)}return r.value}}return null},"delete":function(e,t){i.cache.isSupported()&&(e=this.keyGen(e,void 0,t),localStorage.removeItem(e))},clear:function(){if(i.cache.isSupported()){for(var e in localStorage)(e.indexOf(s)>-1||e.indexOf("global")>-1)&&i.cache["delete"](e);localStorage.clear()}},isSupported:function(){if(i.options.get("cache"))try{return"localStorage"in window&&null!==window.localStorage}catch(e){return!1}return!1},keyGen:function(e,t,i){"object"==typeof e&&(e=JSON.stringify(e));var a,r,n=void 0===t?2166136261:t;for(a=0,r=e.length;r>a;a++)n^=e.charCodeAt(a),n+=(n<<1)+(n<<4)+(n<<7)+(n<<8)+(n<<24);return(i?"global":s)+("0000000"+(n>>>0).toString(16)).substr(-8)}},this.options={init:function(){var e=$.extend({},r,t.data(),{multiple:null===r.multiple?t.is("[multiple]"):r.multiple,disabled:null===r.disabled?t.is("[disabled]"):r.disabled,originalValue:t[0].value});return this.set(e),e},get:function(e){var a=t.data("flexdatalist");return e?i.isDefined(a,e)?a[e]:null:a},set:function(e,a){var r=this.get();return i.isDefined(r,e)&&i.isDefined(a)?r[e]=a:i.isObject(e)&&(r=this._normalize(e)),t.data("flexdatalist",r),t},_normalize:function(e){if(e.searchIn=i.csvToArray(e.searchIn),e.relatives=e.relatives&&$(e.relatives).length>0?$(e.relatives):null,e.textProperty=null===e.textProperty?e.searchIn[0]:e.textProperty,e.visibleProperties=i.csvToArray(e.visibleProperties,e.searchIn),"*"===e.valueProperty&&e.multiple&&!e.selectionRequired)throw new Error("Selection must be required for multiple, JSON fields!");return e}},this.position=function(){var e=l;o&&(e=o),$("ul.flexdatalist-results").css({width:e.outerWidth()+"px",top:e.offset().top+e.outerHeight()+"px",left:e.offset().left+"px"})},this.fdisabled=function(e){if(this.isDefined(e)){if(t.prop("disabled",e),l.prop("disabled",e),o){o.css("background-color",t.css("background-color"));var i=o.find("li .fdl-remove"),a=o.find("li.input-container");e?(o.addClass("disabled"),a.hide(),i.hide()):(o.removeClass("disabled"),a.show(),i.show())}this.options.set("disabled",e)}return this.options.get("disabled")},this.isDup=function(e){return this.options.get("allowDuplicateValues")?!1:n.length>0&&n.indexOf(this.fvalue.text(e))>-1},this.keyNum=function(e){return e.which||e.keyCode},this.isEmpty=function(e){return i.isDefined(e)?null===e?!0:e===!0?!1:0===this.length(e)?!0:""===$.trim(e):!0},this.isObject=function(e){return e&&"object"==typeof e},this.length=function(e){return this.isObject(e)?Object.keys(e).length:"number"==typeof e||"number"==typeof e.length?e.toString().length:0},this.isDefined=function(e,t){var i="undefined"!=typeof e;return i&&"undefined"!=typeof t?"undefined"!=typeof e[t]:i},this.unixtime=function(e){var t=new Date;return e&&(t=new Date(e)),Math.round(t.getTime()/1e3)},this.csvToArray=function(e,t){return 0===e.length?t:"string"==typeof e?e.split(i.options.get("valuesSeparator")):e},this.debug=function(e,a){var r=i.options.get();r.debug&&(a||(a={}),e="Flexdatalist: "+e,console.warn(e),console.log($.extend({inputName:t.attr("name"),options:r},a)),console.log("--- /flexdatalist ---"))},this.init()})},jQuery(function(e){var t=e(document);t.data("flexdatalist")||e(document).mouseup(function(t){var i=e(".flexdatalist-results"),a=i.data("target");a&&a.is(":focus")||i.is(t.target)||0!==i.has(t.target).length||i.remove()}).keydown(function(t){var i=e(".flexdatalist-results"),a=i.find("li"),r=a.filter(".active"),n=r.index(),s=a.length,l=t.which||t.keyCode;if(0!==s){if(27===l)return void i.remove();if(13===l)t.preventDefault(),r.click();else if(40===l||38===l){t.preventDefault(),40===l?r=s>n&&r.nextAll(".item").first().length>0?r.removeClass("active").nextAll(".item").first().addClass("active"):a.removeClass("active").filter(".item:first").addClass("active"):38===l&&(r=n>0&&r.prevAll(".item").first().length>0?r.removeClass("active").prevAll(".item").first().addClass("active"):a.removeClass("active").filter(".item:last").addClass("active"));var o=(0===r.prev().length?r:r.prev()).position().top;i.animate({scrollTop:o+i.scrollTop()},100)}}}).data("flexdatalist",!0),jQuery("input.flexdatalist:not(.flexdatalist-set)").flexdatalist()}),function(e){var t=e.fn.val;e.fn.val=function(i){var a=e(this).hasClass("flexdatalist");return"undefined"==typeof i?a?this[0].fvalue.get(!0):t.call(this):a?this[0].fvalue.set(i):t.call(this,i)}}(jQuery);
//# sourceMappingURL=jquery.flexdatalist.min.js.map
<MSG> redo search on focus as an option

<DFF> @@ -1,1 +1,1 @@
-jQuery.fn.flexdatalist=function(e,t){"use strict";var i=function(e){e.each(function(){var e=$(this),t=e.data("flexdatalist");e.removeClass("flexdatalist-set").attr("type","text").val(t&&t.originalValue?t.originalValue:"").removeData("flexdatalist").next(".flexdatalist-alias, ul.flexdatalist-multiple").remove()})};if("string"==typeof e&&"reset"!==e){var a=this[0];if("destroy"===e)i($(this));else if("value"===e){if("undefined"==typeof t)return a.fvalue.get();a.fvalue.set(t)}else if("add"===e){if("undefined"==typeof t)return a.debug("Missing value to add!");a.fvalue.add(t)}else if("toggle"===e){if("undefined"==typeof t)return a.debug("Missing value to toggle!");a.fvalue.toggle(t)}else if("remove"===e){if("undefined"==typeof t)return a.debug("Missing value to remove!");a.fvalue.remove(t)}else if("disabled"===e){if("undefined"==typeof t)return a.fdisabled();a.fdisabled(t)}else if("string"==typeof e){if("undefined"==typeof t)return a.options.get(e);a.options.set(e,t)}return this}this&&"undefined"!=typeof this[0]&&"undefined"!=typeof this[0].fvalue&&i($(this));var r=$.extend({url:null,data:[],params:{},relatives:null,chainedRelatives:!1,cache:!0,cacheLifetime:60,minLength:2,groupBy:!1,selectionRequired:!1,focusFirstResult:!1,textProperty:null,valueProperty:null,visibleProperties:[],iconProperty:"thumb",searchIn:["label"],searchContain:!1,searchEqual:!1,searchByWord:!1,searchDisabled:!1,searchDelay:300,normalizeString:null,multiple:null,disabled:null,maxShownResults:100,removeOnBackspace:!0,noResultsText:'No results found for "{keyword}"',toggleSelected:!1,allowDuplicateValues:!1,requestType:"get",requestContentType:"x-www-form-urlencoded",resultsProperty:"results",keywordParamName:"keyword",limitOfValues:0,valuesSeparator:",",debug:!0},e);return this.each(function(e){var t=$(this),i=this,a=null,n=[],s="flex"+e,l=null,o=null;this.init=function(){var e=this.options.init();this.set.up(),this.fvalue.clear()._load(e.originalValue,function(){i.fdisabled(e.disabled)}),l.on("focusin",function(e){i.action.redoSearchFocus(e),i.action.showAllResults(e),o&&o.addClass("focus")}).on("input keydown",function(e){9===i.keyNum(e)&&i.results.remove(),i.action.keypressValue(e,188),i.action.backSpaceKeyRemove(e)}).on("input keyup",function(e){i.action.keypressValue(e,13),i.action.keypressSearch(e),i.action.copyValue(e),i.action.backSpaceKeyRemove(e)}).on("focusout",function(){o&&o.removeClass("focus")}),window.onresize=function(){i.position()}},this.action={keypressValue:function(e,t){var a=i.keyNum(e),r=l[0].value,n=i.options.get();if(r.length>0&&a===t&&!n.selectionRequired&&n.multiple){var r=l[0].value;e.preventDefault(),i.fvalue.extract(r),i.results.remove()}},keypressSearch:function(e){var t=i.keyNum(e),r=l.val(),n=r.length,s=i.options.get();clearTimeout(a),(0!==s.minLength||0!==n||9!==t&&16!==t)&&(0===n&&s.minLength>0||n<s.minLength?i.results.remove():(!t||13!==t&&(37>t||t>40))&&(i.results.remove(),a=setTimeout(function(){0===s.minLength&&n>0||s.minLength>0&&n>=s.minLength?i.data.load(function(e){i.search.get(r,e,function(e){i.results.show(e)})}):i.results.remove()},s.searchDelay)))},redoSearchFocus:function(e){var t=i.fvalue.get(),a=i.options.get(),r=l.val();(r.length>0&&a.multiple||r.length>0&&0===t.length)&&this.keypressSearch(e)},copyValue:function(e){if(13!==i.keyNum(e)){var t=l.val(),a=i.options.get();a.multiple||(a.selectionRequired?i.fvalue.clear():(i.fvalue.extract(t),0===t.length&&(n=[])))}},backSpaceKeyRemove:function(e){var t=i.options.get();if(t.removeOnBackspace){var a=l.val(),r=l.data("_remove");r?(i.fvalue.remove(r),l.data("_remove",null)):0===a.length&&t.multiple&&8===i.keyNum(e)&&l.data("_remove",l.parents("li:eq(0)").prev())}},showAllResults:function(){var e=l.val();e=$.trim(e),""===e&&0===i.options.get("minLength")&&i.data.load(function(e){i.results.show(e)})}},this.set={up:function(){l=this.alias(),i.options.get("multiple")?o=this.multipleInput(l):l.insertAfter(t),l.attr("autofocus")&&l.focus(),this.chained()},alias:function(){var e=t.clone(!1).attr({list:null,name:t.attr("name")?"flexdatalist-"+t.attr("name"):null,id:t.attr("id")?t.attr("id")+"-flexdatalist":null,value:""}).addClass("flexdatalist-alias").removeClass("flexdatalist").attr("autocomplete","off");return t.addClass("flexdatalist flexdatalist-set").prop("type","hidden"),e},multipleInput:function(e){return o=$('<ul tabindex="1">').addClass("flexdatalist-multiple").css({"border-color":t.css("border-left-color"),"border-width":t.css("border-left-width"),"border-style":t.css("border-left-style"),"border-radius":t.css("border-top-left-radius"),"background-color":t.css("background-color")}).insertAfter(t).click(function(){$(this).find("input").focus()}),$('<li class="input-container">').addClass("flexdatalist-multiple-value").append(e).appendTo(o),o},chained:function(){var e=i.options.get();if(e.relatives&&e.chainedRelatives){var a=function(){e.relatives.each(function(){var e=i.isEmpty($(this).val()),a=i.isEmpty(t[0].value);!e&&a||i.fvalue.clear(),i.fdisabled(e)})};e.relatives.on("change",function(){a()}),a(!0)}}},this.fvalue={get:function(e){var t=i.value,a=i.options.get();return!a.multiple&&!this.isJSON()||e?t:this.toObj(t)},set:function(e,a){return i.fdisabled()||(a||this.clear(!0),this._load(e)),t},add:function(e){return i.options.get("multiple")&&this.set(e,!0),this},toggle:function(e){return i.fdisabled()||this.multiple.toggle(e),this},remove:function(e){if(!i.fdisabled()){e=this.toObj(e),t.trigger("before:flexdatalist.remove",[e]);var a=[];if($.isArray(e))$.each(e,function(e,t){var r=i.fvalue.multiple.remove(t);r&&a.push(r)});else{var r=this.multiple.remove(e);r&&a.push(r)}t.trigger("after:flexdatalist.remove",[e,a]).trigger("change:flexdatalist",[a,i.options.get()]).trigger("change")}return this},_load:function(e,t){var a=i.options.get(),r=this.toStr(e),n=this.get(!0);if(t=t?t:$.noop,0==r.length&&0==n.length)return void t(e);if(e=this.toObj(e),!i.isEmpty(e)&&"string"==typeof a.valueProperty&&!this.isJSON(e)){var s=a.searchIn,l=a.searchEqual;return a.searchIn=a.valueProperty.split(","),a.searchEqual=!0,void i.data.load(function(r){i.search.get(e,r,function(r){i.isEmpty(r)||i.fvalue.extract(r),t(e),a.searchIn=s,a.searchEqual=l})},e)}i.fvalue.extract(e),t(e)},extract:function(e){var a=i.options.get(),r=[];t.trigger("before:flexdatalist.value",[e,a]),$.isArray(e)?$.each(e,function(e,t){r.push(i.fvalue._extract(t))}):r=i.fvalue._extract(e),t.trigger("after:flexdatalist.value",[r,a]).trigger("change:flexdatalist",[r,a]).trigger("change")},_extract:function(e){var a=this.text(e),r=this.value(e),s=(t[0].value,i.options.get());if(s.multiple){if(!i.isEmpty(a)){if(i.isDup(a))return;n.push(a),this.multiple.add(r,a)}}else this.single(r,a);return{value:r,text:a}},single:function(e,i){i&&i!==l.val()&&l.val(i,!0),t[0].value=e},multiple:{add:function(e,t){var a=this,r=this.li(e,t);i.options.get();r.click(function(){a.toggle($(this))}).find(".fdl-remove").click(function(){a.remove($(this).parent())}),this.push(e),l.val("",!0),this.checkLimit()},push:function(e){var a=i.fvalue.get();e=i.fvalue.toObj(e),a.push(e),e=i.fvalue.toStr(a),t[0].value=e},toggle:function(e){var a=i.options.get();if(a.toggleSelected){var r=this.findLi(e);if(r){var n=r.index(),s=r.data(),l=r.hasClass("disabled")?"enable":"disable",o=i.fvalue.get(),u=[{value:s.value,text:s.text,action:l},a];if(t.trigger("before:flexdatalist.toggle",u),"enable"===l){var f=r.data("value");o.splice(n,0,f),r.removeClass("disabled")}else o.splice(n,1),r.addClass("disabled");o=i.fvalue.toStr(o),i.value=o,t.trigger("after:flexdatalist.toggle",u).trigger("change:flexdatalist",u).trigger("change")}}},remove:function(e){var a=this.findLi(e);if(a){var r=i.fvalue.get(),s=a.index(),l=a.data(),o=(i.options.get(),{value:l.value,text:l.text});return r.splice(s,1),r=i.fvalue.toStr(r),t[0].value=r,a.remove(),i.fvalue.multiple.checkLimit(),n.splice(s,1),o}},removeAll:function(){var e=i.fvalue.get(),a=i.options.get();t.trigger("before:flexdatalist.remove.all",[e,a]),o.find("li:not(.input-container)").remove(),t[0].value="",n=[],t.trigger("after:flexdatalist.remove.all",[e,a])},li:function(e,t){var a=o.find("li.input-container");return $("<li>").addClass("value"+(i.options.get("toggleSelected")?" toggle":"")).append('<span class="text">'+t+"</span>").append('<span class="fdl-remove">&times;</span>').data({text:t,value:e}).insertBefore(a)},checkLimit:function(){var e=i.options.get("limitOfValues");if(e>0){var t=o.find("li.input-container"),a=n.length;e==a?t.hide():t.show()}},findLi:function(e){if(e instanceof jQuery)0===e.length&&(e=null);else{var t=e;e=null,o.find("li:not(.input-container)").each(function(){var i=$(this);return i.data("value")===t?(e=i,!1):void 0})}return e},isEmpty:function(){return this.get().length>0}},value:function(e){var t=e,a=i.options.get();return i.isObject(e)&&(this.isJSON()||this.isMixed()?(delete e.name_highlight,t=this.toStr(e)):t=i.isDefined(e,a.valueProperty)?e[a.valueProperty]:i.isDefined(e,a.searchIn[0])?e[a.searchIn[0]]:null),t},text:function(e){var t=e,a=i.options.get();return i.isObject(e)&&(t=e[a.searchIn[0]],t=i.isDefined(e,a.textProperty)?e[a.textProperty]:this.placeholders.replace(e,a.textProperty,t)),$("<div>").html(t).text()},placeholders:{replace:function(e,t,a){if(i.isObject(e)&&"string"==typeof t){var r=this.parse(t);if(!i.isEmpty(e)&&r)return $.each(r,function(a,r){i.isDefined(e,r)&&(t=t.replace(a,e[r]))}),t}return a},parse:function(e){var t=e.match(/\{.+?\}/g);if(t){var i={};return t.map(function(e){i[e]=e.slice(1,-1)}),i}return!1}},clear:function(e){var a=t[0].value,r=i.options.get();return r.multiple&&this.multiple.removeAll(),t[0].value="",""!==a&&t.trigger("change:flexdatalist",[null,r]).trigger("change"),e&&l.val("",!0),n=[],this},toObj:function(e){if("object"!=typeof e){var t=i.options.get();i.isEmpty(e)||!i.isDefined(e)?e=t.multiple?[]:this.isJSON()?{}:"":this.isCSV()?(e=e.toString().split(t.valuesSeparator),e=$.map(e,function(e){return $.trim(e)})):(this.isJSON()||this.isMixed()&&this.isJSON(e))&&(e=JSON.parse(e))}return e},toStr:function(e){return"string"!=typeof e&&(i.isEmpty(e)||!i.isDefined(e)?e="":"number"==typeof e?e=e.toString():this.isCSV()?e=e.join(i.options.get("valuesSeparator")):(this.isJSON()||this.isMixed())&&(e=JSON.stringify(e))),$.trim(e)},isJSON:function(e){if("undefined"!=typeof e){if(i.isObject(e))e=JSON.stringify(e);else if("string"!=typeof e)return!1;return 0===e.indexOf("{")||0===e.indexOf("[{")}var t=i.options.get(),a=t.valueProperty;return t.selectionRequired&&(i.isObject(a)||"*"===a)},isMixed:function(){var e=i.options.get();return!e.selectionRequired&&"*"===e.valueProperty},isCSV:function(){return!this.isJSON()&&i.options.get("multiple")}},this.data={load:function(e,i){var a=this,r=[];t.trigger("before:flexdatalist.data"),this.url(function(i){r=r.concat(i),a["static"](function(i){r=r.concat(i),a.datalist(function(i){r=r.concat(i),t.trigger("after:flexdatalist.data",[r]),e(r)})})},i)},"static":function(e){var t=i.options.get();if("string"==typeof t.data){var a=t.data,r=i.cache.read(a,!0);if(r)return void e(r);this.remote({url:a,success:function(r){t.data=r,e(r),i.cache.write(a,r,t.cacheLifetime,!0)}})}else"object"!=typeof t.data&&(t.data=[]),e(t.data)},datalist:function(e){var a=t.attr("list"),r=[];i.isEmpty(a)||$("#"+a).find("option").each(function(){var e=$(this),t=e.val(),i=e.text();r.push({label:i.length>0?i:t,value:t})}),e(r)},url:function(e,t){var a=l.val(),r=i.options.get(),n=r.keywordParamName,s=i.fvalue.get(),o=this.relativesData();if(i.isEmpty(r.url))return e([]);var u={};"post"===r.requestType&&($.each(r,function(e,t){0!=e.indexOf("_")&&"data"!=e&&(u[e]=t)}),delete u.relatives);var f=i.cache.keyGen({relative:o,load:t,keyword:a,contain:r.searchContain},r.url),c=i.cache.read(f);if(c)return void e(c);var d=$.extend(o,r.params,{load:t,contain:r.searchContain,selected:s,original:r.originalValue,options:u});d[n]=a,this.remote({url:r.url,data:d,success:function(t){var n=l.val();n.length>=a.length&&e(t),i.cache.write(f,t,r.cacheLifetime)}})},remote:function(e){var a=this,r=i.options.get();t.hasClass("flexdatalist-loading")||(t.addClass("flexdatalist-loading"),"json"===r.requestContentType&&(e.data=JSON.stringify(data)),$.ajax($.extend({type:r.requestType,dataType:"json",contentType:"application/"+r.requestContentType+"; charset=UTF-8",complete:function(){t.removeClass("flexdatalist-loading")}},e,{success:function(t){t=a.extractRemoteData(t),e.success(t)}})))},extractRemoteData:function(e){var t=i.options.get(),a=i.isDefined(e,t.resultsProperty)?e[t.resultsProperty]:e;return"string"==typeof a&&0===a.indexOf("[{")&&(a=JSON.parse(a)),a&&a.options&&i.options.set($.extend({},t,a.options)),i.isObject(a)?a:[]},relativesData:function(){var e=i.options.get("relatives"),t={};return e&&(t.relatives={},e.each(function(){var e=$(this),i=e.attr("name").split("][").join("-").split("]").join("-").split("[").join("-").replace(/^\|+|\-+$/g,"");t.relatives[i]=e.val()})),t}},this.search={get:function(e,a,r){var n=this,s=i.options.get();if(s.searchDisabled)return void i.debug("Search is disabled!");var l=i.cache.read(e);if(!l){if(t.trigger("before:flexdatalist.search",[e,a]),!i.isEmpty(e)){l=[];for(var o=n.split(e),u=0;u<a.length;u++){var f=a[u];i.isDup(f)||(f=n.matches(f,o),f&&l.push(f))}}i.cache.write(e,l,2),t.trigger("after:flexdatalist.search",[e,a,l])}r(l)},matches:function(e,t){var a=$.extend({},e),r=[],n=i.options.get(),s=n.searchIn;if(t.length>0)for(var l=0;l<s.length;l++){var o=s[l];if(i.isDefined(e,o)&&e[o]){for(var u=e[o].toString(),f=u,c=this.split(u),d=0;d<t.length;d++){var h=t[d];this.find(h,c)&&(r.push(h),f=this.highlight(h,f))}f!==u&&(a[o+"_highlight"]=this.highlight(f))}}return 0===r.length||n.searchByWord&&r.length<t.length-1?!1:a},highlight:function(e,t){return t?t.replace(new RegExp(e,i.options.get("searchContain")?"ig":"i"),"|:|$&|::|"):(e=e.split("|:|").join('<span class="highlight">'),e.split("|::|").join("</span>"))},find:function(e,t){for(var a=i.options.get(),r=0;r<t.length;r++){var n=t[r];if(n=this.normalizeString(n),e=this.normalizeString(e),a.searchEqual)return n==e;if(a.searchContain?n.indexOf(e)>=0:0===n.indexOf(e))return!0}return!1},split:function(e){if("string"==typeof e&&(e=[$.trim(e)]),i.options.get("searchByWord"))for(var t=0;t<e.length;t++){var a=$.trim(e[t]);if(a.indexOf(" ")>0){var r=a.split(" ");$.merge(e,r)}}return e},normalizeString:function(e){if("string"==typeof e){var t=i.options.get("normalizeString");return"function"==typeof t&&(e=t(e)),e.toUpperCase()}return e}},this.results={show:function(e){var a=this,r=i.options.get();if(this.remove(!0),e){if(0===e.length)return void this.empty(r.noResultsText);var n=this.container();r.groupBy?(e=this.group(e),Object.keys(e).forEach(function(t){var s=e[t],l=r.groupBy,o=i.results.highlight(s[0],l,t);$("<li>").addClass("group").append($("<span>").addClass("group-name").html(o)).append($("<span>").addClass("group-item-count").text(" "+s.length)).appendTo(n);a.items(s,n)})):this.items(e,n);var s=n.find("li:not(.group)");s.on("click",function(){var e=$(this).data("item");e&&(i.fvalue.extract(e),a.remove(),t.trigger("select:flexdatalist",[e,r]))}).hover(function(){s.removeClass("active"),$(this).addClass("active")},function(){$(this).removeClass("active")}),r.focusFirstResult&&s.filter(":first").addClass("active")}},empty:function(e){if(!i.isEmpty(e)){var t=this.container(),a=l.val();e=e.split("{keyword}").join(a),$("<li>").addClass("item no-results").append(e).appendTo(t)}},items:function(e,a){var r=i.options.get("maxShownResults");t.trigger("show:flexdatalist.results",[e]);for(var n=0;n<e.length&&!(r>0&&r===n);n++)this.item(e[n]).appendTo(a);t.trigger("shown:flexdatalist.results",[e])},item:function(e){for(var t=$("<li>").data("item",e).addClass("item"),a=i.options.get(),r=a.visibleProperties,n=0;n<r.length;n++){var s=r[n];if((!a.groupBy||a.groupBy!==s)&&i.isDefined(e,s)){var l={};if(s===a.iconProperty)l=$("<img>").addClass("item item-"+s).attr("src",e[s]);else{var o=i.results.highlight(e,s);l=$("<span>").addClass("item item-"+s).html(o+" ")}l.appendTo(t)}}return t},container:function(){var e=t;o&&(e=o);var a=$("ul.flexdatalist-results");return 0===a.length&&(a=$("<ul>").addClass("flexdatalist-results").appendTo("body").css({"border-color":e.css("border-left-color"),"border-width":"1px","border-bottom-left-radius":e.css("border-bottom-left-radius"),"border-bottom-right-radius":e.css("border-bottom-right-radius")}).data({target:l,input:t}),i.position(l)),a},group:function(e){for(var t=[],a=i.options.get("groupBy"),r=0;r<e.length;r++){var n=e[r];if(i.isDefined(n,a)){var s=n[a];i.isDefined(t,s)||(t[s]=[]),t[s].push(n)}}return t},highlight:function(e,t,a){return i.isDefined(e,t+"_highlight")?e[t+"_highlight"]:i.isDefined(e,t)?e[t]:a},remove:function(e){var i="ul.flexdatalist-results";e&&(i="ul.flexdatalist-results li"),$(i).remove(),t.trigger("removed:flexdatalist.results")}},this.cache={write:function(e,t,a,r){if(i.cache.isSupported()){e=this.keyGen(e,void 0,r);var n={value:t,timestamp:i.unixtime(),lifetime:a?a:!1};localStorage.setItem(e,JSON.stringify(n))}},read:function(e,t){if(i.cache.isSupported()){e=this.keyGen(e,void 0,t);var a=localStorage.getItem(e);if(a){var r=JSON.parse(a);if(r.lifetime){var n=i.unixtime()-r.timestamp;return r.lifetime>n?r.value:(i.cache["delete"](e),null)}return r.value}}return null},"delete":function(e,t){i.cache.isSupported()&&(e=this.keyGen(e,void 0,t),localStorage.removeItem(e))},clear:function(){if(i.cache.isSupported()){for(var e in localStorage)(e.indexOf(s)>-1||e.indexOf("global")>-1)&&i.cache["delete"](e);localStorage.clear()}},isSupported:function(){if(i.options.get("cache"))try{return"localStorage"in window&&null!==window.localStorage}catch(e){return!1}return!1},keyGen:function(e,t,i){"object"==typeof e&&(e=JSON.stringify(e));var a,r,n=void 0===t?2166136261:t;for(a=0,r=e.length;r>a;a++)n^=e.charCodeAt(a),n+=(n<<1)+(n<<4)+(n<<7)+(n<<8)+(n<<24);return(i?"global":s)+("0000000"+(n>>>0).toString(16)).substr(-8)}},this.options={init:function(){var e=$.extend({},r,t.data(),{multiple:null===r.multiple?t.is("[multiple]"):r.multiple,disabled:null===r.disabled?t.is("[disabled]"):r.disabled,originalValue:t[0].value});return this.set(e),e},get:function(e){var a=t.data("flexdatalist");return e?i.isDefined(a,e)?a[e]:null:a},set:function(e,a){var r=this.get();return i.isDefined(r,e)&&i.isDefined(a)?r[e]=a:i.isObject(e)&&(r=this._normalize(e)),t.data("flexdatalist",r),t},_normalize:function(e){if(e.searchIn=i.csvToArray(e.searchIn),e.relatives=e.relatives&&$(e.relatives).length>0?$(e.relatives):null,e.textProperty=null===e.textProperty?e.searchIn[0]:e.textProperty,e.visibleProperties=i.csvToArray(e.visibleProperties,e.searchIn),"*"===e.valueProperty&&e.multiple&&!e.selectionRequired)throw new Error("Selection must be required for multiple, JSON fields!");return e}},this.position=function(){var e=l;o&&(e=o),$("ul.flexdatalist-results").css({width:e.outerWidth()+"px",top:e.offset().top+e.outerHeight()+"px",left:e.offset().left+"px"})},this.fdisabled=function(e){if(this.isDefined(e)){if(t.prop("disabled",e),l.prop("disabled",e),o){o.css("background-color",t.css("background-color"));var i=o.find("li .fdl-remove"),a=o.find("li.input-container");e?(o.addClass("disabled"),a.hide(),i.hide()):(o.removeClass("disabled"),a.show(),i.show())}this.options.set("disabled",e)}return this.options.get("disabled")},this.isDup=function(e){return this.options.get("allowDuplicateValues")?!1:n.length>0&&n.indexOf(this.fvalue.text(e))>-1},this.keyNum=function(e){return e.which||e.keyCode},this.isEmpty=function(e){return i.isDefined(e)?null===e?!0:e===!0?!1:0===this.length(e)?!0:""===$.trim(e):!0},this.isObject=function(e){return e&&"object"==typeof e},this.length=function(e){return this.isObject(e)?Object.keys(e).length:"number"==typeof e||"number"==typeof e.length?e.toString().length:0},this.isDefined=function(e,t){var i="undefined"!=typeof e;return i&&"undefined"!=typeof t?"undefined"!=typeof e[t]:i},this.unixtime=function(e){var t=new Date;return e&&(t=new Date(e)),Math.round(t.getTime()/1e3)},this.csvToArray=function(e,t){return 0===e.length?t:"string"==typeof e?e.split(i.options.get("valuesSeparator")):e},this.debug=function(e,a){var r=i.options.get();r.debug&&(a||(a={}),e="Flexdatalist: "+e,console.warn(e),console.log($.extend({inputName:t.attr("name"),options:r},a)),console.log("--- /flexdatalist ---"))},this.init()})},jQuery(function(e){var t=e(document);t.data("flexdatalist")||e(document).mouseup(function(t){var i=e(".flexdatalist-results"),a=i.data("target");a&&a.is(":focus")||i.is(t.target)||0!==i.has(t.target).length||i.remove()}).keydown(function(t){var i=e(".flexdatalist-results"),a=i.find("li"),r=a.filter(".active"),n=r.index(),s=a.length,l=t.which||t.keyCode;if(0!==s){if(27===l)return void i.remove();if(13===l)t.preventDefault(),r.click();else if(40===l||38===l){t.preventDefault(),40===l?r=s>n&&r.nextAll(".item").first().length>0?r.removeClass("active").nextAll(".item").first().addClass("active"):a.removeClass("active").filter(".item:first").addClass("active"):38===l&&(r=n>0&&r.prevAll(".item").first().length>0?r.removeClass("active").prevAll(".item").first().addClass("active"):a.removeClass("active").filter(".item:last").addClass("active"));var o=(0===r.prev().length?r:r.prev()).position().top;i.animate({scrollTop:o+i.scrollTop()},100)}}}).data("flexdatalist",!0),jQuery("input.flexdatalist:not(.flexdatalist-set)").flexdatalist()}),function(e){var t=e.fn.val;e.fn.val=function(i){var a=e(this).hasClass("flexdatalist");return"undefined"==typeof i?a?this[0].fvalue.get(!0):t.call(this):a?this[0].fvalue.set(i):t.call(this,i)}}(jQuery);
\ No newline at end of file
+jQuery.fn.flexdatalist=function(e,t){"use strict";var i=function(e){e.each(function(){var e=$(this),t=e.data("flexdatalist");e.removeClass("flexdatalist-set").attr("type","text").val(t&&t.originalValue?t.originalValue:"").removeData("flexdatalist").next(".flexdatalist-alias, ul.flexdatalist-multiple").remove()})};if("string"==typeof e&&"reset"!==e){var a=this[0];if("destroy"===e)i($(this));else if("value"===e){if("undefined"==typeof t)return a.fvalue.get();a.fvalue.set(t)}else if("add"===e){if("undefined"==typeof t)return a.debug("Missing value to add!");a.fvalue.add(t)}else if("toggle"===e){if("undefined"==typeof t)return a.debug("Missing value to toggle!");a.fvalue.toggle(t)}else if("remove"===e){if("undefined"==typeof t)return a.debug("Missing value to remove!");a.fvalue.remove(t)}else if("disabled"===e){if("undefined"==typeof t)return a.fdisabled();a.fdisabled(t)}else if("string"==typeof e){if("undefined"==typeof t)return a.options.get(e);a.options.set(e,t)}return this}this&&"undefined"!=typeof this[0]&&"undefined"!=typeof this[0].fvalue&&i($(this));var r=$.extend({url:null,data:[],params:{},relatives:null,chainedRelatives:!1,cache:!0,cacheLifetime:60,minLength:2,groupBy:!1,selectionRequired:!1,focusFirstResult:!1,textProperty:null,valueProperty:null,visibleProperties:[],iconProperty:"thumb",searchIn:["label"],searchContain:!1,searchEqual:!1,searchByWord:!1,searchDisabled:!1,searchDelay:300,normalizeString:null,multiple:null,disabled:null,maxShownResults:100,removeOnBackspace:!0,noResultsText:'No results found for "{keyword}"',toggleSelected:!1,allowDuplicateValues:!1,redoSearchOnFocus:!0,requestType:"get",requestContentType:"x-www-form-urlencoded",resultsProperty:"results",keywordParamName:"keyword",limitOfValues:0,valuesSeparator:",",debug:!0},e);return this.each(function(e){var t=$(this),i=this,a=null,n=[],s="flex"+e,l=null,o=null;this.init=function(){var e=this.options.init();this.set.up(),this.fvalue.clear()._load(e.originalValue,function(){i.fdisabled(e.disabled)}),l.on("focusin",function(e){i.action.redoSearchFocus(e),i.action.showAllResults(e),o&&o.addClass("focus")}).on("input keydown",function(e){9===i.keyNum(e)&&i.results.remove(),i.action.keypressValue(e,188),i.action.backSpaceKeyRemove(e)}).on("input keyup",function(e){i.action.keypressValue(e,13),i.action.keypressSearch(e),i.action.copyValue(e),i.action.backSpaceKeyRemove(e)}).on("focusout",function(){o&&o.removeClass("focus")}),window.onresize=function(){i.position()}},this.action={keypressValue:function(e,t){var a=i.keyNum(e),r=l[0].value,n=i.options.get();if(r.length>0&&a===t&&!n.selectionRequired&&n.multiple){var r=l[0].value;e.preventDefault(),i.fvalue.extract(r),i.results.remove()}},keypressSearch:function(e){var t=i.keyNum(e),r=l.val(),n=r.length,s=i.options.get();clearTimeout(a),(0!==s.minLength||0!==n||9!==t&&16!==t)&&(0===n&&s.minLength>0||n<s.minLength?i.results.remove():(!t||13!==t&&(37>t||t>40))&&(i.results.remove(),a=setTimeout(function(){0===s.minLength&&n>0||s.minLength>0&&n>=s.minLength?i.data.load(function(e){i.search.get(r,e,function(e){i.results.show(e)})}):i.results.remove()},s.searchDelay)))},redoSearchFocus:function(e){var t=i.fvalue.get(),a=i.options.get(),r=l.val();a.redoSearchOnFocus&&(r.length>0&&a.multiple||r.length>0&&0===t.length)&&this.keypressSearch(e)},copyValue:function(e){if(13!==i.keyNum(e)){var t=l.val(),a=i.options.get();a.multiple||(a.selectionRequired?i.fvalue.clear():(i.fvalue.extract(t),0===t.length&&(n=[])))}},backSpaceKeyRemove:function(e){var t=i.options.get();if(t.removeOnBackspace){var a=l.val(),r=l.data("_remove");r?(i.fvalue.remove(r),l.data("_remove",null)):0===a.length&&t.multiple&&8===i.keyNum(e)&&l.data("_remove",l.parents("li:eq(0)").prev())}},showAllResults:function(){var e=l.val();e=$.trim(e),""===e&&0===i.options.get("minLength")&&i.data.load(function(e){i.results.show(e)})}},this.set={up:function(){l=this.alias(),i.options.get("multiple")?o=this.multipleInput(l):l.insertAfter(t),l.attr("autofocus")&&l.focus(),this.chained()},alias:function(){var e=t.clone(!1).attr({list:null,name:t.attr("name")?"flexdatalist-"+t.attr("name"):null,id:t.attr("id")?t.attr("id")+"-flexdatalist":null,value:""}).addClass("flexdatalist-alias").removeClass("flexdatalist").attr("autocomplete","off");return t.addClass("flexdatalist flexdatalist-set").prop("type","hidden"),e},multipleInput:function(e){return o=$('<ul tabindex="1">').addClass("flexdatalist-multiple").css({"border-color":t.css("border-left-color"),"border-width":t.css("border-left-width"),"border-style":t.css("border-left-style"),"border-radius":t.css("border-top-left-radius"),"background-color":t.css("background-color")}).insertAfter(t).click(function(){$(this).find("input").focus()}),$('<li class="input-container">').addClass("flexdatalist-multiple-value").append(e).appendTo(o),o},chained:function(){var e=i.options.get();if(e.relatives&&e.chainedRelatives){var a=function(){e.relatives.each(function(){var e=i.isEmpty($(this).val()),a=i.isEmpty(t[0].value);!e&&a||i.fvalue.clear(),i.fdisabled(e)})};e.relatives.on("change",function(){a()}),a(!0)}}},this.fvalue={get:function(e){var t=i.value,a=i.options.get();return!a.multiple&&!this.isJSON()||e?t:this.toObj(t)},set:function(e,a){return i.fdisabled()||(a||this.clear(!0),this._load(e)),t},add:function(e){return i.options.get("multiple")&&this.set(e,!0),this},toggle:function(e){return i.fdisabled()||this.multiple.toggle(e),this},remove:function(e){if(!i.fdisabled()){e=this.toObj(e),t.trigger("before:flexdatalist.remove",[e]);var a=[];if($.isArray(e))$.each(e,function(e,t){var r=i.fvalue.multiple.remove(t);r&&a.push(r)});else{var r=this.multiple.remove(e);r&&a.push(r)}t.trigger("after:flexdatalist.remove",[e,a]).trigger("change:flexdatalist",[a,i.options.get()]).trigger("change")}return this},_load:function(e,t){var a=i.options.get(),r=this.toStr(e),n=this.get(!0);if(t=t?t:$.noop,0==r.length&&0==n.length)return void t(e);if(e=this.toObj(e),!i.isEmpty(e)&&"string"==typeof a.valueProperty&&!this.isJSON(e)){var s=a.searchIn,l=a.searchEqual;return a.searchIn=a.valueProperty.split(","),a.searchEqual=!0,void i.data.load(function(r){i.search.get(e,r,function(r){i.isEmpty(r)||i.fvalue.extract(r),t(e),a.searchIn=s,a.searchEqual=l})},e)}i.fvalue.extract(e),t(e)},extract:function(e){var a=i.options.get(),r=[];t.trigger("before:flexdatalist.value",[e,a]),$.isArray(e)?$.each(e,function(e,t){r.push(i.fvalue._extract(t))}):r=i.fvalue._extract(e),t.trigger("after:flexdatalist.value",[r,a]).trigger("change:flexdatalist",[r,a]).trigger("change")},_extract:function(e){var a=this.text(e),r=this.value(e),s=(t[0].value,i.options.get());if(s.multiple){if(!i.isEmpty(a)){if(i.isDup(a))return;n.push(a),this.multiple.add(r,a)}}else this.single(r,a);return{value:r,text:a}},single:function(e,i){i&&i!==l.val()&&l.val(i,!0),t[0].value=e},multiple:{add:function(e,t){var a=this,r=this.li(e,t);i.options.get();r.click(function(){a.toggle($(this))}).find(".fdl-remove").click(function(){a.remove($(this).parent())}),this.push(e),l.val("",!0),this.checkLimit()},push:function(e){var a=i.fvalue.get();e=i.fvalue.toObj(e),a.push(e),e=i.fvalue.toStr(a),t[0].value=e},toggle:function(e){var a=i.options.get();if(a.toggleSelected){var r=this.findLi(e);if(r){var n=r.index(),s=r.data(),l=r.hasClass("disabled")?"enable":"disable",o=i.fvalue.get(),u=[{value:s.value,text:s.text,action:l},a];if(t.trigger("before:flexdatalist.toggle",u),"enable"===l){var f=r.data("value");o.splice(n,0,f),r.removeClass("disabled")}else o.splice(n,1),r.addClass("disabled");o=i.fvalue.toStr(o),i.value=o,t.trigger("after:flexdatalist.toggle",u).trigger("change:flexdatalist",u).trigger("change")}}},remove:function(e){var a=this.findLi(e);if(a){var r=i.fvalue.get(),s=a.index(),l=a.data(),o=(i.options.get(),{value:l.value,text:l.text});return r.splice(s,1),r=i.fvalue.toStr(r),t[0].value=r,a.remove(),i.fvalue.multiple.checkLimit(),n.splice(s,1),o}},removeAll:function(){var e=i.fvalue.get(),a=i.options.get();t.trigger("before:flexdatalist.remove.all",[e,a]),o.find("li:not(.input-container)").remove(),t[0].value="",n=[],t.trigger("after:flexdatalist.remove.all",[e,a])},li:function(e,t){var a=o.find("li.input-container");return $("<li>").addClass("value"+(i.options.get("toggleSelected")?" toggle":"")).append('<span class="text">'+t+"</span>").append('<span class="fdl-remove">&times;</span>').data({text:t,value:e}).insertBefore(a)},checkLimit:function(){var e=i.options.get("limitOfValues");if(e>0){var t=o.find("li.input-container"),a=n.length;e==a?t.hide():t.show()}},findLi:function(e){if(e instanceof jQuery)0===e.length&&(e=null);else{var t=e;e=null,o.find("li:not(.input-container)").each(function(){var i=$(this);return i.data("value")===t?(e=i,!1):void 0})}return e},isEmpty:function(){return this.get().length>0}},value:function(e){var t=e,a=i.options.get();return i.isObject(e)&&(this.isJSON()||this.isMixed()?(delete e.name_highlight,t=this.toStr(e)):t=i.isDefined(e,a.valueProperty)?e[a.valueProperty]:i.isDefined(e,a.searchIn[0])?e[a.searchIn[0]]:null),t},text:function(e){var t=e,a=i.options.get();return i.isObject(e)&&(t=e[a.searchIn[0]],t=i.isDefined(e,a.textProperty)?e[a.textProperty]:this.placeholders.replace(e,a.textProperty,t)),$("<div>").html(t).text()},placeholders:{replace:function(e,t,a){if(i.isObject(e)&&"string"==typeof t){var r=this.parse(t);if(!i.isEmpty(e)&&r)return $.each(r,function(a,r){i.isDefined(e,r)&&(t=t.replace(a,e[r]))}),t}return a},parse:function(e){var t=e.match(/\{.+?\}/g);if(t){var i={};return t.map(function(e){i[e]=e.slice(1,-1)}),i}return!1}},clear:function(e){var a=t[0].value,r=i.options.get();return r.multiple&&this.multiple.removeAll(),t[0].value="",""!==a&&t.trigger("change:flexdatalist",[null,r]).trigger("change"),e&&l.val("",!0),n=[],this},toObj:function(e){if("object"!=typeof e){var t=i.options.get();i.isEmpty(e)||!i.isDefined(e)?e=t.multiple?[]:this.isJSON()?{}:"":this.isCSV()?(e=e.toString().split(t.valuesSeparator),e=$.map(e,function(e){return $.trim(e)})):(this.isJSON()||this.isMixed()&&this.isJSON(e))&&(e=JSON.parse(e))}return e},toStr:function(e){return"string"!=typeof e&&(i.isEmpty(e)||!i.isDefined(e)?e="":"number"==typeof e?e=e.toString():this.isCSV()?e=e.join(i.options.get("valuesSeparator")):(this.isJSON()||this.isMixed())&&(e=JSON.stringify(e))),$.trim(e)},isJSON:function(e){if("undefined"!=typeof e){if(i.isObject(e))e=JSON.stringify(e);else if("string"!=typeof e)return!1;return 0===e.indexOf("{")||0===e.indexOf("[{")}var t=i.options.get(),a=t.valueProperty;return t.selectionRequired&&(i.isObject(a)||"*"===a)},isMixed:function(){var e=i.options.get();return!e.selectionRequired&&"*"===e.valueProperty},isCSV:function(){return!this.isJSON()&&i.options.get("multiple")}},this.data={load:function(e,i){var a=this,r=[];t.trigger("before:flexdatalist.data"),this.url(function(i){r=r.concat(i),a["static"](function(i){r=r.concat(i),a.datalist(function(i){r=r.concat(i),t.trigger("after:flexdatalist.data",[r]),e(r)})})},i)},"static":function(e){var t=i.options.get();if("string"==typeof t.data){var a=t.data,r=i.cache.read(a,!0);if(r)return void e(r);this.remote({url:a,success:function(r){t.data=r,e(r),i.cache.write(a,r,t.cacheLifetime,!0)}})}else"object"!=typeof t.data&&(t.data=[]),e(t.data)},datalist:function(e){var a=t.attr("list"),r=[];i.isEmpty(a)||$("#"+a).find("option").each(function(){var e=$(this),t=e.val(),i=e.text();r.push({label:i.length>0?i:t,value:t})}),e(r)},url:function(e,t){var a=l.val(),r=i.options.get(),n=r.keywordParamName,s=i.fvalue.get(),o=this.relativesData();if(i.isEmpty(r.url))return e([]);var u={};"post"===r.requestType&&($.each(r,function(e,t){0!=e.indexOf("_")&&"data"!=e&&(u[e]=t)}),delete u.relatives);var f=i.cache.keyGen({relative:o,load:t,keyword:a,contain:r.searchContain},r.url),c=i.cache.read(f);if(c)return void e(c);var d=$.extend(o,r.params,{load:t,contain:r.searchContain,selected:s,original:r.originalValue,options:u});d[n]=a,this.remote({url:r.url,data:d,success:function(t){var n=l.val();n.length>=a.length&&e(t),i.cache.write(f,t,r.cacheLifetime)}})},remote:function(e){var a=this,r=i.options.get();t.hasClass("flexdatalist-loading")||(t.addClass("flexdatalist-loading"),"json"===r.requestContentType&&(e.data=JSON.stringify(data)),$.ajax($.extend({type:r.requestType,dataType:"json",contentType:"application/"+r.requestContentType+"; charset=UTF-8",complete:function(){t.removeClass("flexdatalist-loading")}},e,{success:function(t){t=a.extractRemoteData(t),e.success(t)}})))},extractRemoteData:function(e){var t=i.options.get(),a=i.isDefined(e,t.resultsProperty)?e[t.resultsProperty]:e;return"string"==typeof a&&0===a.indexOf("[{")&&(a=JSON.parse(a)),a&&a.options&&i.options.set($.extend({},t,a.options)),i.isObject(a)?a:[]},relativesData:function(){var e=i.options.get("relatives"),t={};return e&&(t.relatives={},e.each(function(){var e=$(this),i=e.attr("name").split("][").join("-").split("]").join("-").split("[").join("-").replace(/^\|+|\-+$/g,"");t.relatives[i]=e.val()})),t}},this.search={get:function(e,a,r){var n=this,s=i.options.get();if(s.searchDisabled)return void i.debug("Search is disabled!");var l=i.cache.read(e);if(!l){if(t.trigger("before:flexdatalist.search",[e,a]),!i.isEmpty(e)){l=[];for(var o=n.split(e),u=0;u<a.length;u++){var f=a[u];i.isDup(f)||(f=n.matches(f,o),f&&l.push(f))}}i.cache.write(e,l,2),t.trigger("after:flexdatalist.search",[e,a,l])}r(l)},matches:function(e,t){var a=$.extend({},e),r=[],n=i.options.get(),s=n.searchIn;if(t.length>0)for(var l=0;l<s.length;l++){var o=s[l];if(i.isDefined(e,o)&&e[o]){for(var u=e[o].toString(),f=u,c=this.split(u),d=0;d<t.length;d++){var h=t[d];this.find(h,c)&&(r.push(h),f=this.highlight(h,f))}f!==u&&(a[o+"_highlight"]=this.highlight(f))}}return 0===r.length||n.searchByWord&&r.length<t.length-1?!1:a},highlight:function(e,t){return t?t.replace(new RegExp(e,i.options.get("searchContain")?"ig":"i"),"|:|$&|::|"):(e=e.split("|:|").join('<span class="highlight">'),e.split("|::|").join("</span>"))},find:function(e,t){for(var a=i.options.get(),r=0;r<t.length;r++){var n=t[r];if(n=this.normalizeString(n),e=this.normalizeString(e),a.searchEqual)return n==e;if(a.searchContain?n.indexOf(e)>=0:0===n.indexOf(e))return!0}return!1},split:function(e){if("string"==typeof e&&(e=[$.trim(e)]),i.options.get("searchByWord"))for(var t=0;t<e.length;t++){var a=$.trim(e[t]);if(a.indexOf(" ")>0){var r=a.split(" ");$.merge(e,r)}}return e},normalizeString:function(e){if("string"==typeof e){var t=i.options.get("normalizeString");return"function"==typeof t&&(e=t(e)),e.toUpperCase()}return e}},this.results={show:function(e){var a=this,r=i.options.get();if(this.remove(!0),e){if(0===e.length)return void this.empty(r.noResultsText);var n=this.container();r.groupBy?(e=this.group(e),Object.keys(e).forEach(function(t){var s=e[t],l=r.groupBy,o=i.results.highlight(s[0],l,t);$("<li>").addClass("group").append($("<span>").addClass("group-name").html(o)).append($("<span>").addClass("group-item-count").text(" "+s.length)).appendTo(n);a.items(s,n)})):this.items(e,n);var s=n.find("li:not(.group)");s.on("click",function(){var e=$(this).data("item");e&&(i.fvalue.extract(e),a.remove(),t.trigger("select:flexdatalist",[e,r]))}).hover(function(){s.removeClass("active"),$(this).addClass("active")},function(){$(this).removeClass("active")}),r.focusFirstResult&&s.filter(":first").addClass("active")}},empty:function(e){if(!i.isEmpty(e)){var t=this.container(),a=l.val();e=e.split("{keyword}").join(a),$("<li>").addClass("item no-results").append(e).appendTo(t)}},items:function(e,a){var r=i.options.get("maxShownResults");t.trigger("show:flexdatalist.results",[e]);for(var n=0;n<e.length&&!(r>0&&r===n);n++)this.item(e[n]).appendTo(a);t.trigger("shown:flexdatalist.results",[e])},item:function(e){for(var t=$("<li>").data("item",e).addClass("item"),a=i.options.get(),r=a.visibleProperties,n=0;n<r.length;n++){var s=r[n];if((!a.groupBy||a.groupBy!==s)&&i.isDefined(e,s)){var l={};if(s===a.iconProperty)l=$("<img>").addClass("item item-"+s).attr("src",e[s]);else{var o=i.results.highlight(e,s);l=$("<span>").addClass("item item-"+s).html(o+" ")}l.appendTo(t)}}return t},container:function(){var e=t;o&&(e=o);var a=$("ul.flexdatalist-results");return 0===a.length&&(a=$("<ul>").addClass("flexdatalist-results").appendTo("body").css({"border-color":e.css("border-left-color"),"border-width":"1px","border-bottom-left-radius":e.css("border-bottom-left-radius"),"border-bottom-right-radius":e.css("border-bottom-right-radius")}).data({target:l,input:t}),i.position(l)),a},group:function(e){for(var t=[],a=i.options.get("groupBy"),r=0;r<e.length;r++){var n=e[r];if(i.isDefined(n,a)){var s=n[a];i.isDefined(t,s)||(t[s]=[]),t[s].push(n)}}return t},highlight:function(e,t,a){return i.isDefined(e,t+"_highlight")?e[t+"_highlight"]:i.isDefined(e,t)?e[t]:a},remove:function(e){var i="ul.flexdatalist-results";e&&(i="ul.flexdatalist-results li"),$(i).remove(),t.trigger("removed:flexdatalist.results")}},this.cache={write:function(e,t,a,r){if(i.cache.isSupported()){e=this.keyGen(e,void 0,r);var n={value:t,timestamp:i.unixtime(),lifetime:a?a:!1};localStorage.setItem(e,JSON.stringify(n))}},read:function(e,t){if(i.cache.isSupported()){e=this.keyGen(e,void 0,t);var a=localStorage.getItem(e);if(a){var r=JSON.parse(a);if(r.lifetime){var n=i.unixtime()-r.timestamp;return r.lifetime>n?r.value:(i.cache["delete"](e),null)}return r.value}}return null},"delete":function(e,t){i.cache.isSupported()&&(e=this.keyGen(e,void 0,t),localStorage.removeItem(e))},clear:function(){if(i.cache.isSupported()){for(var e in localStorage)(e.indexOf(s)>-1||e.indexOf("global")>-1)&&i.cache["delete"](e);localStorage.clear()}},isSupported:function(){if(i.options.get("cache"))try{return"localStorage"in window&&null!==window.localStorage}catch(e){return!1}return!1},keyGen:function(e,t,i){"object"==typeof e&&(e=JSON.stringify(e));var a,r,n=void 0===t?2166136261:t;for(a=0,r=e.length;r>a;a++)n^=e.charCodeAt(a),n+=(n<<1)+(n<<4)+(n<<7)+(n<<8)+(n<<24);return(i?"global":s)+("0000000"+(n>>>0).toString(16)).substr(-8)}},this.options={init:function(){var e=$.extend({},r,t.data(),{multiple:null===r.multiple?t.is("[multiple]"):r.multiple,disabled:null===r.disabled?t.is("[disabled]"):r.disabled,originalValue:t[0].value});return this.set(e),e},get:function(e){var a=t.data("flexdatalist");return e?i.isDefined(a,e)?a[e]:null:a},set:function(e,a){var r=this.get();return i.isDefined(r,e)&&i.isDefined(a)?r[e]=a:i.isObject(e)&&(r=this._normalize(e)),t.data("flexdatalist",r),t},_normalize:function(e){if(e.searchIn=i.csvToArray(e.searchIn),e.relatives=e.relatives&&$(e.relatives).length>0?$(e.relatives):null,e.textProperty=null===e.textProperty?e.searchIn[0]:e.textProperty,e.visibleProperties=i.csvToArray(e.visibleProperties,e.searchIn),"*"===e.valueProperty&&e.multiple&&!e.selectionRequired)throw new Error("Selection must be required for multiple, JSON fields!");return e}},this.position=function(){var e=l;o&&(e=o),$("ul.flexdatalist-results").css({width:e.outerWidth()+"px",top:e.offset().top+e.outerHeight()+"px",left:e.offset().left+"px"})},this.fdisabled=function(e){if(this.isDefined(e)){if(t.prop("disabled",e),l.prop("disabled",e),o){o.css("background-color",t.css("background-color"));var i=o.find("li .fdl-remove"),a=o.find("li.input-container");e?(o.addClass("disabled"),a.hide(),i.hide()):(o.removeClass("disabled"),a.show(),i.show())}this.options.set("disabled",e)}return this.options.get("disabled")},this.isDup=function(e){return this.options.get("allowDuplicateValues")?!1:n.length>0&&n.indexOf(this.fvalue.text(e))>-1},this.keyNum=function(e){return e.which||e.keyCode},this.isEmpty=function(e){return i.isDefined(e)?null===e?!0:e===!0?!1:0===this.length(e)?!0:""===$.trim(e):!0},this.isObject=function(e){return e&&"object"==typeof e},this.length=function(e){return this.isObject(e)?Object.keys(e).length:"number"==typeof e||"number"==typeof e.length?e.toString().length:0},this.isDefined=function(e,t){var i="undefined"!=typeof e;return i&&"undefined"!=typeof t?"undefined"!=typeof e[t]:i},this.unixtime=function(e){var t=new Date;return e&&(t=new Date(e)),Math.round(t.getTime()/1e3)},this.csvToArray=function(e,t){return 0===e.length?t:"string"==typeof e?e.split(i.options.get("valuesSeparator")):e},this.debug=function(e,a){var r=i.options.get();r.debug&&(a||(a={}),e="Flexdatalist: "+e,console.warn(e),console.log($.extend({inputName:t.attr("name"),options:r},a)),console.log("--- /flexdatalist ---"))},this.init()})},jQuery(function(e){var t=e(document);t.data("flexdatalist")||e(document).mouseup(function(t){var i=e(".flexdatalist-results"),a=i.data("target");a&&a.is(":focus")||i.is(t.target)||0!==i.has(t.target).length||i.remove()}).keydown(function(t){var i=e(".flexdatalist-results"),a=i.find("li"),r=a.filter(".active"),n=r.index(),s=a.length,l=t.which||t.keyCode;if(0!==s){if(27===l)return void i.remove();if(13===l)t.preventDefault(),r.click();else if(40===l||38===l){t.preventDefault(),40===l?r=s>n&&r.nextAll(".item").first().length>0?r.removeClass("active").nextAll(".item").first().addClass("active"):a.removeClass("active").filter(".item:first").addClass("active"):38===l&&(r=n>0&&r.prevAll(".item").first().length>0?r.removeClass("active").prevAll(".item").first().addClass("active"):a.removeClass("active").filter(".item:last").addClass("active"));var o=(0===r.prev().length?r:r.prev()).position().top;i.animate({scrollTop:o+i.scrollTop()},100)}}}).data("flexdatalist",!0),jQuery("input.flexdatalist:not(.flexdatalist-set)").flexdatalist()}),function(e){var t=e.fn.val;e.fn.val=function(i){var a=e(this).hasClass("flexdatalist");return"undefined"==typeof i?a?this[0].fvalue.get(!0):t.call(this):a?this[0].fvalue.set(i):t.call(this,i)}}(jQuery);
\ No newline at end of file

redo search on focus as an option