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: "
\n
\n \n {{_placeholder}}\n \n (this._settings.itemsShowLimit-1)\" [attr.title]=\"item.tooltip\">\n {{item.text}} \n x\n \n\n \n \n 0\">+{{itemShowRemaining()}}\n \n \n \n
\n
\n \n \n
\n
\n", providers: [DROPDOWN_CONTROL_VALUE_ACCESSOR], changeDetection: ChangeDetectionStrategy.OnPush, styles: [".multiselect-dropdown{position:relative;width:100%;font-size:inherit;font-family:inherit}.multiselect-dropdown .dropdown-btn{display:inline-block;border:1px solid #adadad;width:100%;padding:6px 12px;margin-bottom:0;font-weight:400;line-height:1.52857143;text-align:left;vertical-align:middle;cursor:pointer;background-image:none;border-radius:4px}.multiselect-dropdown .dropdown-btn .selected-item-container{display:flex;float:left;max-width:93%}.multiselect-dropdown .dropdown-btn .selected-item-container .selected-item{border:1px solid #337ab7;margin-right:4px;margin-bottom:4px;background:#337ab7;padding:0 5px;color:#fff;border-radius:2px;float:left}.multiselect-dropdown .dropdown-btn .selected-item-container .selected-item span{overflow:hidden;text-overflow:ellipsis}.multiselect-dropdown .dropdown-btn .selected-item-container .selected-item a{text-decoration:none}.multiselect-dropdown .dropdown-btn .selected-item:hover{box-shadow:1px 1px #959595}.multiselect-dropdown .dropdown-btn .dropdown-multiselect__caret{line-height:16px;display:block;position:absolute;box-sizing:border-box;width:40px;height:38px;right:1px;top:0;padding:4px 8px;margin:0;text-decoration:none;text-align:center;cursor:pointer;transition:transform .2s}.multiselect-dropdown .dropdown-btn .dropdown-multiselect__caret:before{position:relative;right:0;top:65%;color:#999;margin-top:4px;border-style:solid;border-width:8px 8px 0;border-color:#999 transparent;content:\"\"}.multiselect-dropdown .dropdown-btn .dropdown-multiselect--active .dropdown-multiselect__caret{transform:rotateZ(180deg)}.multiselect-dropdown .disabled>span{background-color:#eceeef}.dropdown-list{position:absolute;padding-top:6px;width:100%;z-index:9999;border:1px solid #ccc;border-radius:3px;background:#fff;margin-top:10px;box-shadow:0 1px 5px #959595}.dropdown-list ul{padding:0;list-style:none;overflow:auto;margin:0}.dropdown-list li{padding:6px 10px;cursor:pointer;text-align:left}.dropdown-list .filter-textbox{border-bottom:1px solid #ccc;position:relative;padding:10px}.dropdown-list .filter-textbox input{border:0;width:100%;padding:0 0 0 26px}.dropdown-list .filter-textbox input:focus{outline:0}.multiselect-item-checkbox:hover{background-color:#e4e3e3}.multiselect-item-checkbox input[type=checkbox]{border:0;clip:rect(0 0 0 0);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px}.multiselect-item-checkbox input[type=checkbox]:focus+div:before,.multiselect-item-checkbox input[type=checkbox]:hover+div:before{border-color:#337ab7;background-color:#f2f2f2}.multiselect-item-checkbox input[type=checkbox]:active+div:before{transition-duration:0s}.multiselect-item-checkbox input[type=checkbox]+div{position:relative;padding-left:2em;vertical-align:middle;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;margin:0;color:#000}.multiselect-item-checkbox input[type=checkbox]+div:before{box-sizing:content-box;content:\"\";color:#337ab7;position:absolute;top:50%;left:0;width:14px;height:14px;margin-top:-9px;border:2px solid #337ab7;text-align:center;transition:.4s}.multiselect-item-checkbox input[type=checkbox]+div:after{box-sizing:content-box;content:\"\";position:absolute;transform:scale(0);transform-origin:50%;transition:transform .2s ease-out;background-color:transparent;top:50%;left:4px;width:8px;height:3px;margin-top:-4px;border-style:solid;border-color:#fff;border-width:0 0 3px 3px;-o-border-image:none;border-image:none;transform:rotate(-45deg) scale(0)}.multiselect-item-checkbox input[type=checkbox]:disabled+div:before{border-color:#ccc}.multiselect-item-checkbox input[type=checkbox]:disabled:focus+div:before .multiselect-item-checkbox input[type=checkbox]:disabled:hover+div:before{background-color:inherit}.multiselect-item-checkbox input[type=checkbox]:disabled:checked+div:before{background-color:#ccc}.multiselect-item-checkbox input[type=checkbox]:checked+div:after{content:\"\";transition:transform .2s ease-out;transform:rotate(-45deg) scale(1)}.multiselect-item-checkbox input[type=checkbox]:checked+div:before{-webkit-animation:.2s ease-in borderscale;animation:.2s ease-in borderscale;background:#337ab7}@-webkit-keyframes borderscale{50%{box-shadow:0 0 0 2px #337ab7}}@keyframes borderscale{50%{box-shadow:0 0 0 2px #337ab7}}"] }) ], MultiSelectComponent); let ClickOutsideDirective = class ClickOutsideDirective { constructor(_elementRef) { this._elementRef = _elementRef; this.clickOutside = new EventEmitter(); } onClick(event, targetElement) { if (!targetElement) { return; } const clickedInside = this._elementRef.nativeElement.contains(targetElement); if (!clickedInside) { this.clickOutside.emit(event); } } }; ClickOutsideDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Output() ], ClickOutsideDirective.prototype, "clickOutside", void 0); __decorate([ HostListener('document:click', ['$event', '$event.target']) ], ClickOutsideDirective.prototype, "onClick", null); ClickOutsideDirective = __decorate([ Directive({ selector: '[clickOutside]' }) ], ClickOutsideDirective); var NgMultiSelectDropDownModule_1; let NgMultiSelectDropDownModule = NgMultiSelectDropDownModule_1 = class NgMultiSelectDropDownModule { static forRoot() { return { ngModule: NgMultiSelectDropDownModule_1 }; } }; NgMultiSelectDropDownModule = NgMultiSelectDropDownModule_1 = __decorate([ NgModule({ imports: [CommonModule, FormsModule], declarations: [MultiSelectComponent, ClickOutsideDirective, ListFilterPipe], providers: [ListFilterPipe], exports: [MultiSelectComponent] }) ], NgMultiSelectDropDownModule); /** * Generated bundle index. Do not edit. */ export { MultiSelectComponent, NgMultiSelectDropDownModule, DROPDOWN_CONTROL_VALUE_ACCESSOR as ɵa, ListFilterPipe as ɵb, ClickOutsideDirective as ɵc }; //# sourceMappingURL=ng-multiselect-dropdown.js.map