UNPKG

10.2 kBJavaScriptView Raw
1import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, ChangeDetectorRef, Input, Output, ViewChild, ContentChildren, NgModule } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { PrimeTemplate, SharedModule } from 'primeng/api';
4import { InputTextModule } from 'primeng/inputtext';
5import { NG_VALUE_ACCESSOR } from '@angular/forms';
6
7const CHIPS_VALUE_ACCESSOR = {
8 provide: NG_VALUE_ACCESSOR,
9 useExisting: forwardRef(() => Chips),
10 multi: true
11};
12class Chips {
13 constructor(el, cd) {
14 this.el = el;
15 this.cd = cd;
16 this.allowDuplicate = true;
17 this.onAdd = new EventEmitter();
18 this.onRemove = new EventEmitter();
19 this.onFocus = new EventEmitter();
20 this.onBlur = new EventEmitter();
21 this.onChipClick = new EventEmitter();
22 this.onModelChange = () => { };
23 this.onModelTouched = () => { };
24 }
25 ngAfterContentInit() {
26 this.templates.forEach((item) => {
27 switch (item.getType()) {
28 case 'item':
29 this.itemTemplate = item.template;
30 break;
31 default:
32 this.itemTemplate = item.template;
33 break;
34 }
35 });
36 }
37 onClick() {
38 this.inputViewChild.nativeElement.focus();
39 }
40 onInput() {
41 this.updateFilledState();
42 }
43 onPaste(event) {
44 if (this.separator) {
45 let pastedData = (event.clipboardData || window['clipboardData']).getData('Text');
46 pastedData.split(this.separator).forEach(val => {
47 this.addItem(event, val, true);
48 });
49 this.inputViewChild.nativeElement.value = '';
50 }
51 this.updateFilledState();
52 }
53 updateFilledState() {
54 if (!this.value || this.value.length === 0) {
55 this.filled = (this.inputViewChild.nativeElement && this.inputViewChild.nativeElement.value != '');
56 }
57 else {
58 this.filled = true;
59 }
60 }
61 onItemClick(event, item) {
62 this.onChipClick.emit({
63 originalEvent: event,
64 value: item
65 });
66 }
67 writeValue(value) {
68 this.value = value;
69 this.updateMaxedOut();
70 this.cd.markForCheck();
71 }
72 registerOnChange(fn) {
73 this.onModelChange = fn;
74 }
75 registerOnTouched(fn) {
76 this.onModelTouched = fn;
77 }
78 setDisabledState(val) {
79 this.disabled = val;
80 this.cd.markForCheck();
81 }
82 resolveFieldData(data, field) {
83 if (data && field) {
84 if (field.indexOf('.') == -1) {
85 return data[field];
86 }
87 else {
88 let fields = field.split('.');
89 let value = data;
90 for (var i = 0, len = fields.length; i < len; ++i) {
91 value = value[fields[i]];
92 }
93 return value;
94 }
95 }
96 else {
97 return null;
98 }
99 }
100 onInputFocus(event) {
101 this.focus = true;
102 this.onFocus.emit(event);
103 }
104 onInputBlur(event) {
105 this.focus = false;
106 if (this.addOnBlur && this.inputViewChild.nativeElement.value) {
107 this.addItem(event, this.inputViewChild.nativeElement.value, false);
108 }
109 this.onModelTouched();
110 this.onBlur.emit(event);
111 }
112 removeItem(event, index) {
113 if (this.disabled) {
114 return;
115 }
116 let removedItem = this.value[index];
117 this.value = this.value.filter((val, i) => i != index);
118 this.onModelChange(this.value);
119 this.onRemove.emit({
120 originalEvent: event,
121 value: removedItem
122 });
123 this.updateFilledState();
124 this.updateMaxedOut();
125 }
126 addItem(event, item, preventDefault) {
127 this.value = this.value || [];
128 if (item && item.trim().length) {
129 if (this.allowDuplicate || this.value.indexOf(item) === -1) {
130 this.value = [...this.value, item];
131 this.onModelChange(this.value);
132 this.onAdd.emit({
133 originalEvent: event,
134 value: item
135 });
136 }
137 }
138 this.updateFilledState();
139 this.updateMaxedOut();
140 this.inputViewChild.nativeElement.value = '';
141 if (preventDefault) {
142 event.preventDefault();
143 }
144 }
145 onKeydown(event) {
146 switch (event.which) {
147 //backspace
148 case 8:
149 if (this.inputViewChild.nativeElement.value.length === 0 && this.value && this.value.length > 0) {
150 this.value = [...this.value];
151 let removedItem = this.value.pop();
152 this.onModelChange(this.value);
153 this.onRemove.emit({
154 originalEvent: event,
155 value: removedItem
156 });
157 this.updateFilledState();
158 }
159 break;
160 //enter
161 case 13:
162 this.addItem(event, this.inputViewChild.nativeElement.value, true);
163 break;
164 case 9:
165 if (this.addOnTab && this.inputViewChild.nativeElement.value !== '') {
166 this.addItem(event, this.inputViewChild.nativeElement.value, true);
167 }
168 break;
169 default:
170 if (this.max && this.value && this.max === this.value.length) {
171 event.preventDefault();
172 }
173 else if (this.separator) {
174 if (this.separator === ',' && event.which === 188) {
175 this.addItem(event, this.inputViewChild.nativeElement.value, true);
176 }
177 }
178 break;
179 }
180 }
181 updateMaxedOut() {
182 if (this.inputViewChild && this.inputViewChild.nativeElement) {
183 if (this.max && this.value && this.max === this.value.length)
184 this.inputViewChild.nativeElement.disabled = true;
185 else
186 this.inputViewChild.nativeElement.disabled = this.disabled || false;
187 }
188 }
189}
190Chips.decorators = [
191 { type: Component, args: [{
192 selector: 'p-chips',
193 template: `
194 <div [ngClass]="'p-chips p-component'" [ngStyle]="style" [class]="styleClass" (click)="onClick()">
195 <ul [ngClass]="{'p-inputtext p-chips-multiple-container':true,'p-focus':focus,'p-disabled':disabled}">
196 <li #token *ngFor="let item of value; let i = index;" class="p-chips-token" (click)="onItemClick($event, item)">
197 <ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item}"></ng-container>
198 <span *ngIf="!itemTemplate" class="p-chips-token-label">{{field ? resolveFieldData(item,field) : item}}</span>
199 <span *ngIf="!disabled" class="p-chips-token-icon pi pi-times-circle" (click)="removeItem($event,i)"></span>
200 </li>
201 <li class="p-chips-input-token">
202 <input #inputtext type="text" [attr.id]="inputId" [attr.placeholder]="(value && value.length ? null : placeholder)" [attr.tabindex]="tabindex" (keydown)="onKeydown($event)"
203 (input)="onInput()" (paste)="onPaste($event)" [attr.aria-labelledby]="ariaLabelledBy" (focus)="onInputFocus($event)" (blur)="onInputBlur($event)" [disabled]="disabled" [ngStyle]="inputStyle" [class]="inputStyleClass">
204 </li>
205 </ul>
206 </div>
207 `,
208 host: {
209 '[class.p-inputwrapper-filled]': 'filled',
210 '[class.p-inputwrapper-focus]': 'focus'
211 },
212 providers: [CHIPS_VALUE_ACCESSOR],
213 changeDetection: ChangeDetectionStrategy.OnPush,
214 encapsulation: ViewEncapsulation.None,
215 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}"]
216 },] }
217];
218Chips.ctorParameters = () => [
219 { type: ElementRef },
220 { type: ChangeDetectorRef }
221];
222Chips.propDecorators = {
223 style: [{ type: Input }],
224 styleClass: [{ type: Input }],
225 disabled: [{ type: Input }],
226 field: [{ type: Input }],
227 placeholder: [{ type: Input }],
228 max: [{ type: Input }],
229 ariaLabelledBy: [{ type: Input }],
230 tabindex: [{ type: Input }],
231 inputId: [{ type: Input }],
232 allowDuplicate: [{ type: Input }],
233 inputStyle: [{ type: Input }],
234 inputStyleClass: [{ type: Input }],
235 addOnTab: [{ type: Input }],
236 addOnBlur: [{ type: Input }],
237 separator: [{ type: Input }],
238 onAdd: [{ type: Output }],
239 onRemove: [{ type: Output }],
240 onFocus: [{ type: Output }],
241 onBlur: [{ type: Output }],
242 onChipClick: [{ type: Output }],
243 inputViewChild: [{ type: ViewChild, args: ['inputtext',] }],
244 templates: [{ type: ContentChildren, args: [PrimeTemplate,] }]
245};
246class ChipsModule {
247}
248ChipsModule.decorators = [
249 { type: NgModule, args: [{
250 imports: [CommonModule, InputTextModule, SharedModule],
251 exports: [Chips, InputTextModule, SharedModule],
252 declarations: [Chips]
253 },] }
254];
255
256/**
257 * Generated bundle index. Do not edit.
258 */
259
260export { CHIPS_VALUE_ACCESSOR, Chips, ChipsModule };
261//# sourceMappingURL=primeng-chips.js.map