diff options
author | Vladislav Zarakovsky <vlad.zar@gmail.com> | 2016-02-01 12:49:34 +0300 |
---|---|---|
committer | Vladislav Zarakovsky <vlad.zar@gmail.com> | 2016-02-01 12:49:34 +0300 |
commit | 71d52f767ca9d5114332bcb1faff56696f0c31d2 (patch) | |
tree | dd8144a9623345108ae291c199a90eaf66c157dc | |
parent | dd4db9083127c70e9e71822a9e75cdcf48e54601 (diff) | |
download | awesomplete-71d52f767ca9d5114332bcb1faff56696f0c31d2.zip awesomplete-71d52f767ca9d5114332bcb1faff56696f0c31d2.tar.gz awesomplete-71d52f767ca9d5114332bcb1faff56696f0c31d2.tar.bz2 |
Add item data to awesomplete-select event
-rw-r--r-- | awesomplete.js | 15 | ||||
-rw-r--r-- | test/api/selectSpec.js | 1 |
2 files changed, 12 insertions, 4 deletions
diff --git a/awesomplete.js b/awesomplete.js index d7857e7..4024391 100644 --- a/awesomplete.js +++ b/awesomplete.js @@ -189,6 +189,7 @@ _.prototype = { if (selected) { var allowed = $.fire(this.input, "awesomplete-select", { text: selected.textContent, + data: this._suggestions[elementSiblingIndex(selected)], origin: origin || selected }); @@ -209,15 +210,15 @@ _.prototype = { // Populate list with options that match this.ul.innerHTML = ""; - this._list + this._suggestions = this._list .filter(function(item) { return me.filter(item, value); }) .sort(this.sort) - .every(function(text, i) { - me.ul.appendChild(me.item(text, value)); + .slice(0, this.maxItems); - return i < me.maxItems - 1; + this._suggestions.forEach(function(text) { + me.ul.appendChild(me.item(text, value)); }); if (this.ul.children.length === 0) { @@ -290,6 +291,12 @@ function configure(instance, properties, o) { } } +function elementSiblingIndex(el) { + /* eslint-disable no-cond-assign */ + for (var i = 0; el = el.previousElementSibling; i++); + return i; +} + // Helpers var slice = Array.prototype.slice; diff --git a/test/api/selectSpec.js b/test/api/selectSpec.js index 8d8a186..84895b1 100644 --- a/test/api/selectSpec.js +++ b/test/api/selectSpec.js @@ -47,6 +47,7 @@ describe("awesomplete.select", function () { expect(handler).toHaveBeenCalledWith( jasmine.objectContaining({ text: expectedTxt, + data: expectedTxt, origin: this.selectArgument || this.subject.ul.children[0] }) ); |