forked from max-favilli/tagmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-tagmanager.js
457 lines (391 loc) · 15.3 KB
/
bootstrap-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
/*
*/
"use strict";
(function (jQuery) {
var SourceArray = ["Lombardia", "Abruzzo", "Toscana", "Liguria"];
var SourceAjaxArray = new Array();
var delimeters = new Array();
var backspace = new Array();
var inputObj = null;
var tagList = new Array();
var tagLiID = new Array();
var lastTagId = 0;
var hiddenTagList = null;
var tagManagerOptions = null;
var queuedTag = "";
if (typeof console === "undefined" || typeof console.log === "undefined") {
console = {};
console.log = function () { };
}
inputObj = this;
jQuery.fn.trimTag = function (tag) {
var txt = jQuery.trim(tag);
var l = txt.length;
var t = 0;
// console.log(
// "tag to trim : " + txt
// );
//remove from end
for (var i = l - 1; i >= 0; i--) {
// console.log(
// "char to evaluate : " + txt[i] + " (" + txt.charCodeAt(i) + ")"
// );
if (-1 != jQuery.inArray(txt.charCodeAt(i), delimeters)) {
// console.log(
// "char to remove from end: " + txt[i]
// );
t++;
} else
break;
}
txt = txt.substring(0, l - t);
l = txt.length;
t = 0;
//remove from head
for (var i = 0; i < l; i++) {
// console.log(
// "char to evaluate : " + txt[i] + " (" + txt.charCodeAt(i) + ")"
// );
if (-1 != jQuery.inArray(txt.charCodeAt(i), delimeters)) {
// console.log(
// "char to remove from head: " + txt[i]
// );
t++;
} else
break;
}
txt = txt.substring(t, l);
// console.log(
// "trimmed to : " + txt
// );
return txt;
};
jQuery.fn.setupTypeahead = function (tag) {
var obj = jQuery(this);
if(!obj.typeahead)
return;
if (tagManagerOptions.typeaheadSource != null) {
obj.typeahead();
obj.data('active', true);
obj.data('typeahead').source = tagManagerOptions.typeaheadSource;
obj.data('active', false);
} else if (tagManagerOptions.typeaheadAjaxSource != null) {
obj.typeahead();
jQuery.getJSON(tagManagerOptions.typeaheadAjaxSource, function (data) {
if (data != undefined && data.tags != undefined) {
SourceAjaxArray.length = 0;
jQuery.each(data.tags, function (key, val) {
var a = 1;
//jQuery('#addexpenses select[name="who"]').append('<option value="' + val.who + '">' + val.name + '</option>');
SourceAjaxArray.push(val.tag);
//obj.attr("data-sources", SourceArray);
obj.data('active', true);
obj.data('typeahead').source = SourceAjaxArray;
obj.data('active', false);
//obj.attr("data-sources", SourceAjaxArray.join(", "));
});
}
});
}
};
jQuery.fn.refreshHiddenTagList = function (robj) {
var obj;
if (robj == null)
obj = jQuery(this);
else
obj = robj;
var tlis = obj.data("tlis");
var lhiddenTagList = obj.data("lhiddenTagList");
if(lhiddenTagList == undefined)
return;
jQuery(lhiddenTagList).val(tlis.join(","));
};
jQuery.fn.spliceTag = function (TagId,robj) {
var obj;
if (robj == null)
return;
else
obj = robj;
var tlis = obj.data("tlis");
var tlid = obj.data("tlid");
console.log(
"TagIdToRemove: " + TagId
);
var p = jQuery.inArray(TagId, tlid)
console.log(
"position: " + p
);
if (-1 != p) {
jQuery("#myTag_" + TagId).remove();
tlis.splice(p, 1);
tlid.splice(p, 1);
obj.refreshHiddenTagList(obj);
console.log(tlis);
}
if ( tagManagerOptions.maxTags > 0 && tlis.length < tagManagerOptions.maxTags ) {
obj.show();
}
};
jQuery.fn.popTag = function (robj) {
var obj;
if (robj == null)
obj = jQuery(this);
else
obj = robj;
var tlis = obj.data("tlis");
var tlid = obj.data("tlid");
if (tlid.length > 0) {
var TagId = tlid.pop();
tlis.pop();
console.log(
"TagIdToRemove: " + TagId
);
jQuery("#myTag_" + TagId).remove();
jQuery(obj).refreshHiddenTagList();
console.log(tlis);
}
};
jQuery.fn.pushTag = function (tag, robj) {
if (!tag || tag.length <= 0) {
return;
}
if (tagManagerOptions.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 (tagManagerOptions.validator !== undefined) {
if ( tagManagerOptions.validator(tag) !== true ) return;
}
var obj;
if (robj == null)
obj = jQuery(this);
else
obj = robj;
var tlis = obj.data("tlis");
var tlid = obj.data("tlid");
// dont accept new tags beyond the defined maximum
if ( tagManagerOptions.maxTags > 0 && tlis.length >= tagManagerOptions.maxTags ) return;
var alreadyInList = false;
var p = jQuery.inArray(tag, tlis);
if (-1 != p) {
console.log(
"tag:" + tag + " !!already in list!!"
);
alreadyInList = true;
}
if (alreadyInList) {
var pTagId = tlid[p];
jQuery("#myTag_" + pTagId).stop().animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100).animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100).animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100).animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100).animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100).animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100);
} else {
var TagId = lastTagId++;
tlis.push(tag);
tlid.push(TagId);
var html = '';
html += '<span class="myTag" id="myTag_' + TagId + '"><span>' + tag + ' </span><a href="#" class="myTagRemover" id="myRemover_' + TagId + '" TagIdToRemove="' + TagId + '" title="Removing tag">x</a></span>';
console.log(
"tagList: " + tlis
);
obj.before(html);
jQuery("#myRemover_" + TagId).on("click", obj, function (e) {
e.preventDefault();
var TagIdToRemove = parseInt(jQuery(this).attr("TagIdToRemove"));
e.data.spliceTag(TagIdToRemove,e.data);
// var TagIdToRemove = parseInt(jQuery(this).attr("TagIdToRemove"));
// jQuery(this).spliceTag(parseInt(jQuery(this).attr("TagIdToRemove")));
});
obj.refreshHiddenTagList(obj);
if ( tagManagerOptions.maxTags > 0 && tlis.length >= tagManagerOptions.maxTags ) {
obj.hide();
}
}
obj.val("");
};
jQuery.fn.tagsManager = function (options) {
tagManagerOptions = {
prefilled: null,
CapitalizeFirstLetter: true,
preventSubmitOnEnter: true,
typeahead: false,
typeaheadAjaxSource: null,
typeaheadSource: null,
delimeters: [44, 188, 13],
backspace: [8],
maxTags: 0 ,
inputTag: '<input type="text" name="TagList" class="tag" id="TagList" />'
};
jQuery.extend(tagManagerOptions, options);
return this.each(function() {
// use baseObj instead obj to keep it hidden
var baseObj = jQuery(this);
// use inputTag for our visible input
var obj = $(tagManagerOptions.inputTag);
// return if element exist
if (baseObj.parent().find("input[name='"+obj.attr('name')+"']").length > 0){
return;
}
//let's store some instance specific data directly into the DOM object
var tlis = new Array();
var tlid = new Array();
obj.data("tlis", tlis); //list of string tags
obj.data("tlid", tlid); //list of ID of the string tags
// hide the element
baseObj.hide();
baseObj.after(obj);
obj.data("lhiddenTagList", baseObj );
delimeters = tagManagerOptions.delimeters;
backspace = tagManagerOptions.backspace;
if (lastTagId > 0) {
while (lastTagId > 0) {
obj.popTag(obj);
lastTagId--;
}
}
if (tagManagerOptions.typeahead) {
obj.setupTypeahead();
//obj.typeahead({ source: SourceArray })
}
// disable submit on enter for this input field
obj.on("focus", function (e) {
if (jQuery(this).popover) {
jQuery(this).popover("hide");
//jQuery(this).popover = null;
}
});
obj.on("keypress", function (e) {
if (jQuery(this).popover) {
jQuery(this).popover("hide");
//jQuery(this).popover = null;
}
if (tagManagerOptions.preventSubmitOnEnter) {
if (e.which == 13) {
e.cancelBubble = true;
e.returnValue = false;
e.stopPropagation();
e.preventDefault();
//e.keyCode = 9;
}
}
console.log(
"keyup: " + e.keyCode
);
});
obj.on("keyup", obj, function (e) {
if(e.which == 9){
var ao = jQuery(".typeahead:visible");
if (ao[0] != undefined) {
console.log('pressed tab with typeaheadIsVisible is visible');
} else {
console.log('pressed tab with typeaheadIsVisible is NOT visible');
}
}
var p = jQuery.inArray(e.which, delimeters);
if (-1 != p) {
//user just entered a valid delimeter
var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
user_input = jQuery(this).trimTag(user_input);
e.data.pushTag(user_input,e.data);
console.log(
"pushTag: keyup "
);
}
// console.log(
// "keyup: " + e.which
// );
});
obj.on("keydown", obj, function (e) {
var p = jQuery.inArray(e.which, backspace);
if (-1 != p) {
//user just entered backspace or equivalent
var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
var i = user_input.length;
if (i <= 0) {
console.log(
"backspace detected"
);
e.data.popTag();
}
}
});
obj.change(function (e) {
e.cancelBubble = true;
e.returnValue = false;
e.stopPropagation();
e.preventDefault();
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_explorer = navigator.userAgent.indexOf('MSIE') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_safari = navigator.userAgent.indexOf("Safari") > -1;
if(!is_chrome && !is_safari )
jQuery(this).focus();
console.log('Handler for .change() called, value selected:' + obj.val());
var ao = jQuery(".typeahead:visible");
if (ao[0] != undefined){
console.log('change: typeaheadIsVisible is visible');
//when the user click with the mouse on the typeahead li element we get the change event fired twice, once when the input field loose focus and later with the input field value is replaced with li value
var user_input = jQuery(".typeahead .active").attr("data-value");
user_input = jQuery(this).trimTag(user_input);
if( queuedTag == obj.val() && queuedTag == user_input ){
queuedTag = "";
obj.val(queuedTag);
}else{
jQuery(this).pushTag(user_input);
queuedTag = user_input;
console.log('Handler for .change() called, typeahead value pushed:' + queuedTag);
}
} else {
console.log('change: typeaheadIsVisible is NOT visible');
var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
user_input = jQuery(this).trimTag(user_input);
jQuery(this).pushTag(user_input);
console.log(
"pushTag: change "
);
}
});
if (1 == 1 || !tagManagerOptions.typeahead) {
obj.on("blur", function (e) {
//lost focus
e.cancelBubble = true;
e.returnValue = false;
e.stopPropagation();
e.preventDefault();
var push = true;
if (tagManagerOptions.typeahead) {
var ao = jQuery(".typeahead:visible");
if (ao[0] != undefined) {
console.log('blur: typeaheadIsVisible is visible');
push = false;
} else {
console.log('blur: typeaheadIsVisible is NOT visible');
push = true;
}
}
if (push) {
console.log('lost focus');
var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
user_input = jQuery(this).trimTag(user_input);
jQuery(this).pushTag(user_input);
console.log(
"pushTag: blur"
);
}
});
}
if (tagManagerOptions.prefilled != null) {
if (typeof (tagManagerOptions.prefilled) == "object") {
var pta = tagManagerOptions.prefilled;
jQuery.each(pta, function (key, val) {
var a = 1;
obj.pushTag(val, obj);
});
} else if (typeof (tagManagerOptions.prefilled) == "string") {
var pta = tagManagerOptions.prefilled.split(',');
jQuery.each(pta, function (key, val) {
var a = 1;
obj.pushTag(val, obj);
});
}
}
});
}
})(jQuery);