"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var common_1 = require("@angular/common"); var shared_1 = require("../common/shared"); var domhandler_1 = require("../dom/domhandler"); var objectutils_1 = require("../utils/objectutils"); var forms_1 = require("@angular/forms"); var filterutils_1 = require("../utils/filterutils"); exports.LISTBOX_VALUE_ACCESSOR = { provide: forms_1.NG_VALUE_ACCESSOR, useExisting: core_1.forwardRef(function () { return Listbox; }), multi: true }; var Listbox = /** @class */ (function () { function Listbox(el, cd) { this.el = el; this.cd = cd; this.checkbox = false; this.filter = false; this.filterMode = 'contains'; this.metaKeySelection = true; this.showToggleAll = true; this.onChange = new core_1.EventEmitter(); this.onClick = new core_1.EventEmitter(); this.onDblClick = new core_1.EventEmitter(); this.onModelChange = function () { }; this.onModelTouched = function () { }; this.disabledSelectedOptions = []; } Object.defineProperty(Listbox.prototype, "options", { get: function () { return this._options; }, set: function (val) { var opts = this.optionLabel ? objectutils_1.ObjectUtils.generateSelectItems(val, this.optionLabel) : val; this._options = opts; }, enumerable: true, configurable: true }); Object.defineProperty(Listbox.prototype, "filterValue", { get: function () { return this._filterValue; }, set: function (val) { this._filterValue = val; }, enumerable: true, configurable: true }); Listbox.prototype.ngAfterContentInit = function () { var _this = this; this.templates.forEach(function (item) { switch (item.getType()) { case 'item': _this.itemTemplate = item.template; break; default: _this.itemTemplate = item.template; break; } }); }; Listbox.prototype.writeValue = function (value) { this.value = value; this.setDisabledSelectedOptions(); this.cd.markForCheck(); }; Listbox.prototype.registerOnChange = function (fn) { this.onModelChange = fn; }; Listbox.prototype.registerOnTouched = function (fn) { this.onModelTouched = fn; }; Listbox.prototype.setDisabledState = function (val) { this.disabled = val; }; Listbox.prototype.onOptionClick = function (event, option) { if (this.disabled || option.disabled || this.readonly) { return; } if (this.multiple) { if (this.checkbox) this.onOptionClickCheckbox(event, option); else this.onOptionClickMultiple(event, option); } else { this.onOptionClickSingle(event, option); } this.onClick.emit({ originalEvent: event, option: option, value: this.value }); this.optionTouched = false; }; Listbox.prototype.onOptionTouchEnd = function (event, option) { if (this.disabled || option.disabled || this.readonly) { return; } this.optionTouched = true; }; Listbox.prototype.onOptionDoubleClick = function (event, option) { if (this.disabled || option.disabled || this.readonly) { return; } this.onDblClick.emit({ originalEvent: event, option: option, value: this.value }); }; Listbox.prototype.onOptionClickSingle = function (event, option) { var selected = this.isSelected(option); var valueChanged = false; var metaSelection = this.optionTouched ? false : this.metaKeySelection; if (metaSelection) { var metaKey = (event.metaKey || event.ctrlKey); if (selected) { if (metaKey) { this.value = null; valueChanged = true; } } else { this.value = option.value; valueChanged = true; } } else { this.value = selected ? null : option.value; valueChanged = true; } if (valueChanged) { this.onModelChange(this.value); this.onChange.emit({ originalEvent: event, value: this.value }); } }; Listbox.prototype.onOptionClickMultiple = function (event, option) { var selected = this.isSelected(option); var valueChanged = false; var metaSelection = this.optionTouched ? false : this.metaKeySelection; if (metaSelection) { var metaKey = (event.metaKey || event.ctrlKey); if (selected) { if (metaKey) { this.removeOption(option); } else { this.value = [option.value]; } valueChanged = true; } else { this.value = (metaKey) ? this.value || [] : []; this.value = this.value.concat([option.value]); valueChanged = true; } } else { if (selected) { this.removeOption(option); } else { this.value = (this.value || []).concat([option.value]); } valueChanged = true; } if (valueChanged) { this.onModelChange(this.value); this.onChange.emit({ originalEvent: event, value: this.value }); } }; Listbox.prototype.onOptionClickCheckbox = function (event, option) { if (this.disabled || this.readonly) { return; } var selected = this.isSelected(option); if (selected) { this.removeOption(option); } else { this.value = this.value ? this.value : []; this.value = this.value.concat([option.value]); } this.onModelChange(this.value); this.onChange.emit({ originalEvent: event, value: this.value }); }; Listbox.prototype.removeOption = function (option) { var _this = this; this.value = this.value.filter(function (val) { return !objectutils_1.ObjectUtils.equals(val, option.value, _this.dataKey); }); }; Listbox.prototype.isSelected = function (option) { var selected = false; if (this.multiple) { if (this.value) { for (var _i = 0, _a = this.value; _i < _a.length; _i++) { var val = _a[_i]; if (objectutils_1.ObjectUtils.equals(val, option.value, this.dataKey)) { selected = true; break; } } } } else { selected = objectutils_1.ObjectUtils.equals(this.value, option.value, this.dataKey); } return selected; }; Object.defineProperty(Listbox.prototype, "allChecked", { get: function () { if (this.filterValue) { return this.allFilteredSelected(); } else { var optionCount = this.getEnabledOptionCount(); var disabledSelectedOptionCount = this.disabledSelectedOptions.length; return this.value && this.options && (this.value.length > 0 && this.value.length == optionCount + disabledSelectedOptionCount); } }, enumerable: true, configurable: true }); Listbox.prototype.getEnabledOptionCount = function () { if (this.options) { var count = 0; for (var _i = 0, _a = this.options; _i < _a.length; _i++) { var opt = _a[_i]; if (!opt.disabled) { count++; } } return count; } else { return 0; } }; Listbox.prototype.allFilteredSelected = function () { var allSelected; var options = this.filterValue ? this.getFilteredOptions() : this.options; if (this.value && options && options.length) { allSelected = true; for (var _i = 0, _a = this.options; _i < _a.length; _i++) { var opt = _a[_i]; if (this.isItemVisible(opt)) { if (!this.isSelected(opt)) { allSelected = false; break; } } } } return allSelected; }; Listbox.prototype.onFilter = function (event) { this._filterValue = event.target.value; }; Listbox.prototype.toggleAll = function (event) { if (this.disabled || this.readonly || !this.options || this.options.length === 0) { return; } if (this.allChecked) { if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) { var value = []; value = this.disabledSelectedOptions.slice(); this.value = value; } else { this.value = []; } } else { if (this.options) { this.value = []; if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) { this.value = this.disabledSelectedOptions.slice(); } for (var i = 0; i < this.options.length; i++) { var opt = this.options[i]; if (this.isItemVisible(opt) && !opt.disabled) { this.value.push(opt.value); } } } } this.onModelChange(this.value); this.onChange.emit({ originalEvent: event, value: this.value }); event.preventDefault(); }; Listbox.prototype.isItemVisible = function (option) { if (this.filterValue) { var visible = void 0; var filterText = objectutils_1.ObjectUtils.removeAccents(this.filterValue).toLowerCase(); if (this.filterMode) { visible = filterutils_1.FilterUtils[this.filterMode](option.label, this.filterValue); } else { visible = true; } return visible; } else { return true; } }; Listbox.prototype.onInputFocus = function (event) { this.focus = true; }; Listbox.prototype.onInputBlur = function (event) { this.focus = false; }; Listbox.prototype.onOptionKeyDown = function (event, option) { if (this.readonly) { return; } var item = event.currentTarget; switch (event.which) { //down case 40: var nextItem = this.findNextItem(item); if (nextItem) { nextItem.focus(); } event.preventDefault(); break; //up case 38: var prevItem = this.findPrevItem(item); if (prevItem) { prevItem.focus(); } event.preventDefault(); break; //enter case 13: this.onOptionClick(event, option); event.preventDefault(); break; } }; Listbox.prototype.findNextItem = function (item) { var nextItem = item.nextElementSibling; if (nextItem) return domhandler_1.DomHandler.hasClass(nextItem, 'ui-state-disabled') || domhandler_1.DomHandler.isHidden(nextItem) ? this.findNextItem(nextItem) : nextItem; else return null; }; Listbox.prototype.findPrevItem = function (item) { var prevItem = item.previousElementSibling; if (prevItem) return domhandler_1.DomHandler.hasClass(prevItem, 'ui-state-disabled') || domhandler_1.DomHandler.isHidden(prevItem) ? this.findPrevItem(prevItem) : prevItem; else return null; }; Listbox.prototype.getFilteredOptions = function () { var filteredOptions = []; if (this.filterValue) { for (var i = 0; i < this.options.length; i++) { var opt = this.options[i]; if (this.isItemVisible(opt) && !opt.disabled) { filteredOptions.push(opt); } } return filteredOptions; } else { return this.options; } }; Listbox.prototype.onHeaderCheckboxFocus = function () { this.headerCheckboxFocus = true; }; Listbox.prototype.onHeaderCheckboxBlur = function () { this.headerCheckboxFocus = false; }; Listbox.prototype.setDisabledSelectedOptions = function () { if (this.options) { this.disabledSelectedOptions = []; if (this.value) { for (var _i = 0, _a = this.options; _i < _a.length; _i++) { var opt = _a[_i]; if (opt.disabled && this.isSelected(opt)) { this.disabledSelectedOptions.push(opt.value); } } } } }; __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "multiple", void 0); __decorate([ core_1.Input(), __metadata("design:type", Object) ], Listbox.prototype, "style", void 0); __decorate([ core_1.Input(), __metadata("design:type", String) ], Listbox.prototype, "styleClass", void 0); __decorate([ core_1.Input(), __metadata("design:type", Object) ], Listbox.prototype, "listStyle", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "readonly", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "disabled", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "checkbox", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "filter", void 0); __decorate([ core_1.Input(), __metadata("design:type", String) ], Listbox.prototype, "filterMode", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "metaKeySelection", void 0); __decorate([ core_1.Input(), __metadata("design:type", String) ], Listbox.prototype, "dataKey", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Listbox.prototype, "showToggleAll", void 0); __decorate([ core_1.Input(), __metadata("design:type", String) ], Listbox.prototype, "optionLabel", void 0); __decorate([ core_1.Input(), __metadata("design:type", String) ], Listbox.prototype, "ariaFilterLabel", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], Listbox.prototype, "onChange", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], Listbox.prototype, "onClick", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], Listbox.prototype, "onDblClick", void 0); __decorate([ core_1.ViewChild('headerchkbox', { static: false }), __metadata("design:type", core_1.ElementRef) ], Listbox.prototype, "headerCheckboxViewChild", void 0); __decorate([ core_1.ContentChild(shared_1.Header, { static: false }), __metadata("design:type", Object) ], Listbox.prototype, "headerFacet", void 0); __decorate([ core_1.ContentChild(shared_1.Footer, { static: false }), __metadata("design:type", Object) ], Listbox.prototype, "footerFacet", void 0); __decorate([ core_1.ContentChildren(shared_1.PrimeTemplate), __metadata("design:type", core_1.QueryList) ], Listbox.prototype, "templates", void 0); __decorate([ core_1.Input(), __metadata("design:type", Array), __metadata("design:paramtypes", [Array]) ], Listbox.prototype, "options", null); __decorate([ core_1.Input(), __metadata("design:type", String), __metadata("design:paramtypes", [String]) ], Listbox.prototype, "filterValue", null); Listbox = __decorate([ core_1.Component({ selector: 'p-listbox', template: "\n
\n
\n \n
\n
\n \n
\n
\n
\n
\n \n
\n
\n \n
\n
\n
\n \n \n
\n
\n
\n \n
\n \n
\n ", providers: [exports.LISTBOX_VALUE_ACCESSOR] }), __metadata("design:paramtypes", [core_1.ElementRef, core_1.ChangeDetectorRef]) ], Listbox); return Listbox; }()); exports.Listbox = Listbox; var ListboxModule = /** @class */ (function () { function ListboxModule() { } ListboxModule = __decorate([ core_1.NgModule({ imports: [common_1.CommonModule, shared_1.SharedModule], exports: [Listbox, shared_1.SharedModule], declarations: [Listbox] }) ], ListboxModule); return ListboxModule; }()); exports.ListboxModule = ListboxModule; //# sourceMappingURL=listbox.js.map