UNPKG

primeng

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![npm downloads](https://img.shields.io/npm/dm/primeng.sv

264 lines (260 loc) 10.1 kB
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.disabled) { 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: ` <div [ngClass]="'p-chips p-component'" [ngStyle]="style" [class]="styleClass" (click)="onClick()"> <ul [ngClass]="{'p-inputtext p-chips-multiple-container':true,'p-focus':focus,'p-disabled':disabled}"> <li #token *ngFor="let item of value; let i = index;" class="p-chips-token" (click)="onItemClick($event, item)"> <ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item}"></ng-container> <span *ngIf="!itemTemplate" class="p-chips-token-label">{{field ? resolveFieldData(item,field) : item}}</span> <span *ngIf="!disabled" class="p-chips-token-icon pi pi-times-circle" (click)="removeItem($event,i)"></span> </li> <li class="p-chips-input-token"> <input #inputtext type="text" [attr.id]="inputId" [attr.placeholder]="(value && value.length ? null : placeholder)" [attr.tabindex]="tabindex" (keydown)="onKeydown($event)" (input)="onInput()" (paste)="onPaste($event)" [attr.aria-labelledby]="ariaLabelledBy" (focus)="onInputFocus($event)" (blur)="onInputBlur($event)" [disabled]="disabled" [ngStyle]="inputStyle" [class]="inputStyleClass"> </li> </ul> </div> `, 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:inline-flex}.p-chips-multiple-container{align-items:center;cursor:text;display:flex;flex-wrap:wrap;list-style-type:none;margin:0;overflow:hidden;padding:0}.p-chips-token{align-items:center;cursor:default;display:inline-flex;flex:0 0 auto}.p-chips-input-token{display:inline-flex;flex:1 1 auto}.p-chips-token-icon{cursor:pointer}.p-chips-input-token input{background-color:transparent;border:0;border-radius:0;box-shadow:none;margin:0;outline:0 none;padding:0;width:100%}.p-fluid .p-chips{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