Skip to content
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
75 changes: 35 additions & 40 deletions js/tag-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
onTagAdded : null,
onTagRemoved: null,
// `autocomplete.source` is the replacement for tagSource.
tagSource: null
tagSource: null,
items : null
// Do not use the above deprecated options.
},

Expand Down Expand Up @@ -190,34 +191,21 @@
that.tagInput.focus();
}
});



// Single field support.
var addedExistingFromSingleFieldNode = false;
if (this.options.singleField) {
if (this.options.singleFieldNode) {
// Add existing tags from the input field.
var node = $(this.options.singleFieldNode);
var tags = node.val().split(this.options.singleFieldDelimiter);
node.val('');
$.each(tags, function(index, tag) {
that.createTag(tag, null, true);
addedExistingFromSingleFieldNode = true;
});
} else {
// Create our single field input after our list.
if(!this.options.singleFieldNode){
// Create our single field input after our list.
this.options.singleFieldNode = $('<input type="hidden" style="display:none;" value="" name="' + this.options.fieldName + '" />');
this.tagList.after(this.options.singleFieldNode);
}
}
}

// Add existing tags from the list, if any.
if (!addedExistingFromSingleFieldNode) {
this.tagList.children('li').each(function() {
if (!$(this).hasClass('tagit-new')) {
that.createTag($(this).text(), $(this).attr('class'), true);
$(this).remove();
}
});

if(this.options.items){
for (index = 0, len = this.options.items.length; index < len; ++index) {
this.createTag(this.options.items[index],null,true);
}
}

// Events.
Expand Down Expand Up @@ -268,22 +256,28 @@
// Autocomplete will create its own tag from a selection and close automatically.
if (!(that.options.autocomplete.autoFocus && that.tagInput.data('autocomplete-open'))) {
that.tagInput.autocomplete('close');
that.createTag(that._cleanedInput());
that.createTag({
label:that._cleanedInput(),
value:that._cleanedInput()
});
}
}
}).blur(function(e){
// Create a tag when the element loses focus.
// Clean tag when the element loses focus.
// If autocomplete is enabled and suggestion was clicked, don't add it.
if (!that.tagInput.data('autocomplete-open')) {
that.createTag(that._cleanedInput());
that.createTag({
label:that._cleanedInput(),
value:that._cleanedInput()
});
}
});

// Autocomplete.
if (this.options.availableTags || this.options.tagSource || this.options.autocomplete.source) {
var autocompleteOptions = {
select: function(event, ui) {
that.createTag(ui.item.value);
that.createTag(ui.item);
// Preventing the tag input to be updated with the chosen value.
return false;
}
Expand Down Expand Up @@ -436,21 +430,23 @@
return Boolean($.effects && ($.effects[name] || ($.effects.effect && $.effects.effect[name])));
},

createTag: function(value, additionalClass, duringInitialization) {
createTag: function(item, additionalClass, duringInitialization) {
var that = this;

value = $.trim(value);
if(typeof item === "string"){
item = $.trim(item);
}

if(this.options.preprocessTag) {
value = this.options.preprocessTag(value);
item = this.options.preprocessTag(item);
}

if (value === '') {
if (!item || (!item.label && !item.value)) {
return false;
}

if (!this.options.allowDuplicates && !this._isNew(value)) {
var existingTag = this._findTagByLabel(value);
if (!this.options.allowDuplicates && !this._isNew(item.label || item.value || item)) {
var existingTag = this._findTagByLabel(item.label || item.value || item);
if (this._trigger('onTagExists', null, {
existingTag: existingTag,
duringInitialization: duringInitialization
Expand All @@ -467,10 +463,10 @@
return false;
}

var label = $(this.options.onTagClicked ? '<a class="tagit-label"></a>' : '<span class="tagit-label"></span>').text(value);
var label = $(this.options.onTagClicked ? '<a class="tagit-label"></a>' : '<span class="tagit-label"></span>').text(item.label || item.value || item);

// Create tag.
var tag = $('<li></li>')
var tag = $('<li data-value="' + (item.value || item.label || item) + '"></li>')
.addClass('tagit-choice ui-widget-content ui-state-default ui-corner-all')
.addClass(additionalClass)
.append(label);
Expand All @@ -494,7 +490,7 @@

// Unless options.singleField is set, each tag has a hidden input field inline.
if (!this.options.singleField) {
var escapedValue = label.html();
var escapedValue = item.value || item.label || item;
tag.append('<input type="hidden" value="' + escapedValue + '" name="' + this.options.fieldName + '" class="tagit-hidden-field" />');
}

Expand All @@ -508,7 +504,7 @@

if (this.options.singleField) {
var tags = this.assignedTags();
tags.push(value);
tags.push(item.value || item.label || item);
this._updateSingleTagsField(tags);
}

Expand Down Expand Up @@ -545,7 +541,7 @@

if (this.options.singleField) {
var tags = this.assignedTags();
var removedTagLabel = this.tagLabel(tag);
var removedTagLabel = tag.data('value');
tags = $.grep(tags, function(el){
return el != removedTagLabel;
});
Expand Down Expand Up @@ -588,4 +584,3 @@

});
})(jQuery);