Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed the list property to accept a function. #17153

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
}
}
};
Expand Down
6 changes: 6 additions & 0 deletions test/init/listSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand Down