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'; var ListItem = /** @class */ (function () { function ListItem(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; } } return ListItem; }()); var ListFilterPipe = /** @class */ (function () { function ListFilterPipe() { } ListFilterPipe.prototype.transform = function (items, filter) { var _this = this; if (!items || !filter) { return items; } return items.filter(function (item) { return _this.applyFilter(item, filter); }); }; ListFilterPipe.prototype.applyFilter = function (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); return ListFilterPipe; }()); var DROPDOWN_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(function () { return MultiSelectComponent; }), multi: true }; var noop = function () { }; var ɵ0 = noop; var MultiSelectComponent = /** @class */ (function () { function MultiSelectComponent(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; } Object.defineProperty(MultiSelectComponent.prototype, "placeholder", { set: function (value) { if (value) { this._placeholder = value; } else { this._placeholder = "Select"; } }, enumerable: true, configurable: true }); Object.defineProperty(MultiSelectComponent.prototype, "settings", { set: function (value) { if (value) { this._settings = Object.assign(this.defaultSettings, value); } else { this._settings = Object.assign(this.defaultSettings); } }, enumerable: true, configurable: true }); Object.defineProperty(MultiSelectComponent.prototype, "data", { set: function (value) { var _this = this; if (!value) { this._data = []; } else { var firstItem = value[0]; this._sourceDataType = typeof firstItem; this._sourceDataFields = this.getFields(firstItem); this._data = value.map(function (item) { return _this.deobjectify(item); }); } }, enumerable: true, configurable: true }); MultiSelectComponent.prototype.onFilterTextChange = function ($event) { this.onFilterChange.emit($event); }; MultiSelectComponent.prototype.onItemClick = function ($event, item) { if (this.disabled || item.isDisabled) { return false; } var found = this.isSelected(item); var 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(); } }; MultiSelectComponent.prototype.writeValue = function (value) { var _this = this; 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 { var _data = value.map(function (item) { return _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 MultiSelectComponent.prototype.registerOnChange = function (fn) { this.onChangeCallback = fn; }; // From ControlValueAccessor interface MultiSelectComponent.prototype.registerOnTouched = function (fn) { this.onTouchedCallback = fn; }; // Set touched on blur MultiSelectComponent.prototype.onTouched = function () { // this.closeDropdown(); this.onTouchedCallback(); }; MultiSelectComponent.prototype.trackByFn = function (index, item) { return item.id; }; MultiSelectComponent.prototype.isSelected = function (clickedItem) { var found = false; this.selectedItems.forEach(function (item) { if (clickedItem.id === item.id) { found = true; } }); return found; }; MultiSelectComponent.prototype.isLimitSelectionReached = function () { return this._settings.limitSelection === this.selectedItems.length; }; MultiSelectComponent.prototype.isAllItemsSelected = function () { // get disabld item count var filteredItems = this.listFilterPipe.transform(this._data, this.filter); var itemDisabledCount = filteredItems.filter(function (item) { return 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; }; MultiSelectComponent.prototype.showButton = function () { 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; } }; MultiSelectComponent.prototype.itemShowRemaining = function () { return this.selectedItems.length - this._settings.itemsShowLimit; }; MultiSelectComponent.prototype.addSelected = function (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)); }; MultiSelectComponent.prototype.removeSelected = function (itemSel) { var _this = this; this.selectedItems.forEach(function (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)); }; MultiSelectComponent.prototype.emittedValue = function (val) { var _this = this; var selected = []; if (Array.isArray(val)) { val.map(function (item) { selected.push(_this.objectify(item)); }); } else { if (val) { return this.objectify(val); } } return selected; }; MultiSelectComponent.prototype.objectify = function (val) { if (this._sourceDataType === 'object') { var 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; } }; MultiSelectComponent.prototype.deobjectify = function (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] }); } }; MultiSelectComponent.prototype.toggleDropdown = function (evt) { evt.preventDefault(); if (this.disabled && this._settings.singleSelection) { return; } this._settings.defaultOpen = !this._settings.defaultOpen; if (!this._settings.defaultOpen) { this.onDropDownClose.emit(); } }; MultiSelectComponent.prototype.closeDropdown = function () { this._settings.defaultOpen = false; // clear search text if (this._settings.clearSearchFilter) { this.filter.text = ""; } this.onDropDownClose.emit(); }; MultiSelectComponent.prototype.toggleSelectAll = function () { 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(function (item) { return !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)); }; MultiSelectComponent.prototype.getFields = function (inputData) { var fields = []; if (typeof inputData !== "object") { return fields; } // tslint:disable-next-line:forin for (var prop in inputData) { fields.push(prop); } return fields; }; MultiSelectComponent.ctorParameters = function () { return [ { 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: "