UNPKG

14.1 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/inputtext'), require('@angular/forms')) :
3 typeof define === 'function' && define.amd ? define('primeng/chips', ['exports', '@angular/core', '@angular/common', 'primeng/api', 'primeng/inputtext', '@angular/forms'], factory) :
4 (global = global || self, factory((global.primeng = global.primeng || {}, global.primeng.chips = {}), global.ng.core, global.ng.common, global.primeng.api, global.primeng.inputtext, global.ng.forms));
5}(this, (function (exports, core, common, api, inputtext, 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 CHIPS_VALUE_ACCESSOR = {
34 provide: forms.NG_VALUE_ACCESSOR,
35 useExisting: core.forwardRef(function () { return Chips; }),
36 multi: true
37 };
38 var Chips = /** @class */ (function () {
39 function Chips(el) {
40 this.el = el;
41 this.allowDuplicate = true;
42 this.onAdd = new core.EventEmitter();
43 this.onRemove = new core.EventEmitter();
44 this.onFocus = new core.EventEmitter();
45 this.onBlur = new core.EventEmitter();
46 this.onChipClick = new core.EventEmitter();
47 this.onModelChange = function () { };
48 this.onModelTouched = function () { };
49 }
50 Chips.prototype.ngAfterContentInit = function () {
51 var _this = this;
52 this.templates.forEach(function (item) {
53 switch (item.getType()) {
54 case 'item':
55 _this.itemTemplate = item.template;
56 break;
57 default:
58 _this.itemTemplate = item.template;
59 break;
60 }
61 });
62 };
63 Chips.prototype.onClick = function () {
64 this.inputViewChild.nativeElement.focus();
65 };
66 Chips.prototype.onInput = function () {
67 this.updateFilledState();
68 };
69 Chips.prototype.onPaste = function (event) {
70 var _this = this;
71 if (this.separator) {
72 var pastedData = (event.clipboardData || window['clipboardData']).getData('Text');
73 pastedData.split(this.separator).forEach(function (val) {
74 _this.addItem(event, val, true);
75 });
76 this.inputViewChild.nativeElement.value = '';
77 }
78 this.updateFilledState();
79 };
80 Chips.prototype.updateFilledState = function () {
81 if (!this.value || this.value.length === 0) {
82 this.filled = (this.inputViewChild.nativeElement && this.inputViewChild.nativeElement.value != '');
83 }
84 else {
85 this.filled = true;
86 }
87 };
88 Chips.prototype.onItemClick = function (event, item) {
89 this.onChipClick.emit({
90 originalEvent: event,
91 value: item
92 });
93 };
94 Chips.prototype.writeValue = function (value) {
95 this.value = value;
96 this.updateMaxedOut();
97 };
98 Chips.prototype.registerOnChange = function (fn) {
99 this.onModelChange = fn;
100 };
101 Chips.prototype.registerOnTouched = function (fn) {
102 this.onModelTouched = fn;
103 };
104 Chips.prototype.setDisabledState = function (val) {
105 this.disabled = val;
106 };
107 Chips.prototype.resolveFieldData = function (data, field) {
108 if (data && field) {
109 if (field.indexOf('.') == -1) {
110 return data[field];
111 }
112 else {
113 var fields = field.split('.');
114 var value = data;
115 for (var i = 0, len = fields.length; i < len; ++i) {
116 value = value[fields[i]];
117 }
118 return value;
119 }
120 }
121 else {
122 return null;
123 }
124 };
125 Chips.prototype.onInputFocus = function (event) {
126 this.focus = true;
127 this.onFocus.emit(event);
128 };
129 Chips.prototype.onInputBlur = function (event) {
130 this.focus = false;
131 if (this.addOnBlur && this.inputViewChild.nativeElement.value) {
132 this.addItem(event, this.inputViewChild.nativeElement.value, false);
133 }
134 this.onModelTouched();
135 this.onBlur.emit(event);
136 };
137 Chips.prototype.removeItem = function (event, index) {
138 if (this.disabled) {
139 return;
140 }
141 var removedItem = this.value[index];
142 this.value = this.value.filter(function (val, i) { return i != index; });
143 this.onModelChange(this.value);
144 this.onRemove.emit({
145 originalEvent: event,
146 value: removedItem
147 });
148 this.updateFilledState();
149 this.updateMaxedOut();
150 };
151 Chips.prototype.addItem = function (event, item, preventDefault) {
152 this.value = this.value || [];
153 if (item && item.trim().length) {
154 if (this.allowDuplicate || this.value.indexOf(item) === -1) {
155 this.value = __spread(this.value, [item]);
156 this.onModelChange(this.value);
157 this.onAdd.emit({
158 originalEvent: event,
159 value: item
160 });
161 }
162 }
163 this.updateFilledState();
164 this.updateMaxedOut();
165 this.inputViewChild.nativeElement.value = '';
166 if (preventDefault) {
167 event.preventDefault();
168 }
169 };
170 Chips.prototype.onKeydown = function (event) {
171 switch (event.which) {
172 //backspace
173 case 8:
174 if (this.inputViewChild.nativeElement.value.length === 0 && this.value && this.value.length > 0) {
175 this.value = __spread(this.value);
176 var removedItem = this.value.pop();
177 this.onModelChange(this.value);
178 this.onRemove.emit({
179 originalEvent: event,
180 value: removedItem
181 });
182 this.updateFilledState();
183 }
184 break;
185 //enter
186 case 13:
187 this.addItem(event, this.inputViewChild.nativeElement.value, true);
188 break;
189 case 9:
190 if (this.addOnTab && this.inputViewChild.nativeElement.value !== '') {
191 this.addItem(event, this.inputViewChild.nativeElement.value, true);
192 }
193 break;
194 default:
195 if (this.max && this.value && this.max === this.value.length) {
196 event.preventDefault();
197 }
198 else if (this.separator) {
199 if (this.separator === ',' && event.which === 188) {
200 this.addItem(event, this.inputViewChild.nativeElement.value, true);
201 }
202 }
203 break;
204 }
205 };
206 Chips.prototype.updateMaxedOut = function () {
207 if (this.inputViewChild && this.inputViewChild.nativeElement) {
208 if (this.max && this.value && this.max === this.value.length)
209 this.inputViewChild.nativeElement.disabled = true;
210 else
211 this.inputViewChild.nativeElement.disabled = this.disabled || false;
212 }
213 };
214 Chips.ctorParameters = function () { return [
215 { type: core.ElementRef }
216 ]; };
217 __decorate([
218 core.Input()
219 ], Chips.prototype, "style", void 0);
220 __decorate([
221 core.Input()
222 ], Chips.prototype, "styleClass", void 0);
223 __decorate([
224 core.Input()
225 ], Chips.prototype, "disabled", void 0);
226 __decorate([
227 core.Input()
228 ], Chips.prototype, "field", void 0);
229 __decorate([
230 core.Input()
231 ], Chips.prototype, "placeholder", void 0);
232 __decorate([
233 core.Input()
234 ], Chips.prototype, "max", void 0);
235 __decorate([
236 core.Input()
237 ], Chips.prototype, "ariaLabelledBy", void 0);
238 __decorate([
239 core.Input()
240 ], Chips.prototype, "tabindex", void 0);
241 __decorate([
242 core.Input()
243 ], Chips.prototype, "inputId", void 0);
244 __decorate([
245 core.Input()
246 ], Chips.prototype, "allowDuplicate", void 0);
247 __decorate([
248 core.Input()
249 ], Chips.prototype, "inputStyle", void 0);
250 __decorate([
251 core.Input()
252 ], Chips.prototype, "inputStyleClass", void 0);
253 __decorate([
254 core.Input()
255 ], Chips.prototype, "addOnTab", void 0);
256 __decorate([
257 core.Input()
258 ], Chips.prototype, "addOnBlur", void 0);
259 __decorate([
260 core.Input()
261 ], Chips.prototype, "separator", void 0);
262 __decorate([
263 core.Output()
264 ], Chips.prototype, "onAdd", void 0);
265 __decorate([
266 core.Output()
267 ], Chips.prototype, "onRemove", void 0);
268 __decorate([
269 core.Output()
270 ], Chips.prototype, "onFocus", void 0);
271 __decorate([
272 core.Output()
273 ], Chips.prototype, "onBlur", void 0);
274 __decorate([
275 core.Output()
276 ], Chips.prototype, "onChipClick", void 0);
277 __decorate([
278 core.ViewChild('inputtext')
279 ], Chips.prototype, "inputViewChild", void 0);
280 __decorate([
281 core.ContentChildren(api.PrimeTemplate)
282 ], Chips.prototype, "templates", void 0);
283 Chips = __decorate([
284 core.Component({
285 selector: 'p-chips',
286 template: "\n <div [ngClass]=\"'ui-chips ui-widget'\" [ngStyle]=\"style\" [class]=\"styleClass\" (click)=\"onClick()\">\n <ul [ngClass]=\"{'ui-inputtext ui-state-default ui-corner-all':true,'ui-state-focus':focus,'ui-state-disabled':disabled}\">\n <li #token *ngFor=\"let item of value; let i = index;\" class=\"ui-chips-token ui-state-highlight ui-corner-all\" (click)=\"onItemClick($event, item)\">\n <span *ngIf=\"!disabled\" class=\"ui-chips-token-icon pi pi-fw pi-times\" (click)=\"removeItem($event,i)\"></span>\n <span *ngIf=\"!itemTemplate\" class=\"ui-chips-token-label\">{{field ? resolveFieldData(item,field) : item}}</span>\n <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: item}\"></ng-container>\n </li>\n <li class=\"ui-chips-input-token\">\n <input #inputtext type=\"text\" [attr.id]=\"inputId\" [attr.placeholder]=\"(value && value.length ? null : placeholder)\" [attr.tabindex]=\"tabindex\" (keydown)=\"onKeydown($event)\"\n (input)=\"onInput()\" (paste)=\"onPaste($event)\" [attr.aria-labelledby]=\"ariaLabelledBy\" (focus)=\"onInputFocus($event)\" (blur)=\"onInputBlur($event)\" [disabled]=\"disabled\" [ngStyle]=\"inputStyle\" [class]=\"inputStyleClass\">\n </li>\n </ul>\n </div>\n ",
287 host: {
288 '[class.ui-inputwrapper-filled]': 'filled',
289 '[class.ui-inputwrapper-focus]': 'focus'
290 },
291 providers: [CHIPS_VALUE_ACCESSOR],
292 changeDetection: core.ChangeDetectionStrategy.Default
293 })
294 ], Chips);
295 return Chips;
296 }());
297 var ChipsModule = /** @class */ (function () {
298 function ChipsModule() {
299 }
300 ChipsModule = __decorate([
301 core.NgModule({
302 imports: [common.CommonModule, inputtext.InputTextModule, api.SharedModule],
303 exports: [Chips, inputtext.InputTextModule, api.SharedModule],
304 declarations: [Chips]
305 })
306 ], ChipsModule);
307 return ChipsModule;
308 }());
309
310 exports.CHIPS_VALUE_ACCESSOR = CHIPS_VALUE_ACCESSOR;
311 exports.Chips = Chips;
312 exports.ChipsModule = ChipsModule;
313
314 Object.defineProperty(exports, '__esModule', { value: true });
315
316})));
317//# sourceMappingURL=primeng-chips.umd.js.map