From f1578b9e6f07738c2950d1637b5a9020be703dac Mon Sep 17 00:00:00 2001 From: Mike Hoffman Date: Thu, 19 Jul 2018 18:51:20 -0400 Subject: [PATCH 1/2] The .list property can now also be a function. It's passed the current input and should return an array. This makes it easy to have the list generated dynmically as the user types. --- awesomplete.js | 62 +++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/awesomplete.js b/awesomplete.js index da1d5412..6db7a00d 100644 --- a/awesomplete.js +++ b/awesomplete.js @@ -137,7 +137,7 @@ var _ = function (input, o) { _.prototype = { set list(list) { - if (Array.isArray(list)) { + if (Array.isArray(list) || typeof list === "function") { this._list = list; } else if (typeof list === "string" && list.indexOf(",") > -1) { @@ -295,45 +295,45 @@ _.prototype = { var me = this; var value = this.input.value; - if (value.length >= this.minChars && this._list && this._list.length > 0) { - this.index = -1; - // Populate list with options that match - this.ul.innerHTML = ""; - - this.suggestions = this._list - .map(function(item) { - return new Suggestion(me.data(item, value)); - }) - .filter(function(item) { - return me.filter(item, value); - }); + var suggestions = []; - if (this.sort !== false) { - this.suggestions = this.suggestions.sort(this.sort); + if (value.length >= this.minChars) { + var list = this._list; + if (typeof this._list === "function") { + list = this._list(value); } - this.suggestions = this.suggestions.slice(0, this.maxItems); + if (list && list.length > 0) { - this.suggestions.forEach(function(text, index) { - me.ul.appendChild(me.item(text, value, index)); - }); - - if (this.ul.children.length === 0) { + suggestions = list + .map(function (item) { + return new Suggestion(me.data(item, value)); + }) + .filter(function (item) { + return me.filter(item, value); + }); - this.status.textContent = "No results found"; + if (this.sort !== false) { + suggestions.sort(this.sort); + } + } + } - this.close({ reason: "nomatches" }); + this.suggestions = suggestions.slice(0, this.maxItems); + if (this.suggestions.length > 0) { + this.index = -1; + // Populate list with options that match + this.ul.innerHTML = ""; - } else { - this.open(); + this.suggestions.forEach(function (text, index) { + me.ul.appendChild(me.item(text, value, index)); + }); - this.status.textContent = this.ul.children.length + " results found"; - } - } - else { + this.open(); + this.status.textContent = this.ul.children.length + " results found"; + } else { + this.status.textContent = "No results found"; this.close({ reason: "nomatches" }); - - this.status.textContent = "No results found"; } } }; From 37c58daba7f00f88fbc17d625bdbea887088943f Mon Sep 17 00:00:00 2001 From: Mike Hoffman Date: Thu, 19 Jul 2018 20:04:35 -0400 Subject: [PATCH 2/2] Updated listSpec.js --- test/init/listSpec.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/init/listSpec.js b/test/init/listSpec.js index eff78d61..e89799ab 100644 --- a/test/init/listSpec.js +++ b/test/init/listSpec.js @@ -39,6 +39,12 @@ describe("Awesomplete list", function () { ]); }); + it("assigns from function", function () { + var someFunction = function() { return ["List", "from", "a", "function"]; } + this.subject.list = someFunction; + expect(this.subject._list).toEqual(someFunction); + }); + it("does not assigns from not found list", function () { this.subject.list = "#nosuchlist"; expect(this.subject._list).toEqual([]);