diff --git a/src/configuration.js b/src/configuration.js index aa734d2..b25f11d 100644 --- a/src/configuration.js +++ b/src/configuration.js @@ -1,5 +1,5 @@ /* - * EasyAutocomplete - Configuration + * EasyAutocomplete - Configuration */ var EasyAutocomplete = (function(scope){ @@ -81,7 +81,7 @@ var EasyAutocomplete = (function(scope){ onChooseEvent: function() {}, onKeyEnterEvent: function() {}, onMouseOverEvent: function() {}, - onMouseOutEvent: function() {}, + onMouseOutEvent: function() {}, onShowListEvent: function() {}, onHideListEvent: function() {} }, @@ -98,6 +98,8 @@ var EasyAutocomplete = (function(scope){ adjustWidth: true, + noResults: false, + ajaxSettings: {}, preparePostData: function(data, inputPhrase) {return data;}, @@ -114,7 +116,7 @@ var EasyAutocomplete = (function(scope){ }] }; - + var externalObjects = ["ajaxSettings", "template"]; this.get = function(propertyName) { @@ -126,8 +128,8 @@ var EasyAutocomplete = (function(scope){ if (defaults[name] === value) { return true; } - } - + } + return false; }; @@ -159,7 +161,7 @@ var EasyAutocomplete = (function(scope){ mergeOptions(); if (defaults.loggerEnabled === true) { - printPropertiesThatDoesntExist(console, options); + printPropertiesThatDoesntExist(console, options); } addAjaxSettings(); @@ -174,7 +176,7 @@ var EasyAutocomplete = (function(scope){ function prepareDefaults() { if (options.dataType === "xml") { - + if (!options.getValue) { options.getValue = function(element) { @@ -182,11 +184,11 @@ var EasyAutocomplete = (function(scope){ }; } - + if (!options.list) { options.list = {}; - } + } if (!options.list.sort) { options.list.sort = {}; @@ -196,7 +198,7 @@ var EasyAutocomplete = (function(scope){ options.list.sort.method = function(a, b) { a = options.getValue(a); b = options.getValue(b); - + //Alphabeticall sort if (a < b) { return -1; @@ -227,7 +229,7 @@ var EasyAutocomplete = (function(scope){ var categories = []; - for (var i = 0, length = options.categories.length; i < length; i += 1) { + for (var i = 0, length = options.categories.length; i < length; i += 1) { var category = options.categories[i]; @@ -258,7 +260,7 @@ var EasyAutocomplete = (function(scope){ for (var propertyName in source) { if (target[propertyName] !== undefined && target[propertyName] !== null) { - if (typeof target[propertyName] !== "object" || + if (typeof target[propertyName] !== "object" || target[propertyName] instanceof Array) { mergedObject[propertyName] = target[propertyName]; } else { @@ -266,7 +268,7 @@ var EasyAutocomplete = (function(scope){ } } } - + /* If data is an object */ if (target.data !== undefined && target.data !== null && typeof target.data === "object") { mergedObject.data = target.data; @@ -274,11 +276,11 @@ var EasyAutocomplete = (function(scope){ return mergedObject; } - } + } function processAfterMerge() { - + if (defaults.url !== "list-required" && typeof defaults.url !== "function") { var defaultUrl = defaults.url; defaults.url = function() { @@ -303,7 +305,7 @@ var EasyAutocomplete = (function(scope){ } else { defaults.listLocation = function(data) { return data[defaultlistLocation]; - }; + }; } } @@ -325,9 +327,9 @@ var EasyAutocomplete = (function(scope){ if (options.ajaxSettings !== undefined && typeof options.ajaxSettings === "object") { defaults.ajaxSettings = options.ajaxSettings; } else { - defaults.ajaxSettings = {}; + defaults.ajaxSettings = {}; } - + } function isAssigned(name) { @@ -341,19 +343,19 @@ var EasyAutocomplete = (function(scope){ //Consol is object that should have method log that prints string //Normally invoke this function with console as consol function printPropertiesThatDoesntExist(consol, optionsToCheck) { - + checkPropertiesIfExist(defaults, optionsToCheck); function checkPropertiesIfExist(source, target) { for(var property in target) { if (source[property] === undefined) { - consol.log("Property '" + property + "' does not exist in EasyAutocomplete options API."); + consol.log("Property '" + property + "' does not exist in EasyAutocomplete options API."); } if (typeof source[property] === "object" && $.inArray(property, externalObjects) === -1) { checkPropertiesIfExist(source[property], target[property]); } - } + } } } }; @@ -361,4 +363,3 @@ var EasyAutocomplete = (function(scope){ return scope; })(EasyAutocomplete || {}); - diff --git a/src/core.js b/src/core.js index 4203dfa..419e756 100644 --- a/src/core.js +++ b/src/core.js @@ -4,9 +4,9 @@ */ var EasyAutocomplete = (function(scope) { - + scope.main = function Core($input, options) { - + var module = { name: "EasyAutocomplete", shortcut: "eac" @@ -19,14 +19,14 @@ var EasyAutocomplete = (function(scope) { listBuilderService = new scope.ListBuilderService(config, scope.proccess), checkParam = config.equals, - $field = $input, + $field = $input, $container = "", elementsList = [], selectedElement = -1, requestDelayTimeoutId; scope.consts = consts; - + //------------------------ GETTERS -------------------------- @@ -63,7 +63,7 @@ var EasyAutocomplete = (function(scope) { return this.getItemData(selectedElement); }; - //------------------------ PUBLIC METHODS STARTS -------------------------- + //------------------------ PUBLIC METHODS STARTS -------------------------- this.build = function() { prepareField(); @@ -73,7 +73,7 @@ var EasyAutocomplete = (function(scope) { init(); }; - //------------------------ PUBLIC METHODS ENDS -------------------------- + //------------------------ PUBLIC METHODS ENDS -------------------------- //Main method @@ -96,7 +96,7 @@ var EasyAutocomplete = (function(scope) { prepareField(); - bindEvents(); + bindEvents(); } @@ -109,14 +109,14 @@ var EasyAutocomplete = (function(scope) { //TODO Rebuild this function function prepareField() { - + if ($field.parent().hasClass(consts.getValue("WRAPPER_CSS_CLASS"))) { removeContainer(); removeWrapper(); - } - + } + createWrapper(); - createContainer(); + createContainer(); $container = $("#" + getContainerId()); @@ -131,7 +131,7 @@ var EasyAutocomplete = (function(scope) { var $wrapper = $("
"), classes = consts.getValue("WRAPPER_CSS_CLASS"); - + if (config.get("theme") && config.get("theme") !== "") { classes += " eac-" + config.get("theme"); } @@ -143,7 +143,7 @@ var EasyAutocomplete = (function(scope) { if (template.getTemplateClass() !== "") { classes += " " + template.getTemplateClass(); } - + $wrapper .addClass(classes); @@ -154,16 +154,16 @@ var EasyAutocomplete = (function(scope) { if (config.get("adjustWidth") === true) { - adjustWrapperWidth(); + adjustWrapperWidth(); } - + } function adjustWrapperWidth() { var fieldWidth = $field.outerWidth(); - $field.parent().css("width", fieldWidth); + $field.parent().css("width", fieldWidth); } function removeWrapper() { @@ -207,7 +207,7 @@ var EasyAutocomplete = (function(scope) { } config.get("list").onShowListEvent(); - + }) /* List hide animation */ .on("hide.eac", function() { @@ -243,7 +243,7 @@ var EasyAutocomplete = (function(scope) { config.get("list").onSelectItemEvent(); }) .on("loadElements.eac", function(event, listBuilders, phrase) { - + var $item = "", $listContainer = $elements_container.find("ul"); @@ -270,7 +270,7 @@ var EasyAutocomplete = (function(scope) { for(var i = 0, listDataLength = listData.length; i < listDataLength && counter < listBuilders[builderIndex].maxListSize; i += 1) { $item = $("
  • "); - + (function() { var j = i, @@ -291,7 +291,7 @@ var EasyAutocomplete = (function(scope) { .mouseover(function() { selectedElement = itemCounter; - selectElement(itemCounter); + selectElement(itemCounter); config.get("list").onMouseOverEvent(); }) @@ -307,6 +307,10 @@ var EasyAutocomplete = (function(scope) { } } + if (config.get("noResults") && counter == 0) { + $listContainer.append("
  • " + config.get("noResults +
  • ")); + } + $elements_container.append($listContainer); config.get("list").onLoadEvent(); @@ -324,11 +328,11 @@ var EasyAutocomplete = (function(scope) { function highlight(string, phrase) { if(config.get("highlightPhrase") && phrase !== "") { - return highlightPhrase(string, phrase); + return highlightPhrase(string, phrase); } else { return string; } - + } function escapeRegExp(str) { @@ -346,7 +350,7 @@ var EasyAutocomplete = (function(scope) { //Generate unique element id function getContainerId() { - + var elementId = $field.attr("id"); elementId = consts.getValue("CONTAINER_ID") + elementId; @@ -364,8 +368,8 @@ var EasyAutocomplete = (function(scope) { bindAllEvents(); - //------------------------ FUNCTIONS -------------------------- - + //------------------------ FUNCTIONS -------------------------- + function bindAllEvents() { if (checkParam("autocompleteOff", true)) { @@ -439,7 +443,7 @@ var EasyAutocomplete = (function(scope) { selectElement(selectedElement); - } + } break; case 40: @@ -455,7 +459,7 @@ var EasyAutocomplete = (function(scope) { $field.val(config.get("getValue")(elementsList[selectedElement])); selectElement(selectedElement); - + } break; @@ -483,13 +487,13 @@ var EasyAutocomplete = (function(scope) { } else { hideContainer(); } - + } break; } - + function loadData(inputPhrase) { @@ -506,13 +510,13 @@ var EasyAutocomplete = (function(scope) { var listBuilders = listBuilderService.init(data); listBuilders = listBuilderService.updateCategories(listBuilders, data); - + listBuilders = listBuilderService.processData(listBuilders, inputPhrase); loadElements(listBuilders, inputPhrase); if ($field.parent().find("li").length > 0) { - showContainer(); + showContainer(); } else { hideContainer(); } @@ -536,13 +540,13 @@ var EasyAutocomplete = (function(scope) { settings.data = config.get("preparePostData")(settings.data, inputPhrase); - $.ajax(settings) + $.ajax(settings) .done(function(data) { var listBuilders = listBuilderService.init(data); listBuilders = listBuilderService.updateCategories(listBuilders, data); - + listBuilders = listBuilderService.convertXml(listBuilders); @@ -551,12 +555,12 @@ var EasyAutocomplete = (function(scope) { listBuilders = listBuilderService.processData(listBuilders, inputPhrase); - loadElements(listBuilders, inputPhrase); - + loadElements(listBuilders, inputPhrase); + } - if (listBuilderService.checkIfDataExists(listBuilders) && $field.parent().find("li").length > 0) { - showContainer(); + if ((listBuilderService.checkIfDataExists(listBuilders) && $field.parent().find("li").length > 0) || config.get("noResults")) { + showContainer(); } else { hideContainer(); } @@ -572,7 +576,7 @@ var EasyAutocomplete = (function(scope) { }); } - + function createAjaxSettings() { @@ -616,7 +620,7 @@ var EasyAutocomplete = (function(scope) { evt = evt || window.event; var keyCode = evt.keyCode; if (keyCode === 38) { - suppressKeypress = true; + suppressKeypress = true; return false; } }) @@ -648,11 +652,11 @@ var EasyAutocomplete = (function(scope) { $field.focus(function() { if ($field.val() !== "" && elementsList.length > 0) { - + selectedElement = -1; - showContainer(); + showContainer(); } - + }); } @@ -660,8 +664,8 @@ var EasyAutocomplete = (function(scope) { $field.blur(function() { //TODO - setTimeout(function() { - + setTimeout(function() { + selectedElement = -1; hideContainer(); }, 250); @@ -674,7 +678,7 @@ var EasyAutocomplete = (function(scope) { } - + //--------------------------------------------------------------------- //------------------------ EVENTS ------------------------------------- @@ -691,7 +695,7 @@ var EasyAutocomplete = (function(scope) { } function selectElement(index) { - + $container.trigger("selectElement.eac", index); } @@ -729,13 +733,13 @@ var EasyAutocomplete = (function(scope) { var fieldId = ""; do { - fieldId = "eac-" + Math.floor(Math.random() * 10000); + fieldId = "eac-" + Math.floor(Math.random() * 10000); } while ($("#" + fieldId).length !== 0); - + elementId = scope.consts.getValue("CONTAINER_ID") + fieldId; $(input).attr("id", fieldId); - + }; scope.setHandle = function(handle, id) {