diff --git a/jquery.flexdatalist.js b/jquery.flexdatalist.js index d1726d7..44d12a6 100644 --- a/jquery.flexdatalist.js +++ b/jquery.flexdatalist.js @@ -3,7 +3,7 @@ * Autocomplete input fields, with support for datalists. * * Version: - * 2.1.2.1 + * 2.1.2.2 * * Depends: * jquery.js > 1.8.3 @@ -170,12 +170,16 @@ jQuery.fn.flexdatalist = function (_option, _value) { _this.action.copyValue(event); _this.action.backSpaceKeyRemove(event); _this.action.showAllResults(event); + _this.action.clearValue(event); + _this.action.removeResults(event); }) // Focusout - .on('focusout', function () { + .on('focusout', function (event) { if ($multiple) { $multiple.removeClass('focus'); } + _this.action.clearText(event); + _this.action.clearValue(event); }); window.onresize = function (event) { @@ -202,7 +206,8 @@ jQuery.fn.flexdatalist = function (_option, _value) { var key = _this.keyNum(event), val = $alias[0].value, options = _this.options.get(); - if (val.length > 0 && key === keyCode + if (val.length > 0 + && key === keyCode && !options.selectionRequired && options.multiple) { var val = $alias[0].value; @@ -221,17 +226,7 @@ jQuery.fn.flexdatalist = function (_option, _value) { options = _this.options.get(); clearTimeout(_searchTimeout); - - // Ignore TAB and Shift - // Prevent closing results when navigating (in and out of focus) with tabs - if (options.minLength === 0 && length === 0 && (key === 9 || key === 16)) { - return; - } - if ((length === 0 && options.minLength > 0) || length < options.minLength) { - _this.results.remove(); - // Ignore Enter and Directional keys - } else if (!key || (key !== 13 && (key < 37 || key > 40))) { - _this.results.remove(); + if (!key || (key !== 13 && (key < 37 || key > 40))) { _searchTimeout = setTimeout(function () { if ((options.minLength === 0 && length > 0) || (options.minLength > 0 && length >= options.minLength)) { _this.data.load(function (data) { @@ -261,13 +256,8 @@ jQuery.fn.flexdatalist = function (_option, _value) { if (_this.keyNum(event) !== 13) { var keyword = $alias.val(), options = _this.options.get(); - if (!options.multiple) { - if (!options.selectionRequired) { - _this.fvalue.extract(keyword); - } - if (keyword.length === 0) { - _this.fvalue.clear(); - } + if (!options.multiple && !options.selectionRequired) { + _this.fvalue.extract(keyword); } } }, @@ -298,6 +288,40 @@ jQuery.fn.flexdatalist = function (_option, _value) { _this.results.show(data); }); } + }, + /** + * Clear text/alias input when criteria is met. + */ + clearText: function (event) { + var val = _this.fvalue.get(), + options = _this.options.get(); + + if (!options.multiple && options.selectionRequired && val.length === 0) { + $alias[0].value = ''; + } + }, + /** + * Clear value when criteria is met. + */ + clearValue: function (event) { + var val = _this.fvalue.get(), + keyword = $alias.val(), + options = _this.options.get(); + + if (!options.multiple && keyword.length <= options.minLength) { + _this.fvalue.clear(); + } + }, + /** + * Remove results when criteria is met. + */ + removeResults: function (event) { + var val = _this.fvalue.get(), + keyword = $alias.val(), + options = _this.options.get(); + if (options.minLength > 0 && keyword.length < options.minLength) { + _this.results.remove(); + } } } diff --git a/jquery.flexdatalist.min.js b/jquery.flexdatalist.min.js index a23f5cf..6bd7e7b 100644 --- a/jquery.flexdatalist.min.js +++ b/jquery.flexdatalist.min.js @@ -1,2 +1,2 @@ -/** jQuery.Flexdatalist 2.1.2 **/ -jQuery.fn.flexdatalist=function(e,t){"use strict";var i=function(e,t){e.each(function(){var e=$(this),i=e.data(),a=i.flexdatalist,r=i.aliascontainer;r&&(e.removeClass("flexdatalist-set").attr("type","text").val(a&&a.originalValue&&!t?a.originalValue:"").removeData("flexdatalist").removeData("aliascontainer"),r.remove())})};if("string"==typeof e&&"reset"!==e){if("undefined"!=typeof this[0].fvalue){var a=this[0];if("destroy"===e)i(this,t);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}e={_option:t}}this.length>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(),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),i.action.showAllResults(e)}).on("focusout",function(){o&&o.removeClass("focus")}),window.onresize=function(){i.position()},this.fvalue._load(e.originalValue,function(){e.selectionRequired&&i.fvalue.clear(!0,!0),i.fdisabled(e.disabled),t.trigger("init:flexdatalist",[e])},!0)},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||nt||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)})})},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.extract(t),0===t.length&&i.fvalue.clear())}},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(),t.data("aliascontainer",o?o:l),this.chained()},alias:function(){var e=t.attr("id")?t.attr("id")+"-flexdatalist":s,i=t.clone(!1).attr({list:null,name:t.attr("name")?"flexdatalist-"+t.attr("name"):null,id:e,value:""}).addClass("flexdatalist-alias "+e).removeClass("flexdatalist").attr("autocomplete","off");return t.addClass("flexdatalist flexdatalist-set").prop("type","hidden"),i},multipleInput:function(i){return o=$('