UNPKG

@anypoint-web-components/anypoint-selector

Version:

Port of iron-selector that works with ES6 classes. Manages a list of elements that can be selected.

100 lines (93 loc) 2.59 kB
/** * Port of `@polymer/iron-selector/iron-selection.js`. */ export class AnypointSelection { /** * @param {!Function} selectCallback * @suppress {missingProvide} */ constructor(selectCallback) { this.multi = false; this.selection = []; this.selectCallback = selectCallback; } /** * Retrieves the selected item(s). * * @return {*} Returns the selected item(s). If the multi property is true, * `get` will return an array, otherwise it will return * the selected item or undefined if there is no selection. */ get() { return this.multi ? this.selection.slice() : this.selection[0]; } /** * Clears all the selection except the ones indicated. * * @param {any[]?} excludes items to be excluded. */ clear(excludes) { this.selection.slice().forEach((item) => { if (!excludes || excludes.indexOf(item) === -1) { this.setItemSelected(item, false); } }); } /** * Indicates if a given item is selected. * * @param {*} item The item whose selection state should be checked. * @return {boolean} Returns true if `item` is selected. */ isSelected(item) { return this.selection.indexOf(item) >= 0; } /** * Sets the selection state for a given item to either selected or deselected. * * @param {*} item The item to select. * @param {boolean} isSelected True for selected, false for deselected. */ setItemSelected(item, isSelected) { if (item !== null) { if (isSelected !== this.isSelected(item)) { // proceed to update selection only if requested state differs from // current if (isSelected) { this.selection.push(item); } else { const i = this.selection.indexOf(item); if (i >= 0) { this.selection.splice(i, 1); } } if (this.selectCallback) { this.selectCallback(item, isSelected); } } } } /** * Sets the selection state for a given item. If the `multi` property * is true, then the selected state of `item` will be toggled; otherwise * the `item` will be selected. * * @param {*} item The item to select. */ select(item) { if (this.multi) { this.toggle(item); } else if (this.get() !== item) { this.setItemSelected(this.get(), false); this.setItemSelected(item, true); } } /** * Toggles the selection state for `item`. * * @param {*} item The item to toggle. */ toggle(item) { this.setItemSelected(item, !this.isSelected(item)); } }