1 line
14 KiB
JavaScript
1 line
14 KiB
JavaScript
jQuery.fn.flexdatalist=function(e,t){"use strict";var i,a=$(document),s=$(this),r=this;if(i=function(){var t=$(this),i={},a="",s=null;t.attr("name");t.hasClass("flexdatalist-set")&&r._destroy(t),t._options=function(e,i){var a=t.data("flexdatalist");if(!r._isDefined(e))return t.data("flexdatalist");if(r._isDefined(i))a[e]=i;else{if(!r._isObject(e))return r._isDefined(a,e)?a[e]:null;a=e}return a.searchIn=r._csvToArray(a.searchIn),a.relatives=a.relatives&&$(a.relatives).length>0?$(a.relatives):null,a.textProperty=null===a.textProperty?a.searchIn[0]:a.textProperty,a.visibleProperties=r._csvToArray(a.visibleProperties,a.searchIn),t.data("flexdatalist",a),t},t._options($.extend({url:null,data:[],params:{},relatives:null,chainedRelatives:!1,cache:!0,minLength:2,groupBy:!1,selectionRequired:!1,focusFirstResult:!1,textProperty:null,valueProperty:null,visibleProperties:[],searchIn:["label"],searchContain:!1,searchEqual:!1,searchDisabled:!1,normalizeString:null,multiple:t.attr("multiple"),maxShownResults:100,toggleSelected:!1,_values:[]},e,t.data()));var n=t.clone(!1).attr({list:null,name:null}).addClass("flexdatalist-alias").removeClass("flexdatalist");if(t._options("multiple")){var l=$("<ul>").addClass("flexdatalist-multiple").css({"background-color":t.css("background-color"),"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")}).insertAfter(t).click(function(){$(this).find("input").focus()});$('<li class="input-container">').addClass("flexdatalist-multiple-value").append(n).appendTo(l)}else n.insertAfter(t);t.addClass("flexdatalist").attr("type","hidden"),t._init=function(){var e=t._options();n.on("input keydown",function(i){var a=t._keyword();188===r._keyNum(i)&&!e.selectionRequired&&e.multiple?(i.preventDefault(),t._value(a),t._removeResults()):9===r._keyNum(i)&&t._removeResults()}).on("input keyup",function(i){if(t._changed()&&13!==r._keyNum(i)){var s=t._keyword();e.multiple||e.selectionRequired||t._value(s),s.length>=e.minLength?t._search(function(e){t._showResults(e)}):t._removeResults(),!e.multiple&&(0===s.length||s.length<e.minLength)&&t._value("")}a=t._keyword()}).focus(function(){var i=t._keyword();0===e.minLength?""===i&&t._tdata(function(e){t._showResults(e)}):i.length>=e.minLength&&!t._selected()&&t._search(function(e){t._showResults(e)})}).attr("autocomplete","off"),n.attr("autofocus")&&n.focus(),window.onresize=function(){t._position()},t.addClass("flexdatalist-set")},t._changed=function(){return a!==t._keyword()},t._chained=function(){var e=t._options();if(e.relatives&&e.chainedRelatives){var a=function(i){e.relatives.each(function(){var a=r._isEmpty($(this).val()),s=r._isEmpty(t.val());n.prop("disabled",a),i||!a&&s||(t._value(""),n.val(""),e.multiple&&l.find("li .remove").click())})};e.relatives.on("change",function(){a(),i={}}),a(!0)}},t._initValue=function(){var e=t.attr("value");r._isEmpty(e)||(t._options("originalValue",t.val()),t.val(""),n.val(""),t._parseValue(e,function(e){r._isEmpty(e)||t._values(e),a=t._keyword()}))},t._parseValue=function(e,i){var a=t._options();if(t._toJSON())try{i(JSON.parse(e))}catch(s){}else if(t._toCSV()||"string"==typeof a.valueProperty){var r=e.split(",");if("string"==typeof a.valueProperty){var n=a.searchIn;a.searchIn=a.valueProperty.split(","),a.searchEqual=!0,t._search(function(e){e.length>0&&i(e),a.searchIn=n,a.searchEqual=!1},r)}else i(r)}else i(e)},t._tdata=function(e){t.hasClass("flexdatalist-loading")||(t.trigger("before:flexdatalist.data"),t._url(function(i){t._data(function(a){a=a.concat(i);for(var s=t._options("_values"),r=0;r<a.length;r++){var n=a[r];s&&s.indexOf(t._getText(n))>-1&&delete a[r]}t.trigger("after:flexdatalist.data",[a]),e(a)})}))},t._data=function(e){var i=t._options();t._keyword();"string"==typeof i.data?t._remote({url:i.data,success:function(a){var s=t._remoteData(a);i.data=s,e(s)}}):e(i.data)},t._url=function(e){var i=t._options(),a=t._keyword(),n=a;return r._isEmpty(i.url)||i.minLength>a.length?e([]):(clearTimeout(s),void(s=setTimeout(function(){i.cache&&2!==i.cache&&(n=a.substring(0,i.minLength>0?i.minLength:1));var s=t._cache(n);return s?void e(s):void t._remote({url:i.url,data:$.extend(t._relativesData(),i.params,{keyword:a,contain:i.searchContain,selected:t.val()}),success:function(s){t.removeClass("flexdatalist-loading");var r=t._remoteData(s),l=t._keyword();l.length>a.length?t._search(function(e){t._showResults(e)}):l.length>=i.minLength&&e(r),t._cache(n,r)}})},200)))},t._remote=function(e){t.addClass("flexdatalist-loading"),e=$.extend({type:"post",dataType:"json",complete:function(){t.removeClass("flexdatalist-loading")}},e),$.ajax(e)},t._remoteData=function(e){var t=e.results?e.results:e;return"string"==typeof t&&0===t.indexOf("[{")&&(t=JSON.parse(t)),r._isObject(t)?t:[]},t._relativesData=function(){var e=t._options("relatives"),i={};return e&&(i.relatives={},e.each(function(){var e=$(this);i.relatives[e.attr("name")]=e.val()})),i},t._datalist=function(){var e=t._options(),i=t.attr("list");return r._isEmpty(i)||(e.data=[],$("#"+i).find("option").each(function(){var t=$(this).val();e.data.push({label:t,value:t})})),t},t._cache=function(e,a){if(t._options("cache")){if(e=t._normalizeString(e),!r._isDefined(a))return r._isDefined(i,e)&&(a=i[e]),a;i[e]=a}return null},t._search=function(e,i){t._tdata(function(a){var s=[],n=t._options();if(n.searchDisabled)return e(a);r._isDefined(i)||(i=t._keyword()),"string"==typeof i&&(i=[i]),t.trigger("before:flexdatalist.search",[i,a]);for(var l=0;l<i.length;l++)for(var o=i[l],u=0;u<a.length;u++){var d=t._matches(a[u],o,n.values);d&&s.push(d)}t.trigger("after:flexdatalist.search",[i,a,s]),e(s)})},t._matches=function(e,i){for(var a=!1,s=$.extend({},e),n=t._options(),l=n.searchIn,o=0;o<l.length;o++){var u=l[o];if(r._isDefined(e,u)){var d=e[u].toString();t._find(i,d)&&(s[u+"_highlight"]=t._highlight(i,d),a=!0)}}return a?s:null},t._highlight=function(e,i){return i.replace(new RegExp(e,t._options("searchContain")?"ig":"i"),'<span class="highlight">$&</span>')},t._find=function(e,i){var a=t._options();return i=t._normalizeString(i),e=t._normalizeString(e),a.searchEqual?i==e:a.searchContain?i.indexOf(e)>=0:0===i.indexOf(e)},t._showResults=function(e){t._removeResults();var i=t._getResultsContainer(),a=t._options();if(0!==e.length){a.groupBy?(e=t._groupData(e),Object.keys(e).forEach(function(s){var r=e[s],n=a.groupBy,l=t._getHighlight(r[0],n,s);$("<li>").addClass("group").append($("<span>").addClass("group-name").html(l)).append($("<span>").addClass("group-item-count").text(" "+r.length)).appendTo(i);t._items(r,i)})):t._items(e,i);var s=i.find("li:not(.group)");s.on("click",function(){var e=$(this).data("item");e&&(t._selected(!0)._removeResults()._value(e),t.trigger("select:flexdatalist",[e,a]))}).hover(function(){s.removeClass("active"),$(this).addClass("active")},function(){$(this).removeClass("active")}),a.focusFirstResult&&s.filter(":first").addClass("active"),t._position()}},t._groupData=function(e){for(var i=[],a=t._options("groupBy"),s=0;s<e.length;s++){var n=e[s];if(r._isDefined(n,a)){var l=n[a];r._isDefined(i,l)||(i[l]=[]),i[l].push(n)}}return i},t._items=function(e,i){var a=t._options("maxShownResults");t.trigger("show:flexdatalist.results",[e]);for(var s=0;s<e.length&&!(a>0&&a===s);s++)t._item(e[s]).appendTo(i);t.trigger("shown:flexdatalist.results",[e])},t._item=function(e){for(var i=$("<li>").data("item",e).addClass("item"),a=t._options(),s=a.visibleProperties,n=0;n<s.length;n++){var l=s[n];if((!a.groupBy||a.groupBy!==l)&&r._isDefined(e,l)){var o={};if("thumb"===l)o=$("<img>").addClass("item item-"+l).attr("src",e[l]);else{var u=t._getHighlight(e,l);o=$("<span>").addClass("item item-"+l).html(u+" ")}o.appendTo(i)}}return i},t._getHighlight=function(e,t,i){return r._isDefined(e,t+"_highlight")?e[t+"_highlight"]:r._isDefined(e,t)?e[t]:i},t._getResultsContainer=function(){var e=t;t._options("multiple")&&(e=l);var i=$("ul.flexdatalist-results");return 0===i.length&&(i=$("<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",n)),i},t._removeResults=function(){return $("ul.flexdatalist-results").remove(),t},t._selected=function(e){var i="flexdatalist-selected";return r._isDefined(e)?(e?t.addClass(i):t.removeClass(i),t):t.hasClass(i)},t._values=function(e){return $.isArray(e)&&!r._isEmpty(e)?void $.each(e,function(e,i){t._value(i)}):void t._value(e)},t._value=function(e){var i=t._options(),s=t._getText(e),r=t._getValue(e);if(s.length>0&&i._values.push(s),i.multiple){if(""===e)return t;n.val("");var o=$("<li>").addClass("value"+(i.toggleSelected?" toggle":"")).append('<span class="text">'+s+"</span>").append('<span class="fdl-remove">×</span>').insertBefore(l.find("li.input-container"));o.find("span.fdl-remove").click(function(){var e=$(this).parent(),a=e.index();if(!e.hasClass("disabled")&&(t._toJSON()||t._toCSV())){var s=t._inputValue();s.splice(a,1),i._values.splice(a,1),t._inputValue(s)}e.remove()}),i.toggleSelected&&o.click(function(){var e=$(this),a=t._inputValue(),s=e.index();if(e.hasClass("disabled")){var r=e.data("_value");a.splice(s,0,r),i._values.splice(s,0,t._getText(r)),e.removeClass("disabled")}else{var r=a.splice(s,1);e.data("_value",r[0]),i._values.splice(s,1),e.addClass("disabled")}t._inputValue(a)})}else s&&s!==n.val()&&n.val(s);return t._inputValue(r,s),a=t._keyword(),t},t._inputValue=function(e,i){var a=t._toJSON(),s=t._toCSV();return r._isDefined(e)?(r._isObject(e)&&(a&&!r._isEmpty(e)?e=JSON.stringify(e):s&&(e=e.join(","))),""===e&&t._options("_values",[]),t.val(e),t.trigger("change:flexdatalist",[e,i,t._options()]).trigger("change"),e):(e=t.val(),e?a?e=JSON.parse(e):s&&(e=e.split(",")):(a||s)&&(e=[]),e)},t._getText=function(e){var i=e,a=t._options();return r._isObject(e)&&(i=e[a.searchIn[0]],i=r._isDefined(e,a.textProperty)?e[a.textProperty]:t._replacePlaceholders(e,a.textProperty,i)),$("<div>").html(i).text()},t._getValue=function(e){var i=e,a=t._options();if(r._isObject(e))if(i=e[a.searchIn[0]],"*"===a.valueProperty)i=e;else if(r._isDefined(e,a.valueProperty))i=e[a.valueProperty];else if(t._toJSON()){var i={},s=a.valueProperty,n=a.textProperty;if(n){var l=n;"string"==typeof n&&(l=t._parsePlaceholders(n)),r._isObject(l)&&$.each(l,function(e,t){s.push(t)})}else r._isDefined(e,n)&&s.push(n);$.each(s,function(t,a){r._isDefined(e,a)&&(i[a]=e[a])})}if(a.multiple&&(t._toJSON()||t._toCSV())){var o=t._inputValue();!r._isEmpty(i)&&r._isObject(o)&&(o.push(i),i=o)}return i},t._replacePlaceholders=function(e,i,a){if(r._isObject(e)&&"string"==typeof i){var s=t._parsePlaceholders(i);if(!r._isEmpty(e)&&s)return $.each(s,function(t,a){r._isDefined(e,a)&&(i=i.replace(t,e[a]))}),i}return a},t._parsePlaceholders=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},t._normalizeString=function(e){if("string"==typeof e){var i=t._options("normalizeString");return"function"==typeof i&&(e=i(e)),e.toUpperCase()}return e},t._keyword=function(){return n.val().replace(/^\s+/,"")},t._toJSON=function(){var e=t._options("valueProperty");return r._isObject(e)||"*"===e},t._toCSV=function(){return!t._toJSON()&&t._options("multiple")},t._position=function(){var e=n;t._options("multiple")&&(e=l),$("ul.flexdatalist-results").css({width:e.outerWidth()+"px",top:e.offset().top+e.outerHeight()+"px",left:e.offset().left+"px","z-index":e.css("z-index")+1})},t._datalist(),t._init(),t._initValue(),t._chained(),a=t._keyword()},this._destroy=function(e){e||(e=s),e.each(function(){var e=$(this).data("flexdatalist");$(this).removeClass("flexdatalist-set").off().attr("type","text").val(e&&e.originalValue?e.originalValue:"").data("flexdatalist",null).next(".flexdatalist-alias, ul.flexdatalist-multiple").remove()})},this._reset=function(){this._destroy()},"string"==typeof e)if("function"==typeof this["_"+e]){if(!this["_"+e]())return this}else{if(!t){var n=s.data("flexdatalist");return n[e]}if(t){var n=s.data("flexdatalist");return n[e]=t,s.data("flexdatalist",n),this}}return this._keyNum=function(e){return e.which||e.keyCode},a.data("flexdatalist")||$(document).mouseup(function(e){var t=$(".flexdatalist-results"),i=t.data("target");i&&i.is(":focus")||t.is(e.target)||0!==t.has(e.target).length||t.remove()}).keydown(function(e){var t=$(".flexdatalist-results"),i=t.find("li"),a=i.filter(".active"),s=a.index(),n=i.length,l=r._keyNum(e);if(0!==n)if(13===l)e.preventDefault(),a.click();else if(40===l||38===l){e.preventDefault(),40===l?a=n>s&&a.nextAll(".item").first().length>0?a.removeClass("active").nextAll(".item").first().addClass("active"):i.removeClass("active").filter(".item:first").addClass("active"):38===l&&(a=s>0&&a.prevAll(".item").first().length>0?a.removeClass("active").prevAll(".item").first().addClass("active"):i.removeClass("active").filter(".item:last").addClass("active"));var o=(0===a.prev().length?a:a.prev()).position().top;t.animate({scrollTop:o+t.scrollTop()},100)}}).data("flexdatalist",!0),this._isEmpty=function(e){return r._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._csvToArray=function(e,t){return 0===e.length?t:"string"==typeof e?e.split(","):e},this._length=function(e){return this._isObject(e)?Object.keys(e).length:"number"==typeof e.length?e.length:0},this._isDefined=function(e,t){var i="undefined"!=typeof e;return i&&"undefined"!=typeof t?"undefined"!=typeof e[t]:i},this.each(i)},$(function(){$("input.flexdatalist").flexdatalist()}); |