forked from max-favilli/tagmanager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagmanager.js
516 lines (431 loc) · 19.8 KB
/
tagmanager.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
/* ===================================================
* tagmanager.js v3.0.1
* http://welldonethings.com/tags/manager
* ===================================================
* Copyright 2012 Max Favilli
*
* Licensed under the Mozilla Public License, Version 2.0 You may not use this work except in compliance with the License.
*
* http://www.mozilla.org/MPL/2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================== */
(function($) {
"use strict";
var defaults = {
prefilled: null,
CapitalizeFirstLetter: false,
preventSubmitOnEnter: true, // deprecated
isClearInputOnEsc: true, // deprecated
externalTagId: false,
AjaxPush: null,
AjaxPushAllTags: null,
AjaxPushParameters: null,
delimiters: [9, 13, 44], // tab, enter, comma
backspace: [8],
maxTags: 0,
hiddenTagListName: null, // deprecated
hiddenTagListId: null, // deprecated
replace: true,
output: null,
deleteTagsOnBackspace: true, // deprecated
tagsContainer: null,
tagCloseIcon: 'x',
tagClass: '',
validator: null,
onlyTagList: false,
tagList: null,
fillInputOnTagRemove: false,
},
publicMethods = {
pushTag : function (tag, ignoreEvents, externalTagId) {
var $self = $(this), opts = $self.data('opts'), alreadyInList, tlisLowerCase, max, tagId,
tlis = $self.data("tlis"), tlid = $self.data("tlid"), idx, newTagId, newTagRemoveId, escaped,
html, $el, lastTagId, lastTagObj;
tag = privateMethods.trimTag(tag, opts.delimiterChars);
if (!tag || tag.length <= 0) { return; }
// check if restricted only to the tagList suggestions
if (opts.onlyTagList && undefined !== opts.tagList ){
//if the list has been updated by look pushed tag in the tagList. if not found return
if (opts.tagList){
var $tagList = opts.tagList;
// change each array item to lower case
$.each($tagList, function(index, item) {
$tagList[index] = item.toLowerCase();
});
var suggestion = $.inArray(tag.toLowerCase(), $tagList);
if ( -1 === suggestion ) {
//console.log("tag:" + tag + " not in tagList, not adding it");
return;
}
}
}
if (opts.CapitalizeFirstLetter && tag.length > 1) {
tag = tag.charAt(0).toUpperCase() + tag.slice(1).toLowerCase();
}
// call the validator (if any) and do not let the tag pass if invalid
if (opts.validator && !opts.validator(tag)) { return; }
// dont accept new tags beyond the defined maximum
if (opts.maxTags > 0 && tlis.length >= opts.maxTags) { return; }
alreadyInList = false;
//use jQuery.map to make this work in IE8 (pure JS map is JS 1.6 but IE8 only supports JS 1.5)
tlisLowerCase = jQuery.map(tlis, function(elem) {
return elem.toLowerCase();
});
idx = $.inArray(tag.toLowerCase(), tlisLowerCase);
if (-1 !== idx) {
// console.log("tag:" + tag + " !!already in list!!");
alreadyInList = true;
}
if (alreadyInList) {
$self.trigger('tm:duplicated', tag);
$("#" + $self.data("tm_rndid") + "_" + tlid[idx]).stop()
.animate({backgroundColor: opts.blinkBGColor_1}, 100)
.animate({backgroundColor: opts.blinkBGColor_2}, 100)
.animate({backgroundColor: opts.blinkBGColor_1}, 100)
.animate({backgroundColor: opts.blinkBGColor_2}, 100)
.animate({backgroundColor: opts.blinkBGColor_1}, 100)
.animate({backgroundColor: opts.blinkBGColor_2}, 100);
} else {
if (opts.externalTagId === true) {
if (externalTagId === undefined) {
$.error('externalTagId is not passed for tag -' + tag);
}
tagId = externalTagId;
} else {
max = Math.max.apply(null, tlid);
max = max === -Infinity ? 0 : max;
tagId = ++max;
}
if (!ignoreEvents) { $self.trigger('tm:pushing', [tag, tagId]); }
tlis.push(tag);
tlid.push(tagId);
if (!ignoreEvents)
if (opts.AjaxPush !== null && opts.AjaxPushAllTags == null) {
if ($.inArray(tag, opts.prefilled) === -1) {
$.post(opts.AjaxPush, $.extend({tag: tag}, opts.AjaxPushParameters));
}
}
// console.log("tagList: " + tlis);
newTagId = $self.data("tm_rndid") + '_' + tagId;
newTagRemoveId = $self.data("tm_rndid") + '_Remover_' + tagId;
escaped = $("<span/>").text(tag).html();
html = '<span class="' + privateMethods.tagClasses.call($self) + '" id="' + newTagId + '">';
html+= '<span>' + escaped + '</span>';
html+= '<a href="#" class="tm-tag-remove" id="' + newTagRemoveId + '" TagIdToRemove="' + tagId + '">';
html+= opts.tagCloseIcon + '</a></span> ';
$el = $(html);
if (opts.tagsContainer !== null) {
$(opts.tagsContainer).append($el);
} else {
if (tlid.length > 1) {
lastTagObj = $("#" + $self.data("tm_rndid") + "_" + tlid[tlid.length - 2]);
lastTagObj.after($el);
} else {
$self.before($el);
}
}
$el.find("#" + newTagRemoveId).on("click", $self, function(e) {
e.preventDefault();
if (opts.externalTagId === true) {
var TagIdToRemove = $(this).attr("TagIdToRemove");
} else {
var TagIdToRemove = parseInt($(this).attr("TagIdToRemove"));
}
privateMethods.spliceTag.call($self, TagIdToRemove, e.data);
});
privateMethods.refreshHiddenTagList.call($self);
if (!ignoreEvents) { $self.trigger('tm:pushed', [tag, tagId]); }
privateMethods.showOrHide.call($self);
//if (tagManagerOptions.maxTags > 0 && tlis.length >= tagManagerOptions.maxTags) {
// obj.hide();
//}
}
$self.val("");
},
popTag : function () {
var $self = $(this), tagId, tagBeingRemoved,
tlis = $self.data("tlis"),
tlid = $self.data("tlid");
if (tlid.length > 0) {
tagId = tlid.pop();
tagBeingRemoved = tlis[tlis.length - 1];
$self.trigger('tm:popping', [tagBeingRemoved, tagId]);
tlis.pop();
// console.log("TagIdToRemove: " + tagId);
$("#" + $self.data("tm_rndid") + "_" + tagId).remove();
privateMethods.refreshHiddenTagList.call($self);
$self.trigger('tm:popped', [tagBeingRemoved, tagId]);
// console.log(tlis);
}
},
removeTagByID : function (TagID) {
var $self = this;
privateMethods.spliceTag.call($self,TagID);
},
empty : function(send_trigger) {
var $self = $(this), tlis = $self.data("tlis"), tlid = $self.data("tlid"), tagId;
while (tlid.length > 0) {
tagId = tlid.pop();
tlis.pop();
// console.log("TagIdToRemove: " + tagId);
$("#" + $self.data("tm_rndid") + "_" + tagId).remove();
privateMethods.refreshHiddenTagList.call($self);
// console.log(tlis);
}
send_trigger = (typeof send_trigger === "undefined") ? true : send_trigger;
if (send_trigger === true) $self.trigger('tm:emptied', null);
privateMethods.showOrHide.call($self);
//if (tagManagerOptions.maxTags > 0 && tlis.length < tagManagerOptions.maxTags) {
// obj.show();
//}
},
tags : function() {
var $self = this, tlis = $self.data("tlis");
return tlis;
},
// Return tags ID
tags_id: function() {
var $self = this, tlid = $self.data("tlid");
return tlid;
}
},
privateMethods = {
showOrHide : function () {
var $self = this, opts = $self.data('opts'), tlis = $self.data("tlis");
if (opts.maxTags > 0 && tlis.length < opts.maxTags) {
$self.show();
$self.trigger('tm:show');
}
if (opts.maxTags > 0 && tlis.length >= opts.maxTags) {
$self.hide();
$self.trigger('tm:hide');
}
},
tagClasses : function () {
var $self = $(this), opts = $self.data('opts'), tagBaseClass = opts.tagBaseClass,
inputBaseClass = opts.inputBaseClass, cl;
// 1) default class (tm-tag)
cl = tagBaseClass;
// 2) interpolate from input class: tm-input-xxx --> tm-tag-xxx
if ($self.attr('class')) {
$.each($self.attr('class').split(' '), function (index, value) {
if (value.indexOf(inputBaseClass + '-') !== -1) {
cl += ' ' + tagBaseClass + value.substring(inputBaseClass.length);
}
});
}
// 3) tags from tagClass option
cl += (opts.tagClass ? ' ' + opts.tagClass : '');
return cl;
},
trimTag : function (tag, delimiterChars) {
var i;
tag = $.trim(tag);
// truncate at the first delimiter char
i = 0;
for (i; i < tag.length; i++) {
if ($.inArray(tag.charCodeAt(i), delimiterChars) !== -1) { break; }
}
return tag.substring(0, i);
},
refreshHiddenTagList : function () {
var $self = $(this), tlis = $self.data("tlis"), lhiddenTagList = $self.data("lhiddenTagList");
if (lhiddenTagList) {
$(lhiddenTagList).val(tlis.join($self.data('opts').baseDelimiter)).change();
}
$self.trigger('tm:refresh', tlis.join($self.data('opts').baseDelimiter));
},
killEvent : function (e) {
e.cancelBubble = true;
e.returnValue = false;
e.stopPropagation();
e.preventDefault();
},
keyInArray : function (e, ary) {
return $.inArray(e.which, ary) !== -1;
},
applyDelimiter : function (e) {
var $self = $(this);
publicMethods.pushTag.call($self,$(this).val());
e.preventDefault();
},
prefill : function (pta) {
var $self = $(this);
$.each(pta, function (key, val) {
publicMethods.pushTag.call($self, val, true);
});
},
pushAllTags : function (e, tag) {
var $self = $(this), opts = $self.data('opts'), tlis = $self.data("tlis");
if (opts.AjaxPushAllTags) {
if (e.type !== 'tm:pushed' || $.inArray(tag, opts.prefilled) === -1) {
$.post(opts.AjaxPush, $.extend({ tags: tlis.join(opts.baseDelimiter) }, opts.AjaxPushParameters));
}
}
},
spliceTag : function (tagId) {
var $self = this, tlis = $self.data("tlis"), tlid = $self.data("tlid"), idx = $.inArray(tagId, tlid),
tagBeingRemoved;
// console.log("TagIdToRemove: " + tagId);
// console.log("position: " + idx);
if (-1 !== idx) {
tagBeingRemoved = tlis[idx];
$self.trigger('tm:splicing', [tagBeingRemoved, tagId]);
$("#" + $self.data("tm_rndid") + "_" + tagId).remove();
tlis.splice(idx, 1);
tlid.splice(idx, 1);
privateMethods.refreshHiddenTagList.call($self);
$self.trigger('tm:spliced', [tagBeingRemoved, tagId]);
if (tlis.length == 0) {
$self.trigger('tm:empty', null);
}
// console.log(tlis);
}
privateMethods.showOrHide.call($self);
//if (tagManagerOptions.maxTags > 0 && tlis.length < tagManagerOptions.maxTags) {
// obj.show();
//}
},
init : function (options) {
var opts = $.extend({}, defaults, options), delimiters, keyNums;
opts.hiddenTagListName = (opts.hiddenTagListName === null)
? 'hidden-' + this.attr('name')
: opts.hiddenTagListName;
delimiters = opts.delimeters || opts.delimiters; // 'delimeter' is deprecated
keyNums = [9, 13, 17, 18, 19, 37, 38, 39, 40]; // delimiter values to be handled as key codes
opts.delimiterChars = [];
opts.delimiterKeys = [];
$.each(delimiters, function (i, v) {
if ($.inArray(v, keyNums) !== -1) {
opts.delimiterKeys.push(v);
} else {
opts.delimiterChars.push(v);
}
});
opts.baseDelimiter = String.fromCharCode(opts.delimiterChars[0] || 44);
opts.tagBaseClass = 'tm-tag';
opts.inputBaseClass = 'tm-input';
if (!$.isFunction(opts.validator)) { opts.validator = null; }
this.each(function() {
var $self = $(this), hiddenObj ='', rndid ='', albet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// prevent double-initialization of TagManager
if ($self.data('tagManager')) { return false; }
$self.data('tagManager', true);
for (var i = 0; i < 5; i++) {
rndid += albet.charAt(Math.floor(Math.random() * albet.length));
}
$self.data("tm_rndid", rndid);
// store instance-specific data in the DOM object
$self.data('opts',opts)
.data('tlis', []) //list of string tags
.data('tlid', []); //list of ID of the string tags
if (opts.output === null) {
hiddenObj = $('<input/>', {
type: 'hidden',
name: opts.hiddenTagListName
});
$self.after(hiddenObj);
$self.data("lhiddenTagList", hiddenObj);
} else {
$self.data("lhiddenTagList", $(opts.output));
}
if (opts.AjaxPushAllTags) {
$self.on('tm:spliced', privateMethods.pushAllTags);
$self.on('tm:popped', privateMethods.pushAllTags);
$self.on('tm:pushed', privateMethods.pushAllTags);
}
// hide popovers on focus and keypress events
$self.on('focus keypress', function(e) {
if ($(this).popover) { $(this).popover('hide'); }
});
// handle ESC (keyup used for browser compatibility)
if (opts.isClearInputOnEsc) {
$self.on('keyup', function(e) {
if (e.which === 27) {
// console.log('esc detected');
$(this).val('');
privateMethods.killEvent(e);
}
});
}
$self.on('keypress', function(e) {
// push ASCII-based delimiters
if (privateMethods.keyInArray(e, opts.delimiterChars)) {
privateMethods.applyDelimiter.call($self, e);
}
});
$self.on('keydown', function(e) {
// disable ENTER
if (e.which === 13) {
if (opts.preventSubmitOnEnter) {
privateMethods.killEvent(e);
}
}
// push key-based delimiters (includes <enter> by default)
if (privateMethods.keyInArray(e, opts.delimiterKeys)) {
privateMethods.applyDelimiter.call($self, e);
}
});
// BACKSPACE (keydown used for browser compatibility)
if (opts.deleteTagsOnBackspace) {
$self.on('keydown', function(e) {
if (privateMethods.keyInArray(e, opts.backspace)) {
// console.log("backspace detected");
if ($(this).val().length <= 0) {
publicMethods.popTag.call($self);
privateMethods.killEvent(e);
}
}
});
}
// on tag pop fill back the tag's content to the input field
if (opts.fillInputOnTagRemove) {
$self.on('tm:popped', function(e, tag) {
$(this).val(tag);
});
}
$self.change(function(e) {
if (!/webkit/.test(navigator.userAgent.toLowerCase())) {
$self.focus();
} // why?
/* unimplemented mode to push tag on blur
else if (tagManagerOptions.pushTagOnBlur) {
console.log('change: pushTagOnBlur ' + tag);
pushTag($(this).val());
} */
privateMethods.killEvent(e);
});
if (opts.prefilled !== null) {
if (typeof (opts.prefilled) === "object") {
privateMethods.prefill.call($self, opts.prefilled);
} else if (typeof (opts.prefilled) === "string") {
privateMethods.prefill.call($self, opts.prefilled.split(opts.baseDelimiter));
} else if (typeof (opts.prefilled) === "function") {
privateMethods.prefill.call($self, opts.prefilled());
}
} else if (opts.output !== null) {
if ($(opts.output) && $(opts.output).val()) { var existing_tags = $(opts.output); }
privateMethods.prefill.call($self,$(opts.output).val().split(opts.baseDelimiter));
}
});
return this;
}
};
$.fn.tagsManager = function(method) {
var $self = $(this);
if (!(0 in this)) { return this; }
if ( publicMethods[method] ) {
return publicMethods[method].apply( $self, Array.prototype.slice.call(arguments, 1) );
} else if ( typeof method === 'object' || ! method ) {
return privateMethods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist.' );
return false;
}
};
}(jQuery));