import { __decorate } from 'tslib'; import { Pipe, forwardRef, EventEmitter, ChangeDetectorRef, Input, Output, HostListener, Component, ChangeDetectionStrategy, ElementRef, Directive, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms'; import { CommonModule } from '@angular/common'; class ListItem { constructor(source) { if (typeof source === 'string' || typeof source === 'number') { this.id = this.text = source; this.isDisabled = false; } if (typeof source === 'object') { this.id = source.id; this.text = source.text; this.tooltip = source.tooltip; this.isDisabled = source.isDisabled; } } } let ListFilterPipe = class ListFilterPipe { transform(items, filter) { if (!items || !filter) { return items; } return items.filter((item) => this.applyFilter(item, filter)); } applyFilter(item, filter) { if (typeof item.text === 'string' && typeof filter.text === 'string') { return !(filter.text && item.text && item.text.toLowerCase().indexOf(filter.text.toLowerCase()) === -1); } else { return !(filter.text && item.text && item.text.toString().toLowerCase().indexOf(filter.text.toString().toLowerCase()) === -1); } } }; ListFilterPipe = __decorate([ Pipe({ name: 'multiSelectFilter', pure: false }) ], ListFilterPipe); const DROPDOWN_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => MultiSelectComponent), multi: true }; const noop = () => { }; const ɵ0 = noop; let MultiSelectComponent = class MultiSelectComponent { constructor(listFilterPipe, cdr) { this.listFilterPipe = listFilterPipe; this.cdr = cdr; this._data = []; this.selectedItems = []; this.isDropdownOpen = true; this._placeholder = "Select"; this._sourceDataType = null; // to keep note of the source data type. could be array of string/number/object this._sourceDataFields = []; // store source data fields names this.filter = new ListItem(this.data); this.defaultSettings = { singleSelection: false, idField: "id", textField: "text", tooltipField: "tooltip", disabledField: "isDisabled", enableCheckAll: true, selectAllText: "Select All", unSelectAllText: "UnSelect All", allowSearchFilter: false, limitSelection: -1, clearSearchFilter: true, maxHeight: 197, itemsShowLimit: 999999999999, searchPlaceholderText: "Search", noDataAvailablePlaceholderText: "No data available", noFilteredDataAvailablePlaceholderText: "No filtered data available", closeDropDownOnSelection: false, showSelectedItemsAtTop: false, defaultOpen: false, allowRemoteDataSearch: false }; this.disabled = false; this.onFilterChange = new EventEmitter(); this.onDropDownClose = new EventEmitter(); this.onSelect = new EventEmitter(); this.onDeSelect = new EventEmitter(); this.onSelectAll = new EventEmitter(); this.onDeSelectAll = new EventEmitter(); this.onTouchedCallback = noop; this.onChangeCallback = noop; } set placeholder(value) { if (value) { this._placeholder = value; } else { this._placeholder = "Select"; } } set settings(value) { if (value) { this._settings = Object.assign(this.defaultSettings, value); } else { this._settings = Object.assign(this.defaultSettings); } } set data(value) { if (!value) { this._data = []; } else { const firstItem = value[0]; this._sourceDataType = typeof firstItem; this._sourceDataFields = this.getFields(firstItem); this._data = value.map(item => this.deobjectify(item)); } } onFilterTextChange($event) { this.onFilterChange.emit($event); } onItemClick($event, item) { if (this.disabled || item.isDisabled) { return false; } const found = this.isSelected(item); const allowAdd = this._settings.limitSelection === -1 || (this._settings.limitSelection > 0 && this.selectedItems.length < this._settings.limitSelection); if (!found) { if (allowAdd) { this.addSelected(item); } } else { this.removeSelected(item); } if (this._settings.singleSelection && this._settings.closeDropDownOnSelection) { this.closeDropdown(); } } writeValue(value) { if (value !== undefined && value !== null && value.length > 0) { if (this._settings.singleSelection) { try { if (value.length >= 1) { this.selectedItems = [this.deobjectify(value[0])]; } } catch (e) { // console.error(e.body.msg); } } else { const _data = value.map((item) => this.deobjectify(item)); if (this._settings.limitSelection > 0) { this.selectedItems = _data.splice(0, this._settings.limitSelection); } else { this.selectedItems = _data; } } } else { this.selectedItems = []; } this.onChangeCallback(value); this.cdr.markForCheck(); } // From ControlValueAccessor interface registerOnChange(fn) { this.onChangeCallback = fn; } // From ControlValueAccessor interface registerOnTouched(fn) { this.onTouchedCallback = fn; } // Set touched on blur onTouched() { // this.closeDropdown(); this.onTouchedCallback(); } trackByFn(index, item) { return item.id; } isSelected(clickedItem) { let found = false; this.selectedItems.forEach(item => { if (clickedItem.id === item.id) { found = true; } }); return found; } isLimitSelectionReached() { return this._settings.limitSelection === this.selectedItems.length; } isAllItemsSelected() { // get disabld item count let filteredItems = this.listFilterPipe.transform(this._data, this.filter); const itemDisabledCount = filteredItems.filter(item => item.isDisabled).length; // take disabled items into consideration when checking if ((!this.data || this.data.length === 0) && this._settings.allowRemoteDataSearch) { return false; } return filteredItems.length === this.selectedItems.length + itemDisabledCount; } showButton() { if (!this._settings.singleSelection) { if (this._settings.limitSelection > 0) { return false; } // this._settings.enableCheckAll = this._settings.limitSelection === -1 ? true : false; return true; // !this._settings.singleSelection && this._settings.enableCheckAll && this._data.length > 0; } else { // should be disabled in single selection mode return false; } } itemShowRemaining() { return this.selectedItems.length - this._settings.itemsShowLimit; } addSelected(item) { if (this._settings.singleSelection) { this.selectedItems = []; this.selectedItems.push(item); } else { this.selectedItems.push(item); } this.onChangeCallback(this.emittedValue(this.selectedItems)); this.onSelect.emit(this.emittedValue(item)); } removeSelected(itemSel) { this.selectedItems.forEach(item => { if (itemSel.id === item.id) { this.selectedItems.splice(this.selectedItems.indexOf(item), 1); } }); this.onChangeCallback(this.emittedValue(this.selectedItems)); this.onDeSelect.emit(this.emittedValue(itemSel)); } emittedValue(val) { const selected = []; if (Array.isArray(val)) { val.map(item => { selected.push(this.objectify(item)); }); } else { if (val) { return this.objectify(val); } } return selected; } objectify(val) { if (this._sourceDataType === 'object') { const obj = {}; obj[this._settings.idField] = val.id; obj[this._settings.textField] = val.text; if (this._sourceDataFields.includes(this._settings.disabledField)) { obj[this._settings.disabledField] = val.isDisabled; } if (this._sourceDataFields.includes(this._settings.tooltipField)) { obj[this._settings.tooltipField] = val.tooltip; } return obj; } if (this._sourceDataType === 'number') { return Number(val.id); } else { return val.text; } } deobjectify(item) { if (typeof item === "string" || typeof item === "number") { return new ListItem(item); } else { return new ListItem({ id: item[this._settings.idField], text: item[this._settings.textField], tooltip: item[this._settings.tooltipField], isDisabled: item[this._settings.disabledField] }); } } toggleDropdown(evt) { evt.preventDefault(); if (this.disabled && this._settings.singleSelection) { return; } this._settings.defaultOpen = !this._settings.defaultOpen; if (!this._settings.defaultOpen) { this.onDropDownClose.emit(); } } closeDropdown() { this._settings.defaultOpen = false; // clear search text if (this._settings.clearSearchFilter) { this.filter.text = ""; } this.onDropDownClose.emit(); } toggleSelectAll() { if (this.disabled) { return false; } if (!this.isAllItemsSelected()) { // filter out disabled item first before slicing this.selectedItems = this.listFilterPipe.transform(this._data, this.filter).filter(item => !item.isDisabled).slice(); this.onSelectAll.emit(this.emittedValue(this.selectedItems)); } else { this.selectedItems = []; this.onDeSelectAll.emit(this.emittedValue(this.selectedItems)); } this.onChangeCallback(this.emittedValue(this.selectedItems)); } getFields(inputData) { const fields = []; if (typeof inputData !== "object") { return fields; } // tslint:disable-next-line:forin for (const prop in inputData) { fields.push(prop); } return fields; } }; MultiSelectComponent.ctorParameters = () => [ { type: ListFilterPipe }, { type: ChangeDetectorRef } ]; __decorate([ Input() ], MultiSelectComponent.prototype, "placeholder", null); __decorate([ Input() ], MultiSelectComponent.prototype, "disabled", void 0); __decorate([ Input() ], MultiSelectComponent.prototype, "settings", null); __decorate([ Input() ], MultiSelectComponent.prototype, "data", null); __decorate([ Output("onFilterChange") ], MultiSelectComponent.prototype, "onFilterChange", void 0); __decorate([ Output("onDropDownClose") ], MultiSelectComponent.prototype, "onDropDownClose", void 0); __decorate([ Output("onSelect") ], MultiSelectComponent.prototype, "onSelect", void 0); __decorate([ Output("onDeSelect") ], MultiSelectComponent.prototype, "onDeSelect", void 0); __decorate([ Output("onSelectAll") ], MultiSelectComponent.prototype, "onSelectAll", void 0); __decorate([ Output("onDeSelectAll") ], MultiSelectComponent.prototype, "onDeSelectAll", void 0); __decorate([ HostListener("blur") ], MultiSelectComponent.prototype, "onTouched", null); MultiSelectComponent = __decorate([ Component({ selector: "ng-multiselect-dropdown", template: "