UNPKG

azure-devops-ui

Version:

React components for building web UI in Azure DevOps

137 lines (136 loc) 6.35 kB
import { Selection } from '../../Utilities/Selection'; export class ListSelection extends Selection { constructor(options) { super(typeof options === "boolean" || options === undefined ? options : { alwaysMerge: options.alwaysMerge, multiSelect: options.multiSelect, unselectableRanges: options.unselectableRanges, selectedRanges: options.selectedRanges }); this.selectOnFocus = true; if (typeof options !== "boolean" && options !== undefined) { this.selectOnFocus = options.selectOnFocus === undefined ? true : options.selectOnFocus; } } } export class FilteredListSelection extends ListSelection { constructor(selection) { super({ alwaysMerge: selection.alwaysMerge, multiSelect: selection.multiSelect, unselectableRanges: selection.unselectableRanges, selectedRanges: selection.value, selectOnFocus: selection.selectOnFocus }); this.filteredIndexMap = []; this.updateFilteredSelection = (filteredIndexMap, multiSelect = this.selection.multiSelect) => { if (filteredIndexMap.length === 0) { this.value = [...this.selection.value]; this.unselectableRanges = [...this.selection.unselectableRanges]; } else { const newSelection = new Selection(multiSelect); filteredIndexMap.map((mappedIndex, index) => { if (this.selection.selected(mappedIndex)) { newSelection.select(index, 1, true, multiSelect); } if (!this.selection.selectable(mappedIndex)) { newSelection.addUnselectable(index); } }); this.value = [...newSelection.value]; this.unselectableRanges = [...newSelection.unselectableRanges]; } this.filteredIndexMap = filteredIndexMap; }; this.selectionChanged = (value, action) => { switch (action) { case "addUnselectable": for (let rangeIndex = 0; rangeIndex < value.length; rangeIndex++) { for (let unselectableIndex = value[rangeIndex].beginIndex; unselectableIndex <= value[rangeIndex].endIndex; unselectableIndex++) { const index = this.filteredIndexMap.length > 0 ? this.filteredIndexMap.indexOf(unselectableIndex) : unselectableIndex; if (this.selectable(index)) { this.addUnselectable(index, 1); } } } break; case "removeUnselectable": for (let rangeIndex = 0; rangeIndex < value.length; rangeIndex++) { for (let unselectableIndex = value[rangeIndex].beginIndex; unselectableIndex <= value[rangeIndex].endIndex; unselectableIndex++) { const index = this.filteredIndexMap.length > 0 ? this.filteredIndexMap.indexOf(unselectableIndex) : unselectableIndex; if (!this.selectable(index)) { this.removeUnselectable(index, 1); } } } break; case "setUnselectable": case "set": this.updateFilteredSelection(this.filteredIndexMap); break; case "select": for (let rangeIndex = 0; rangeIndex < value.length; rangeIndex++) { for (let selectionIndex = value[rangeIndex].beginIndex; selectionIndex <= value[rangeIndex].endIndex; selectionIndex++) { const index = this.filteredIndexMap.length > 0 ? this.filteredIndexMap.indexOf(selectionIndex) : selectionIndex; if (index > -1 && !this.selected(index)) { this.select(index, 1, true); } } } break; case "unselect": for (let rangeIndex = 0; rangeIndex < value.length; rangeIndex++) { for (let selectionIndex = value[rangeIndex].beginIndex; selectionIndex <= value[rangeIndex].endIndex; selectionIndex++) { const index = this.filteredIndexMap.length > 0 ? this.filteredIndexMap.indexOf(selectionIndex) : selectionIndex; if (this.selected(index)) { this.unselect(index, 1); } } } break; } }; this.selection = selection; } select(index, count, merge, multiSelect) { super.select(index, count, merge, multiSelect); if (this.filteredIndexMap.length > 0) { if (!merge) { this.clear(); } count = count || 1; for (let i = 0; i < count; i++) { this.selection.select(this.filteredIndexMap[index + i], 1, true, multiSelect); } } else { this.selection.select(index, count, merge, multiSelect); } } unselect(index, count) { super.unselect(index, count); if (this.filteredIndexMap.length > 0) { count = count || 1; for (let i = 0; i < count; i++) { this.selection.unselect(this.filteredIndexMap[index + i], 1); } } else { this.selection.unselect(index, count); } } clear() { super.clear(); if (this.filteredIndexMap.length > 0) { for (let i = 0; i < this.filteredIndexMap.length; i++) { this.selection.unselect(this.filteredIndexMap[i]); } } else { this.selection.clear(); } } }