@progress/kendo-angular-dropdowns
Version:
A wide variety of native Angular dropdown components including AutoComplete, ComboBox, DropDownList, DropDownTree, MultiColumnComboBox, MultiSelect, and MultiSelectTree
167 lines (166 loc) • 5.86 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Injectable, EventEmitter } from '@angular/core';
import { DisabledItemsService } from '../disabled-items/disabled-items.service';
import { isPresent } from '../util';
import * as i0 from "@angular/core";
import * as i1 from "../disabled-items/disabled-items.service";
/**
* @hidden
*/
export class SelectionService {
disabledItemsService;
onSelect = new EventEmitter();
onChange = new EventEmitter();
onFocus = new EventEmitter();
total = 0;
lastClickedIndex;
selectedIndices = [];
focusedIndex;
constructor(disabledItemsService) {
this.disabledItemsService = disabledItemsService;
}
getTotal() {
return this.total;
}
isSelected(index) {
return isPresent(this.selectedIndices.find(current => current === index));
}
isFocused(index) {
return index === this.focused;
}
focus(index) {
if (this.isFocused(index)) {
return;
}
this.focused = index;
this.onFocus.emit(index);
}
select(index) {
if (this.isSelected(index)) {
return;
}
this.selectedIndices = [index];
this.focused = index;
this.onSelect.emit({
indices: [index],
newSelection: isPresent(index)
});
}
add(index, preventClosingPopup) {
if (this.isSelected(index)) {
return;
}
this.selectedIndices.push(index);
this.focused = index;
this.onChange.emit({
added: index,
indices: this.selectedIndices.slice(),
preventClosingPopup: preventClosingPopup
});
}
indicesToBeRemoved = [];
indicesToBeAdded = [];
emitMultipleAddedRemoved() {
this.onChange.emit({
added: this.indicesToBeAdded,
removed: this.indicesToBeRemoved,
indices: this.selectedIndices.slice(),
preventClosingPopup: true,
isMultipleSelection: true
});
this.indicesToBeAdded = [];
this.indicesToBeRemoved = [];
}
addMultiple(indices) {
this.indicesToBeAdded = indices.slice();
this.selectedIndices.push(...indices);
}
deselect(index, preventClosingPopup) {
if (!this.isSelected(index)) {
return;
}
const position = this.selectedIndices.indexOf(index);
this.selectedIndices.splice(position, 1);
this.focused = index;
if (this.selected.length === 0) {
this.lastClickedIndex = null;
}
this.onChange.emit({
indices: this.selectedIndices.slice(),
removed: index,
preventClosingPopup: preventClosingPopup
});
}
unselectMultiple(indices) {
indices.forEach((index) => {
const position = this.selectedIndices.indexOf(index);
this.selectedIndices.splice(position, 1);
});
this.indicesToBeRemoved = indices.slice();
}
change(index) {
const newSelection = isPresent(index) && !this.isSelected(index);
this.selectedIndices = [index];
this.focused = index;
this.onChange.emit({
indices: [index],
newSelection: newSelection
});
}
resetSelection(index) {
this.selectedIndices = index instanceof Array ? index : [index];
this.focused = this.selectedIndices[this.selectedIndices.length - 1];
}
get selected() {
return this.selectedIndices.slice();
}
get focused() {
return this.focusedIndex;
}
set focused(index) {
if (this.focusedIndex !== index) {
this.focusedIndex = index;
this.onFocus.emit(index);
}
}
selectFromTo(from, to) {
const addedIndices = [];
for (let i = from; i <= to; i++) {
if (!this.isSelected(i) && !this.disabledItemsService.isIndexDisabled(i)) {
addedIndices.push(i);
}
}
this.addMultiple(addedIndices);
}
unselectFromTo(from, to) {
const indicesToBeUnselected = [];
for (let i = from; i >= to; i--) {
if (this.isSelected(i) && !this.disabledItemsService.isIndexDisabled(i)) {
indicesToBeUnselected.push(i);
}
}
this.unselectMultiple(indicesToBeUnselected);
}
unselectNotNeededIndices(startOfSelection, endOfSelection, totalItems) {
const indicesToBeUnselected = [];
for (let i = 0; i < startOfSelection; i++) {
if (this.isSelected(i)) {
indicesToBeUnselected.push(i);
}
}
for (let i = endOfSelection + 1; i < totalItems; i++) {
if (this.isSelected(i)) {
indicesToBeUnselected.push(i);
}
}
this.unselectMultiple(indicesToBeUnselected);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SelectionService, deps: [{ token: i1.DisabledItemsService }], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SelectionService });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SelectionService, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: i1.DisabledItemsService }]; } });