Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update jquery.taghandler.js #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 159 additions & 79 deletions js/jquery.taghandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ http://www.famfamfam.com/lab/icons/silk/
Loader image created at:
Preloaders.net
http://preloaders.net/

Updates from Inducido
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()

------------------------------------------------------------------------------
Description
Expand Down Expand Up @@ -118,6 +128,7 @@ getURL URL for retrieving tag lists via ajax {}
initLoad indicates if all tags should be loaded on init true
updateData data field with additional info for updtateURL {}
updateURL URL for saving tags via ajax ''
updateField URL for saving tags ''

Callback options:
-----------------
Expand Down Expand Up @@ -186,6 +197,25 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.

*/


if(String.prototype.alltrim==null)
String.prototype.alltrim = function()
{
//return this.replace(/(^\s*)|(\s*$)/g,'');
//bizzarement \s ne remplacais pas les \n en debut de chain alors que
// \s veut dire espace et \r ou \n en regexp-->balec, je fait bourrin
//je viens de comprendre: chorme a un trim natif
return this.replace(/(^[\n\s\r]*)|([\n\s\r]*$)/g,'');
}

if(String.prototype.equalsIgnoreCase==null)
String.prototype.equalsIgnoreCase=myEqualsIgnoreCase;

function myEqualsIgnoreCase(arg)
{
return (new String(this.toLowerCase())==(new String(arg)).toLowerCase());
}

(function ($) {

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

// adds autocomplete functionality for the tag names
if (opts.autocomplete && typeof($.fn.autocomplete) == 'function' && opts.initLoad) {
$(inputField).autocomplete({
source: tags.availableTags,
select: function (event, ui) {
var $el = $(this);
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
alert('Maximum tags allowed: ' + opts.maxTags);
}
else {
var newTag = $.trim(ui.item.value);
var rc = 1;
if (typeof(opts.onAdd) == "function") {
rc = opts.onAdd.call(this, newTag);
}
if (rc || typeof(rc) == "undefined") {
tags = addTag(this, newTag, tags, opts.sortTags);
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
if (opts.updateField !== '') {
saveTagsToField(tags, opts, tagContainer.id);
}
$(inputField).autocomplete("option", "source", tags.availableTags);
if (typeof(opts.afterAdd) == "function") {
opts.afterAdd.call(this, newTag);
}
}
}
$el.focus();
}
$el.val("");
return false;
},
minLength: opts.minChars
});
}
// Make an AJAX request to get the list of tags based on typed data
else if (opts.autocomplete && typeof($.fn.autocomplete) == 'function') {
$(inputField).autocomplete({
source: function (request, response) {
opts.getData[opts.queryname] = request.term;
var lastXhr = $.getJSON(opts.getURL, opts.getData, function (data, status, xhr) {
response(data.availableTags);
});
},
select: function (event, ui) {
var $el = $(this);
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
alert('Maximum tags allowed: ' + opts.maxTags);
}
else {
var newTag = $.trim(ui.item.value);
var rc = 1;
if (typeof(opts.onAdd) == "function") {
opts.onAdd.call(this, newTag);
}
if (rc || typeof(rc) == "undefined") {
tags = addTag(this, $.trim(ui.item.value), tags, opts.sortTags);
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
if (opts.updateField !== '') {
saveTagsToField(tags, opts, tagContainer.id);
}
if (typeof(opts.afterAdd) == "function") {
opts.afterAdd.call(this, newTag);
}
}
}
$el.focus();
}
$el.val('');
return false;
},
minLength: opts.minChars
});

}

// initializes the tag lists
// tag lists will be pulled from a URL
if (opts.getURL !== '' && opts.initLoad) {
Expand Down Expand Up @@ -349,6 +461,10 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
if (opts.updateField !== '') {
saveTagsToField(tags, opts, tagContainer.id);
}

}

if (typeof(opts.afterDelete) == "function") {
Expand Down Expand Up @@ -392,6 +508,9 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
if (opts.updateField !== '') {
saveTagsToField(tags, opts, tagContainer.id);
}
if (opts.autocomplete && typeof($.fn.autocomplete) == 'function' && opts.initload) {
$(inputField).autocomplete("option", "source", tags.availableTags);
}
Expand Down Expand Up @@ -419,6 +538,9 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
if (opts.updateField !== '') {
saveTagsToField(tags, opts, tagContainer.id);
}
if (typeof(opts.afterDelete) == "function") {
opts.afterDelete.call(this, $.trim(deleted_tag));
}
Expand All @@ -429,81 +551,6 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
}
});

// adds autocomplete functionality for the tag names
if (opts.autocomplete && typeof($.fn.autocomplete) == 'function' && opts.initLoad) {
$(inputField).autocomplete({
source: tags.availableTags,
select: function (event, ui) {
var $el = $(this);
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
alert('Maximum tags allowed: ' + opts.maxTags);
}
else {
var newTag = $.trim(ui.item.value);
var rc = 1;
if (typeof(opts.onAdd) == "function") {
rc = opts.onAdd.call(this, newTag);
}
if (rc || typeof(rc) == "undefined") {
tags = addTag(this, newTag, tags, opts.sortTags);
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
$(inputField).autocomplete("option", "source", tags.availableTags);
if (typeof(opts.afterAdd) == "function") {
opts.afterAdd.call(this, newTag);
}
}
}
$el.focus();
}
$el.val("");
return false;
},
minLength: opts.minChars
});
}
// Make an AJAX request to get the list of tags based on typed data
else if (opts.autocomplete && typeof($.fn.autocomplete) == 'function') {
$(inputField).autocomplete({
source: function (request, response) {
opts.getData[opts.queryname] = request.term;
var lastXhr = $.getJSON(opts.getURL, opts.getData, function (data, status, xhr) {
response(data.availableTags);
});
},
select: function (event, ui) {
var $el = $(this);
if (!checkTag($.trim(ui.item.value), tags.assignedTags)) {
if (opts.maxTags > 0 && tags.assignedTags.length >= opts.maxTags) {
alert('Maximum tags allowed: ' + opts.maxTags);
}
else {
var newTag = $.trim(ui.item.value);
var rc = 1;
if (typeof(opts.onAdd) == "function") {
opts.onAdd.call(this, newTag);
}
if (rc || typeof(rc) == "undefined") {
tags = addTag(this, $.trim(ui.item.value), tags, opts.sortTags);
if (opts.updateURL !== '' && opts.autoUpdate) {
saveTags(tags, opts, tagContainer.id);
}
if (typeof(opts.afterAdd) == "function") {
opts.afterAdd.call(this, newTag);
}
}
}
$el.focus();
}
$el.val('');
return false;
},
minLength: opts.minChars
});
}

// sets the input field to show the autocomplete list on focus
// when there is no value
$(inputField).focus(function () {
Expand Down Expand Up @@ -555,15 +602,22 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
queryname: 'q',
sortTags: true,
updateData: {},
updateURL: ''
updateURL: '',
updateField: ''
};

// checks to to see if a tag is already found in a list of tags
// now return the correected value
function checkTag(value, tags) {
var check = false;
jQuery.each(tags, function (i, e) {
if (e === value) {
check = true;

//alltrim to handle when there's space in the tag list by mistake
if (e && e.alltrim().equalsIgnoreCase(value)) {
// if ((new String(e.toString())).toLowercase() === (new String(value.toString())).toString().toLowercase()) {
// if (e === value) {
// check = true;
check = e.alltrim();
return false;
}
});
Expand All @@ -574,7 +628,18 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
// removes a tag from a tag list
function removeTagFromList(value, tags) {
jQuery.each(tags, function (i, e) {
if (e === value) {

//if (e === value) {
//added the ignorecase fonctionality
//"2G".toLowercase()
//TypeError: Object 2G has no method 'toLowercase' -- d'ou le new String
// var se=(new String(e.toString())).toLowercase();
// var sv=(new String(value.toString())).toLowercase();
//alltrim to handle when there's space in the tag list by mistake
if (e && e.alltrim().equalsIgnoreCase(value)) {
// if ((new String(e.toString())).toLowercase() === (new String(value.toString())).toString().toLowercase()) {
// if (se.toLowercase() === sv.toLowercase()) {
// if (se === sv) {
tags.splice(i, 1);
}
});
Expand All @@ -584,6 +649,10 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.

// adds a tag to the tag box and the assignedTags list
function addTag(tagField, value, tags, sort) {
newvalue=checkTag($.trim(value), tags.availableTags);
if(newvalue)
value=newvalue;

tags.assignedTags.push(value);
tags.availableTags = removeTagFromList(value, tags.availableTags);
$("<li />").addClass("tagItem").text(value).insertBefore($(tagField).parent());
Expand Down Expand Up @@ -618,6 +687,17 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
return tags;
}

// saves the tags to a field
function saveTagsToField(tags, opts, tcID) {
var sendData = {
tags: tags.assignedTags
};
test= $('#'+tcID).tagHandler("getSerializedTags");
// test=getSerializedTags();
$(opts.updateField).val(test);

}

// saves the tags to the server via ajax
function saveTags(tags, opts, tcID) {
var sendData = {
Expand Down