Skip to content

Commit

Permalink
highlight the existing tag when attempting to create another one
Browse files Browse the repository at this point in the history
  • Loading branch information
aehlke committed Nov 25, 2012
1 parent 98d78b2 commit 4017f7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ First, load [jQuery](http://jquery.com/) (v1.4 or greater), [jQuery UI](http://j
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/tag-it.js" type="text/javascript" charset="utf-8"></script>

If you're using a custom jQuery UI build, it must contain the Core, Widget, Position, and Autocomplete components. The Effects Core with "Blind" Effect components are optional, but used if available.
If you're using a custom jQuery UI build, it must contain the Core, Widget, Position, and Autocomplete components. The Effects Core with "Blind" and "Highlight" Effect components are optional, but used if available.

The plugin requires a jQuery UI theme to be present, as well as its own included base CSS file ([jquery.tagit.css](http://github.com/aehlke/tag-it/raw/master/css/jquery.tagit.css)). Here we use the Flick theme as an example:

Expand Down Expand Up @@ -232,6 +232,8 @@ duplicate tags during the widget initialization.

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

By default it will visually highlight the existing tag, unless you return *false* in your callback.

### onTagClicked (function, Callback)

Can be used to add custom behaviour when the tag is clicked.
Expand Down
20 changes: 16 additions & 4 deletions js/tag-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@
},

_lastTag: function() {
return this.tagList.children('.tagit-choice:last:not(.removed)');
return this.tagList.find('.tagit-choice:last:not(.removed)');
},

_tags: function() {
return this.tagList.children('.tagit-choice:not(.removed)');
return this.tagList.find('.tagit-choice:not(.removed)');
},

assignedTags: function() {
Expand Down Expand Up @@ -375,6 +375,10 @@
return $.trim(str.toLowerCase());
},

_effectExists: function(name) {
return Boolean($.effects && ($.effects[name] || ($.effects.effect && $.effects.effect[name])));
},

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

Expand All @@ -385,7 +389,15 @@
}

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

Expand Down Expand Up @@ -466,7 +478,7 @@

if (animate) {
tag.addClass('removed'); // Excludes this tag from _tags.
var hide_args = ($.effects && ($.effects.blind || $.effects.effect.blind)) ? ['blind', {direction: 'horizontal'}, 'fast'] : ['fast'];
var hide_args = this._effectExists('blind') ? ['blind', {direction: 'horizontal'}, 'fast'] : ['fast'];

hide_args.push(function() {
tag.remove();
Expand Down
15 changes: 8 additions & 7 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 4017f7e

Please sign in to comment.