From e27d5227de41318f33eeadb98a45e6a9c9da7664 Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Fri, 4 Mar 2016 08:31:41 -0500 Subject: [PATCH 01/11] Changed UI element creation - Changed `  ` to CSS margin. - Changed `x` as close button to `×`. --- src/jquery.tagsinput.css | 63 ++++- src/jquery.tagsinput.js | 562 +++++++++++++++++++-------------------- 2 files changed, 337 insertions(+), 288 deletions(-) diff --git a/src/jquery.tagsinput.css b/src/jquery.tagsinput.css index c595e24..f8421ce 100644 --- a/src/jquery.tagsinput.css +++ b/src/jquery.tagsinput.css @@ -1,7 +1,56 @@ -div.tagsinput { border:1px solid #CCC; background: #FFF; padding:5px; width:300px; height:100px; overflow-y: auto;} -div.tagsinput span.tag { border: 1px solid #a5d24a; -moz-border-radius:2px; -webkit-border-radius:2px; display: block; float: left; padding: 5px; text-decoration:none; background: #cde69c; color: #638421; margin-right: 5px; margin-bottom:5px;font-family: helvetica; font-size:13px;} -div.tagsinput span.tag a { font-weight: bold; color: #82ad2b; text-decoration:none; font-size: 11px; } -div.tagsinput input { width:80px; margin:0px; font-family: helvetica; font-size: 13px; border:1px solid transparent; padding:5px; background: transparent; color: #000; outline:0px; margin-right:5px; margin-bottom:5px; } -div.tagsinput div { display:block; float: left; } -.tags_clear { clear: both; width: 100%; height: 0px; } -.not_valid {background: #FBD8DB !important; color: #90111A !important;} +div.tagsinput { + border: 1px solid #ccc; + background: #fff; + padding: 5px; + width: 300px; + height: 100px; + overflow-y: auto; +} +div.tagsinput span.tag { + border: 1px solid #a5d24a; + -moz-border-radius: 2px; + -webkit-border-radius: 2px; + display: block; + float: left; + padding: 5px; + text-decoration: none; + background: #cde69c; + color: #638421; + margin-right: 5px; + margin-bottom: 5px; + font-family: helvetica; + font-size: 13px; +} +div.tagsinput span.tag a { + margin-left: 5px; + font-weight: bold; + color: #82ad2b; + text-decoration: none; + font-size: 11px; +} +div.tagsinput input { + width: 80px; + margin: 0; + font-family: helvetica; + font-size: 13px; + border: 1px solid transparent; + padding: 5px; + background: transparent; + color: #000; + outline: 0; + margin-right: 5px; + margin-bottom: 5px; +} +div.tagsinput div { + display: block; + float: left; +} +.tags_clear { + clear: both; + width: 100%; + height: 0; +} +.not_valid { + background: #fbd8db !important; + color: #90111a !important; +} diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index b6c75e4..332d209 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -12,56 +12,56 @@ ben@xoxco.com -*/ + */ -(function($) { + (function($) { - var delimiter = new Array(); - var tags_callbacks = new Array(); - $.fn.doAutosize = function(o){ - var minWidth = $(this).data('minwidth'), - maxWidth = $(this).data('maxwidth'), - val = '', - input = $(this), - testSubject = $('#'+$(this).data('tester_id')); + var delimiter = new Array(); + var tags_callbacks = new Array(); + $.fn.doAutosize = function(o){ + var minWidth = $(this).data('minwidth'), + maxWidth = $(this).data('maxwidth'), + val = '', + input = $(this), + testSubject = $('#'+$(this).data('tester_id')); - if (val === (val = input.val())) {return;} + if (val === (val = input.val())) {return;} // Enter new content into testSubject var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>'); testSubject.html(escaped); // Calculate new width + whether to change var testerWidth = testSubject.width(), - newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, - currentWidth = input.width(), - isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) - || (newWidth > minWidth && newWidth < maxWidth); + newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, + currentWidth = input.width(), + isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) + || (newWidth > minWidth && newWidth < maxWidth); // Animate width if (isValidWidthChange) { - input.width(newWidth); - } + input.width(newWidth); + } - }; - $.fn.resetAutosize = function(options){ + }; + $.fn.resetAutosize = function(options){ // alert(JSON.stringify(options)); var minWidth = $(this).data('minwidth') || options.minInputWidth || $(this).width(), - maxWidth = $(this).data('maxwidth') || options.maxInputWidth || ($(this).closest('.tagsinput').width() - options.inputPadding), - val = '', - input = $(this), - testSubject = $('').css({ - position: 'absolute', - top: -9999, - left: -9999, - width: 'auto', - fontSize: input.css('fontSize'), - fontFamily: input.css('fontFamily'), - fontWeight: input.css('fontWeight'), - letterSpacing: input.css('letterSpacing'), - whiteSpace: 'nowrap' - }), - testerId = $(this).attr('id')+'_autosize_tester'; + maxWidth = $(this).data('maxwidth') || options.maxInputWidth || ($(this).closest('.tagsinput').width() - options.inputPadding), + val = '', + input = $(this), + testSubject = $('').css({ + position: 'absolute', + top: -9999, + left: -9999, + width: 'auto', + fontSize: input.css('fontSize'), + fontFamily: input.css('fontFamily'), + fontWeight: input.css('fontWeight'), + letterSpacing: input.css('letterSpacing'), + whiteSpace: 'nowrap' + }), + testerId = $(this).attr('id')+'_autosize_tester'; if(! $('#'+testerId).length > 0){ testSubject.attr('id', testerId); testSubject.appendTo('body'); @@ -73,108 +73,108 @@ input.css('width', minWidth); }; - $.fn.addTag = function(value,options) { - options = jQuery.extend({focus:false,callback:true},options); - this.each(function() { - var id = $(this).attr('id'); + $.fn.addTag = function(value,options) { + options = jQuery.extend({focus:false,callback:true},options); + this.each(function() { + var id = $(this).attr('id'); - var tagslist = $(this).val().split(delimiter[id]); - if (tagslist[0] == '') { - tagslist = new Array(); - } + var tagslist = $(this).val().split(delimiter[id]); + if (tagslist[0] == '') { + tagslist = new Array(); + } - value = jQuery.trim(value); + value = jQuery.trim(value); - if (options.unique) { - var skipTag = $(this).tagExist(value); - if(skipTag == true) { + if (options.unique) { + var skipTag = $(this).tagExist(value); + if(skipTag == true) { //Marks fake input as not_valid to let styling it - $('#'+id+'_tag').addClass('not_valid'); - } - } else { - var skipTag = false; - } - - if (value !='' && skipTag != true) { - $('').addClass('tag').append( - $('').text(value).append('  '), - $('', { - href : '#', - title : 'Removing tag', - text : 'x' - }).click(function () { - return $('#' + id).removeTag(escape(value)); - }) - ).insertBefore('#' + id + '_addTag'); - - tagslist.push(value); - - $('#'+id+'_tag').val(''); - if (options.focus) { - $('#'+id+'_tag').focus(); - } else { - $('#'+id+'_tag').blur(); - } - - $.fn.tagsInput.updateTagsField(this,tagslist); - - if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) { - var f = tags_callbacks[id]['onAddTag']; - f.call(this, value); - } - if(tags_callbacks[id] && tags_callbacks[id]['onChange']) - { - var i = tagslist.length; - var f = tags_callbacks[id]['onChange']; - f.call(this, $(this), tagslist[i-1]); - } - } - - }); - - return false; - }; - - $.fn.removeTag = function(value) { - value = unescape(value); - this.each(function() { - var id = $(this).attr('id'); - - var old = $(this).val().split(delimiter[id]); - - $('#'+id+'_tagsinput .tag').remove(); - str = ''; - for (i=0; i< old.length; i++) { - if (old[i]!=value) { - str = str + delimiter[id] +old[i]; - } - } - - $.fn.tagsInput.importTags(this,str); - - if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) { - var f = tags_callbacks[id]['onRemoveTag']; - f.call(this, value); - } - }); - - return false; - }; - - $.fn.tagExist = function(val) { - var id = $(this).attr('id'); - var tagslist = $(this).val().split(delimiter[id]); + $('#'+id+'_tag').addClass('not_valid'); + } + } else { + var skipTag = false; + } + + if (value !='' && skipTag != true) { + $('').addClass('tag').append( + $('').text(value), + $('', { + href : '#', + title : 'Removing tag', + text : '×' + }).click(function () { + return $('#' + id).removeTag(escape(value)); + }) + ).insertBefore('#' + id + '_addTag'); + + tagslist.push(value); + + $('#'+id+'_tag').val(''); + if (options.focus) { + $('#'+id+'_tag').focus(); + } else { + $('#'+id+'_tag').blur(); + } + + $.fn.tagsInput.updateTagsField(this,tagslist); + + if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) { + var f = tags_callbacks[id]['onAddTag']; + f.call(this, value); + } + if(tags_callbacks[id] && tags_callbacks[id]['onChange']) + { + var i = tagslist.length; + var f = tags_callbacks[id]['onChange']; + f.call(this, $(this), tagslist[i-1]); + } + } + + }); + + return false; + }; + + $.fn.removeTag = function(value) { + value = unescape(value); + this.each(function() { + var id = $(this).attr('id'); + + var old = $(this).val().split(delimiter[id]); + + $('#'+id+'_tagsinput .tag').remove(); + str = ''; + for (i=0; i< old.length; i++) { + if (old[i]!=value) { + str = str + delimiter[id] +old[i]; + } + } + + $.fn.tagsInput.importTags(this,str); + + if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) { + var f = tags_callbacks[id]['onRemoveTag']; + f.call(this, value); + } +}); + + return false; + }; + + $.fn.tagExist = function(val) { + var id = $(this).attr('id'); + var tagslist = $(this).val().split(delimiter[id]); return (jQuery.inArray(val, tagslist) >= 0); //true when tag exists, false when not }; // clear all existing tags and import new ones from a string $.fn.importTags = function(str) { - var id = $(this).attr('id'); - $('#'+id+'_tagsinput .tag').remove(); - $.fn.tagsInput.importTags(this,str); - } + var id = $(this).attr('id'); + $('#'+id+'_tagsinput .tag').remove(); + $.fn.tagsInput.importTags(this,str); + } - $.fn.tagsInput = function(options) { + $.fn.tagsInput = function(options) { var settings = jQuery.extend({ interactive:true, defaultText:'add a tag', @@ -192,174 +192,174 @@ inputPadding: 6*2 },options); - var uniqueIdCounter = 0; + var uniqueIdCounter = 0; - this.each(function() { + this.each(function() { // If we have already initialized the field, do not do it again if (typeof $(this).attr('data-tagsinput-init') !== 'undefined') { - return; - } + return; + } // Mark the field as having been initialized $(this).attr('data-tagsinput-init', true); - if (settings.hide) { - $(this).hide(); - } - var id = $(this).attr('id'); - if (!id || delimiter[$(this).attr('id')]) { - id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); - } - - var data = jQuery.extend({ - pid:id, - real_input: '#'+id, - holder: '#'+id+'_tagsinput', - input_wrapper: '#'+id+'_addTag', - fake_input: '#'+id+'_tag' - },settings); - - delimiter[id] = data.delimiter; - - if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { - tags_callbacks[id] = new Array(); - tags_callbacks[id]['onAddTag'] = settings.onAddTag; - tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag; - tags_callbacks[id]['onChange'] = settings.onChange; - } - - var markup = '
'; - - if (settings.interactive) { - markup = markup + ''; - } - - markup = markup + '
'; - - $(markup).insertAfter(this); - - $(data.holder).css('width',settings.width); - $(data.holder).css('min-height',settings.height); - $(data.holder).css('height',settings.height); - - if ($(data.real_input).val()!='') { - $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); - } - if (settings.interactive) { - $(data.fake_input).val($(data.fake_input).attr('data-default')); - $(data.fake_input).css('color',settings.placeholderColor); - $(data.fake_input).resetAutosize(settings); - - $(data.holder).bind('click',data,function(event) { - $(event.data.fake_input).focus(); - }); - - $(data.fake_input).bind('focus',data,function(event) { - if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) { - $(event.data.fake_input).val(''); - } - $(event.data.fake_input).css('color','#000000'); - }); - - if (settings.autocomplete_url != undefined) { - autocomplete_options = {source: settings.autocomplete_url}; - for (attrname in settings.autocomplete) { - autocomplete_options[attrname] = settings.autocomplete[attrname]; - } - - if (jQuery.Autocompleter !== undefined) { - $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); - $(data.fake_input).bind('result',data,function(event,data,formatted) { - if (data) { - $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); - } - }); - } else if (jQuery.ui.autocomplete !== undefined) { - $(data.fake_input).autocomplete(autocomplete_options); - $(data.fake_input).bind('autocompleteselect',data,function(event,ui) { - $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); - return false; - }); - } - - - } else { + if (settings.hide) { + $(this).hide(); + } + var id = $(this).attr('id'); + if (!id || delimiter[$(this).attr('id')]) { + id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); + } + + var data = jQuery.extend({ + pid:id, + real_input: '#'+id, + holder: '#'+id+'_tagsinput', + input_wrapper: '#'+id+'_addTag', + fake_input: '#'+id+'_tag' + },settings); + + delimiter[id] = data.delimiter; + + if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { + tags_callbacks[id] = new Array(); + tags_callbacks[id]['onAddTag'] = settings.onAddTag; + tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag; + tags_callbacks[id]['onChange'] = settings.onChange; + } + + var markup = '
'; + + if (settings.interactive) { + markup = markup + ''; + } + + markup = markup + '
'; + + $(markup).insertAfter(this); + + $(data.holder).css('width',settings.width); + $(data.holder).css('min-height',settings.height); + $(data.holder).css('height',settings.height); + + if ($(data.real_input).val()!='') { + $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); + } + if (settings.interactive) { + $(data.fake_input).val($(data.fake_input).attr('data-default')); + $(data.fake_input).css('color',settings.placeholderColor); + $(data.fake_input).resetAutosize(settings); + + $(data.holder).bind('click',data,function(event) { + $(event.data.fake_input).focus(); + }); + + $(data.fake_input).bind('focus',data,function(event) { + if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) { + $(event.data.fake_input).val(''); + } + $(event.data.fake_input).css('color','#000000'); + }); + + if (settings.autocomplete_url != undefined) { + autocomplete_options = {source: settings.autocomplete_url}; + for (attrname in settings.autocomplete) { + autocomplete_options[attrname] = settings.autocomplete[attrname]; + } + + if (jQuery.Autocompleter !== undefined) { + $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); + $(data.fake_input).bind('result',data,function(event,data,formatted) { + if (data) { + $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); + } + }); + } else if (jQuery.ui.autocomplete !== undefined) { + $(data.fake_input).autocomplete(autocomplete_options); + $(data.fake_input).bind('autocompleteselect',data,function(event,ui) { + $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); + return false; + }); + } + + + } else { // if a user tabs out of the field, create a new tag // this is only available if autocomplete is not used. $(data.fake_input).bind('blur',data,function(event) { var d = $(this).attr('data-default'); if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) { if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) - $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); - } else { - $(event.data.fake_input).val($(event.data.fake_input).attr('data-default')); - $(event.data.fake_input).css('color',settings.placeholderColor); - } - return false; - }); - - } + $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); + } else { + $(event.data.fake_input).val($(event.data.fake_input).attr('data-default')); + $(event.data.fake_input).css('color',settings.placeholderColor); + } + return false; + }); + + } // if user types a default delimiter like comma,semicolon and then create a new tag $(data.fake_input).bind('keypress',data,function(event) { if (_checkDelimiter(event)) { - event.preventDefault(); - if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) - $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); - $(event.data.fake_input).resetAutosize(settings); - return false; - } else if (event.data.autosize) { - $(event.data.fake_input).doAutosize(settings); - - } - }); + event.preventDefault(); + if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) + $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); + $(event.data.fake_input).resetAutosize(settings); + return false; + } else if (event.data.autosize) { + $(event.data.fake_input).doAutosize(settings); + + } + }); //Delete last tag on backspace data.removeWithBackspace && $(data.fake_input).bind('keydown', function(event) { if(event.keyCode == 8 && $(this).val() == '') { - event.preventDefault(); - var last_tag = $(this).closest('.tagsinput').find('.tag:last').text(); - var id = $(this).attr('id').replace(/_tag$/, ''); - last_tag = last_tag.replace(/[\s]+x$/, ''); - $('#' + id).removeTag(escape(last_tag)); - $(this).trigger('focus'); - } - }); + event.preventDefault(); + var last_tag = $(this).closest('.tagsinput').find('.tag:last').text(); + var id = $(this).attr('id').replace(/_tag$/, ''); + last_tag = last_tag.replace(/[\s]+x$/, ''); + $('#' + id).removeTag(escape(last_tag)); + $(this).trigger('focus'); + } + }); $(data.fake_input).blur(); //Removes the not_valid class when user changes the value of the fake input if(data.unique) { - $(data.fake_input).keydown(function(event){ - if(event.keyCode == 8 || String.fromCharCode(event.which).match(/\w+|[áéíóúÁÉÍÓÚñÑ,/]+/)) { - $(this).removeClass('not_valid'); - } - }); - } + $(data.fake_input).keydown(function(event){ + if(event.keyCode == 8 || String.fromCharCode(event.which).match(/\w+|[áéíóúÁÉÍÓÚñÑ,/]+/)) { + $(this).removeClass('not_valid'); + } + }); + } } // if settings.interactive }); - return this; - - }; - - $.fn.tagsInput.updateTagsField = function(obj,tagslist) { - var id = $(obj).attr('id'); - $(obj).val(tagslist.join(delimiter[id])); - }; - - $.fn.tagsInput.importTags = function(obj,val) { - $(obj).val(''); - var id = $(obj).attr('id'); - var tags = val.split(delimiter[id]); - for (i=0; i Date: Fri, 4 Mar 2016 08:36:32 -0500 Subject: [PATCH 02/11] Updated version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8195019..36e6b00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jQuery-Tags-Input", - "version": "1.3.5", + "version": "1.3.6", "description": "", "main": "jquery.tagsinput.js", "directories": { From ecf3468134c2f0ddbefbb532c0719ece3f4f09a7 Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Fri, 4 Mar 2016 08:37:00 -0500 Subject: [PATCH 03/11] Version update --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 36e6b00..61baa29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jQuery-Tags-Input", - "version": "1.3.6", + "version": "1.3.7", "description": "", "main": "jquery.tagsinput.js", "directories": { From 3d94150dcca55ca92ae53e2903725141f0a04b11 Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Thu, 10 Mar 2016 11:33:02 -0500 Subject: [PATCH 04/11] Improved CSS sizes and margin for close button --- src/jquery.tagsinput.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jquery.tagsinput.css b/src/jquery.tagsinput.css index f8421ce..ae72f22 100644 --- a/src/jquery.tagsinput.css +++ b/src/jquery.tagsinput.css @@ -23,10 +23,11 @@ div.tagsinput span.tag { } div.tagsinput span.tag a { margin-left: 5px; + padding: 2px; font-weight: bold; color: #82ad2b; text-decoration: none; - font-size: 11px; + font-size: 13px; } div.tagsinput input { width: 80px; From cc461491c0bd5f4de439b61fd7040d42c0494fe6 Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Thu, 10 Mar 2016 11:34:40 -0500 Subject: [PATCH 05/11] Improved `importTags` and HTML element change - Changed HTML element `×` to literal Character - Changed `importTags` to use reges of characters from the delimiter. --- src/jquery.tagsinput.js | 54 +++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index 332d209..1655d17 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -101,8 +101,9 @@ $('
', { href : '#', title : 'Removing tag', - text : '×' - }).click(function () { + html : '×' + }).click(function (e) { + e.preventDefault(); return $('#' + id).removeTag(escape(value)); }) ).insertBefore('#' + id + '_addTag'); @@ -350,7 +351,8 @@ $.fn.tagsInput.updateTagsField = function(obj,tagslist) { $.fn.tagsInput.importTags = function(obj,val) { $(obj).val(''); var id = $(obj).attr('id'); - var tags = val.split(delimiter[id]); + var regex = new RegExp(delimiter[id].join('|')); + var tags = val.split(regex); for (i=0; i Date: Thu, 10 Mar 2016 14:43:21 -0500 Subject: [PATCH 06/11] Fixed indentation --- src/jquery.tagsinput.js | 527 ++++++++++++++++++++-------------------- 1 file changed, 265 insertions(+), 262 deletions(-) diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index 1655d17..c760011 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -1,50 +1,49 @@ /* +jQuery Tags Input Plugin 1.3.3 - jQuery Tags Input Plugin 1.3.3 +Copyright (c) 2011 XOXCO, Inc - Copyright (c) 2011 XOXCO, Inc +Documentation for this plugin lives here: +http://xoxco.com/clickable/jquery-tags-input - Documentation for this plugin lives here: - http://xoxco.com/clickable/jquery-tags-input +Licensed under the MIT license: +http://www.opensource.org/licenses/mit-license.php - Licensed under the MIT license: - http://www.opensource.org/licenses/mit-license.php - - ben@xoxco.com +ben@xoxco.com +*/ - */ +(function($) { + var delimiter = new Array(); + var tags_callbacks = new Array(); + + $.fn.doAutosize = function(o){ + var minWidth = $(this).data('minwidth'), + maxWidth = $(this).data('maxwidth'), + val = '', + input = $(this), + testSubject = $('#'+$(this).data('tester_id')); - (function($) { - - var delimiter = new Array(); - var tags_callbacks = new Array(); - $.fn.doAutosize = function(o){ - var minWidth = $(this).data('minwidth'), - maxWidth = $(this).data('maxwidth'), - val = '', - input = $(this), - testSubject = $('#'+$(this).data('tester_id')); - - if (val === (val = input.val())) {return;} - - // Enter new content into testSubject - var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>'); - testSubject.html(escaped); - // Calculate new width + whether to change - var testerWidth = testSubject.width(), - newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, - currentWidth = input.width(), - isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) - || (newWidth > minWidth && newWidth < maxWidth); - - // Animate width - if (isValidWidthChange) { - input.width(newWidth); - } - - - }; - $.fn.resetAutosize = function(options){ + if (val === (val = input.val())) { + return; + } + + // Enter new content into testSubject + var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>'); + testSubject.html(escaped); + + // Calculate new width + whether to change + var testerWidth = testSubject.width(), + newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, + currentWidth = input.width(), + isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) || (newWidth > minWidth && newWidth < maxWidth); + + // Animate width + if (isValidWidthChange) { + input.width(newWidth); + } + }; + + $.fn.resetAutosize = function(options){ // alert(JSON.stringify(options)); var minWidth = $(this).data('minwidth') || options.minInputWidth || $(this).width(), maxWidth = $(this).data('maxwidth') || options.maxInputWidth || ($(this).closest('.tagsinput').width() - options.inputPadding), @@ -74,29 +73,33 @@ }; $.fn.addTag = function(value,options) { - options = jQuery.extend({focus:false,callback:true},options); - this.each(function() { - var id = $(this).attr('id'); + options = jQuery.extend({ + focus:false, + callback:true + },options); + + this.each(function() { + var id = $(this).attr('id'); - var tagslist = $(this).val().split(delimiter[id]); - if (tagslist[0] == '') { - tagslist = new Array(); - } + var tagslist = $(this).val().split(delimiter[id]); + if (tagslist[0] == '') { + tagslist = new Array(); + } - value = jQuery.trim(value); + value = jQuery.trim(value); - if (options.unique) { - var skipTag = $(this).tagExist(value); - if(skipTag == true) { - //Marks fake input as not_valid to let styling it - $('#'+id+'_tag').addClass('not_valid'); - } - } else { - var skipTag = false; - } + if (options.unique) { + var skipTag = $(this).tagExist(value); + if(skipTag == true) { + //Marks fake input as not_valid to let styling it + $('#'+id+'_tag').addClass('not_valid'); + } + } else { + var skipTag = false; + } - if (value !='' && skipTag != true) { - $('').addClass('tag').append( + if (value !='' && skipTag != true) { + $('').addClass('tag').append( $('').text(value), $('', { href : '#', @@ -106,70 +109,69 @@ e.preventDefault(); return $('#' + id).removeTag(escape(value)); }) - ).insertBefore('#' + id + '_addTag'); + ).insertBefore('#' + id + '_addTag'); - tagslist.push(value); + tagslist.push(value); - $('#'+id+'_tag').val(''); - if (options.focus) { - $('#'+id+'_tag').focus(); - } else { - $('#'+id+'_tag').blur(); - } + $('#'+id+'_tag').val(''); + if (options.focus) { + $('#'+id+'_tag').focus(); + } else { + $('#'+id+'_tag').blur(); + } - $.fn.tagsInput.updateTagsField(this,tagslist); + $.fn.tagsInput.updateTagsField(this,tagslist); - if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) { - var f = tags_callbacks[id]['onAddTag']; - f.call(this, value); - } - if(tags_callbacks[id] && tags_callbacks[id]['onChange']) - { - var i = tagslist.length; - var f = tags_callbacks[id]['onChange']; - f.call(this, $(this), tagslist[i-1]); - } + if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) { + var f = tags_callbacks[id]['onAddTag']; + f.call(this, value); } + + if(tags_callbacks[id] && tags_callbacks[id]['onChange']) { + var i = tagslist.length; + var f = tags_callbacks[id]['onChange']; + f.call(this, $(this), tagslist[i-1]); + } + } + }); - }); - - return false; - }; + return false; + }; - $.fn.removeTag = function(value) { - value = unescape(value); - this.each(function() { - var id = $(this).attr('id'); + $.fn.removeTag = function(value) { + value = unescape(value); + this.each(function() { + var id = $(this).attr('id'); - var old = $(this).val().split(delimiter[id]); + var old = $(this).val().split(delimiter[id]); - $('#'+id+'_tagsinput .tag').remove(); - str = ''; - for (i=0; i< old.length; i++) { - if (old[i]!=value) { - str = str + delimiter[id] +old[i]; - } - } + $('#'+id+'_tagsinput .tag').remove(); + str = ''; + for (i=0; i< old.length; i++) { + if (old[i]!=value) { + str = str + delimiter[id] +old[i]; + } + } - $.fn.tagsInput.importTags(this,str); + $.fn.tagsInput.importTags(this,str); - if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) { - var f = tags_callbacks[id]['onRemoveTag']; - f.call(this, value); - } -}); + if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) { + var f = tags_callbacks[id]['onRemoveTag']; + f.call(this, value); + } + }); - return false; - }; + return false; + }; - $.fn.tagExist = function(val) { - var id = $(this).attr('id'); - var tagslist = $(this).val().split(delimiter[id]); - return (jQuery.inArray(val, tagslist) >= 0); //true when tag exists, false when not - }; + $.fn.tagExist = function(val) { + var id = $(this).attr('id'); + var tagslist = $(this).val().split(delimiter[id]); + return (jQuery.inArray(val, tagslist) >= 0); //true when tag exists, false when not + }; - // clear all existing tags and import new ones from a string - $.fn.importTags = function(str) { + // clear all existing tags and import new ones from a string + $.fn.importTags = function(str) { var id = $(this).attr('id'); $('#'+id+'_tagsinput .tag').remove(); $.fn.tagsInput.importTags(this,str); @@ -196,197 +198,198 @@ var uniqueIdCounter = 0; this.each(function() { - // If we have already initialized the field, do not do it again - if (typeof $(this).attr('data-tagsinput-init') !== 'undefined') { - return; - } - - // Mark the field as having been initialized - $(this).attr('data-tagsinput-init', true); + // If we have already initialized the field, do not do it again + if (typeof $(this).attr('data-tagsinput-init') !== 'undefined') { + return; + } - if (settings.hide) { - $(this).hide(); - } - var id = $(this).attr('id'); - if (!id || delimiter[$(this).attr('id')]) { - id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); - } + // Mark the field as having been initialized + $(this).attr('data-tagsinput-init', true); - var data = jQuery.extend({ - pid:id, - real_input: '#'+id, - holder: '#'+id+'_tagsinput', - input_wrapper: '#'+id+'_addTag', - fake_input: '#'+id+'_tag' - },settings); - - delimiter[id] = data.delimiter; - - if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { - tags_callbacks[id] = new Array(); - tags_callbacks[id]['onAddTag'] = settings.onAddTag; - tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag; - tags_callbacks[id]['onChange'] = settings.onChange; - } + if (settings.hide) { + $(this).hide(); + } + + var id = $(this).attr('id'); + if (!id || delimiter[$(this).attr('id')]) { + id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); + } - var markup = '
'; + var data = jQuery.extend({ + pid:id, + real_input: '#'+id, + holder: '#'+id+'_tagsinput', + input_wrapper: '#'+id+'_addTag', + fake_input: '#'+id+'_tag' + },settings); + + delimiter[id] = data.delimiter; + + if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { + tags_callbacks[id] = new Array(); + tags_callbacks[id]['onAddTag'] = settings.onAddTag; + tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag; + tags_callbacks[id]['onChange'] = settings.onChange; + } - if (settings.interactive) { - markup = markup + ''; - } + var markup = '
'; - markup = markup + '
'; + if (settings.interactive) { + markup = markup + ''; + } - $(markup).insertAfter(this); + markup = markup + '
'; - $(data.holder).css('width',settings.width); - $(data.holder).css('min-height',settings.height); - $(data.holder).css('height',settings.height); + $(markup).insertAfter(this); - if ($(data.real_input).val()!='') { - $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); - } - if (settings.interactive) { - $(data.fake_input).val($(data.fake_input).attr('data-default')); - $(data.fake_input).css('color',settings.placeholderColor); - $(data.fake_input).resetAutosize(settings); + $(data.holder).css('width',settings.width); + $(data.holder).css('min-height',settings.height); + $(data.holder).css('height',settings.height); - $(data.holder).bind('click',data,function(event) { - $(event.data.fake_input).focus(); - }); + if ($(data.real_input).val()!='') { + $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); + } + + if (settings.interactive) { + $(data.fake_input).val($(data.fake_input).attr('data-default')); + $(data.fake_input).css('color',settings.placeholderColor); + $(data.fake_input).resetAutosize(settings); + + $(data.holder).bind('click',data,function(event) { + $(event.data.fake_input).focus(); + }); - $(data.fake_input).bind('focus',data,function(event) { - if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) { + $(data.fake_input).bind('focus',data,function(event) { + if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) { $(event.data.fake_input).val(''); } $(event.data.fake_input).css('color','#000000'); }); - if (settings.autocomplete_url != undefined) { - autocomplete_options = {source: settings.autocomplete_url}; - for (attrname in settings.autocomplete) { + if (settings.autocomplete_url != undefined) { + autocomplete_options = {source: settings.autocomplete_url}; + + for (attrname in settings.autocomplete) { autocomplete_options[attrname] = settings.autocomplete[attrname]; } if (jQuery.Autocompleter !== undefined) { $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); $(data.fake_input).bind('result',data,function(event,data,formatted) { - if (data) { - $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); - } - }); + if (data) { + $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); + } + }); } else if (jQuery.ui.autocomplete !== undefined) { $(data.fake_input).autocomplete(autocomplete_options); $(data.fake_input).bind('autocompleteselect',data,function(event,ui) { - $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); - return false; - }); + $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); + return false; + }); } - - } else { - // if a user tabs out of the field, create a new tag - // this is only available if autocomplete is not used. - $(data.fake_input).bind('blur',data,function(event) { - var d = $(this).attr('data-default'); - if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) { - if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) - $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); - } else { - $(event.data.fake_input).val($(event.data.fake_input).attr('data-default')); - $(event.data.fake_input).css('color',settings.placeholderColor); + // if a user tabs out of the field, create a new tag + // this is only available if autocomplete is not used. + $(data.fake_input).bind('blur',data,function(event) { + var d = $(this).attr('data-default'); + if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) { + if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { + $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); + } + } else { + $(event.data.fake_input).val($(event.data.fake_input).attr('data-default')); + $(event.data.fake_input).css('color',settings.placeholderColor); + } + return false; + }); + } + + // if user types a default delimiter like comma,semicolon and then create a new tag + $(data.fake_input).bind('keypress',data,function(event) { + if (_checkDelimiter(event)) { + event.preventDefault(); + if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { + $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); + } + + $(event.data.fake_input).resetAutosize(settings); + return false; + } else if (event.data.autosize) { + $(event.data.fake_input).doAutosize(settings); } - return false; }); - + + //Delete last tag on backspace + data.removeWithBackspace && $(data.fake_input).bind('keydown', function(event) { + if(event.keyCode == 8 && $(this).val() == '') { + event.preventDefault(); + var last_tag = $(this).closest('.tagsinput').find('.tag:last span').text(); + var id = $(this).attr('id').replace(/_tag$/, ''); + $('#' + id).removeTag(escape(last_tag)); + $(this).trigger('focus'); } - // if user types a default delimiter like comma,semicolon and then create a new tag - $(data.fake_input).bind('keypress',data,function(event) { - if (_checkDelimiter(event)) { - event.preventDefault(); - if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) - $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); - $(event.data.fake_input).resetAutosize(settings); - return false; - } else if (event.data.autosize) { - $(event.data.fake_input).doAutosize(settings); - - } - }); - //Delete last tag on backspace - data.removeWithBackspace && $(data.fake_input).bind('keydown', function(event) - { - if(event.keyCode == 8 && $(this).val() == '') - { - event.preventDefault(); - var last_tag = $(this).closest('.tagsinput').find('.tag:last').text(); - var id = $(this).attr('id').replace(/_tag$/, ''); - last_tag = last_tag.replace(/[\s]+x$/, ''); - $('#' + id).removeTag(escape(last_tag)); - $(this).trigger('focus'); - } - }); - $(data.fake_input).blur(); - - //Removes the not_valid class when user changes the value of the fake input - if(data.unique) { + }); + $(data.fake_input).blur(); + + //Removes the not_valid class when user changes the value of the fake input + if(data.unique) { $(data.fake_input).keydown(function(event){ if(event.keyCode == 8 || String.fromCharCode(event.which).match(/\w+|[áéíóúÁÉÍÓÚñÑ,/]+/)) { $(this).removeClass('not_valid'); } }); } - } // if settings.interactive - }); - -return this; - -}; - -$.fn.tagsInput.updateTagsField = function(obj,tagslist) { - var id = $(obj).attr('id'); - $(obj).val(tagslist.join(delimiter[id])); -}; - -$.fn.tagsInput.importTags = function(obj,val) { - $(obj).val(''); - var id = $(obj).attr('id'); - var regex = new RegExp(delimiter[id].join('|')); - var tags = val.split(regex); - for (i=0; i Date: Thu, 10 Mar 2016 16:09:34 -0500 Subject: [PATCH 07/11] Reorganization --- src/jquery.tagsinput.js | 364 +++++++++++++++++++++------------------- 1 file changed, 191 insertions(+), 173 deletions(-) diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index c760011..43b8971 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -15,6 +15,176 @@ ben@xoxco.com (function($) { var delimiter = new Array(); var tags_callbacks = new Array(); + + $.fn.tagsInput = function(options) { + var settings = jQuery.extend({ + interactive:true, + defaultText:'add a tag', + minChars:0, + width:'300px', + height:'100px', + autocomplete: {selectFirst: false }, + hide:true, + delimiter: ',', + unique:true, + removeWithBackspace:true, + placeholderColor:'#666666', + autosize: true, + comfortZone: 20, + inputPadding: 6*2 + },options); + + var uniqueIdCounter = 0; + + this.each(function() { + // If we have already initialized the field, do not do it again + if (typeof $(this).attr('data-tagsinput-init') !== 'undefined') { + return; + } + + // Mark the field as having been initialized + $(this).attr('data-tagsinput-init', true); + + if (settings.hide) { + $(this).hide(); + } + + var id = $(this).attr('id'); + if (!id || delimiter[$(this).attr('id')]) { + id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); + } + + var data = jQuery.extend({ + pid:id, + real_input: '#'+id, + holder: '#'+id+'_tagsinput', + input_wrapper: '#'+id+'_addTag', + fake_input: '#'+id+'_tag' + },settings); + + delimiter[id] = data.delimiter; + + if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { + tags_callbacks[id] = new Array(); + tags_callbacks[id]['onAddTag'] = settings.onAddTag; + tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag; + tags_callbacks[id]['onChange'] = settings.onChange; + } + + var markup = '
'; + + if (settings.interactive) { + markup = markup + ''; + } + + markup = markup + '
'; + + $(markup).insertAfter(this); + + $(data.holder).css('width',settings.width); + $(data.holder).css('min-height',settings.height); + $(data.holder).css('height',settings.height); + + if ($(data.real_input).val()!='') { + $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); + } + + if (settings.interactive) { + $(data.fake_input).val(settings.defaultText); + $(data.fake_input).css('color',settings.placeholderColor); + $(data.fake_input).resetAutosize(settings); + + $(data.holder).bind('click',data,function(event) { + $(event.data.fake_input).focus(); + }); + + $(data.fake_input).bind('focus',data,function(event) { + if($(event.data.fake_input).val() == $(event.data.fake_input).attr('data-default-placeholder')) { + $(event.data.fake_input).val(''); + } + $(event.data.fake_input).css('color','#000000'); + }); + + if (settings.autocomplete_url != undefined) { + autocomplete_options = {source: settings.autocomplete_url}; + + for (attrname in settings.autocomplete) { + autocomplete_options[attrname] = settings.autocomplete[attrname]; + } + + if (jQuery.Autocompleter !== undefined) { + $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); + $(data.fake_input).bind('result',data,function(event,data,formatted) { + if (data) { + $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); + } + }); + } else if (jQuery.ui.autocomplete !== undefined) { + $(data.fake_input).autocomplete(autocomplete_options); + $(data.fake_input).bind('autocompleteselect',data,function(event,ui) { + $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); + }); + } + } else { + // if a user tabs out of the field, create a new tag + // this is only available if autocomplete is not used. + $(data.fake_input).bind('blur',data,function(event) { + var d = $(this).attr('data-default-placeholder'); + if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) { + if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { + $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); + } + } else { + $(event.data.fake_input).val(d); + $(event.data.fake_input).css('color',settings.placeholderColor); + } + }); + } + + // if user types a default delimiter like comma,semicolon and then create a new tag + $(data.fake_input).bind('keypress',data,function(event) { + if (_checkDelimiter(event)) { + event.preventDefault(); + if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { + $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); + } + + $(event.data.fake_input).resetAutosize(settings); + } else if (event.data.autosize) { + $(event.data.fake_input).doAutosize(settings); + } + }); + + //Delete last tag on backspace + if(data.removeWithBackspace) { + $(data.fake_input).bind('keydown', function(event) { + if(event.keyCode == 8 && $(this).val() == '') { + event.preventDefault(); + var last_tag = $(this).closest('.tagsinput').find('.tag:last span').text(); + if(last_tag) { + var id = $(this).attr('id').replace(/_tag$/, ''); + $('#' + id).removeTag(escape(last_tag)); + } + $(this).trigger('focus'); + } + }); + } + + $(data.fake_input).blur(); + + //Removes the not_valid class when user changes the value of the fake input + if(data.unique) { + $(data.fake_input).keydown(function(event){ + if(event.keyCode == 8 || String.fromCharCode(event.which).match(/\w+|[áéíóúÁÉÍÓÚñÑ,/]+/)) { + $(this).removeClass('not_valid'); + } + }); + } + } // if settings.interactive + }); + + return this; + }; $.fn.doAutosize = function(o){ var minWidth = $(this).data('minwidth'), @@ -80,8 +250,9 @@ ben@xoxco.com this.each(function() { var id = $(this).attr('id'); - - var tagslist = $(this).val().split(delimiter[id]); + + var regex = new RegExp(delimiter[id].join('|')); + var tagslist = $(this).val().split(regex); if (tagslist[0] == '') { tagslist = new Array(); } @@ -104,7 +275,7 @@ ben@xoxco.com $('
', { href : '#', title : 'Removing tag', - html : '×' + html : "×" }).click(function (e) { e.preventDefault(); return $('#' + id).removeTag(escape(value)); @@ -143,7 +314,8 @@ ben@xoxco.com this.each(function() { var id = $(this).attr('id'); - var old = $(this).val().split(delimiter[id]); + var regex = new RegExp(delimiter[id].join('|')); + var old = $(this).val().split(regex); $('#'+id+'_tagsinput .tag').remove(); str = ''; @@ -166,7 +338,8 @@ ben@xoxco.com $.fn.tagExist = function(val) { var id = $(this).attr('id'); - var tagslist = $(this).val().split(delimiter[id]); + var regex = new RegExp(delimiter[id].join('|')); + var tagslist = $(this).val().split(regex); return (jQuery.inArray(val, tagslist) >= 0); //true when tag exists, false when not }; @@ -177,174 +350,6 @@ ben@xoxco.com $.fn.tagsInput.importTags(this,str); } - $.fn.tagsInput = function(options) { - var settings = jQuery.extend({ - interactive:true, - defaultText:'add a tag', - minChars:0, - width:'300px', - height:'100px', - autocomplete: {selectFirst: false }, - hide:true, - delimiter: ',', - unique:true, - removeWithBackspace:true, - placeholderColor:'#666666', - autosize: true, - comfortZone: 20, - inputPadding: 6*2 - },options); - - var uniqueIdCounter = 0; - - this.each(function() { - // If we have already initialized the field, do not do it again - if (typeof $(this).attr('data-tagsinput-init') !== 'undefined') { - return; - } - - // Mark the field as having been initialized - $(this).attr('data-tagsinput-init', true); - - if (settings.hide) { - $(this).hide(); - } - - var id = $(this).attr('id'); - if (!id || delimiter[$(this).attr('id')]) { - id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); - } - - var data = jQuery.extend({ - pid:id, - real_input: '#'+id, - holder: '#'+id+'_tagsinput', - input_wrapper: '#'+id+'_addTag', - fake_input: '#'+id+'_tag' - },settings); - - delimiter[id] = data.delimiter; - - if (settings.onAddTag || settings.onRemoveTag || settings.onChange) { - tags_callbacks[id] = new Array(); - tags_callbacks[id]['onAddTag'] = settings.onAddTag; - tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag; - tags_callbacks[id]['onChange'] = settings.onChange; - } - - var markup = '
'; - - if (settings.interactive) { - markup = markup + ''; - } - - markup = markup + '
'; - - $(markup).insertAfter(this); - - $(data.holder).css('width',settings.width); - $(data.holder).css('min-height',settings.height); - $(data.holder).css('height',settings.height); - - if ($(data.real_input).val()!='') { - $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); - } - - if (settings.interactive) { - $(data.fake_input).val($(data.fake_input).attr('data-default')); - $(data.fake_input).css('color',settings.placeholderColor); - $(data.fake_input).resetAutosize(settings); - - $(data.holder).bind('click',data,function(event) { - $(event.data.fake_input).focus(); - }); - - $(data.fake_input).bind('focus',data,function(event) { - if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) { - $(event.data.fake_input).val(''); - } - $(event.data.fake_input).css('color','#000000'); - }); - - if (settings.autocomplete_url != undefined) { - autocomplete_options = {source: settings.autocomplete_url}; - - for (attrname in settings.autocomplete) { - autocomplete_options[attrname] = settings.autocomplete[attrname]; - } - - if (jQuery.Autocompleter !== undefined) { - $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); - $(data.fake_input).bind('result',data,function(event,data,formatted) { - if (data) { - $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); - } - }); - } else if (jQuery.ui.autocomplete !== undefined) { - $(data.fake_input).autocomplete(autocomplete_options); - $(data.fake_input).bind('autocompleteselect',data,function(event,ui) { - $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); - return false; - }); - } - } else { - // if a user tabs out of the field, create a new tag - // this is only available if autocomplete is not used. - $(data.fake_input).bind('blur',data,function(event) { - var d = $(this).attr('data-default'); - if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) { - if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { - $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); - } - } else { - $(event.data.fake_input).val($(event.data.fake_input).attr('data-default')); - $(event.data.fake_input).css('color',settings.placeholderColor); - } - return false; - }); - } - - // if user types a default delimiter like comma,semicolon and then create a new tag - $(data.fake_input).bind('keypress',data,function(event) { - if (_checkDelimiter(event)) { - event.preventDefault(); - if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { - $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); - } - - $(event.data.fake_input).resetAutosize(settings); - return false; - } else if (event.data.autosize) { - $(event.data.fake_input).doAutosize(settings); - } - }); - - //Delete last tag on backspace - data.removeWithBackspace && $(data.fake_input).bind('keydown', function(event) { - if(event.keyCode == 8 && $(this).val() == '') { - event.preventDefault(); - var last_tag = $(this).closest('.tagsinput').find('.tag:last span').text(); - var id = $(this).attr('id').replace(/_tag$/, ''); - $('#' + id).removeTag(escape(last_tag)); - $(this).trigger('focus'); - } - }); - $(data.fake_input).blur(); - - //Removes the not_valid class when user changes the value of the fake input - if(data.unique) { - $(data.fake_input).keydown(function(event){ - if(event.keyCode == 8 || String.fromCharCode(event.which).match(/\w+|[áéíóúÁÉÍÓÚñÑ,/]+/)) { - $(this).removeClass('not_valid'); - } - }); - } - } // if settings.interactive - }); - - return this; - }; - $.fn.tagsInput.updateTagsField = function(obj,tagslist) { var id = $(obj).attr('id'); $(obj).val(tagslist.join(delimiter[id])); @@ -365,6 +370,19 @@ ben@xoxco.com f.call(obj, obj, tags[i]); } }; + + $.fn.destroyTagsInput = function() { + var data = jQuery.extend({ + pid:id, + real_input: '#'+id, + holder: '#'+id+'_tagsinput', + input_wrapper: '#'+id+'_addTag', + fake_input: '#'+id+'_tag' + },settings); + + $(data.holder).unbind(); + $(data.fake_input).unbind(); + }; /** * check delimiter Array From 0c8ac482412bc836c3659af25330fa88f23480df Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Thu, 10 Mar 2016 18:43:09 -0500 Subject: [PATCH 08/11] Updates for destroy --- src/jquery.tagsinput.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index 43b8971..b195a27 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -372,13 +372,18 @@ ben@xoxco.com }; $.fn.destroyTagsInput = function() { - var data = jQuery.extend({ - pid:id, + var id = $(this).attr('id'); + var uniqueIdCounter = 0; + if (!id || delimiter[$(this).attr('id')]) { + id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); + } + + var data = { real_input: '#'+id, holder: '#'+id+'_tagsinput', input_wrapper: '#'+id+'_addTag', fake_input: '#'+id+'_tag' - },settings); + }; $(data.holder).unbind(); $(data.fake_input).unbind(); From 439fa1488c9adc03f22bdff104553a15bd874526 Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Mon, 18 Apr 2016 10:48:47 -0400 Subject: [PATCH 09/11] Changed to unicode --- src/jquery.tagsinput.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index b195a27..d1e6f9a 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -48,7 +48,7 @@ ben@xoxco.com if (settings.hide) { $(this).hide(); } - + var id = $(this).attr('id'); if (!id || delimiter[$(this).attr('id')]) { id = $(this).attr('id', 'tags' + new Date().getTime() + (uniqueIdCounter++)).attr('id'); @@ -88,7 +88,7 @@ ben@xoxco.com if ($(data.real_input).val()!='') { $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); } - + if (settings.interactive) { $(data.fake_input).val(settings.defaultText); $(data.fake_input).css('color',settings.placeholderColor); @@ -107,7 +107,7 @@ ben@xoxco.com if (settings.autocomplete_url != undefined) { autocomplete_options = {source: settings.autocomplete_url}; - + for (attrname in settings.autocomplete) { autocomplete_options[attrname] = settings.autocomplete[attrname]; } @@ -140,7 +140,7 @@ ben@xoxco.com } }); } - + // if user types a default delimiter like comma,semicolon and then create a new tag $(data.fake_input).bind('keypress',data,function(event) { if (_checkDelimiter(event)) { @@ -148,13 +148,13 @@ ben@xoxco.com if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) ) { $(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)}); } - + $(event.data.fake_input).resetAutosize(settings); } else if (event.data.autosize) { $(event.data.fake_input).doAutosize(settings); } }); - + //Delete last tag on backspace if(data.removeWithBackspace) { $(data.fake_input).bind('keydown', function(event) { @@ -169,7 +169,7 @@ ben@xoxco.com } }); } - + $(data.fake_input).blur(); //Removes the not_valid class when user changes the value of the fake input @@ -185,7 +185,7 @@ ben@xoxco.com return this; }; - + $.fn.doAutosize = function(o){ var minWidth = $(this).data('minwidth'), maxWidth = $(this).data('maxwidth'), @@ -196,11 +196,11 @@ ben@xoxco.com if (val === (val = input.val())) { return; } - + // Enter new content into testSubject var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>'); testSubject.html(escaped); - + // Calculate new width + whether to change var testerWidth = testSubject.width(), newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, @@ -212,7 +212,7 @@ ben@xoxco.com input.width(newWidth); } }; - + $.fn.resetAutosize = function(options){ // alert(JSON.stringify(options)); var minWidth = $(this).data('minwidth') || options.minInputWidth || $(this).width(), @@ -247,10 +247,10 @@ ben@xoxco.com focus:false, callback:true },options); - + this.each(function() { var id = $(this).attr('id'); - + var regex = new RegExp(delimiter[id].join('|')); var tagslist = $(this).val().split(regex); if (tagslist[0] == '') { @@ -275,7 +275,7 @@ ben@xoxco.com $('
', { href : '#', title : 'Removing tag', - html : "×" + html : "\u02DF" }).click(function (e) { e.preventDefault(); return $('#' + id).removeTag(escape(value)); @@ -297,7 +297,7 @@ ben@xoxco.com var f = tags_callbacks[id]['onAddTag']; f.call(this, value); } - + if(tags_callbacks[id] && tags_callbacks[id]['onChange']) { var i = tagslist.length; var f = tags_callbacks[id]['onChange']; @@ -360,31 +360,31 @@ ben@xoxco.com var id = $(obj).attr('id'); var regex = new RegExp(delimiter[id].join('|')); var tags = val.split(regex); - + for (i=0; i Date: Mon, 18 Apr 2016 10:49:16 -0400 Subject: [PATCH 10/11] Version update --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 61baa29..2b165ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jQuery-Tags-Input", - "version": "1.3.7", + "version": "1.3.8", "description": "", "main": "jquery.tagsinput.js", "directories": { From c615d615440d68c5d4f4a70cd37c336f83b60955 Mon Sep 17 00:00:00 2001 From: Chris Honniball Date: Mon, 18 Apr 2016 10:57:44 -0400 Subject: [PATCH 11/11] Unicode character and improved styles --- package.json | 2 +- src/jquery.tagsinput.css | 8 ++++++-- src/jquery.tagsinput.js | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 2b165ec..5890ef7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jQuery-Tags-Input", - "version": "1.3.8", + "version": "1.3.9", "description": "", "main": "jquery.tagsinput.js", "directories": { diff --git a/src/jquery.tagsinput.css b/src/jquery.tagsinput.css index ae72f22..bb58227 100644 --- a/src/jquery.tagsinput.css +++ b/src/jquery.tagsinput.css @@ -7,12 +7,13 @@ div.tagsinput { overflow-y: auto; } div.tagsinput span.tag { + position: relative; border: 1px solid #a5d24a; -moz-border-radius: 2px; -webkit-border-radius: 2px; display: block; float: left; - padding: 5px; + padding: 5px 20px 5px 5px; text-decoration: none; background: #cde69c; color: #638421; @@ -22,7 +23,10 @@ div.tagsinput span.tag { font-size: 13px; } div.tagsinput span.tag a { - margin-left: 5px; + position: absolute; + top: 11px; + right: 5px; + line-height: 0px; padding: 2px; font-weight: bold; color: #82ad2b; diff --git a/src/jquery.tagsinput.js b/src/jquery.tagsinput.js index d1e6f9a..0e62fd7 100644 --- a/src/jquery.tagsinput.js +++ b/src/jquery.tagsinput.js @@ -275,7 +275,7 @@ ben@xoxco.com $('', { href : '#', title : 'Removing tag', - html : "\u02DF" + html : "\u00D7" }).click(function (e) { e.preventDefault(); return $('#' + id).removeTag(escape(value));