Skip to content

Commit 745ed62

Browse files
committed
Update jquery.taghandler.js
if I type "postgressql" & in my avaliable list I have "PostgresSQL" (or type "java" vs "Java"), I want the autocomplete to add the latter. I also added a foolproof behavior (that I got) where "PostgresSQL" is actually "PostgresSQL " int the available list (with a space) another update : on top of the "ignorecase" feature, I added a feature like UpdateURL but "updateField" instead --> it is meant to automatically update an hidden field using the result of getSerializedTags()
1 parent 4ad6f00 commit 745ed62

File tree

1 file changed

+159
-79
lines changed

1 file changed

+159
-79
lines changed

js/jquery.taghandler.js

+159-79
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ http://www.famfamfam.com/lab/icons/silk/
2929
Loader image created at:
3030
Preloaders.net
3131
http://preloaders.net/
32+
33+
Updates from Inducido
34+
if I type "postgressql" & in my avaliable list I have "PostgresSQL" (or type "java" vs "Java"),
35+
I want the autocomplete to add the latter.
36+
I also added a foolproof behavior (that I got) where "PostgresSQL" is actually "PostgresSQL "
37+
int the available list (with a space)
38+
39+
another update : on top of the "ignorecase" feature, I added a feature like UpdateURL but
40+
"updateField" instead --> it is meant to automatically update an hidden field using the result
41+
of getSerializedTags()
3242
3343
------------------------------------------------------------------------------
3444
Description
@@ -118,6 +128,7 @@ getURL URL for retrieving tag lists via ajax {}
118128
initLoad indicates if all tags should be loaded on init true
119129
updateData data field with additional info for updtateURL {}
120130
updateURL URL for saving tags via ajax ''
131+
updateField URL for saving tags ''
121132
122133
Callback options:
123134
-----------------
@@ -186,6 +197,25 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
186197
187198
*/
188199

200+
201+
if(String.prototype.alltrim==null)
202+
String.prototype.alltrim = function()
203+
{
204+
//return this.replace(/(^\s*)|(\s*$)/g,'');
205+
//bizzarement \s ne remplacais pas les \n en debut de chain alors que
206+
// \s veut dire espace et \r ou \n en regexp-->balec, je fait bourrin
207+
//je viens de comprendre: chorme a un trim natif
208+
return this.replace(/(^[\n\s\r]*)|([\n\s\r]*$)/g,'');
209+
}
210+
211+
if(String.prototype.equalsIgnoreCase==null)
212+
String.prototype.equalsIgnoreCase=myEqualsIgnoreCase;
213+
214+
function myEqualsIgnoreCase(arg)
215+
{
216+
return (new String(this.toLowerCase())==(new String(arg)).toLowerCase());
217+
}
218+
189219
(function ($) {
190220

191221
// some helper methods
@@ -262,6 +292,88 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
262292
$("<div />").attr({ id: tagContainer.id + "_loader", title: "Saving Tags" }).addClass("tagLoader").appendTo(tagContainerObject.parent());
263293
}
264294

295+
// adds autocomplete functionality for the tag names
296+
if (opts.autocomplete && typeof($.fn.autocomplete) == 'function' && opts.initLoad) {
297+
$(inputField).autocomplete({
298+
source: tags.availableTags,
299+
select: function (event, ui) {
300+
var $el = $(this);
301+
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
302+
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
303+
alert('Maximum tags allowed: ' + opts.maxTags);
304+
}
305+
else {
306+
var newTag = $.trim(ui.item.value);
307+
var rc = 1;
308+
if (typeof(opts.onAdd) == "function") {
309+
rc = opts.onAdd.call(this, newTag);
310+
}
311+
if (rc || typeof(rc) == "undefined") {
312+
tags = addTag(this, newTag, tags, opts.sortTags);
313+
if (opts.updateURL !== '' && opts.autoUpdate) {
314+
saveTags(tags, opts, tagContainer.id);
315+
}
316+
if (opts.updateField !== '') {
317+
saveTagsToField(tags, opts, tagContainer.id);
318+
}
319+
$(inputField).autocomplete("option", "source", tags.availableTags);
320+
if (typeof(opts.afterAdd) == "function") {
321+
opts.afterAdd.call(this, newTag);
322+
}
323+
}
324+
}
325+
$el.focus();
326+
}
327+
$el.val("");
328+
return false;
329+
},
330+
minLength: opts.minChars
331+
});
332+
}
333+
// Make an AJAX request to get the list of tags based on typed data
334+
else if (opts.autocomplete && typeof($.fn.autocomplete) == 'function') {
335+
$(inputField).autocomplete({
336+
source: function (request, response) {
337+
opts.getData[opts.queryname] = request.term;
338+
var lastXhr = $.getJSON(opts.getURL, opts.getData, function (data, status, xhr) {
339+
response(data.availableTags);
340+
});
341+
},
342+
select: function (event, ui) {
343+
var $el = $(this);
344+
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
345+
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
346+
alert('Maximum tags allowed: ' + opts.maxTags);
347+
}
348+
else {
349+
var newTag = $.trim(ui.item.value);
350+
var rc = 1;
351+
if (typeof(opts.onAdd) == "function") {
352+
opts.onAdd.call(this, newTag);
353+
}
354+
if (rc || typeof(rc) == "undefined") {
355+
tags = addTag(this, $.trim(ui.item.value), tags, opts.sortTags);
356+
if (opts.updateURL !== '' && opts.autoUpdate) {
357+
saveTags(tags, opts, tagContainer.id);
358+
}
359+
if (opts.updateField !== '') {
360+
saveTagsToField(tags, opts, tagContainer.id);
361+
}
362+
if (typeof(opts.afterAdd) == "function") {
363+
opts.afterAdd.call(this, newTag);
364+
}
365+
}
366+
}
367+
$el.focus();
368+
}
369+
$el.val('');
370+
return false;
371+
},
372+
minLength: opts.minChars
373+
});
374+
375+
}
376+
265377
// initializes the tag lists
266378
// tag lists will be pulled from a URL
267379
if (opts.getURL !== '' && opts.initLoad) {
@@ -349,6 +461,10 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
349461
if (opts.updateURL !== '' && opts.autoUpdate) {
350462
saveTags(tags, opts, tagContainer.id);
351463
}
464+
if (opts.updateField !== '') {
465+
saveTagsToField(tags, opts, tagContainer.id);
466+
}
467+
352468
}
353469

354470
if (typeof(opts.afterDelete) == "function") {
@@ -392,6 +508,9 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
392508
if (opts.updateURL !== '' && opts.autoUpdate) {
393509
saveTags(tags, opts, tagContainer.id);
394510
}
511+
if (opts.updateField !== '') {
512+
saveTagsToField(tags, opts, tagContainer.id);
513+
}
395514
if (opts.autocomplete && typeof($.fn.autocomplete) == 'function' && opts.initload) {
396515
$(inputField).autocomplete("option", "source", tags.availableTags);
397516
}
@@ -419,6 +538,9 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
419538
if (opts.updateURL !== '' && opts.autoUpdate) {
420539
saveTags(tags, opts, tagContainer.id);
421540
}
541+
if (opts.updateField !== '') {
542+
saveTagsToField(tags, opts, tagContainer.id);
543+
}
422544
if (typeof(opts.afterDelete) == "function") {
423545
opts.afterDelete.call(this, $.trim(deleted_tag));
424546
}
@@ -429,81 +551,6 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
429551
}
430552
});
431553

432-
// adds autocomplete functionality for the tag names
433-
if (opts.autocomplete && typeof($.fn.autocomplete) == 'function' && opts.initLoad) {
434-
$(inputField).autocomplete({
435-
source: tags.availableTags,
436-
select: function (event, ui) {
437-
var $el = $(this);
438-
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
439-
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
440-
alert('Maximum tags allowed: ' + opts.maxTags);
441-
}
442-
else {
443-
var newTag = $.trim(ui.item.value);
444-
var rc = 1;
445-
if (typeof(opts.onAdd) == "function") {
446-
rc = opts.onAdd.call(this, newTag);
447-
}
448-
if (rc || typeof(rc) == "undefined") {
449-
tags = addTag(this, newTag, tags, opts.sortTags);
450-
if (opts.updateURL !== '' && opts.autoUpdate) {
451-
saveTags(tags, opts, tagContainer.id);
452-
}
453-
$(inputField).autocomplete("option", "source", tags.availableTags);
454-
if (typeof(opts.afterAdd) == "function") {
455-
opts.afterAdd.call(this, newTag);
456-
}
457-
}
458-
}
459-
$el.focus();
460-
}
461-
$el.val("");
462-
return false;
463-
},
464-
minLength: opts.minChars
465-
});
466-
}
467-
// Make an AJAX request to get the list of tags based on typed data
468-
else if (opts.autocomplete && typeof($.fn.autocomplete) == 'function') {
469-
$(inputField).autocomplete({
470-
source: function (request, response) {
471-
opts.getData[opts.queryname] = request.term;
472-
var lastXhr = $.getJSON(opts.getURL, opts.getData, function (data, status, xhr) {
473-
response(data.availableTags);
474-
});
475-
},
476-
select: function (event, ui) {
477-
var $el = $(this);
478-
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
479-
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
480-
alert('Maximum tags allowed: ' + opts.maxTags);
481-
}
482-
else {
483-
var newTag = $.trim(ui.item.value);
484-
var rc = 1;
485-
if (typeof(opts.onAdd) == "function") {
486-
opts.onAdd.call(this, newTag);
487-
}
488-
if (rc || typeof(rc) == "undefined") {
489-
tags = addTag(this, $.trim(ui.item.value), tags, opts.sortTags);
490-
if (opts.updateURL !== '' && opts.autoUpdate) {
491-
saveTags(tags, opts, tagContainer.id);
492-
}
493-
if (typeof(opts.afterAdd) == "function") {
494-
opts.afterAdd.call(this, newTag);
495-
}
496-
}
497-
}
498-
$el.focus();
499-
}
500-
$el.val('');
501-
return false;
502-
},
503-
minLength: opts.minChars
504-
});
505-
}
506-
507554
// sets the input field to show the autocomplete list on focus
508555
// when there is no value
509556
$(inputField).focus(function () {
@@ -555,15 +602,22 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
555602
queryname: 'q',
556603
sortTags: true,
557604
updateData: {},
558-
updateURL: ''
605+
updateURL: '',
606+
updateField: ''
559607
};
560608

561609
// checks to to see if a tag is already found in a list of tags
610+
// now return the correected value
562611
function checkTag(value, tags) {
563612
var check = false;
564613
jQuery.each(tags, function (i, e) {
565-
if (e === value) {
566-
check = true;
614+
615+
//alltrim to handle when there's space in the tag list by mistake
616+
if (e && e.alltrim().equalsIgnoreCase(value)) {
617+
// if ((new String(e.toString())).toLowercase() === (new String(value.toString())).toString().toLowercase()) {
618+
// if (e === value) {
619+
// check = true;
620+
check = e.alltrim();
567621
return false;
568622
}
569623
});
@@ -574,7 +628,18 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
574628
// removes a tag from a tag list
575629
function removeTagFromList(value, tags) {
576630
jQuery.each(tags, function (i, e) {
577-
if (e === value) {
631+
632+
//if (e === value) {
633+
//added the ignorecase fonctionality
634+
//"2G".toLowercase()
635+
//TypeError: Object 2G has no method 'toLowercase' -- d'ou le new String
636+
// var se=(new String(e.toString())).toLowercase();
637+
// var sv=(new String(value.toString())).toLowercase();
638+
//alltrim to handle when there's space in the tag list by mistake
639+
if (e && e.alltrim().equalsIgnoreCase(value)) {
640+
// if ((new String(e.toString())).toLowercase() === (new String(value.toString())).toString().toLowercase()) {
641+
// if (se.toLowercase() === sv.toLowercase()) {
642+
// if (se === sv) {
578643
tags.splice(i, 1);
579644
}
580645
});
@@ -584,6 +649,10 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
584649

585650
// adds a tag to the tag box and the assignedTags list
586651
function addTag(tagField, value, tags, sort) {
652+
newvalue=checkTag($.trim(value), tags.availableTags);
653+
if(newvalue)
654+
value=newvalue;
655+
587656
tags.assignedTags.push(value);
588657
tags.availableTags = removeTagFromList(value, tags.availableTags);
589658
$("<li />").addClass("tagItem").text(value).insertBefore($(tagField).parent());
@@ -618,6 +687,17 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
618687
return tags;
619688
}
620689

690+
// saves the tags to a field
691+
function saveTagsToField(tags, opts, tcID) {
692+
var sendData = {
693+
tags: tags.assignedTags
694+
};
695+
test= $('#'+tcID).tagHandler("getSerializedTags");
696+
// test=getSerializedTags();
697+
$(opts.updateField).val(test);
698+
699+
}
700+
621701
// saves the tags to the server via ajax
622702
function saveTags(tags, opts, tcID) {
623703
var sendData = {

0 commit comments

Comments
 (0)