From 43b97d6e17d5381e100de18fc8f5c277fc657a63 Mon Sep 17 00:00:00 2001 From: pawelczak Date: Tue, 27 Oct 2015 00:49:43 +0100 Subject: [PATCH] Closes #112. Added parameter hideOnEmptyPhrase --- demo/example_theme_square.html | 12 +-- dist/easy-autocomplete.css | 2 +- dist/easy-autocomplete.min.css | 2 +- dist/easy-autocomplete.themes.css | 2 +- dist/easy-autocomplete.themes.min.css | 2 +- dist/jquery.easy-autocomplete.js | 33 +++++--- dist/jquery.easy-autocomplete.min.js | 4 +- src/configuration.js | 3 +- src/core.js | 30 ++++--- test/core/build.js | 111 ++++++++++++++++++++++++++ 10 files changed, 160 insertions(+), 41 deletions(-) diff --git a/demo/example_theme_square.html b/demo/example_theme_square.html index f7f890e..82e5f2c 100644 --- a/demo/example_theme_square.html +++ b/demo/example_theme_square.html @@ -29,30 +29,24 @@

EasyAutocomplete - css themes

getValue: "name", + minCharNumber: 0, + list: { match: { enabled: true }, maxNumberOfElements: 8, - onClickEvent: function() { - $("#square").change(); - } + hideOnEmptyPhrase: true }, - theme: "square" }; $("#square").easyAutocomplete(options); - - $("#square").change(function() { - console.log($(this).val()); - }); - diff --git a/dist/easy-autocomplete.css b/dist/easy-autocomplete.css index d807da6..d823a83 100644 --- a/dist/easy-autocomplete.css +++ b/dist/easy-autocomplete.css @@ -3,7 +3,7 @@ * jQuery plugin for autocompletion * * @author Łukasz Pawełczak (http://github.com/pawelczak) - * @version 1.1.6 + * @version 1.2.0 * Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt */ diff --git a/dist/easy-autocomplete.min.css b/dist/easy-autocomplete.min.css index ead8c3c..d576086 100644 --- a/dist/easy-autocomplete.min.css +++ b/dist/easy-autocomplete.min.css @@ -3,7 +3,7 @@ * jQuery plugin for autocompletion * * @author Łukasz Pawełczak (http://github.com/pawelczak) - * @version 1.1.6 + * @version 1.2.0 * Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt */ diff --git a/dist/easy-autocomplete.themes.css b/dist/easy-autocomplete.themes.css index 3442d6f..b07a4a5 100644 --- a/dist/easy-autocomplete.themes.css +++ b/dist/easy-autocomplete.themes.css @@ -3,7 +3,7 @@ * jQuery plugin for autocompletion * * @author Łukasz Pawełczak (http://github.com/pawelczak) - * @version 1.1.6 + * @version 1.2.0 * Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt */ diff --git a/dist/easy-autocomplete.themes.min.css b/dist/easy-autocomplete.themes.min.css index 6e9194e..03bef52 100644 --- a/dist/easy-autocomplete.themes.min.css +++ b/dist/easy-autocomplete.themes.min.css @@ -3,7 +3,7 @@ * jQuery plugin for autocompletion * * @author Łukasz Pawełczak (http://github.com/pawelczak) - * @version 1.1.6 + * @version 1.2.0 * Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt */ diff --git a/dist/jquery.easy-autocomplete.js b/dist/jquery.easy-autocomplete.js index 60b6d53..0966be2 100644 --- a/dist/jquery.easy-autocomplete.js +++ b/dist/jquery.easy-autocomplete.js @@ -3,7 +3,7 @@ * jQuery plugin for autocompletion * * @author Łukasz Pawełczak (http://github.com/pawelczak) - * @version 1.1.6 + * @version 1.2.0 * Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt */ @@ -36,7 +36,6 @@ var EasyAutocomplete = (function(scope){ matchResponseProperty: false, - list: { sort: { enabled: false, @@ -55,6 +54,8 @@ var EasyAutocomplete = (function(scope){ maxNumberOfElements: 6, + hideOnEmptyPhrase: true, + match: { enabled: false, caseSensitive: false, @@ -1228,7 +1229,15 @@ var EasyAutocomplete = (function(scope) { default: if (event.keyCode > 40 || event.keyCode === 8) { - loadData(); + + var inputPhrase = $field.val(); + + if (!(config.get("list").hideOnEmptyPhrase === true && event.keyCode === 8 && inputPhrase === "")) { + loadData(inputPhrase); + } else { + hideContainer(); + } + } @@ -1236,9 +1245,7 @@ var EasyAutocomplete = (function(scope) { } - function loadData() { - - var inputPhrase = $field.val(); + function loadData(inputPhrase) { if (inputPhrase.length < config.get("minCharNumber")) { @@ -1361,15 +1368,15 @@ var EasyAutocomplete = (function(scope) { }) .keydown(function(event) { - if (event.keyCode === 13 && selectedElement > -1) { + if (event.keyCode === 13 && selectedElement > -1) { - $field.val(config.get("getValue")(elementsList[selectedElement])); - selectedElement = -1; - hideContainer(); + $field.val(config.get("getValue")(elementsList[selectedElement])); + selectedElement = -1; + hideContainer(); - event.preventDefault(); - } - }); + event.preventDefault(); + } + }); } function bindKeypress() { diff --git a/dist/jquery.easy-autocomplete.min.js b/dist/jquery.easy-autocomplete.min.js index c15348d..3fca8c2 100644 --- a/dist/jquery.easy-autocomplete.min.js +++ b/dist/jquery.easy-autocomplete.min.js @@ -3,8 +3,8 @@ * jQuery plugin for autocompletion * * @author Łukasz Pawełczak (http://github.com/pawelczak) - * @version 1.1.6 + * @version 1.2.0 * Copyright MIT License: https://github.com/pawelczak/easy-autocomplete/blob/master/LICENSE.txt */ -var EasyAutocomplete=function(a){return a.Configuration=function(a){function b(){"xml"===a.dataType&&(a.getValue||(a.getValue=function(a){return $(a).text()}),a.list||(a.list={}),a.list.sort||(a.list.sort={}),a.list.sort.method=function(b,c){return b=a.getValue(b),c=a.getValue(c),c>b?-1:b>c?1:0},a.list.match||(a.list.match={}),a.list.match.method=function(b,c){return b=a.getValue(b),c=a.getValue(c),b===c?!0:!1})}function c(){function b(a,c){var d=a||{};for(var e in a)void 0!==c[e]&&null!==c[e]&&("object"!=typeof c[e]||c[e]instanceof Array?d[e]=c[e]:b(a[e],c[e]));return void 0!==c.data&&null!==c.data&&"object"==typeof c.data&&(d.data=c.data),d}h=b(h,a)}function d(){if("list-required"!==h.url&&"function"!=typeof h.url){var b=h.url;h.url=function(){return b}}if(void 0!==h.ajaxSettings.url&&"function"!=typeof h.ajaxSettings.url){var b=h.ajaxSettings.url;h.ajaxSettings.url=function(){return b}}if("string"==typeof h.listLocation){var c=h.listLocation;h.listLocation="XML"===h.dataType.toUpperCase()?function(a){return $(a).find(c)}:function(a){return a[c]}}if("string"==typeof h.getValue){var d=h.getValue;h.getValue=function(a){return a[d]}}void 0!==a.categories&&(h.categoriesAssigned=!0)}function e(){h.ajaxSettings=void 0!==a.ajaxSettings&&"object"==typeof a.ajaxSettings?a.ajaxSettings:{}}function f(a){return void 0!==h[a]&&null!==h[a]?!0:!1}function g(a,b){function c(b,e){for(var f in e)void 0===b[f]&&a.log("Property '"+f+"' does not exist in EasyAutocomplete options API."),"object"!=typeof b[f]||d(f)||c(b[f],e[f])}function d(a){var b=["ajaxSettings","template"];return Array.prototype.contains=function(a){for(var b=this.length;b--;)if(this[b]===a)return!0;return!1},b.contains(a)}c(h,b)}var h={data:"list-required",url:"list-required",dataType:"json",listLocation:function(a){return a},xmlElementName:"",getValue:function(a){return a},autocompleteOff:!0,placeholder:!1,ajaxCallback:function(){},matchResponseProperty:!1,list:{sort:{enabled:!1,method:function(a,b){return a=h.getValue(a),b=h.getValue(b),b>a?-1:a>b?1:0}},maxNumberOfElements:6,match:{enabled:!1,caseSensitive:!1,method:function(a,b){return a=h.getValue(a),b=h.getValue(b),a===b?!0:!1}},showAnimation:{type:"normal",time:400,callback:function(){}},hideAnimation:{type:"normal",time:400,callback:function(){}},onClickEvent:function(){},onSelectItemEvent:function(){},onLoadEvent:function(){},onMouseOverEvent:function(){},onMouseOutEvent:function(){},onShowListEvent:function(){},onHideListEvent:function(){}},highlightPhrase:!0,theme:"",cssClasses:"",minCharNumber:0,ajaxSettings:{},loggerEnabled:!0,template:"",categoriesAssigned:!1,categories:[{listLocation:""}]};this.get=function(a){return h[a]},this.equals=function(a,b){return f(a)&&h[a]===b?!0:!1},this.checkDataUrlProperties=function(){return"list-required"===h.url&&"list-required"===h.data?!1:!0},this.checkRequiredProperties=function(){for(var a in h)if("required"===h[a])return logger.error("Option "+a+" must be defined"),!1;return!0},this.printPropertiesThatDoesntExist=function(a,b){g(a,b)},b(),c(),h.loggerEnabled===!0&&g(console,a),e(),d()},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Logger=function(){this.error=function(a){console.log("ERROR: "+a)},this.warning=function(a){console.log("WARNING: "+a)}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Constans=function(){var a={CONTAINER_CLASS:"easy-autocomplete-container",CONTAINER_ID:"eac-container-",WRAPPER_CSS_CLASS:"easy-autocomplete"};this.getValue=function(b){return a[b]}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.ListBuilderService=function(a,b){function c(b,c){function d(){var d,e={};return void 0!==b.xmlElementName&&(e.xmlElementName=b.xmlElementName),void 0!==b.listLocation?d=b.listLocation:void 0!==a.get("listLocation")&&(d=a.get("listLocation")),void 0!==d?"string"==typeof d?e.data=$(c).find(d):"function"==typeof d&&(e.data=d(c)):e.data=c,e}function e(){var a={};return void 0!==b.listLocation?"string"==typeof b.listLocation?a.data=c[b.listLocation]:"function"==typeof b.listLocation&&(a.data=b.listLocation(c)):a.data=c,a}var f={};if(f="XML"===a.get("dataType").toUpperCase()?d():e(),void 0!==b.header&&(f.header=b.header),void 0!==b.getValue)if("string"==typeof b.getValue){var g=b.getValue;f.getValue=function(a){return a[g]}}else"function"==typeof b.getValue&&(f.getValue=b.getValue);else f.getValue=a.get("getValue");return f}function d(b){var c=[];return void 0===b.xmlElementName&&(b.xmlElementName=a.get("xmlElementName")),$(b.data).find(b.xmlElementName).each(function(){c.push(this)}),c}this.init=function(b){var c=[],d={};return d.data=a.get("listLocation")(b),d.getValue=a.get("getValue"),c.push(d),c},this.updateCategories=function(b,d){if(a.get("categoriesAssigned")){b=[];for(var e=0;ee;e+=1)c[e].data=b(a,c[e].data,d);return c},this.checkIfDataExists=function(a){for(var b=0,c=a.length;c>b;b+=1)if(void 0!==a[b].data&&a[b].data instanceof Array&&a[b].data.length>0)return!0;return!1}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.proccess=function(a,b,c){function d(b,c){var d=[],e="";if(a.get("list").match.enabled)for(var f=0,g=b.length;g>f;f+=1)e=a.get("getValue")(b[f]),a.get("list").match.caseSensitive||("string"==typeof e&&(e=e.toLowerCase()),c=c.toLowerCase()),e.search(c)>-1&&d.push(b[f]);else d=b;return d}function e(b){return b.length>a.get("list").maxNumberOfElements&&(b=b.slice(0,a.get("list").maxNumberOfElements)),b}function f(b){return a.get("list").sort.enabled&&b.sort(a.get("list").sort.method),b}var g=c;return b=d(b,g),b=e(b),b=f(b)},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Template=function(a){var b={basic:{type:"basic",method:function(a){return a}},description:{type:"description",fields:{description:"description"},method:function(a){return a+" - description"},cssClass:"eac-description"},iconLeft:{type:"iconLeft",fields:{icon:""},method:function(a){return a},cssClass:"eac-icon-left"},iconRight:{type:"iconRight",fields:{iconSrc:""},method:function(a){return a},cssClass:"eac-icon-right"},links:{type:"links",fields:{link:""},method:function(a){return a}},custom:{type:"custom",method:function(){}}},c=function(a){var c=a.fields;if("description"===a.type){var d=function(a,b){return a+" - "+b[c.description]+""};return d}if("iconRight"===a.type){var d="";return"string"==typeof c.iconSrc?d=function(a,b){return a+""}:"function"==typeof c.iconSrc&&(d=function(a,b){return a+""}),d}if("iconLeft"===a.type){var d="";return"string"==typeof c.iconSrc?d=function(a,b){return""+a}:"function"==typeof c.iconSrc&&(d=function(a,b){return""+a}),d}if("links"===a.type){var d="";return"string"==typeof c.link?d=function(a,b){return""+a+""}:"function"==typeof c.link&&(d=function(a,b){return""+a+""}),d}return"custom"===a.type?a.method:b.basic.method};prepareBuildMethod=function(a){return a&&a.type&&a.type&&b[a.type]?c(a):b.basic.method},templateClass=function(a){var c=function(){return""};return a&&a.type&&a.type&&b[a.type]?function(){var c=b[a.type].cssClass;return function(){return c}}():c},this.getTemplateClass=templateClass(a),this.build=prepareBuildMethod(a)},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.main=function(b,c){function d(){return 0===t.length?void p.error("Input field doesn't exist."):o.checkDataUrlProperties()?o.checkRequiredProperties()?(e(),void g()):void p.error("Will not work without mentioned properties."):void p.error("One of options variables 'data' or 'url' must be defined.")}function e(){function a(){var a=$("
"),c=n.getValue("WRAPPER_CSS_CLASS");o.get("theme")&&(c+=" eac-"+o.get("theme")),o.get("cssClasses")&&(c+=" "+o.get("cssClasses")),""!==q.getTemplateClass()&&(c+=" "+q.getTemplateClass()),a.addClass(c),t.wrap(a),b()}function b(){var a=t.outerWidth();t.parent().css("width",a)}function c(){t.unwrap()}function d(){var a=$("
").addClass(n.getValue("CONTAINER_CLASS"));a.attr("id",f()).prepend($("
    ")),function(){a.on("show",function(){switch(o.get("list").showAnimation.type){case"slide":var b=o.get("list").showAnimation.time,c=o.get("list").showAnimation.callback;a.find("ul").slideDown(b,c);break;case"fade":var b=o.get("list").showAnimation.time,c=o.get("list").showAnimation.callback;a.find("ul").fadeIn(b),c;break;default:a.find("ul").show()}o.get("list").onShowListEvent()}).on("hide",function(){switch(o.get("list").hideAnimation.type){case"slide":var b=o.get("list").hideAnimation.time,c=o.get("list").hideAnimation.callback;a.find("ul").slideUp(b,c);break;case"fade":var b=o.get("list").hideAnimation.time,c=o.get("list").hideAnimation.callback;a.find("ul").fadeOut(b,c);break;default:a.find("ul").hide()}o.get("list").onHideListEvent()}).on("selectElement",function(){a.find("ul li").removeClass("selected"),a.find("ul li").eq(w).addClass("selected"),o.get("list").onSelectItemEvent()}).on("loadElements",function(b,c,d){var e="",f=($("
      "),a.find("ul"));f.empty().detach(),v=[];for(var h=0,i=0,k=c.length;k>i;i+=1){var l=c[i].data;if(0!==l.length){void 0!==c[i].header&&c[i].header.length>0&&f.append("
      "+c[i].header+"
      ");for(var m=0,n=l.length;n>m;m+=1)e=$("
    • "),function(){var a=m,b=h,f=c[i].getValue(l[a]);e.find(" > div").on("click",function(){t.val(f).trigger("change"),w=b,j(b),o.get("list").onClickEvent()}).mouseover(function(){w=b,j(b),o.get("list").onMouseOverEvent()}).mouseout(function(){o.get("list").onMouseOutEvent()}).html(q.build(g(f,d),l[a]))}(),f.append(e),v.push(l[m]),h+=1}}a.append(f),o.get("list").onLoadEvent()})}(),t.after(a)}function e(){t.next("."+n.getValue("CONTAINER_CLASS")).remove()}function g(a,b){return o.get("highlightPhrase")&&""!==b?h(a,b):a}function h(a,b){return(a+"").replace(new RegExp("("+b+")","gi"),"$1")}t.parent().hasClass(n.getValue("WRAPPER_CSS_CLASS"))&&(e(),c()),a(),d(),u=$("#"+f()),o.get("placeholder")&&t.attr("placeholder",o.get("placeholder"))}function f(){var a=t.attr("id");if(void 0===a||null===a){var b="";do b=m.shortcut+"-"+Math.floor(1e4*Math.random());while(0!==$("#"+b).length);a=n.getValue("CONTAINER_ID")+b,t.attr("id",b)}else a=n.getValue("CONTAINER_ID")+a;return a}function g(){function a(){s("autocompleteOff",!0)&&g(),b(),c(),d(),e(),f()}function b(){t.off("keyup").keyup(function(a){function b(){function a(){var a=new Object,b=o.get("ajaxSettings")||{};for(set in b)a[set]=b[set];return a}function b(a,b){return o.get("matchResponseProperty")!==!1?"string"==typeof o.get("matchResponseProperty")?b[o.get("matchResponseProperty")]==a:"function"==typeof o.get("matchResponseProperty")?o.get("matchResponseProperty")(b)===a:!0:!0}var c=t.val();if(!(c.length0&&w>0&&(w-=1,t.val(o.get("getValue")(v[w])),j(w));break;case 40:a.preventDefault(),v.length>0&&w40||8===a.keyCode)&&b()}})}function c(){t.on("keydown",function(a){a=a||window.event;var b=a.keyCode;return 38===b?(suppressKeypress=!0,!1):void 0}).keydown(function(a){13===a.keyCode&&w>-1&&(t.val(o.get("getValue")(v[w])),w=-1,i(),a.preventDefault())})}function d(){t.off("keypress")}function e(){t.focus(function(){""!==t.val()&&v.length>0&&(w=-1,h())})}function f(){t.blur(function(){setTimeout(function(){w=-1,i()},250)})}function g(){t.attr("autocomplete","off")}a()}function h(){u.trigger("show")}function i(){u.trigger("hide")}function j(a){u.trigger("selectElement",a)}function k(a,b){u.trigger("loadElements",[a,b])}function l(){t.trigger("blur")}var m={name:"EasyAutocomplete",shortcut:"eac"},n=new a.Constans,o=new a.Configuration(c),p=new a.Logger,q=new a.Template(c.template),r=new a.ListBuilderService(o,a.proccess),s=(a.proccess,o.equals),t=b,u="",v=[],w=-1;this.getConstants=function(){return n},this.getConfiguration=function(){return o},this.getContainer=function(){return u},this.getSelectedItemIndex=function(){return w},this.getItemData=function(a){return v.length-1&&void 0!==$.fn.easyAutocompleteHandles[b]?$.fn.easyAutocompleteHandles[b].getItemData(a):-1},$.fn.getSelectedItemData=function(){var a=$(this).attr("id");return void 0!==a&&void 0!==$.fn.easyAutocompleteHandles[a]?$.fn.easyAutocompleteHandles[a].getSelectedItemData():-1}; \ No newline at end of file +var EasyAutocomplete=function(a){return a.Configuration=function(a){function b(){"xml"===a.dataType&&(a.getValue||(a.getValue=function(a){return $(a).text()}),a.list||(a.list={}),a.list.sort||(a.list.sort={}),a.list.sort.method=function(b,c){return b=a.getValue(b),c=a.getValue(c),c>b?-1:b>c?1:0},a.list.match||(a.list.match={}),a.list.match.method=function(b,c){return b=a.getValue(b),c=a.getValue(c),b===c?!0:!1})}function c(){function b(a,c){var d=a||{};for(var e in a)void 0!==c[e]&&null!==c[e]&&("object"!=typeof c[e]||c[e]instanceof Array?d[e]=c[e]:b(a[e],c[e]));return void 0!==c.data&&null!==c.data&&"object"==typeof c.data&&(d.data=c.data),d}h=b(h,a)}function d(){if("list-required"!==h.url&&"function"!=typeof h.url){var b=h.url;h.url=function(){return b}}if(void 0!==h.ajaxSettings.url&&"function"!=typeof h.ajaxSettings.url){var b=h.ajaxSettings.url;h.ajaxSettings.url=function(){return b}}if("string"==typeof h.listLocation){var c=h.listLocation;h.listLocation="XML"===h.dataType.toUpperCase()?function(a){return $(a).find(c)}:function(a){return a[c]}}if("string"==typeof h.getValue){var d=h.getValue;h.getValue=function(a){return a[d]}}void 0!==a.categories&&(h.categoriesAssigned=!0)}function e(){h.ajaxSettings=void 0!==a.ajaxSettings&&"object"==typeof a.ajaxSettings?a.ajaxSettings:{}}function f(a){return void 0!==h[a]&&null!==h[a]?!0:!1}function g(a,b){function c(b,e){for(var f in e)void 0===b[f]&&a.log("Property '"+f+"' does not exist in EasyAutocomplete options API."),"object"!=typeof b[f]||d(f)||c(b[f],e[f])}function d(a){var b=["ajaxSettings","template"];return Array.prototype.contains=function(a){for(var b=this.length;b--;)if(this[b]===a)return!0;return!1},b.contains(a)}c(h,b)}var h={data:"list-required",url:"list-required",dataType:"json",listLocation:function(a){return a},xmlElementName:"",getValue:function(a){return a},autocompleteOff:!0,placeholder:!1,ajaxCallback:function(){},matchResponseProperty:!1,list:{sort:{enabled:!1,method:function(a,b){return a=h.getValue(a),b=h.getValue(b),b>a?-1:a>b?1:0}},maxNumberOfElements:6,hideOnEmptyPhrase:!0,match:{enabled:!1,caseSensitive:!1,method:function(a,b){return a=h.getValue(a),b=h.getValue(b),a===b?!0:!1}},showAnimation:{type:"normal",time:400,callback:function(){}},hideAnimation:{type:"normal",time:400,callback:function(){}},onClickEvent:function(){},onSelectItemEvent:function(){},onLoadEvent:function(){},onMouseOverEvent:function(){},onMouseOutEvent:function(){},onShowListEvent:function(){},onHideListEvent:function(){}},highlightPhrase:!0,theme:"",cssClasses:"",minCharNumber:0,ajaxSettings:{},loggerEnabled:!0,template:"",categoriesAssigned:!1,categories:[{listLocation:""}]};this.get=function(a){return h[a]},this.equals=function(a,b){return f(a)&&h[a]===b?!0:!1},this.checkDataUrlProperties=function(){return"list-required"===h.url&&"list-required"===h.data?!1:!0},this.checkRequiredProperties=function(){for(var a in h)if("required"===h[a])return logger.error("Option "+a+" must be defined"),!1;return!0},this.printPropertiesThatDoesntExist=function(a,b){g(a,b)},b(),c(),h.loggerEnabled===!0&&g(console,a),e(),d()},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Logger=function(){this.error=function(a){console.log("ERROR: "+a)},this.warning=function(a){console.log("WARNING: "+a)}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Constans=function(){var a={CONTAINER_CLASS:"easy-autocomplete-container",CONTAINER_ID:"eac-container-",WRAPPER_CSS_CLASS:"easy-autocomplete"};this.getValue=function(b){return a[b]}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.ListBuilderService=function(a,b){function c(b,c){function d(){var d,e={};return void 0!==b.xmlElementName&&(e.xmlElementName=b.xmlElementName),void 0!==b.listLocation?d=b.listLocation:void 0!==a.get("listLocation")&&(d=a.get("listLocation")),void 0!==d?"string"==typeof d?e.data=$(c).find(d):"function"==typeof d&&(e.data=d(c)):e.data=c,e}function e(){var a={};return void 0!==b.listLocation?"string"==typeof b.listLocation?a.data=c[b.listLocation]:"function"==typeof b.listLocation&&(a.data=b.listLocation(c)):a.data=c,a}var f={};if(f="XML"===a.get("dataType").toUpperCase()?d():e(),void 0!==b.header&&(f.header=b.header),void 0!==b.getValue)if("string"==typeof b.getValue){var g=b.getValue;f.getValue=function(a){return a[g]}}else"function"==typeof b.getValue&&(f.getValue=b.getValue);else f.getValue=a.get("getValue");return f}function d(b){var c=[];return void 0===b.xmlElementName&&(b.xmlElementName=a.get("xmlElementName")),$(b.data).find(b.xmlElementName).each(function(){c.push(this)}),c}this.init=function(b){var c=[],d={};return d.data=a.get("listLocation")(b),d.getValue=a.get("getValue"),c.push(d),c},this.updateCategories=function(b,d){if(a.get("categoriesAssigned")){b=[];for(var e=0;ee;e+=1)c[e].data=b(a,c[e].data,d);return c},this.checkIfDataExists=function(a){for(var b=0,c=a.length;c>b;b+=1)if(void 0!==a[b].data&&a[b].data instanceof Array&&a[b].data.length>0)return!0;return!1}},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.proccess=function(a,b,c){function d(b,c){var d=[],e="";if(a.get("list").match.enabled)for(var f=0,g=b.length;g>f;f+=1)e=a.get("getValue")(b[f]),a.get("list").match.caseSensitive||("string"==typeof e&&(e=e.toLowerCase()),c=c.toLowerCase()),e.search(c)>-1&&d.push(b[f]);else d=b;return d}function e(b){return b.length>a.get("list").maxNumberOfElements&&(b=b.slice(0,a.get("list").maxNumberOfElements)),b}function f(b){return a.get("list").sort.enabled&&b.sort(a.get("list").sort.method),b}var g=c;return b=d(b,g),b=e(b),b=f(b)},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.Template=function(a){var b={basic:{type:"basic",method:function(a){return a}},description:{type:"description",fields:{description:"description"},method:function(a){return a+" - description"},cssClass:"eac-description"},iconLeft:{type:"iconLeft",fields:{icon:""},method:function(a){return a},cssClass:"eac-icon-left"},iconRight:{type:"iconRight",fields:{iconSrc:""},method:function(a){return a},cssClass:"eac-icon-right"},links:{type:"links",fields:{link:""},method:function(a){return a}},custom:{type:"custom",method:function(){}}},c=function(a){var c=a.fields;if("description"===a.type){var d=function(a,b){return a+" - "+b[c.description]+""};return d}if("iconRight"===a.type){var d="";return"string"==typeof c.iconSrc?d=function(a,b){return a+""}:"function"==typeof c.iconSrc&&(d=function(a,b){return a+""}),d}if("iconLeft"===a.type){var d="";return"string"==typeof c.iconSrc?d=function(a,b){return""+a}:"function"==typeof c.iconSrc&&(d=function(a,b){return""+a}),d}if("links"===a.type){var d="";return"string"==typeof c.link?d=function(a,b){return""+a+""}:"function"==typeof c.link&&(d=function(a,b){return""+a+""}),d}return"custom"===a.type?a.method:b.basic.method};prepareBuildMethod=function(a){return a&&a.type&&a.type&&b[a.type]?c(a):b.basic.method},templateClass=function(a){var c=function(){return""};return a&&a.type&&a.type&&b[a.type]?function(){var c=b[a.type].cssClass;return function(){return c}}():c},this.getTemplateClass=templateClass(a),this.build=prepareBuildMethod(a)},a}(EasyAutocomplete||{}),EasyAutocomplete=function(a){return a.main=function(b,c){function d(){return 0===t.length?void p.error("Input field doesn't exist."):o.checkDataUrlProperties()?o.checkRequiredProperties()?(e(),void g()):void p.error("Will not work without mentioned properties."):void p.error("One of options variables 'data' or 'url' must be defined.")}function e(){function a(){var a=$("
      "),c=n.getValue("WRAPPER_CSS_CLASS");o.get("theme")&&(c+=" eac-"+o.get("theme")),o.get("cssClasses")&&(c+=" "+o.get("cssClasses")),""!==q.getTemplateClass()&&(c+=" "+q.getTemplateClass()),a.addClass(c),t.wrap(a),b()}function b(){var a=t.outerWidth();t.parent().css("width",a)}function c(){t.unwrap()}function d(){var a=$("
      ").addClass(n.getValue("CONTAINER_CLASS"));a.attr("id",f()).prepend($("
        ")),function(){a.on("show",function(){switch(o.get("list").showAnimation.type){case"slide":var b=o.get("list").showAnimation.time,c=o.get("list").showAnimation.callback;a.find("ul").slideDown(b,c);break;case"fade":var b=o.get("list").showAnimation.time,c=o.get("list").showAnimation.callback;a.find("ul").fadeIn(b),c;break;default:a.find("ul").show()}o.get("list").onShowListEvent()}).on("hide",function(){switch(o.get("list").hideAnimation.type){case"slide":var b=o.get("list").hideAnimation.time,c=o.get("list").hideAnimation.callback;a.find("ul").slideUp(b,c);break;case"fade":var b=o.get("list").hideAnimation.time,c=o.get("list").hideAnimation.callback;a.find("ul").fadeOut(b,c);break;default:a.find("ul").hide()}o.get("list").onHideListEvent()}).on("selectElement",function(){a.find("ul li").removeClass("selected"),a.find("ul li").eq(w).addClass("selected"),o.get("list").onSelectItemEvent()}).on("loadElements",function(b,c,d){var e="",f=($("
          "),a.find("ul"));f.empty().detach(),v=[];for(var h=0,i=0,k=c.length;k>i;i+=1){var l=c[i].data;if(0!==l.length){void 0!==c[i].header&&c[i].header.length>0&&f.append("
          "+c[i].header+"
          ");for(var m=0,n=l.length;n>m;m+=1)e=$("
        • "),function(){var a=m,b=h,f=c[i].getValue(l[a]);e.find(" > div").on("click",function(){t.val(f).trigger("change"),w=b,j(b),o.get("list").onClickEvent()}).mouseover(function(){w=b,j(b),o.get("list").onMouseOverEvent()}).mouseout(function(){o.get("list").onMouseOutEvent()}).html(q.build(g(f,d),l[a]))}(),f.append(e),v.push(l[m]),h+=1}}a.append(f),o.get("list").onLoadEvent()})}(),t.after(a)}function e(){t.next("."+n.getValue("CONTAINER_CLASS")).remove()}function g(a,b){return o.get("highlightPhrase")&&""!==b?h(a,b):a}function h(a,b){return(a+"").replace(new RegExp("("+b+")","gi"),"$1")}t.parent().hasClass(n.getValue("WRAPPER_CSS_CLASS"))&&(e(),c()),a(),d(),u=$("#"+f()),o.get("placeholder")&&t.attr("placeholder",o.get("placeholder"))}function f(){var a=t.attr("id");if(void 0===a||null===a){var b="";do b=m.shortcut+"-"+Math.floor(1e4*Math.random());while(0!==$("#"+b).length);a=n.getValue("CONTAINER_ID")+b,t.attr("id",b)}else a=n.getValue("CONTAINER_ID")+a;return a}function g(){function a(){s("autocompleteOff",!0)&&g(),b(),c(),d(),e(),f()}function b(){t.off("keyup").keyup(function(a){function b(a){function b(){var a=new Object,b=o.get("ajaxSettings")||{};for(set in b)a[set]=b[set];return a}function c(a,b){return o.get("matchResponseProperty")!==!1?"string"==typeof o.get("matchResponseProperty")?b[o.get("matchResponseProperty")]==a:"function"==typeof o.get("matchResponseProperty")?o.get("matchResponseProperty")(b)===a:!0:!0}if(!(a.length0&&w>0&&(w-=1,t.val(o.get("getValue")(v[w])),j(w));break;case 40:a.preventDefault(),v.length>0&&w40||8===a.keyCode){var c=t.val();o.get("list").hideOnEmptyPhrase!==!0||8!==a.keyCode||""!==c?b(c):i()}}})}function c(){t.on("keydown",function(a){a=a||window.event;var b=a.keyCode;return 38===b?(suppressKeypress=!0,!1):void 0}).keydown(function(a){13===a.keyCode&&w>-1&&(t.val(o.get("getValue")(v[w])),w=-1,i(),a.preventDefault())})}function d(){t.off("keypress")}function e(){t.focus(function(){""!==t.val()&&v.length>0&&(w=-1,h())})}function f(){t.blur(function(){setTimeout(function(){w=-1,i()},250)})}function g(){t.attr("autocomplete","off")}a()}function h(){u.trigger("show")}function i(){u.trigger("hide")}function j(a){u.trigger("selectElement",a)}function k(a,b){u.trigger("loadElements",[a,b])}function l(){t.trigger("blur")}var m={name:"EasyAutocomplete",shortcut:"eac"},n=new a.Constans,o=new a.Configuration(c),p=new a.Logger,q=new a.Template(c.template),r=new a.ListBuilderService(o,a.proccess),s=(a.proccess,o.equals),t=b,u="",v=[],w=-1;this.getConstants=function(){return n},this.getConfiguration=function(){return o},this.getContainer=function(){return u},this.getSelectedItemIndex=function(){return w},this.getItemData=function(a){return v.length-1&&void 0!==$.fn.easyAutocompleteHandles[b]?$.fn.easyAutocompleteHandles[b].getItemData(a):-1},$.fn.getSelectedItemData=function(){var a=$(this).attr("id");return void 0!==a&&void 0!==$.fn.easyAutocompleteHandles[a]?$.fn.easyAutocompleteHandles[a].getSelectedItemData():-1}; \ No newline at end of file diff --git a/src/configuration.js b/src/configuration.js index 7f2d3ac..a428d39 100644 --- a/src/configuration.js +++ b/src/configuration.js @@ -27,7 +27,6 @@ var EasyAutocomplete = (function(scope){ matchResponseProperty: false, - list: { sort: { enabled: false, @@ -48,6 +47,8 @@ var EasyAutocomplete = (function(scope){ maxNumberOfElements: 6, + hideOnEmptyPhrase: true, + match: { enabled: false, caseSensitive: false, diff --git a/src/core.js b/src/core.js index 694ff46..976cd72 100644 --- a/src/core.js +++ b/src/core.js @@ -436,7 +436,15 @@ var EasyAutocomplete = (function(scope) { default: if (event.keyCode > 40 || event.keyCode === 8) { - loadData(); + + var inputPhrase = $field.val(); + + if (!(config.get("list").hideOnEmptyPhrase === true && event.keyCode === 8 && inputPhrase === "")) { + loadData(inputPhrase); + } else { + hideContainer(); + } + } @@ -444,9 +452,7 @@ var EasyAutocomplete = (function(scope) { } - function loadData() { - - var inputPhrase = $field.val(); + function loadData(inputPhrase) { if (inputPhrase.length < config.get("minCharNumber")) { @@ -572,17 +578,17 @@ var EasyAutocomplete = (function(scope) { }) .keydown(function(event) { - if (event.keyCode === 13 && selectedElement > -1) { + if (event.keyCode === 13 && selectedElement > -1) { - //enter + //enter - $field.val(config.get("getValue")(elementsList[selectedElement])); - selectedElement = -1; - hideContainer(); + $field.val(config.get("getValue")(elementsList[selectedElement])); + selectedElement = -1; + hideContainer(); - event.preventDefault(); - } - }); + event.preventDefault(); + } + }); } function bindKeypress() { diff --git a/test/core/build.js b/test/core/build.js index fa187d4..31a5505 100644 --- a/test/core/build.js +++ b/test/core/build.js @@ -319,3 +319,114 @@ QUnit.test("Minimal number of characters in input phrase - true", function( asse }); + + +QUnit.test("List, hideOnEmptyPhrase - false ", function( assert ) { + expect(1); + + //given + var completerOne = new EasyAutocomplete.main($("#inputOne"), {url: "resources/colors_string.json", + + list: { + onLoadEvent: function() { + assertList(); + }, + + hideOnEmptyPhrase: false + } + + }); + + + //execute + + completerOne.init(); + + var e = $.Event('keyup'); + e.keyCode = 8; //backspace + $("#inputOne").val("").trigger(e); + + QUnit.stop(); + + //assert + + function assertList() { + var elements = $("#inputOne").next().find("ul li"); + + assert.equal(3, elements.length, "Response size"); + + QUnit.start(); + } +}); + +QUnit.test("List, hideOnEmptyPhrase - true - empty input", function( assert ) { + expect(1); + + //given + var completerOne = new EasyAutocomplete.main($("#inputOne"), {url: "resources/colors_string.json", + + list: { + + hideOnEmptyPhrase: true + } + + }); + + + //execute + + completerOne.init(); + + var e = $.Event('keyup'); + e.keyCode = 8; //backspace + $("#inputOne").val("").trigger(e); + + + //assert + var elements = $("#inputOne").next().find("ul li"); + + assert.equal(0, elements.length, "Response size"); + + +}); + + +QUnit.test("List, hideOnEmptyPhrase - true - not empty input", function( assert ) { + expect(1); + + //given + var completerOne = new EasyAutocomplete.main($("#inputOne"), {url: "resources/colors_string.json", + + list: { + onLoadEvent: function() { + assertList(); + }, + + hideOnEmptyPhrase: true + } + + }); + + + //execute + + completerOne.init(); + + var e = $.Event('keyup'); + e.keyCode = 8; //backspace + $("#inputOne").val("aaaa").trigger(e); + + QUnit.stop(); + + //assert + + function assertList() { + var elements = $("#inputOne").next().find("ul li"); + + assert.equal(3, elements.length, "Response size"); + + QUnit.start(); + } + + +});