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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 3x 26x 1x 1x 2x 4x 1x 26x 3x 1x 2x 1x 1x 26x 3x 26x 2x 1x 1x 1x 3x 3x 1x 1x 1x 2x 2x 3x 1x 2x 2x 2x 1x 1x 1x 1x 3x 1x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 1x 1x 2x 1x 2x 1x 1x 1x 4x 4x 2x 1x | import { nextTick, Ref, ref, computed } from 'vue';
import { IOption, IOptionGroup } from '../../interfaces/options.interface';
const MINIMUM_BOTTOM_SPACE_NEEDED_ON_BASE_REM_16 = 100;
export default class MultiSelectComponent {
public dropDown: Ref<HTMLElement | null> = ref(null);
public selectedOption: Ref<IOption | null> = ref(null);
public selectedOptions: Ref<IOption[]> = ref([]);
public isActive: Ref<boolean> = ref(false);
public shouldSearchBarBeBelow: Ref<boolean> = ref(false);
private filteredOptions: Ref<IOption[]> = ref([]);
private filteredGroups: Ref<IOptionGroup[]> = ref([]);
constructor(
private emit: (action: string, value: IOption | IOption[] | null) => void,
private options: Ref<IOption[]>,
private placeholder: Ref<string>,
private entity: Ref<string>,
private multipleOptions: Ref<boolean>,
private disabled: Ref<boolean>,
public groups: Ref<IOptionGroup[]>
) { }
public availableOptions = computed((): IOption[] => {
return this.filteredOptions.value.filter((option: IOption) => {
return this.selectedOptions.value.indexOf(option) === -1;
});
});
public availableGroupedOptions = computed((): IOptionGroup[] => {
const options: IOptionGroup[] = [];
this.filteredGroups.value.forEach((group: IOptionGroup) => {
options.push({
label: group.label,
options: group.options.filter((option: IOption) => {
return this.selectedOptions.value.indexOf(option) === -1;
})
});
});
return options;
});
public inputPlaceholder = computed(() => {
if (this.selectedOption.value !== null && !this.multipleOptions.value) {
return this.selectedOption.value.label;
}
if (this.placeholder.value === "") {
return "Select an option";
}
return this.placeholder.value;
});
public optionsSelected = computed((): boolean => {
return this.selectedOption.value !== null || this.selectedOptions.value.length > 0;
});
public searchPlaceholder = computed((): string => {
if (this.entity.value === "") {
return "Search";
}
return `Search by ${this.entity.value}`;
});
public mounted(): void {
this.updateSearch("");
}
public selectOption(option: IOption | null): void {
this.isActive.value = false;
if (this.multipleOptions.value && option) {
this.selectedOptions.value.push(option);
this.emit('updated', this.selectedOptions.value);
return;
}
this.selectedOption.value = option;
this.emit('updated', this.selectedOption.value);
}
public toggleMultiSelect(): void {
if (this.disabled.value) {
return;
}
this.isActive.value = !this.isActive.value;
// Reset the search query.
this.updateSearch("");
// Check if the dropdown overflows the page.
if (this.isActive.value) {
void this.checkDropdownOverflow();
}
}
public deactivate(): void {
this.isActive.value = false;
// Reset the search query.
this.updateSearch("");
}
public deleteOption(deletedOption: IOption): void {
this.selectedOptions.value = this.selectedOptions.value.filter((option: IOption) => {
return option !== deletedOption;
});
this.emit('updated', this.selectedOptions.value);
}
public updateSearch(query: string): void {
if (this.groups.value.length > 0) {
this.filteredGroups.value = this.filterGroups(query);
return;
}
this.filteredOptions.value = this.filterOptions(this.options.value, query);
}
private async checkDropdownOverflow(): Promise<void> {
await nextTick();
const { overflowDirection } = this;
if (overflowDirection === 'bottom') {
await this.overflowDown();
return;
}
await this.overflowUp();
}
private async overflowDown(): Promise<void> {
Iif (this.dropDown.value === null) return;
const dropdown: HTMLElement = this.dropDown.value as HTMLElement;
const bounds = dropdown.getBoundingClientRect();
const overflow: number = (bounds.y + bounds.height) - window.innerHeight;
const remValue = 16;
if (overflow > 0) {
dropdown.style.height = bounds.height - (overflow + remValue) + 'px';
}
dropdown.classList.remove('overflow-up');
this.shouldSearchBarBeBelow.value = false;
}
private async overflowUp(): Promise<void> {
Iif (this.dropDown.value === null) return;
const dropdown: HTMLElement = this.dropDown.value as HTMLElement;
const bounds = dropdown.getBoundingClientRect();
const overflow: number = bounds.y;
dropdown.style.height = bounds.height + overflow + 'px';
dropdown.classList.add('overflow-up');
this.shouldSearchBarBeBelow.value = true;
}
private get overflowDirection(): 'top' | 'bottom' {
Iif (this.dropDown.value === null) return 'bottom';
const dropdown: HTMLElement = this.dropDown.value as HTMLElement;
const bounds = dropdown.getBoundingClientRect();
const overflow: number = (bounds.y + bounds.height) - window.innerHeight;
return overflow <= MINIMUM_BOTTOM_SPACE_NEEDED_ON_BASE_REM_16
? 'bottom'
: 'top';
}
private filterGroups(query: string): IOptionGroup[] {
let filteredGroups: IOptionGroup[] = [];
this.groups.value.forEach((group: IOptionGroup) => {
filteredGroups.push(
{
...group,
options: this.filterOptions(group.options, query)
}
);
})
return filteredGroups;
}
private filterOptions(options: IOption[], query: string): IOption[] {
if (query === "") {
return options;
}
let filteredOptions: IOption[] = [];
options.forEach((option: IOption) => {
let lowercaseValue: string = option.label.toLowerCase();
if (option.label.indexOf(query) !== -1 || lowercaseValue.indexOf(query) !== -1) {
filteredOptions.push(option);
}
});
return filteredOptions;
}
} |