@atlassian/aui
Version:
Atlassian User Interface Framework
89 lines (74 loc) • 2.6 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'exports'], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports);
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports);
global.suggestionsModel = mod.exports;
}
})(this, function (module, exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
function SuggestionsModel() {
this._suggestions = [];
this._activeIndex = -1;
}
SuggestionsModel.prototype = {
onChange: function onChange() {},
onHighlightChange: function onHighlightChange() {},
get: function get(index) {
return this._suggestions[index];
},
set: function set(suggestions) {
var oldSuggestions = this._suggestions;
this._suggestions = suggestions || [];
this.onChange(oldSuggestions);
return this;
},
getNumberOfResults: function getNumberOfResults() {
return this._suggestions.length;
},
setHighlighted: function setHighlighted(toHighlight) {
if (toHighlight) {
for (var i = 0; i < this._suggestions.length; i++) {
if (this._suggestions[i].id === toHighlight.id) {
this.highlight(i);
}
}
}
return this;
},
highlight: function highlight(index) {
this._activeIndex = index;
this.onHighlightChange();
return this;
},
highlightPrevious: function highlightPrevious() {
var current = this._activeIndex;
var previousActiveIndex = current === 0 ? current : current - 1;
this.highlight(previousActiveIndex);
return this;
},
highlightNext: function highlightNext() {
var current = this._activeIndex;
var nextActiveIndex = current === this._suggestions.length - 1 ? current : current + 1;
this.highlight(nextActiveIndex);
return this;
},
highlighted: function highlighted() {
return this.get(this._activeIndex);
},
highlightedIndex: function highlightedIndex() {
return this._activeIndex;
}
};
exports.default = SuggestionsModel;
module.exports = exports['default'];
});
//# sourceMappingURL=suggestions-model.js.map