UNPKG

26.3 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('primeng/api'), require('primeng/dom'), require('primeng/utils'), require('@angular/forms')) :
3 typeof define === 'function' && define.amd ? define('primeng/listbox', ['exports', '@angular/core', '@angular/common', 'primeng/api', 'primeng/dom', 'primeng/utils', '@angular/forms'], factory) :
4 (global = global || self, factory((global.primeng = global.primeng || {}, global.primeng.listbox = {}), global.ng.core, global.ng.common, global.primeng.api, global.primeng.dom, global.primeng.utils, global.ng.forms));
5}(this, (function (exports, core, common, api, dom, utils, forms) { 'use strict';
6
7 var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
8 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
10 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;
11 return c > 3 && r && Object.defineProperty(target, key, r), r;
12 };
13 var __read = (this && this.__read) || function (o, n) {
14 var m = typeof Symbol === "function" && o[Symbol.iterator];
15 if (!m) return o;
16 var i = m.call(o), r, ar = [], e;
17 try {
18 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19 }
20 catch (error) { e = { error: error }; }
21 finally {
22 try {
23 if (r && !r.done && (m = i["return"])) m.call(i);
24 }
25 finally { if (e) throw e.error; }
26 }
27 return ar;
28 };
29 var __spread = (this && this.__spread) || function () {
30 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
31 return ar;
32 };
33 var __values = (this && this.__values) || function(o) {
34 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
35 if (m) return m.call(o);
36 if (o && typeof o.length === "number") return {
37 next: function () {
38 if (o && i >= o.length) o = void 0;
39 return { value: o && o[i++], done: !o };
40 }
41 };
42 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
43 };
44 var LISTBOX_VALUE_ACCESSOR = {
45 provide: forms.NG_VALUE_ACCESSOR,
46 useExisting: core.forwardRef(function () { return Listbox; }),
47 multi: true
48 };
49 var Listbox = /** @class */ (function () {
50 function Listbox(el, cd) {
51 this.el = el;
52 this.cd = cd;
53 this.checkbox = false;
54 this.filter = false;
55 this.filterMode = 'contains';
56 this.metaKeySelection = true;
57 this.showToggleAll = true;
58 this.onChange = new core.EventEmitter();
59 this.onClick = new core.EventEmitter();
60 this.onDblClick = new core.EventEmitter();
61 this.onModelChange = function () { };
62 this.onModelTouched = function () { };
63 this.disabledSelectedOptions = [];
64 }
65 Object.defineProperty(Listbox.prototype, "options", {
66 get: function () {
67 return this._options;
68 },
69 set: function (val) {
70 var opts = this.optionLabel ? utils.ObjectUtils.generateSelectItems(val, this.optionLabel) : val;
71 this._options = opts;
72 },
73 enumerable: true,
74 configurable: true
75 });
76 Object.defineProperty(Listbox.prototype, "filterValue", {
77 get: function () {
78 return this._filterValue;
79 },
80 set: function (val) {
81 this._filterValue = val;
82 },
83 enumerable: true,
84 configurable: true
85 });
86 Listbox.prototype.ngAfterContentInit = function () {
87 var _this = this;
88 this.templates.forEach(function (item) {
89 switch (item.getType()) {
90 case 'item':
91 _this.itemTemplate = item.template;
92 break;
93 default:
94 _this.itemTemplate = item.template;
95 break;
96 }
97 });
98 };
99 Listbox.prototype.writeValue = function (value) {
100 this.value = value;
101 this.setDisabledSelectedOptions();
102 this.cd.markForCheck();
103 };
104 Listbox.prototype.registerOnChange = function (fn) {
105 this.onModelChange = fn;
106 };
107 Listbox.prototype.registerOnTouched = function (fn) {
108 this.onModelTouched = fn;
109 };
110 Listbox.prototype.setDisabledState = function (val) {
111 this.disabled = val;
112 };
113 Listbox.prototype.onOptionClick = function (event, option) {
114 if (this.disabled || option.disabled || this.readonly) {
115 return;
116 }
117 if (this.multiple) {
118 if (this.checkbox)
119 this.onOptionClickCheckbox(event, option);
120 else
121 this.onOptionClickMultiple(event, option);
122 }
123 else {
124 this.onOptionClickSingle(event, option);
125 }
126 this.onClick.emit({
127 originalEvent: event,
128 option: option,
129 value: this.value
130 });
131 this.optionTouched = false;
132 };
133 Listbox.prototype.onOptionTouchEnd = function (event, option) {
134 if (this.disabled || option.disabled || this.readonly) {
135 return;
136 }
137 this.optionTouched = true;
138 };
139 Listbox.prototype.onOptionDoubleClick = function (event, option) {
140 if (this.disabled || option.disabled || this.readonly) {
141 return;
142 }
143 this.onDblClick.emit({
144 originalEvent: event,
145 option: option,
146 value: this.value
147 });
148 };
149 Listbox.prototype.onOptionClickSingle = function (event, option) {
150 var selected = this.isSelected(option);
151 var valueChanged = false;
152 var metaSelection = this.optionTouched ? false : this.metaKeySelection;
153 if (metaSelection) {
154 var metaKey = (event.metaKey || event.ctrlKey);
155 if (selected) {
156 if (metaKey) {
157 this.value = null;
158 valueChanged = true;
159 }
160 }
161 else {
162 this.value = option.value;
163 valueChanged = true;
164 }
165 }
166 else {
167 this.value = selected ? null : option.value;
168 valueChanged = true;
169 }
170 if (valueChanged) {
171 this.onModelChange(this.value);
172 this.onChange.emit({
173 originalEvent: event,
174 value: this.value
175 });
176 }
177 };
178 Listbox.prototype.onOptionClickMultiple = function (event, option) {
179 var selected = this.isSelected(option);
180 var valueChanged = false;
181 var metaSelection = this.optionTouched ? false : this.metaKeySelection;
182 if (metaSelection) {
183 var metaKey = (event.metaKey || event.ctrlKey);
184 if (selected) {
185 if (metaKey) {
186 this.removeOption(option);
187 }
188 else {
189 this.value = [option.value];
190 }
191 valueChanged = true;
192 }
193 else {
194 this.value = (metaKey) ? this.value || [] : [];
195 this.value = __spread(this.value, [option.value]);
196 valueChanged = true;
197 }
198 }
199 else {
200 if (selected) {
201 this.removeOption(option);
202 }
203 else {
204 this.value = __spread(this.value || [], [option.value]);
205 }
206 valueChanged = true;
207 }
208 if (valueChanged) {
209 this.onModelChange(this.value);
210 this.onChange.emit({
211 originalEvent: event,
212 value: this.value
213 });
214 }
215 };
216 Listbox.prototype.onOptionClickCheckbox = function (event, option) {
217 if (this.disabled || this.readonly) {
218 return;
219 }
220 var selected = this.isSelected(option);
221 if (selected) {
222 this.removeOption(option);
223 }
224 else {
225 this.value = this.value ? this.value : [];
226 this.value = __spread(this.value, [option.value]);
227 }
228 this.onModelChange(this.value);
229 this.onChange.emit({
230 originalEvent: event,
231 value: this.value
232 });
233 };
234 Listbox.prototype.removeOption = function (option) {
235 var _this = this;
236 this.value = this.value.filter(function (val) { return !utils.ObjectUtils.equals(val, option.value, _this.dataKey); });
237 };
238 Listbox.prototype.isSelected = function (option) {
239 var e_1, _a;
240 var selected = false;
241 if (this.multiple) {
242 if (this.value) {
243 try {
244 for (var _b = __values(this.value), _c = _b.next(); !_c.done; _c = _b.next()) {
245 var val = _c.value;
246 if (utils.ObjectUtils.equals(val, option.value, this.dataKey)) {
247 selected = true;
248 break;
249 }
250 }
251 }
252 catch (e_1_1) { e_1 = { error: e_1_1 }; }
253 finally {
254 try {
255 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
256 }
257 finally { if (e_1) throw e_1.error; }
258 }
259 }
260 }
261 else {
262 selected = utils.ObjectUtils.equals(this.value, option.value, this.dataKey);
263 }
264 return selected;
265 };
266 Object.defineProperty(Listbox.prototype, "allChecked", {
267 get: function () {
268 if (this.filterValue) {
269 return this.allFilteredSelected();
270 }
271 else {
272 var optionCount = this.getEnabledOptionCount();
273 var disabledSelectedOptionCount = this.disabledSelectedOptions.length;
274 return this.value && this.options && (this.value.length > 0 && this.value.length == optionCount + disabledSelectedOptionCount);
275 }
276 },
277 enumerable: true,
278 configurable: true
279 });
280 Listbox.prototype.getEnabledOptionCount = function () {
281 var e_2, _a;
282 if (this.options) {
283 var count = 0;
284 try {
285 for (var _b = __values(this.options), _c = _b.next(); !_c.done; _c = _b.next()) {
286 var opt = _c.value;
287 if (!opt.disabled) {
288 count++;
289 }
290 }
291 }
292 catch (e_2_1) { e_2 = { error: e_2_1 }; }
293 finally {
294 try {
295 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
296 }
297 finally { if (e_2) throw e_2.error; }
298 }
299 return count;
300 }
301 else {
302 return 0;
303 }
304 };
305 Listbox.prototype.allFilteredSelected = function () {
306 var e_3, _a;
307 var allSelected;
308 var options = this.filterValue ? this.getFilteredOptions() : this.options;
309 if (this.value && options && options.length) {
310 allSelected = true;
311 try {
312 for (var _b = __values(this.options), _c = _b.next(); !_c.done; _c = _b.next()) {
313 var opt = _c.value;
314 if (this.isItemVisible(opt)) {
315 if (!this.isSelected(opt)) {
316 allSelected = false;
317 break;
318 }
319 }
320 }
321 }
322 catch (e_3_1) { e_3 = { error: e_3_1 }; }
323 finally {
324 try {
325 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
326 }
327 finally { if (e_3) throw e_3.error; }
328 }
329 }
330 return allSelected;
331 };
332 Listbox.prototype.onFilter = function (event) {
333 this._filterValue = event.target.value;
334 };
335 Listbox.prototype.toggleAll = function (event) {
336 if (this.disabled || this.readonly || !this.options || this.options.length === 0) {
337 return;
338 }
339 if (this.allChecked) {
340 if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) {
341 var value = [];
342 value = __spread(this.disabledSelectedOptions);
343 this.value = value;
344 }
345 else {
346 this.value = [];
347 }
348 }
349 else {
350 if (this.options) {
351 this.value = [];
352 if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) {
353 this.value = __spread(this.disabledSelectedOptions);
354 }
355 for (var i = 0; i < this.options.length; i++) {
356 var opt = this.options[i];
357 if (this.isItemVisible(opt) && !opt.disabled) {
358 this.value.push(opt.value);
359 }
360 }
361 }
362 }
363 this.onModelChange(this.value);
364 this.onChange.emit({ originalEvent: event, value: this.value });
365 event.preventDefault();
366 };
367 Listbox.prototype.isItemVisible = function (option) {
368 if (this.filterValue) {
369 var visible = void 0;
370 if (this.filterMode) {
371 visible = utils.FilterUtils[this.filterMode](option.label, this.filterValue, this.filterLocale);
372 }
373 else {
374 visible = true;
375 }
376 return visible;
377 }
378 else {
379 return true;
380 }
381 };
382 Listbox.prototype.onInputFocus = function (event) {
383 this.focus = true;
384 };
385 Listbox.prototype.onInputBlur = function (event) {
386 this.focus = false;
387 };
388 Listbox.prototype.onOptionKeyDown = function (event, option) {
389 if (this.readonly) {
390 return;
391 }
392 var item = event.currentTarget;
393 switch (event.which) {
394 //down
395 case 40:
396 var nextItem = this.findNextItem(item);
397 if (nextItem) {
398 nextItem.focus();
399 }
400 event.preventDefault();
401 break;
402 //up
403 case 38:
404 var prevItem = this.findPrevItem(item);
405 if (prevItem) {
406 prevItem.focus();
407 }
408 event.preventDefault();
409 break;
410 //enter
411 case 13:
412 this.onOptionClick(event, option);
413 event.preventDefault();
414 break;
415 }
416 };
417 Listbox.prototype.findNextItem = function (item) {
418 var nextItem = item.nextElementSibling;
419 if (nextItem)
420 return dom.DomHandler.hasClass(nextItem, 'ui-state-disabled') || dom.DomHandler.isHidden(nextItem) ? this.findNextItem(nextItem) : nextItem;
421 else
422 return null;
423 };
424 Listbox.prototype.findPrevItem = function (item) {
425 var prevItem = item.previousElementSibling;
426 if (prevItem)
427 return dom.DomHandler.hasClass(prevItem, 'ui-state-disabled') || dom.DomHandler.isHidden(prevItem) ? this.findPrevItem(prevItem) : prevItem;
428 else
429 return null;
430 };
431 Listbox.prototype.getFilteredOptions = function () {
432 var filteredOptions = [];
433 if (this.filterValue) {
434 for (var i = 0; i < this.options.length; i++) {
435 var opt = this.options[i];
436 if (this.isItemVisible(opt) && !opt.disabled) {
437 filteredOptions.push(opt);
438 }
439 }
440 return filteredOptions;
441 }
442 else {
443 return this.options;
444 }
445 };
446 Listbox.prototype.onHeaderCheckboxFocus = function () {
447 this.headerCheckboxFocus = true;
448 };
449 Listbox.prototype.onHeaderCheckboxBlur = function () {
450 this.headerCheckboxFocus = false;
451 };
452 Listbox.prototype.setDisabledSelectedOptions = function () {
453 var e_4, _a;
454 if (this.options) {
455 this.disabledSelectedOptions = [];
456 if (this.value) {
457 try {
458 for (var _b = __values(this.options), _c = _b.next(); !_c.done; _c = _b.next()) {
459 var opt = _c.value;
460 if (opt.disabled && this.isSelected(opt)) {
461 this.disabledSelectedOptions.push(opt.value);
462 }
463 }
464 }
465 catch (e_4_1) { e_4 = { error: e_4_1 }; }
466 finally {
467 try {
468 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
469 }
470 finally { if (e_4) throw e_4.error; }
471 }
472 }
473 }
474 };
475 Listbox.ctorParameters = function () { return [
476 { type: core.ElementRef },
477 { type: core.ChangeDetectorRef }
478 ]; };
479 __decorate([
480 core.Input()
481 ], Listbox.prototype, "multiple", void 0);
482 __decorate([
483 core.Input()
484 ], Listbox.prototype, "style", void 0);
485 __decorate([
486 core.Input()
487 ], Listbox.prototype, "styleClass", void 0);
488 __decorate([
489 core.Input()
490 ], Listbox.prototype, "listStyle", void 0);
491 __decorate([
492 core.Input()
493 ], Listbox.prototype, "readonly", void 0);
494 __decorate([
495 core.Input()
496 ], Listbox.prototype, "disabled", void 0);
497 __decorate([
498 core.Input()
499 ], Listbox.prototype, "checkbox", void 0);
500 __decorate([
501 core.Input()
502 ], Listbox.prototype, "filter", void 0);
503 __decorate([
504 core.Input()
505 ], Listbox.prototype, "filterMode", void 0);
506 __decorate([
507 core.Input()
508 ], Listbox.prototype, "filterLocale", void 0);
509 __decorate([
510 core.Input()
511 ], Listbox.prototype, "metaKeySelection", void 0);
512 __decorate([
513 core.Input()
514 ], Listbox.prototype, "dataKey", void 0);
515 __decorate([
516 core.Input()
517 ], Listbox.prototype, "showToggleAll", void 0);
518 __decorate([
519 core.Input()
520 ], Listbox.prototype, "optionLabel", void 0);
521 __decorate([
522 core.Input()
523 ], Listbox.prototype, "ariaFilterLabel", void 0);
524 __decorate([
525 core.Input()
526 ], Listbox.prototype, "filterPlaceHolder", void 0);
527 __decorate([
528 core.Output()
529 ], Listbox.prototype, "onChange", void 0);
530 __decorate([
531 core.Output()
532 ], Listbox.prototype, "onClick", void 0);
533 __decorate([
534 core.Output()
535 ], Listbox.prototype, "onDblClick", void 0);
536 __decorate([
537 core.ViewChild('headerchkbox')
538 ], Listbox.prototype, "headerCheckboxViewChild", void 0);
539 __decorate([
540 core.ContentChild(api.Header)
541 ], Listbox.prototype, "headerFacet", void 0);
542 __decorate([
543 core.ContentChild(api.Footer)
544 ], Listbox.prototype, "footerFacet", void 0);
545 __decorate([
546 core.ContentChildren(api.PrimeTemplate)
547 ], Listbox.prototype, "templates", void 0);
548 __decorate([
549 core.Input()
550 ], Listbox.prototype, "options", null);
551 __decorate([
552 core.Input()
553 ], Listbox.prototype, "filterValue", null);
554 Listbox = __decorate([
555 core.Component({
556 selector: 'p-listbox',
557 template: "\n <div [ngClass]=\"{'ui-listbox ui-inputtext ui-widget ui-widget-content ui-corner-all':true,'ui-state-disabled':disabled,'ui-state-focus':focus}\" [ngStyle]=\"style\" [class]=\"styleClass\">\n <div class=\"ui-helper-hidden-accessible\">\n <input type=\"text\" readonly=\"readonly\" (focus)=\"onInputFocus($event)\" (blur)=\"onInputBlur($event)\">\n </div>\n <div class=\"ui-widget-header ui-corner-all ui-listbox-header ui-helper-clearfix\" *ngIf=\"headerFacet\">\n <ng-content select=\"p-header\"></ng-content>\n </div>\n <div class=\"ui-widget-header ui-corner-all ui-listbox-header ui-helper-clearfix\" *ngIf=\"(checkbox && multiple && showToggleAll) || filter\" [ngClass]=\"{'ui-listbox-header-w-checkbox': checkbox}\">\n <div class=\"ui-chkbox ui-widget\" *ngIf=\"checkbox && multiple && showToggleAll\">\n <div class=\"ui-helper-hidden-accessible\">\n <input type=\"checkbox\" readonly=\"readonly\" [checked]=\"allChecked\" (focus)=\"onHeaderCheckboxFocus()\" (blur)=\"onHeaderCheckboxBlur()\" (keydown.space)=\"toggleAll($event)\">\n </div>\n <div #headerchkbox class=\"ui-chkbox-box ui-widget ui-corner-all ui-state-default\" [ngClass]=\"{'ui-state-active': allChecked, 'ui-state-focus': headerCheckboxFocus}\" (click)=\"toggleAll($event)\">\n <span class=\"ui-chkbox-icon ui-clickable\" [ngClass]=\"{'pi pi-check':allChecked}\"></span>\n </div>\n </div>\n <div class=\"ui-listbox-filter-container\" *ngIf=\"filter\">\n <input type=\"text\" [value]=\"filterValue||''\" (input)=\"onFilter($event)\" class=\"ui-inputtext ui-widget ui-state-default ui-corner-all\" [disabled]=\"disabled\" [attr.placeholder]=\"filterPlaceHolder\" [attr.aria-label]=\"ariaFilterLabel\">\n <span class=\"ui-listbox-filter-icon pi pi-search\"></span>\n </div>\n </div>\n <div class=\"ui-listbox-list-wrapper\" [ngStyle]=\"listStyle\">\n <ul class=\"ui-listbox-list\" role=\"listbox\" aria-multiselectable=\"multiple\">\n <li *ngFor=\"let option of options; let i = index;\" [style.display]=\"isItemVisible(option) ? 'block' : 'none'\" [attr.tabindex]=\"option.disabled ? null : '0'\"\n [ngClass]=\"{'ui-listbox-item ui-corner-all':true,'ui-state-highlight':isSelected(option), 'ui-state-disabled': option.disabled}\" role=\"option\" [attr.aria-label]=\"option.label\"\n [attr.aria-selected]=\"isSelected(option)\" (click)=\"onOptionClick($event,option)\" (dblclick)=\"onOptionDoubleClick($event,option)\" (touchend)=\"onOptionTouchEnd($event,option)\" (keydown)=\"onOptionKeyDown($event,option)\">\n <div class=\"ui-chkbox ui-widget\" *ngIf=\"checkbox && multiple\">\n <div class=\"ui-chkbox-box ui-widget ui-corner-all ui-state-default\" [ngClass]=\"{'ui-state-active':isSelected(option)}\">\n <span class=\"ui-chkbox-icon ui-clickable\" [ngClass]=\"{'pi pi-check':isSelected(option)}\"></span>\n </div>\n </div>\n <span *ngIf=\"!itemTemplate\">{{option.label}}</span>\n <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: option, index: i}\"></ng-container>\n </li>\n </ul>\n </div>\n <div class=\"ui-listbox-footer ui-widget-header ui-corner-all\" *ngIf=\"footerFacet\">\n <ng-content select=\"p-footer\"></ng-content>\n </div>\n </div>\n ",
558 providers: [LISTBOX_VALUE_ACCESSOR],
559 changeDetection: core.ChangeDetectionStrategy.Default
560 })
561 ], Listbox);
562 return Listbox;
563 }());
564 var ListboxModule = /** @class */ (function () {
565 function ListboxModule() {
566 }
567 ListboxModule = __decorate([
568 core.NgModule({
569 imports: [common.CommonModule, api.SharedModule],
570 exports: [Listbox, api.SharedModule],
571 declarations: [Listbox]
572 })
573 ], ListboxModule);
574 return ListboxModule;
575 }());
576
577 exports.LISTBOX_VALUE_ACCESSOR = LISTBOX_VALUE_ACCESSOR;
578 exports.Listbox = Listbox;
579 exports.ListboxModule = ListboxModule;
580
581 Object.defineProperty(exports, '__esModule', { value: true });
582
583})));
584//# sourceMappingURL=primeng-listbox.umd.js.map