All files / src/custom-element/mixins/components/selection-container SelectionContainerMixin.ts

43.75% Statements 14/32
37.5% Branches 6/16
71.43% Functions 5/7
45.16% Lines 14/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153  2x         2x   3x   2x       10x                                                                                     15x                         2x   2x         10x   10x   3x   1x       2x                                                                                                                                 2x
import { CustomElementPropertyMetadata, CustomElementStateMetadata } from "../../../interfaces";
import { selectionChanged } from "../selectable/SelectableMixin";
 
/**
 * Notifies the container when one of the selection in the children has changed
 */
const SelectionContainerMixin = Base =>
 
    class SelectionContainer extends Base {
 
        isSelectionContainer = true; // Mark the instance as a selection 
 
        static get properties(): Record<string, CustomElementPropertyMetadata> {
 
            return {
 
                /**
                 * Whether the component is selectable
                 */
                selectable: {
                    type: Boolean,
                    value: true,
                    reflect: true,
                    //inherit: true
                },
 
                /**
                 * Whether we can process multiple selection (false by default)
                 */
                multiple: {
                    type: Boolean,
                    reflect: true
                },
 
                /**
                 * The selected item or items. It is an attribute since it can be passed through a property initially
                 */
                selection: {
                    type: Array,
                    value: [],
                    mutable: true,
                    reflect: true
                },
 
                /**
                 * The handler to call when the selection has changed
                 */
                selectionChanged: {
                    attribute: 'selection-changed',
                    type: Function,
                    defer: true
                }
            };
        }
 
        static get state(): Record<string, CustomElementStateMetadata> {
 
            return {
 
                /**
                 * To track the current selected child for a single selection model
                 */
                selectedChild: {
                    value: undefined
                }
            };
        }
 
        constructor() {
 
            super();
 
            this.updateSelection = this.updateSelection.bind(this);
        }
 
        attributeChangedCallback(attributeName: string, oldValue: string, newValue: string) {
 
            super.attributeChangedCallback?.(attributeName, oldValue, newValue);
 
            if (attributeName === "selectable") {
 
                if (newValue === "true" || newValue === "") {
 
                    this.addEventListener(selectionChanged, this.updateSelection);
                }
                else { // newValue === "false"
 
                    this.removeEventListener('selectionChanged', this.updateSelection);
                }
            }
        }
 
        updateSelection(event: CustomEvent) {
 
            event.stopPropagation();
 
            const {
                multiple,
                selection,
                selectionChanged
            } = this;
 
            const {
                element,
                selected,
                value
            } = event.detail;
 
            if (multiple !== undefined) {
 
                if (selected === true) { // Add the value to the selection
 
                    this.selection = [...selection, value];
                }
                else { // Remove the value from the selection
 
                    this.selection = selection.filter(item => item.id !== value.id);
                }
            }
            else { // Replace the old selection with the new one
 
                const {
                    selectedChild
                } = this;
 
                // Deselect previous selected child
                if (selectedChild !== undefined) {
 
                    selectedChild.selected = false;
                }
 
                this.selection.length = 0; // Clear the selection
 
                if (selected === true) {
 
                    this.selection.push(value);
 
                    this.selectedChild = element;
                }
                else {
 
                    this.selectedChild = undefined;
                }
            }
 
            if (selectionChanged !== undefined) {
 
                selectionChanged(this.selection);
            }
        }
    };
 
export default SelectionContainerMixin;