Skip to content

Commit

Permalink
add onTagExists and fix events
Browse files Browse the repository at this point in the history
  • Loading branch information
aehlke committed Nov 25, 2012
1 parent 37d5535 commit 98d78b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ The function receives an empty event, and an object with a `tag` property.

Behaves the same as **beforeTagRemoved** except that it fires after the tag has been removed from the DOM.

### onTagExists (function, Callback)

Triggered when attempting to add a tag that has already been added in the widget. The callback receives an empty event,
and an object containing the properties `existingTag` and `duringInitialization`, since technically you could try to preload
duplicate tags during the widget initialization.

If the **allowDuplicates** option is enabled, this will not be triggered.

### onTagClicked (function, Callback)

Can be used to add custom behaviour when the tag is clicked.
Expand Down
3 changes: 3 additions & 0 deletions examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
},
onTagClicked: function(evt, ui) {
addEvent('onTagClicked: ' + eventTags.tagit('tagLabel', ui.tag));
},
onTagExists: function(evt, ui) {
addEvent('onTagExists: ' + eventTags.tagit('tagLabel', ui.existingTag));
}
});

Expand Down
21 changes: 15 additions & 6 deletions js/tag-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
if (target.hasClass('tagit-label')) {
var tag = target.closest('.tagit-choice');
if (!tag.hasClass('removed')) {
that._trigger('onTagClicked', e, tag);
that._trigger('onTagClicked', e, {tag: tag});
}
} else {
// Sets the focus() to the input field, if the user
Expand Down Expand Up @@ -352,16 +352,20 @@
this.tagInput.autocomplete('search', '');
},

_isNew: function(value) {
_existingTag: function(value) {
var that = this;
var isNew = true;
var tag = null;
this._tags().each(function(i) {
if (that._formatStr(value) == that._formatStr(that.tagLabel(this))) {
isNew = false;
tag = $(this);
return false;
}
});
return isNew;
return tag;
},

_isNew: function(value) {
return !this._existingTag(value);
},

_formatStr: function(str) {
Expand All @@ -376,7 +380,12 @@

value = $.trim(value);

if (!this.allowDuplicates && (!this._isNew(value) || value === '')) {
if (value === '') {
return false;
}

if (!this.allowDuplicates && !this._isNew(value)) {
this._trigger('onTagExists', null, {existingTag: this._existingTag(value), duringInitialization: duringInitialization});
return false;
}

Expand Down
18 changes: 9 additions & 9 deletions js/tag-it.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98d78b2

Please sign in to comment.