import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, ChangeDetectorRef, Input, Output, ViewChild, ContentChildren, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { PrimeTemplate, SharedModule } from 'primeng/api'; import { InputTextModule } from 'primeng/inputtext'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; const CHIPS_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Chips), multi: true }; class Chips { constructor(el, cd) { this.el = el; this.cd = cd; this.allowDuplicate = true; this.onAdd = new EventEmitter(); this.onRemove = new EventEmitter(); this.onFocus = new EventEmitter(); this.onBlur = new EventEmitter(); this.onChipClick = new EventEmitter(); this.onModelChange = () => { }; this.onModelTouched = () => { }; } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'item': this.itemTemplate = item.template; break; default: this.itemTemplate = item.template; break; } }); } onClick() { this.inputViewChild.nativeElement.focus(); } onInput() { this.updateFilledState(); } onPaste(event) { if (this.separator) { let pastedData = (event.clipboardData || window['clipboardData']).getData('Text'); pastedData.split(this.separator).forEach(val => { this.addItem(event, val, true); }); this.inputViewChild.nativeElement.value = ''; } this.updateFilledState(); } updateFilledState() { if (!this.value || this.value.length === 0) { this.filled = (this.inputViewChild.nativeElement && this.inputViewChild.nativeElement.value != ''); } else { this.filled = true; } } onItemClick(event, item) { this.onChipClick.emit({ originalEvent: event, value: item }); } writeValue(value) { this.value = value; this.updateMaxedOut(); this.cd.markForCheck(); } registerOnChange(fn) { this.onModelChange = fn; } registerOnTouched(fn) { this.onModelTouched = fn; } setDisabledState(val) { this.disabled = val; this.cd.markForCheck(); } resolveFieldData(data, field) { if (data && field) { if (field.indexOf('.') == -1) { return data[field]; } else { let fields = field.split('.'); let value = data; for (var i = 0, len = fields.length; i < len; ++i) { value = value[fields[i]]; } return value; } } else { return null; } } onInputFocus(event) { this.focus = true; this.onFocus.emit(event); } onInputBlur(event) { this.focus = false; if (this.addOnBlur && this.inputViewChild.nativeElement.value) { this.addItem(event, this.inputViewChild.nativeElement.value, false); } this.onModelTouched(); this.onBlur.emit(event); } removeItem(event, index) { if (this.disabled) { return; } let removedItem = this.value[index]; this.value = this.value.filter((val, i) => i != index); this.onModelChange(this.value); this.onRemove.emit({ originalEvent: event, value: removedItem }); this.updateFilledState(); this.updateMaxedOut(); } addItem(event, item, preventDefault) { this.value = this.value || []; if (item && item.trim().length) { if (this.allowDuplicate || this.value.indexOf(item) === -1) { this.value = [...this.value, item]; this.onModelChange(this.value); this.onAdd.emit({ originalEvent: event, value: item }); } } this.updateFilledState(); this.updateMaxedOut(); this.inputViewChild.nativeElement.value = ''; if (preventDefault) { event.preventDefault(); } } onKeydown(event) { switch (event.which) { //backspace case 8: if (this.inputViewChild.nativeElement.value.length === 0 && this.value && this.value.length > 0) { this.value = [...this.value]; let removedItem = this.value.pop(); this.onModelChange(this.value); this.onRemove.emit({ originalEvent: event, value: removedItem }); this.updateFilledState(); } break; //enter case 13: this.addItem(event, this.inputViewChild.nativeElement.value, true); break; case 9: if (this.addOnTab && this.inputViewChild.nativeElement.value !== '') { this.addItem(event, this.inputViewChild.nativeElement.value, true); } break; default: if (this.max && this.value && this.max === this.value.length) { event.preventDefault(); } else if (this.separator) { if (this.separator === ',' && event.which === 188) { this.addItem(event, this.inputViewChild.nativeElement.value, true); } } break; } } updateMaxedOut() { if (this.inputViewChild && this.inputViewChild.nativeElement) { if (this.max && this.value && this.max === this.value.length) this.inputViewChild.nativeElement.disabled = true; else this.inputViewChild.nativeElement.disabled = this.disabled || false; } } } Chips.decorators = [ { type: Component, args: [{ selector: 'p-chips', template: `
`, host: { '[class.p-inputwrapper-filled]': 'filled', '[class.p-inputwrapper-focus]': 'focus' }, providers: [CHIPS_VALUE_ACCESSOR], changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, styles: [".p-chips{display:-ms-inline-flexbox;display:inline-flex}.p-chips-multiple-container{-ms-flex-align:center;align-items:center;cursor:text;display:-ms-flexbox;display:flex;list-style-type:none;margin:0;overflow:hidden;padding:0}.p-chips-token{-ms-flex:0 0 auto;-ms-flex-align:center;align-items:center;cursor:default;flex:0 0 auto}.p-chips-input-token,.p-chips-token{display:-ms-inline-flexbox;display:inline-flex}.p-chips-input-token{-ms-flex:1 1 auto;flex:1 1 auto}.p-chips-token-icon{cursor:pointer}.p-chips-input-token input{background-color:rgba(0,0,0,0);border:0;border-radius:0;box-shadow:none;margin:0;outline:0 none;padding:0;width:100%}.p-fluid .p-chips{display:-ms-flexbox;display:flex}"] },] } ]; Chips.ctorParameters = () => [ { type: ElementRef }, { type: ChangeDetectorRef } ]; Chips.propDecorators = { style: [{ type: Input }], styleClass: [{ type: Input }], disabled: [{ type: Input }], field: [{ type: Input }], placeholder: [{ type: Input }], max: [{ type: Input }], ariaLabelledBy: [{ type: Input }], tabindex: [{ type: Input }], inputId: [{ type: Input }], allowDuplicate: [{ type: Input }], inputStyle: [{ type: Input }], inputStyleClass: [{ type: Input }], addOnTab: [{ type: Input }], addOnBlur: [{ type: Input }], separator: [{ type: Input }], onAdd: [{ type: Output }], onRemove: [{ type: Output }], onFocus: [{ type: Output }], onBlur: [{ type: Output }], onChipClick: [{ type: Output }], inputViewChild: [{ type: ViewChild, args: ['inputtext',] }], templates: [{ type: ContentChildren, args: [PrimeTemplate,] }] }; class ChipsModule { } ChipsModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule, InputTextModule, SharedModule], exports: [Chips, InputTextModule, SharedModule], declarations: [Chips] },] } ]; /** * Generated bundle index. Do not edit. */ export { CHIPS_VALUE_ACCESSOR, Chips, ChipsModule }; //# sourceMappingURL=primeng-chips.js.map