forked from netbrain/ng-tags-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-tags-input.js
876 lines (760 loc) · 33 KB
/
ng-tags-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/*!
* ngTagsInput v2.1.1
* http://mbenford.github.io/ngTagsInput
*
* Copyright (c) 2013-2014 Michael Benford
* License: MIT
*
* Generated at 2014-09-15 08:51:03 +0200
*/
(function() {
'use strict';
var KEYS = {
backspace: 8,
tab: 9,
enter: 13,
escape: 27,
space: 32,
up: 38,
down: 40,
comma: 188
};
var MAX_SAFE_INTEGER = 9007199254740991;
var SUPPORTED_INPUT_TYPES = ['text', 'email', 'url'];
function SimplePubSub() {
var events = {};
return {
on: function(names, handler) {
names.split(' ').forEach(function(name) {
if (!events[name]) {
events[name] = [];
}
events[name].push(handler);
});
return this;
},
trigger: function(name, args) {
angular.forEach(events[name], function(handler) {
handler.call(null, args);
});
return this;
}
};
}
function makeObjectArray(array, key) {
array = array || [];
if (array.length > 0 && !angular.isObject(array[0])) {
array.forEach(function(item, index) {
array[index] = {};
array[index][key] = item;
});
}
return array;
}
function findInObjectArray(array, obj, key) {
var item = null;
for (var i = 0; i < array.length; i++) {
// I'm aware of the internationalization issues regarding toLowerCase()
// but I couldn't come up with a better solution right now
if (safeToString(array[i][key]).toLowerCase() === safeToString(obj[key]).toLowerCase()) {
item = array[i];
break;
}
}
return item;
}
function replaceAll(str, substr, newSubstr) {
if (!substr) {
return str;
}
var expression = substr.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
return str.replace(new RegExp(expression, 'gi'), newSubstr);
}
function safeToString(value) {
return angular.isUndefined(value) || value == null ? '' : value.toString().trim();
}
function encodeHTML(value) {
return value.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
var tagsInput = angular.module('ngTagsInput', []);
/**
* @ngdoc directive
* @name tagsInput
* @module ngTagsInput
*
* @description
* Renders an input box with tag editing support.
*
* @param {string} ngModel Assignable angular expression to data-bind to.
* @param {string=} [displayProperty=text] Property to be rendered as the tag label.
* @param {string=} [type=text] Type of the input element. Only 'text', 'email' and 'url' are supported values.
* @param {number=} tabindex Tab order of the control.
* @param {string=} [placeholder=Add a tag] Placeholder text for the control.
* @param {number=} [minLength=3] Minimum length for a new tag.
* @param {number=} [maxLength=MAX_SAFE_INTEGER] Maximum length allowed for a new tag.
* @param {number=} [minTags=0] Sets minTags validation error key if the number of tags added is less than minTags.
* @param {number=} [maxTags=MAX_SAFE_INTEGER] Sets maxTags validation error key if the number of tags added is greater than maxTags.
* @param {boolean=} [allowLeftoverText=false] Sets leftoverText validation error key if there is any leftover text in
* the input element when the directive loses focus.
* @param {string=} [removeTagSymbol=×] Symbol character for the remove tag button.
* @param {boolean=} [addOnEnter=true] Flag indicating that a new tag will be added on pressing the ENTER key.
* @param {boolean=} [addOnSpace=false] Flag indicating that a new tag will be added on pressing the SPACE key.
* @param {boolean=} [addOnComma=true] Flag indicating that a new tag will be added on pressing the COMMA key.
* @param {boolean=} [addOnBlur=true] Flag indicating that a new tag will be added when the input field loses focus.
* @param {boolean=} [replaceSpacesWithDashes=true] Flag indicating that spaces will be replaced with dashes.
* @param {string=} [allowedTagsPattern=.+] Regular expression that determines whether a new tag is valid.
* @param {boolean=} [enableEditingLastTag=false] Flag indicating that the last tag will be moved back into
* the new tag input box instead of being removed when the backspace key
* is pressed and the input box is empty.
* @param {boolean=} [addFromAutocompleteOnly=false] Flag indicating that only tags coming from the autocomplete list will be allowed.
* When this flag is true, addOnEnter, addOnComma, addOnSpace, addOnBlur and
* allowLeftoverText values are ignored.
* @param {expression} onTagAdded Expression to evaluate upon adding a new tag. The new tag is available as $tag.
* @param {expression} onTagRemoved Expression to evaluate upon removing an existing tag. The removed tag is available as $tag.
* @param {expression} onTagClicked Expression to evaluate upon clicking an existing tag. The clicked tag is available as $tag.
*/
tagsInput.directive('tagsInput', ["$timeout","$document","tagsInputConfig", function($timeout, $document, tagsInputConfig) {
function TagList(options, events) {
var self = {}, getTagText, setTagText, tagIsValid;
getTagText = function(tag) {
return safeToString(tag[options.displayProperty]);
};
setTagText = function(tag, text) {
tag[options.displayProperty] = text;
};
tagIsValid = function(tag) {
var tagText = getTagText(tag);
return tagText &&
tagText.length >= options.minLength &&
tagText.length <= options.maxLength &&
options.allowedTagsPattern.test(tagText) &&
!findInObjectArray(self.items, tag, options.displayProperty);
};
self.items = [];
self.addText = function(text) {
var tag = {};
setTagText(tag, text);
return self.add(tag);
};
self.add = function(tag) {
var tagText = getTagText(tag);
if (options.replaceSpacesWithDashes) {
tagText = tagText.replace(/\s/g, '-');
}
setTagText(tag, tagText);
if (tagIsValid(tag)) {
self.items.push(tag);
events.trigger('tag-added', { $tag: tag });
}
else if (tagText) {
events.trigger('invalid-tag', { $tag: tag });
}
return tag;
};
self.remove = function(index, event) {
if (event) { event.stopPropagation(); }
var tag = self.items.splice(index, 1)[0];
events.trigger('tag-removed', { $tag: tag });
return tag;
};
self.removeLast = function() {
var tag, lastTagIndex = self.items.length - 1;
if (options.enableEditingLastTag || self.selected) {
self.selected = null;
tag = self.remove(lastTagIndex);
}
else if (!self.selected) {
self.selected = self.items[lastTagIndex];
}
return tag;
};
self.tagClick = function(index) {
var tag = self.items[index];
events.trigger('tag-clicked', { $tag: tag });
};
return self;
}
function validateType(type) {
return SUPPORTED_INPUT_TYPES.indexOf(type) !== -1;
}
return {
restrict: 'E',
require: 'ngModel',
scope: {
tags: '=ngModel',
onTagAdded: '&',
onTagRemoved: '&',
onTagClicked: '&'
},
replace: false,
transclude: true,
templateUrl: 'ngTagsInput/tags-input.html',
controller: ["$scope","$attrs","$element", function($scope, $attrs, $element) {
$scope.events = new SimplePubSub();
tagsInputConfig.load('tagsInput', $scope, $attrs, {
type: [String, 'text', validateType],
placeholder: [String, 'Add a tag'],
tabindex: [Number, null],
removeTagSymbol: [String, String.fromCharCode(215)],
replaceSpacesWithDashes: [Boolean, true],
minLength: [Number, 3],
maxLength: [Number, MAX_SAFE_INTEGER],
addOnEnter: [Boolean, true],
addOnSpace: [Boolean, false],
addOnComma: [Boolean, true],
addOnBlur: [Boolean, true],
allowedTagsPattern: [RegExp, /.+/],
enableEditingLastTag: [Boolean, false],
minTags: [Number, 0],
maxTags: [Number, MAX_SAFE_INTEGER],
displayProperty: [String, 'text'],
allowLeftoverText: [Boolean, false],
addFromAutocompleteOnly: [Boolean, false]
});
$scope.tagList = new TagList($scope.options, $scope.events);
this.registerAutocomplete = function() {
var input = $element.find('input');
input.on('keydown', function(e) {
$scope.events.trigger('input-keydown', e);
});
return {
addTag: function(tag) {
return $scope.tagList.add(tag);
},
focusInput: function() {
input[0].focus();
},
getTags: function() {
return $scope.tags;
},
getCurrentTagText: function() {
return $scope.newTag.text;
},
getOptions: function() {
return $scope.options;
},
on: function(name, handler) {
$scope.events.on(name, handler);
return this;
}
};
};
}],
link: function(scope, element, attrs, ngModelCtrl) {
var hotkeys = [KEYS.enter, KEYS.comma, KEYS.space, KEYS.backspace],
tagList = scope.tagList,
events = scope.events,
options = scope.options,
input = element.find('input'),
validationOptions = ['minTags', 'maxTags', 'allowLeftoverText'],
setElementValidity;
setElementValidity = function() {
ngModelCtrl.$setValidity('maxTags', scope.tags.length <= options.maxTags);
ngModelCtrl.$setValidity('minTags', scope.tags.length >= options.minTags);
ngModelCtrl.$setValidity('leftoverText', options.allowLeftoverText ? true : !scope.newTag.text);
};
events
.on('tag-added', scope.onTagAdded)
.on('tag-removed', scope.onTagRemoved)
.on('tag-clicked', scope.onTagClicked)
.on('tag-added', function() {
scope.newTag.text = '';
})
.on('tag-added tag-removed', function() {
ngModelCtrl.$setViewValue(scope.tags);
})
.on('invalid-tag', function() {
scope.newTag.invalid = true;
})
.on('input-change', function() {
tagList.selected = null;
scope.newTag.invalid = null;
})
.on('input-focus', function() {
ngModelCtrl.$setValidity('leftoverText', true);
})
.on('input-blur', function() {
if (!options.addFromAutocompleteOnly) {
if (options.addOnBlur) {
tagList.addText(scope.newTag.text);
}
setElementValidity();
}
})
.on('option-change', function(e) {
if (validationOptions.indexOf(e.name) !== -1) {
setElementValidity();
}
});
scope.newTag = { text: '', invalid: null };
scope.getDisplayText = function(tag) {
return safeToString(tag[options.displayProperty]);
};
scope.track = function(tag) {
return tag[options.displayProperty];
};
scope.newTagChange = function() {
events.trigger('input-change', scope.newTag.text);
};
scope.$watch('tags', function(value) {
scope.tags = makeObjectArray(value, options.displayProperty);
tagList.items = scope.tags;
});
scope.$watch('tags.length', function() {
setElementValidity();
});
input
.on('keydown', function(e) {
// This hack is needed because jqLite doesn't implement stopImmediatePropagation properly.
// I've sent a PR to Angular addressing this issue and hopefully it'll be fixed soon.
// https://github.com/angular/angular.js/pull/4833
if (e.isImmediatePropagationStopped && e.isImmediatePropagationStopped()) {
return;
}
var key = e.keyCode,
isModifier = e.shiftKey || e.altKey || e.ctrlKey || e.metaKey,
addKeys = {},
shouldAdd, shouldRemove;
if (isModifier || hotkeys.indexOf(key) === -1) {
return;
}
addKeys[KEYS.enter] = options.addOnEnter;
addKeys[KEYS.comma] = options.addOnComma;
addKeys[KEYS.space] = options.addOnSpace;
shouldAdd = !options.addFromAutocompleteOnly && addKeys[key];
shouldRemove = !shouldAdd && key === KEYS.backspace && scope.newTag.text.length === 0;
if (shouldAdd) {
tagList.addText(scope.newTag.text);
scope.$apply();
e.preventDefault();
}
else if (shouldRemove) {
var tag = tagList.removeLast();
if (tag && options.enableEditingLastTag) {
scope.newTag.text = tag[options.displayProperty];
}
scope.$apply();
e.preventDefault();
}
})
.on('focus', function() {
if (scope.hasFocus) {
return;
}
scope.hasFocus = true;
events.trigger('input-focus');
scope.$apply();
})
.on('blur', function() {
$timeout(function() {
var activeElement = $document.prop('activeElement'),
lostFocusToBrowserWindow = activeElement === input[0],
lostFocusToChildElement = element[0].contains(activeElement);
if (lostFocusToBrowserWindow || !lostFocusToChildElement) {
scope.hasFocus = false;
events.trigger('input-blur');
}
});
});
element.find('div').on('click', function() {
input[0].focus();
});
}
};
}]);
/**
* @ngdoc directive
* @name autoComplete
* @module ngTagsInput
*
* @description
* Provides autocomplete support for the tagsInput directive.
*
* @param {expression} source Expression to evaluate upon changing the input content. The input value is available as
* $query. The result of the expression must be a promise that eventually resolves to an
* array of strings.
* @param {number=} [debounceDelay=100] Amount of time, in milliseconds, to wait before evaluating the expression in
* the source option after the last keystroke.
* @param {number=} [minLength=3] Minimum number of characters that must be entered before evaluating the expression
* in the source option.
* @param {boolean=} [highlightMatchedText=true] Flag indicating that the matched text will be highlighted in the
* suggestions list.
* @param {number=} [maxResultsToShow=10] Maximum number of results to be displayed at a time.
* @param {boolean=} [loadOnDownArrow=false] Flag indicating that the source option will be evaluated when the down arrow
* key is pressed and the suggestion list is closed. The current input value
* is available as $query.
* @param {boolean=} {loadOnEmpty=false} Flag indicating that the source option will be evaluated when the input content
* becomes empty. The $query variable will be passed to the expression as an empty string.
* @param {boolean=} {loadOnFocus=false} Flag indicating that the source option will be evaluated when the input element
* gains focus. The current input value is available as $query.
*/
tagsInput.directive('autoComplete', ["$document","$timeout","$sce","tagsInputConfig", function($document, $timeout, $sce, tagsInputConfig) {
function SuggestionList(loadFn, options) {
var self = {}, debouncedLoadId, getDifference, lastPromise;
getDifference = function(array1, array2) {
return array1.filter(function(item) {
return !findInObjectArray(array2, item, options.tagsInput.displayProperty);
});
};
self.reset = function() {
lastPromise = null;
self.items = [];
self.visible = false;
self.index = -1;
self.selected = null;
self.query = null;
$timeout.cancel(debouncedLoadId);
};
self.show = function() {
self.selected = null;
self.visible = true;
};
self.load = function(query, tags) {
$timeout.cancel(debouncedLoadId);
debouncedLoadId = $timeout(function() {
self.query = query;
var promise = loadFn({ $query: query });
lastPromise = promise;
promise.then(function(items) {
if (promise !== lastPromise) {
return;
}
items = makeObjectArray(items.data || items, options.tagsInput.displayProperty);
items = getDifference(items, tags);
self.items = items.slice(0, options.maxResultsToShow);
if (self.items.length > 0) {
self.show();
}
else {
self.reset();
}
});
}, options.debounceDelay, false);
};
self.selectNext = function() {
self.select(++self.index);
};
self.selectPrior = function() {
self.select(--self.index);
};
self.select = function(index) {
if (index < 0) {
index = self.items.length - 1;
}
else if (index >= self.items.length) {
index = 0;
}
self.index = index;
self.selected = self.items[index];
};
self.reset();
return self;
}
return {
restrict: 'E',
require: '^tagsInput',
scope: { source: '&' },
templateUrl: 'ngTagsInput/auto-complete.html',
link: function(scope, element, attrs, tagsInputCtrl) {
var hotkeys = [KEYS.enter, KEYS.tab, KEYS.escape, KEYS.up, KEYS.down],
suggestionList, tagsInput, options, getItem, getDisplayText, shouldLoadSuggestions;
tagsInputConfig.load('autoComplete', scope, attrs, {
debounceDelay: [Number, 100],
minLength: [Number, 3],
highlightMatchedText: [Boolean, true],
maxResultsToShow: [Number, 10],
loadOnDownArrow: [Boolean, false],
loadOnEmpty: [Boolean, false],
loadOnFocus: [Boolean, false]
});
options = scope.options;
tagsInput = tagsInputCtrl.registerAutocomplete();
options.tagsInput = tagsInput.getOptions();
suggestionList = new SuggestionList(scope.source, options);
getItem = function(item) {
return item[options.tagsInput.displayProperty];
};
getDisplayText = function(item) {
return safeToString(getItem(item));
};
shouldLoadSuggestions = function(value) {
return value && value.length >= options.minLength || !value && options.loadOnEmpty;
};
scope.suggestionList = suggestionList;
scope.addSuggestionByIndex = function(index) {
suggestionList.select(index);
scope.addSuggestion();
};
scope.addSuggestion = function() {
var added = false;
if (suggestionList.selected) {
tagsInput.addTag(suggestionList.selected);
suggestionList.reset();
tagsInput.focusInput();
added = true;
}
return added;
};
scope.highlight = function(item) {
var text = getDisplayText(item);
text = encodeHTML(text);
if (options.highlightMatchedText) {
text = replaceAll(text, encodeHTML(suggestionList.query), '<em>$&</em>');
}
return $sce.trustAsHtml(text);
};
scope.track = function(item) {
return getItem(item);
};
tagsInput
.on('tag-added tag-removed invalid-tag input-blur', function() {
suggestionList.reset();
})
.on('input-change', function(value) {
if (shouldLoadSuggestions(value)) {
suggestionList.load(value, tagsInput.getTags());
}
else {
suggestionList.reset();
}
})
.on('input-focus', function() {
var value = tagsInput.getCurrentTagText();
if (options.loadOnFocus && shouldLoadSuggestions(value)) {
suggestionList.load(value, tagsInput.getTags());
}
})
.on('input-keydown', function(e) {
// This hack is needed because jqLite doesn't implement stopImmediatePropagation properly.
// I've sent a PR to Angular addressing this issue and hopefully it'll be fixed soon.
// https://github.com/angular/angular.js/pull/4833
var immediatePropagationStopped = false;
e.stopImmediatePropagation = function() {
immediatePropagationStopped = true;
e.stopPropagation();
};
e.isImmediatePropagationStopped = function() {
return immediatePropagationStopped;
};
var key = e.keyCode,
handled = false;
if (hotkeys.indexOf(key) === -1) {
return;
}
if (suggestionList.visible) {
if (key === KEYS.down) {
suggestionList.selectNext();
handled = true;
}
else if (key === KEYS.up) {
suggestionList.selectPrior();
handled = true;
}
else if (key === KEYS.escape) {
suggestionList.reset();
handled = true;
}
else if (key === KEYS.enter || key === KEYS.tab) {
handled = scope.addSuggestion();
}
}
else {
if (key === KEYS.down && scope.options.loadOnDownArrow) {
suggestionList.load(tagsInput.getCurrentTagText(), tagsInput.getTags());
handled = true;
}
}
if (handled) {
e.preventDefault();
e.stopImmediatePropagation();
scope.$apply();
}
});
}
};
}]);
/**
* @ngdoc directive
* @name tiTranscludeAppend
* @module ngTagsInput
*
* @description
* Re-creates the old behavior of ng-transclude. Used internally by tagsInput directive.
*/
tagsInput.directive('tiTranscludeAppend', function() {
return function(scope, element, attrs, ctrl, transcludeFn) {
transcludeFn(function(clone) {
element.append(clone);
});
};
});
/**
* @ngdoc directive
* @name tiAutosize
* @module ngTagsInput
*
* @description
* Automatically sets the input's width so its content is always visible. Used internally by tagsInput directive.
*/
tagsInput.directive('tiAutosize', ["tagsInputConfig", function(tagsInputConfig) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
var threshold = tagsInputConfig.getTextAutosizeThreshold(),
span, resize;
span = angular.element('<span class="input"></span>');
span.css('display', 'none')
.css('visibility', 'hidden')
.css('width', 'auto')
.css('white-space', 'pre');
element.parent().append(span);
resize = function(originalValue) {
var value = originalValue, width;
if (angular.isString(value) && value.length === 0) {
value = attrs.placeholder;
}
if (value) {
span.text(value);
span.css('display', '');
width = span.prop('offsetWidth');
span.css('display', 'none');
}
element.css('width', width ? width + threshold + 'px' : '');
return originalValue;
};
ctrl.$parsers.unshift(resize);
ctrl.$formatters.unshift(resize);
attrs.$observe('placeholder', function(value) {
if (!ctrl.$modelValue) {
resize(value);
}
});
}
};
}]);
/**
* @ngdoc directive
* @name tiBindAttrs
* @module ngTagsInput
*
* @description
* Binds attributes to expressions. Used internally by tagsInput directive.
*/
tagsInput.directive('tiBindAttrs', function() {
return function(scope, element, attrs) {
scope.$watch(attrs.tiBindAttrs, function(value) {
angular.forEach(value, function(value, key) {
attrs.$set(key, value);
});
}, true);
};
});
/**
* @ngdoc service
* @name tagsInputConfig
* @module ngTagsInput
*
* @description
* Sets global configuration settings for both tagsInput and autoComplete directives. It's also used internally to parse and
* initialize options from HTML attributes.
*/
tagsInput.provider('tagsInputConfig', function() {
var globalDefaults = {},
interpolationStatus = {},
autosizeThreshold = 3;
/**
* @ngdoc method
* @name setDefaults
* @description Sets the default configuration option for a directive.
* @methodOf tagsInputConfig
*
* @param {string} directive Name of the directive to be configured. Must be either 'tagsInput' or 'autoComplete'.
* @param {object} defaults Object containing options and their values.
*
* @returns {object} The service itself for chaining purposes.
*/
this.setDefaults = function(directive, defaults) {
globalDefaults[directive] = defaults;
return this;
};
/***
* @ngdoc method
* @name setActiveInterpolation
* @description Sets active interpolation for a set of options.
* @methodOf tagsInputConfig
*
* @param {string} directive Name of the directive to be configured. Must be either 'tagsInput' or 'autoComplete'.
* @param {object} options Object containing which options should have interpolation turned on at all times.
*
* @returns {object} The service itself for chaining purposes.
*/
this.setActiveInterpolation = function(directive, options) {
interpolationStatus[directive] = options;
return this;
};
/***
* @ngdoc method
* @name setTextAutosizeThreshold
* @methodOf tagsInputConfig
*
* @param {number} threshold Threshold to be used by the tagsInput directive to re-size the input element based on its contents.
*
* @returns {object} The service itself for chaining purposes.
*/
this.setTextAutosizeThreshold = function(threshold) {
autosizeThreshold = threshold;
return this;
};
this.$get = ["$interpolate", function($interpolate) {
var converters = {};
converters[String] = function(value) { return value; };
converters[Number] = function(value) { return parseInt(value, 10); };
converters[Boolean] = function(value) { return value.toLowerCase() === 'true'; };
converters[RegExp] = function(value) { return new RegExp(value); };
return {
load: function(directive, scope, attrs, options) {
var defaultValidator = function() { return true; };
scope.options = {};
angular.forEach(options, function(value, key) {
var type, localDefault, validator, converter, getDefault, updateValue;
type = value[0];
localDefault = value[1];
validator = value[2] || defaultValidator;
converter = converters[type];
getDefault = function() {
var globalValue = globalDefaults[directive] && globalDefaults[directive][key];
return angular.isDefined(globalValue) ? globalValue : localDefault;
};
updateValue = function(value) {
scope.options[key] = value && validator(value) ? converter(value) : getDefault();
};
if (interpolationStatus[directive] && interpolationStatus[directive][key]) {
attrs.$observe(key, function(value) {
updateValue(value);
scope.events.trigger('option-change', { name: key, newValue: value });
});
}
else {
updateValue(attrs[key] && $interpolate(attrs[key])(scope.$parent));
}
});
},
getTextAutosizeThreshold: function() {
return autosizeThreshold;
}
};
}];
});
/* HTML templates */
tagsInput.run(["$templateCache", function($templateCache) {
$templateCache.put('ngTagsInput/tags-input.html',
"<div class=\"host\" tabindex=\"-1\" ti-transclude-append=\"\"><div class=\"tags\" ng-class=\"{focused: hasFocus}\"><ul class=\"tag-list\"><li class=\"tag-item\" ng-repeat=\"tag in tagList.items track by track(tag)\" ng-class=\"{ selected: tag == tagList.selected }\" ng-click=\"tagList.tagClick($index)\"><span ng-bind=\"getDisplayText(tag)\"></span> <a class=\"remove-button\" ng-click=\"tagList.remove($index, $event)\" ng-bind=\"options.removeTagSymbol\"></a></li></ul><input class=\"input\" ng-model=\"newTag.text\" ng-change=\"newTagChange()\" ng-trim=\"false\" ng-class=\"{'invalid-tag': newTag.invalid}\" ti-bind-attrs=\"{type: options.type, placeholder: options.placeholder, tabindex: options.tabindex}\" ti-autosize=\"\"></div></div>"
);
$templateCache.put('ngTagsInput/auto-complete.html',
"<div class=\"autocomplete\" ng-show=\"suggestionList.visible\"><ul class=\"suggestion-list\"><li class=\"suggestion-item\" ng-repeat=\"item in suggestionList.items track by track(item)\" ng-class=\"{selected: item == suggestionList.selected}\" ng-click=\"addSuggestionByIndex($index)\" ng-mouseenter=\"suggestionList.select($index)\" ng-bind-html=\"highlight(item)\"></li></ul></div>"
);
}]);
}());