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) [![Build Status](https://travis-ci.org/primefaces/primeng.

304 lines (300 loc) 10.4 kB
import { forwardRef, EventEmitter, ElementRef, Input, Output, ViewChild, ContentChildren, Component, ChangeDetectionStrategy, 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'; 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; }; const CHIPS_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Chips), multi: true }; let Chips = class Chips { constructor(el) { this.el = el; 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(); } registerOnChange(fn) { this.onModelChange = fn; } registerOnTouched(fn) { this.onModelTouched = fn; } setDisabledState(val) { this.disabled = val; } 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.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input() ], Chips.prototype, "style", void 0); __decorate([ Input() ], Chips.prototype, "styleClass", void 0); __decorate([ Input() ], Chips.prototype, "disabled", void 0); __decorate([ Input() ], Chips.prototype, "field", void 0); __decorate([ Input() ], Chips.prototype, "placeholder", void 0); __decorate([ Input() ], Chips.prototype, "max", void 0); __decorate([ Input() ], Chips.prototype, "ariaLabelledBy", void 0); __decorate([ Input() ], Chips.prototype, "tabindex", void 0); __decorate([ Input() ], Chips.prototype, "inputId", void 0); __decorate([ Input() ], Chips.prototype, "allowDuplicate", void 0); __decorate([ Input() ], Chips.prototype, "inputStyle", void 0); __decorate([ Input() ], Chips.prototype, "inputStyleClass", void 0); __decorate([ Input() ], Chips.prototype, "addOnTab", void 0); __decorate([ Input() ], Chips.prototype, "addOnBlur", void 0); __decorate([ Input() ], Chips.prototype, "separator", void 0); __decorate([ Output() ], Chips.prototype, "onAdd", void 0); __decorate([ Output() ], Chips.prototype, "onRemove", void 0); __decorate([ Output() ], Chips.prototype, "onFocus", void 0); __decorate([ Output() ], Chips.prototype, "onBlur", void 0); __decorate([ Output() ], Chips.prototype, "onChipClick", void 0); __decorate([ ViewChild('inputtext') ], Chips.prototype, "inputViewChild", void 0); __decorate([ ContentChildren(PrimeTemplate) ], Chips.prototype, "templates", void 0); Chips = __decorate([ Component({ selector: 'p-chips', template: ` <div [ngClass]="'ui-chips ui-widget'" [ngStyle]="style" [class]="styleClass" (click)="onClick()"> <ul [ngClass]="{'ui-inputtext ui-state-default ui-corner-all':true,'ui-state-focus':focus,'ui-state-disabled':disabled}"> <li #token *ngFor="let item of value; let i = index;" class="ui-chips-token ui-state-highlight ui-corner-all" (click)="onItemClick($event, item)"> <span *ngIf="!disabled" class="ui-chips-token-icon pi pi-fw pi-times" (click)="removeItem($event,i)"></span> <span *ngIf="!itemTemplate" class="ui-chips-token-label">{{field ? resolveFieldData(item,field) : item}}</span> <ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item}"></ng-container> </li> <li class="ui-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.ui-inputwrapper-filled]': 'filled', '[class.ui-inputwrapper-focus]': 'focus' }, providers: [CHIPS_VALUE_ACCESSOR], changeDetection: ChangeDetectionStrategy.Default }) ], Chips); let ChipsModule = class ChipsModule { }; ChipsModule = __decorate([ NgModule({ imports: [CommonModule, InputTextModule, SharedModule], exports: [Chips, InputTextModule, SharedModule], declarations: [Chips] }) ], ChipsModule); /** * Generated bundle index. Do not edit. */ export { CHIPS_VALUE_ACCESSOR, Chips, ChipsModule }; //# sourceMappingURL=primeng-chips.js.map