r3bl-ts-utils
Version:
The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu
82 lines • 3.17 kB
JavaScript
/*
* Copyright (c) 2022 R3BL LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectionModel = void 0;
const expression_lang_utils_1 = require("../../lang-utils/expression-lang-utils");
const tui_colors_1 = require("../../tui-colors");
const DEBUG = false;
/** @immutable */
class SelectionModel {
selected;
singleSelectionMode;
// Mutator methods (all immutable).
/** @immutable */
constructor(initialSelected, singleSelectionMode) {
this.selected = new Set(initialSelected); // Make shallow copy.
this.singleSelectionMode = singleSelectionMode;
}
/** @immutable */
clearAllSelections = () => {
return new SelectionModel([], this.singleSelectionMode);
};
/** @immutable */
toggleSelectionFor = (selectedItem, onSelect, onUnselect) => {
const isSelected = this.isItemSelected(selectedItem);
(0, expression_lang_utils_1._callIfTruthy)(DEBUG, () => {
const green = tui_colors_1.TextColor.builder.green.build();
const red = tui_colors_1.TextColor.builder.red.build();
console.log("| selectedItem.key:", selectedItem.key, "| selectedItem.label:", selectedItem.label, "| isSelected", isSelected ? green("true") : red("false"));
});
const addToSelected = (arg) => {
this.selected.add(arg);
};
const removeFromSelected = (selectedItem) => {
this.selected.delete(selectedItem);
};
const select = (selectedItem) => {
if (this.singleSelectionMode && this.selected.size > 0) {
this.clearAllSelections();
addToSelected(selectedItem);
}
else {
addToSelected(selectedItem);
}
onSelect(selectedItem);
};
const unselect = (selectedItem) => {
removeFromSelected(selectedItem);
onUnselect(selectedItem);
};
isSelected ? unselect(selectedItem) : select(selectedItem);
return new SelectionModel(this.getSelection(), this.singleSelectionMode);
};
// Read only methods.
/**
* @immutable
* https://stackoverflow.com/a/20070691/2085356
*/
getSelection = () => [...this.selected]; // Return shallow copy.
isItemSelected = (query) => {
return this.selected.has(query);
};
toString = () => this.getSelection()
.map(({ label }) => label)
.join(", ");
}
exports.SelectionModel = SelectionModel;
//# sourceMappingURL=selection-model.js.map
;