Skip to content
This repository
Browse code

Merge branch 'master' of github.com:xoxco/jQuery-Tags-Input

  • Loading branch information...
commit 2f564beaad462c45c685e1b15c063df135d50f70 2 parents 0b7d62a + b7d3392
Ben Brown benbrown authored

Showing 1 changed file with 20 additions and 24 deletions. Show diff stats Hide diff stats

  1. +20 24 jquery.tagsinput.js
44 jquery.tagsinput.js
@@ -20,9 +20,9 @@
20 20 var tags_callbacks = new Array();
21 21
22 22 $.fn.addTag = function(value,options) {
23   - var options = jQuery.extend({focus:false,callback:true},options);
  23 + options = jQuery.extend({focus:false,callback:true},options);
24 24 this.each(function() {
25   - id = $(this).attr('id');
  25 + var id = $(this).attr('id');
26 26
27 27 var tagslist = $(this).val().split(delimiter[id]);
28 28 if (tagslist[0] == '') {
@@ -32,11 +32,11 @@
32 32 value = jQuery.trim(value);
33 33
34 34 if (options.unique) {
35   - skipTag = $(tagslist).tagExist(value);
  35 + var skipTag = $(tagslist).tagExist(value);
36 36 } else {
37   - skipTag = false;
  37 + var skipTag = false;
38 38 }
39   -
  39 +
40 40 if (value !='' && skipTag != true) {
41 41 $('<span>').addClass('tag').append(
42 42 $('<span>').text(value).append('&nbsp;&nbsp;'),
@@ -58,6 +58,8 @@
58 58 $('#'+id+'_tag').blur();
59 59 }
60 60
  61 + $.fn.tagsInput.updateTagsField(this,tagslist);
  62 +
61 63 if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) {
62 64 var f = tags_callbacks[id]['onAddTag'];
63 65 f(value);
@@ -69,7 +71,6 @@
69 71 f($(this), tagslist[i]);
70 72 }
71 73 }
72   - $.fn.tagsInput.updateTagsField(this,tagslist);
73 74
74 75 });
75 76
@@ -79,11 +80,10 @@
79 80 $.fn.removeTag = function(value) {
80 81 value = unescape(value);
81 82 this.each(function() {
82   - id = $(this).attr('id');
  83 + var id = $(this).attr('id');
83 84
84 85 var old = $(this).val().split(delimiter[id]);
85   -
86   -
  86 +
87 87 $('#'+id+'_tagsinput .tag').remove();
88 88 str = '';
89 89 for (i=0; i< old.length; i++) {
@@ -104,11 +104,7 @@
104 104 };
105 105
106 106 $.fn.tagExist = function(val) {
107   - if (jQuery.inArray(val, $(this)) == -1) {
108   - return false; /* Cannot find value in array */
109   - } else {
110   - return true; /* Value found */
111   - }
  107 + return (jQuery.inArray(val, $(this)) >= 0); //true when tag exists, false when not
112 108 };
113 109
114 110 // clear all existing tags and import new ones from a string
@@ -138,9 +134,9 @@
138 134 $(this).hide();
139 135 }
140 136
141   - id = $(this).attr('id')
  137 + var id = $(this).attr('id')
142 138
143   - data = jQuery.extend({
  139 + var data = jQuery.extend({
144 140 pid:id,
145 141 real_input: '#'+id,
146 142 holder: '#'+id+'_tagsinput',
@@ -148,7 +144,6 @@
148 144 fake_input: '#'+id+'_tag'
149 145 },settings);
150 146
151   -
152 147 delimiter[id] = data.delimiter;
153 148
154 149 if (settings.onAddTag || settings.onRemoveTag || settings.onChange) {
@@ -199,8 +194,7 @@
199 194 $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete);
200 195 $(data.fake_input).bind('result',data,function(event,data,formatted) {
201 196 if (data) {
202   - d = data + "";
203   - $(event.data.real_input).addTag(d,{focus:true,unique:(settings.unique)});
  197 + $('#'+id).addTag(data + "",{focus:true,unique:(settings.unique)});
204 198 }
205 199 });
206 200 } else if (jQuery.ui.autocomplete !== undefined) {
@@ -229,11 +223,12 @@
229 223
230 224 }
231 225 // if user types a comma, create a new tag
232   - $(data.fake_input).bind('keypress',data,function(event) {
  226 + $(data.fake_input).bind('keypress',data,function(event) {
233 227 if (event.which==event.data.delimiter.charCodeAt(0) || event.which==13 ) {
  228 + event.preventDefault();
234 229 if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) )
235 230 $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)});
236   -
  231 +
237 232 return false;
238 233 }
239 234 });
@@ -248,9 +243,10 @@
248 243 last_tag = last_tag.replace(/[\s]+x$/, '');
249 244 $('#' + id).removeTag(escape(last_tag));
250 245 $(this).trigger('focus');
251   - };
  246 + }
252 247 });
253 248 $(data.fake_input).blur();
  249 +
254 250 } // if settings.interactive
255 251 return false;
256 252 });
@@ -260,13 +256,13 @@
260 256 };
261 257
262 258 $.fn.tagsInput.updateTagsField = function(obj,tagslist) {
263   - id = $(obj).attr('id');
  259 + var id = $(obj).attr('id');
264 260 $(obj).val(tagslist.join(delimiter[id]));
265 261 };
266 262
267 263 $.fn.tagsInput.importTags = function(obj,val) {
268 264 $(obj).val('');
269   - id = $(obj).attr('id');
  265 + var id = $(obj).attr('id');
270 266 var tags = val.split(delimiter[id]);
271 267 for (i=0; i<tags.length; i++) {
272 268 $(obj).addTag(tags[i],{focus:false,callback:false});

0 comments on commit 2f564be

Please sign in to comment.
Something went wrong with that request. Please try again.