igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
161 lines • 4.8 kB
JavaScript
export class SelectionController {
get data() {
return this.host.data;
}
resetSearchTerm() {
this.host.resetSearchTerm();
}
getValue(items, key) {
return items.map((item) => item[key] ?? item);
}
handleChange(detail) {
return this.host.emitEvent('igcChange', { cancelable: true, detail });
}
getItemsByValueKey(keys) {
return keys.map((key) => this.data.find((i) => i[this.host.valueKey] === key));
}
selectValueKeys(keys) {
if (keys.length === 0)
return;
this.getItemsByValueKey(keys).forEach((item) => {
return item && this._selected.add(item);
});
}
deselectValueKeys(keys) {
if (keys.length === 0)
return;
this.getItemsByValueKey(keys).forEach((item) => {
return item && this._selected.delete(item);
});
}
selectObjects(items) {
if (items.length === 0)
return;
items.forEach((item) => {
const i = this.data.includes(item);
if (i) {
this._selected.add(item);
}
});
}
deselectObjects(items) {
if (items.length === 0)
return;
items.forEach((item) => {
const i = this.data.includes(item);
if (i) {
this._selected.delete(item);
}
});
}
selectAll() {
this.data.forEach((item) => {
this._selected.add(item);
});
this.host.requestUpdate();
}
deselectAll() {
this._selected.clear();
this.host.requestUpdate();
}
async select(items, emit = false) {
const { singleSelect } = this.host;
if (singleSelect) {
this._selected.clear();
this.resetSearchTerm();
}
if (!items || items.length === 0) {
!singleSelect && this.selectAll();
return;
}
const _items = singleSelect ? items.slice(0, 1) : items;
const values = this.host.valueKey
? this.getItemsByValueKey(_items)
: _items;
const selected = Array.from(this._selected.values());
const payload = [...values, ...selected];
if (emit &&
!this.handleChange({
newValue: this.getValue(payload, this.host.valueKey),
items: values,
type: 'selection',
})) {
return;
}
if (this.host.valueKey) {
this.selectValueKeys(_items);
}
else {
this.selectObjects(_items);
}
this.host.requestUpdate();
}
async deselect(items, emit = false) {
if (!items || items.length === 0) {
if (emit &&
!this.handleChange({
newValue: [],
items: Array.from(this.selected),
type: 'deselection',
})) {
return;
}
this.deselectAll();
return;
}
const _items = this.host.singleSelect ? items.slice(0, 1) : items;
const values = this.host.valueKey
? this.getItemsByValueKey(_items)
: _items;
const selected = Array.from(this._selected.values());
const payload = selected.filter((item) => item !== values[0]);
if (emit &&
!this.handleChange({
newValue: this.getValue(payload, this.host.valueKey),
items: values,
type: 'deselection',
})) {
return;
}
if (this.host.valueKey) {
this.deselectValueKeys(_items);
}
else {
this.deselectObjects(_items);
}
this.host.requestUpdate();
}
get selected() {
return this._selected;
}
changeSelection(index) {
const item = this.data[index];
if (this.host.valueKey) {
!this.selected.has(item)
? this.select([item[this.host.valueKey]], true)
: this.deselect([item[this.host.valueKey]], true);
}
else {
!this.selected.has(item)
? this.select([item], true)
: this.deselect([item], true);
}
}
selectByIndex(index) {
const item = this.data[index];
if (this.host.valueKey) {
this.select([item[this.host.valueKey]], true);
}
else {
this.select([item], true);
}
}
constructor(host) {
this.host = host;
this._selected = new Set();
this.host.addController(this);
}
hostConnected() { }
hostDisconnected() { }
}
//# sourceMappingURL=selection.js.map