|
5 | 5 |
|
6 | 6 | (function($) { |
7 | 7 | var delimiter = []; |
| 8 | + var inputSettings = []; |
8 | 9 | var callbacks = []; |
9 | 10 |
|
10 | 11 | $.fn.addTag = function(value, options) { |
11 | 12 | options = jQuery.extend({ |
12 | 13 | focus: false, |
13 | | - callback: true, |
14 | | - minChars: 0, |
15 | | - maxChars: null, |
16 | | - limit: null, |
17 | | - validationPattern: null |
| 14 | + callback: true |
18 | 15 | }, options); |
19 | 16 |
|
20 | 17 | this.each(function() { |
|
25 | 22 |
|
26 | 23 | value = jQuery.trim(value); |
27 | 24 |
|
28 | | - if ((options.unique && $(this).tagExist(value)) || !_validateTag(value, options, tagslist, delimiter[id])) { |
| 25 | + if ((options.unique && $(this).tagExist(value)) || !_validateTag(value, inputSettings[id], tagslist, delimiter[id])) { |
29 | 26 | $('#' + id + '_tag').addClass('error'); |
30 | 27 | return false; |
31 | 28 | } |
|
131 | 128 |
|
132 | 129 | var id = $(this).attr('id'); |
133 | 130 | if (!id || _getDelimiter(delimiter[$(this).attr('id')])) { |
134 | | - id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); |
| 131 | + id = $(this).attr('id', 'tags' + new Date().getTime() + (++uniqueIdCounter)).attr('id'); |
135 | 132 | } |
136 | 133 |
|
137 | 134 | var data = jQuery.extend({ |
|
143 | 140 | }, settings); |
144 | 141 |
|
145 | 142 | delimiter[id] = data.delimiter; |
| 143 | + inputSettings[id] = { |
| 144 | + minChars: settings.minChars, |
| 145 | + maxChars: settings.maxChars, |
| 146 | + limit: settings.limit, |
| 147 | + validationPattern: settings.validationPattern |
| 148 | + }; |
146 | 149 |
|
147 | 150 | if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { |
148 | 151 | callbacks[id] = []; |
|
190 | 193 | $(data.fake_input).on('autocompleteselect', data, function(event, ui) { |
191 | 194 | $(event.data.real_input).addTag(ui.item.value, { |
192 | 195 | focus: true, |
193 | | - unique: settings.unique, |
194 | | - minChars: settings.minChars, |
195 | | - maxChars: settings.maxChars, |
196 | | - limit: settings.limit, |
197 | | - validationPattern: settings.validationPattern |
| 196 | + unique: settings.unique |
198 | 197 | }); |
199 | 198 |
|
200 | 199 | return false; |
|
209 | 208 | $(data.fake_input).on('blur', data, function(event) { |
210 | 209 | $(event.data.real_input).addTag($(event.data.fake_input).val(), { |
211 | 210 | focus: true, |
212 | | - unique: settings.unique, |
213 | | - minChars: settings.minChars, |
214 | | - maxChars: settings.maxChars, |
215 | | - limit: settings.limit, |
216 | | - validationPattern: settings.validationPattern |
| 211 | + unique: settings.unique |
217 | 212 | }); |
218 | 213 |
|
219 | 214 | return false; |
|
227 | 222 |
|
228 | 223 | $(event.data.real_input).addTag($(event.data.fake_input).val(), { |
229 | 224 | focus: true, |
230 | | - unique: settings.unique, |
231 | | - minChars: settings.minChars, |
232 | | - maxChars: settings.maxChars, |
233 | | - limit: settings.limit, |
234 | | - validationPattern: settings.validationPattern |
| 225 | + unique: settings.unique |
235 | 226 | }); |
236 | 227 |
|
237 | 228 | return false; |
|
259 | 250 | for (var i = 0; i < tags.length; ++i) { |
260 | 251 | $(event.data.real_input).addTag(tags[i], { |
261 | 252 | focus: true, |
262 | | - unique: settings.unique, |
263 | | - minChars: settings.minChars, |
264 | | - maxChars: settings.maxChars, |
265 | | - limit: settings.limit, |
266 | | - validationPattern: settings.validationPattern |
| 253 | + unique: settings.unique |
267 | 254 | }); |
268 | 255 | } |
269 | 256 |
|
|
308 | 295 | for (i = 0; i < tags.length; ++i) { |
309 | 296 | $(obj).addTag(tags[i], { |
310 | 297 | focus: false, |
311 | | - callback: false, |
312 | | - minChars: 0, |
313 | | - maxChars: null, |
314 | | - limit: null, |
315 | | - validationPattern: null |
| 298 | + callback: false |
316 | 299 | }); |
317 | 300 | } |
318 | 301 |
|
|
332 | 315 | } |
333 | 316 | }; |
334 | 317 |
|
335 | | - var _validateTag = function(value, options, tagslist, delimiter) { |
| 318 | + var _validateTag = function(value, inputSettings, tagslist, delimiter) { |
336 | 319 | var result = true; |
337 | 320 |
|
338 | 321 | if (value === '') result = false; |
339 | | - if (value.length < options.minChars) result = false; |
340 | | - if (options.maxChars !== null && value.length > options.maxChars) result = false; |
341 | | - if (options.limit !== null && tagslist.length >= options.limit) result = false; |
342 | | - if (options.validationPattern !== null && !options.validationPattern.test(value)) result = false; |
| 322 | + if (value.length < inputSettings.minChars) result = false; |
| 323 | + if (inputSettings.maxChars !== null && value.length > inputSettings.maxChars) result = false; |
| 324 | + if (inputSettings.limit !== null && tagslist.length >= inputSettings.limit) result = false; |
| 325 | + if (inputSettings.validationPattern !== null && !inputSettings.validationPattern.test(value)) result = false; |
343 | 326 |
|
344 | 327 | if (typeof delimiter === 'string') { |
345 | 328 | if (value.indexOf(delimiter) > -1) result = false; |
|
0 commit comments