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 | 8x 8x 8x 8x 21x 7x 2x 5x 14x 21x 21x 8x 9x 9x 3x 6x 6x 1x 1x 5x 1x 4x 4x 1x 3x 3x 2x 2x 9x 2x 1x 2x 3x 8x 8x 8x 8x 8x 31x 8x 23x 22x 1x 8x | import Vue, { PropType } from 'vue'; import Component, { mixins } from 'vue-class-component'; import { ChoiceFieldValue, FieldItem, FieldOptions, FieldItemValue } from '../types'; const Props = Vue.extend({ props: { /** The choice field value */ value: { type: [Array, Number, String] as PropType<ChoiceFieldValue>, default: null }, options: { type: Object as PropType<FieldOptions>, default: null }, multiple: { type: Boolean, default: false }, items: { type: Array as PropType<FieldItem[]>, required: true } } }); const MixinsDeclaration = mixins(Props); /** Mixin to control the item selection */ @Component<ChoiceComponent>({ watch: { // Listen the current field value for the component value: { handler(value: ChoiceFieldValue): void { if (value !== null && value !== undefined) { /** In multiple mode, put the value in an array if it wasn't already */ if (this.multiple && !Array.isArray(value)) { this.choiceFieldValue = [value]; } else { this.choiceFieldValue = value; } } else { // Reset the choice value this.choiceFieldValue = this.multiple ? [] : null; } }, immediate: true, deep: true } } }) export class ChoiceComponent extends MixinsDeclaration { choiceFieldValue: ChoiceFieldValue | null = this.multiple ? [] : null; /** * Toggle the item * * @param {FieldItem} item The selected item * @returns {void} */ toggleItem(item: FieldItem): void { const active = this.isSelected(item.value); let newChoiceValue: ChoiceFieldValue; // Set the new value in simple mode if (!this.multiple) { newChoiceValue = active ? null : item.value; } else { newChoiceValue = [ ...this.choiceFieldValue as FieldItemValue[] ]; if (active && Array.isArray(this.choiceFieldValue)) { // Unselect the item const valueIndex = this.choiceFieldValue.indexOf(item.value); newChoiceValue.splice(valueIndex, 1); } else { // Can't select a null value in multiple mode if (item.value === undefined || item.value === null) { return; } const isAlone = Boolean(item.alone); // If item alone, unselect all other if (isAlone) { newChoiceValue = [item.value]; } else { // Else unselect all alone items let index = newChoiceValue.length - 1; while (index >= 0) { const valueSelected = (newChoiceValue as FieldItemValue[])[index]; // Search if the item is alone const isItemSelectedAlone = this.items.find((element) => { return element.value === valueSelected && element.alone; }); if (isItemSelectedAlone) { // Delete alone item newChoiceValue.splice(index, 1); } index -= 1; } // Add the item to the selection newChoiceValue.push(item.value); } } } this.choiceFieldValue = newChoiceValue; this.emitChangeEvent(this.choiceFieldValue); } emitChangeEvent(newValue: ChoiceFieldValue): void { this.$emit('change', newValue); } /** * Check if the button is selected * * @param {FieldItemValue} value The button value to test if it is selected * @returns {boolean} The selected value */ isSelected(value: FieldItemValue): boolean { if (!this.choiceFieldValue) { return false; } if (this.multiple && Array.isArray(this.choiceFieldValue)) { return this.choiceFieldValue.includes(value); } else { return this.choiceFieldValue === value; } } } |