diff --git a/jquery.flexdatalist.js b/jquery.flexdatalist.js index da3dad4..c18b5e4 100644 --- a/jquery.flexdatalist.js +++ b/jquery.flexdatalist.js @@ -3,7 +3,7 @@ * Autocomplete input fields, with support for datalists. * * Version: - * 2.0.3 + * 2.0.4 * * Depends: * jquery.js > 1.8.3 @@ -544,10 +544,19 @@ jQuery.fn.flexdatalist = function (_option, _value) { return; } $li = this.findLi($li); - $this.trigger('before:flexdatalist.toggle', [$li.data('value'), options]); var index = $li.index(), + data = $li.data(), + action = $li.hasClass('disabled') ? 'enable' : 'disable', current = _this.fvalue.get(); - if ($li.hasClass('disabled')) { + + $this.trigger('before:flexdatalist.toggle', [ + action, + data.value, + data.text, + options + ]); + + if (action === 'enable') { var value = $li.data('value'); current.splice(index, 0, value); $li.removeClass('disabled'); @@ -555,14 +564,20 @@ jQuery.fn.flexdatalist = function (_option, _value) { current.splice(index, 1); $li.addClass('disabled'); } + current = _this.fvalue.toStr(current); _this.value = current; $this - .trigger('after:flexdatalist.toggle', [$li.data('value'), options]) + .trigger('after:flexdatalist.toggle', [ + action, + data.value, + data.text, + options + ]) .trigger('change:flexdatalist', [ - $li.data('value'), - $li.data('text'), + data.value, + data.text, options ]).trigger('change'); }, @@ -573,21 +588,25 @@ jQuery.fn.flexdatalist = function (_option, _value) { $li = this.findLi($li); var values = _this.fvalue.get(), index = $li.index(), - options = _this.options.get(); + data = $li.data(), + options = _this.options.get(), + args = [ + data.value, + data.text, + options + ]; - $this.trigger('before:flexdatalist.remove', [$li.data('value'), options]); + $this.trigger('before:flexdatalist.remove', args); + var val = values.splice(index, 1); values = _this.fvalue.toStr(values); $this[0].value = values; $li.remove(); $this - .trigger('after:flexdatalist.remove', [$li.data('value'), options]) - .trigger('change:flexdatalist', [ - $li.data('value'), - $li.data('text'), - options - ]).trigger('change'); + .trigger('after:flexdatalist.remove', args) + .trigger('change:flexdatalist', args) + .trigger('change'); // For allowDuplicateValues _values.splice(index, 1); diff --git a/jquery.flexdatalist.min.js b/jquery.flexdatalist.min.js index 03b035b..a306629 100644 --- a/jquery.flexdatalist.min.js +++ b/jquery.flexdatalist.min.js @@ -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("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,maxShownResults:100,removeOnBackspace:!0,noResultsText:'No results found for "{keyword}"',toggleSelected:!1,allowDuplicateValues:!1,requestType:"get",requestContentType:"default",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(){this.options.init(),this.set.up(),this.fvalue.set(t.attr("value")),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),i.action.backSpaceKeyRemove(e)}).on("input keyup",function(e){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){var t=i.keyNum(e),a=l[0].value,r=i.options.get();if(a.length>0&&(188===t||13===t)&&!r.selectionRequired&&r.multiple){var a=l[0].value;e.preventDefault(),i.fvalue.extract(a),i.results.remove()}},keypressSearch:function(e){var t=i.keyNum(e),r=l.val(),n=r.length,s=i.options.get();clearTimeout(a),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.search.get(function(e){i.results.show(e)}):i.results.remove()},s.searchDelay))},redoSearchFocus:function(e){var t=(i.fvalue.get(),l.val());t.length>0&&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.set(t))}},backSpaceKeyRemove:function(e){var t=i.options.get();if(t.removeOnBackspace){var a=l.val(),r=l.data("_remove");r?(r.find(".fdl-remove").click(),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=$('