32 Commits
Author SHA1 Message Date
junv 291739bd50 if the setting's unique be true,tag should be case sensitive. 2012-12-13 13:37:50 +08:00
Ben Brown e27248adde Add link to jsFiddle to readme. 2012-05-10 12:44:39 -07:00
Ben Brown bc50b025fb Merge pull request #37 from devbrothers/master
Multiple tags-input instances fix
2012-04-11 13:44:40 -07:00
Ben Brown 50a623375d Merge pull request #38 from prymitive/master
Use min-height for input area
2012-04-11 13:43:57 -07:00
Ben Brown b20d10a78f Merge pull request #51 from tybulewicz/master
Fixed issue #49 - tagExist() now works correctly
2012-04-11 13:23:36 -07:00
Tomasz Tybulewicz b56a968f97 Fixed issue #49 - tagExist() now works correctly 2012-03-13 19:59:46 +01:00
Łukasz Mierzwa 7476b8b75f use min-height instead of fixed setting, so that input area can grow if needed 2012-01-02 20:24:50 +01:00
Bartosz Grzybowski 51d058c46b updated script to work properly with multiple instances 2012-01-02 16:30:40 +01:00
Ben Brown 6d2b1d3b72 add semicolon to end of minified version 2011-12-06 09:55:29 -08:00
Ben Brown f8d1362f25 Remove errant comma from parameter list,
fixing issue #34 and #35 for IE
2011-12-06 09:53:46 -08:00
Ben Brown 78487e0592 Add semicolon to end of file in minified version.
Fix to issue #32
2011-11-21 08:35:25 -08:00
Ben Brown 8d06653aa6 readme update 2011-11-16 13:47:10 -08:00
Ben Brown ad2889e6a4 updating readme to markdown 2011-11-16 13:46:21 -08:00
Ben Brown 73c60604f8 updated minified version 2011-11-15 11:49:36 -08:00
Ben Brown 49fc5f3ab1 fix to issue #28
https://github.com/xoxco/jQuery-Tags-Input/issues/28
onChange now receives the tag added or removed.
2011-11-15 11:41:58 -08:00
Ben Brown 5437401613 Merge branch 'autogrow-input' of http://github.com/therabidbanana/jQuery-Tags-Input into therabidbanana-autogrow-input
Conflicts:
	jquery.tagsinput.js
2011-11-15 11:35:32 -08:00
Ben Brown 55145e9390 use data[0] instead of data when using old autocompleter 2011-11-15 11:25:27 -08:00
Ben Brown 00a112153c Merge branch 'master' of github.com:xoxco/jQuery-Tags-Input 2011-11-15 11:10:35 -08:00
Ben Brown 53bce4bf40 Merge pull request #21 from cboden/master
Suggested fix for #20
2011-11-15 11:10:20 -08:00
Ben Brown 08c91e2f67 Merge branch 'master' of github.com:xoxco/jQuery-Tags-Input 2011-11-15 11:07:44 -08:00
Ben Brown b8665626c6 Merge pull request #18 from javiertoledo/master
Mark fake input as not_valid to let users style it on repeated tag
2011-11-15 11:07:34 -08:00
Ben Brown 2f564beaad Merge branch 'master' of github.com:xoxco/jQuery-Tags-Input 2011-11-15 11:05:58 -08:00
Ben Brown 0b7d62a3e1 Update README, bump version number 2011-11-15 11:05:15 -08:00
Ben Brown b7d339216d Merge pull request #17 from javiertoledo/ffac87c585a7e02b1658b628315666de33b489b1
I have addressed the Issue #15 opened by Desi-Planet and wrote a fix
2011-11-15 11:02:45 -08:00
Ben Brown fdf650de12 Merge pull request #26 from xorrizon/master
Fixed onAddTag timing bug
2011-11-15 10:59:12 -08:00
Ben Brown dcc33019ec Fix problem in importTags where tag elements were not properly removed due to missing definition of id variable 2011-11-15 09:45:28 -08:00
Xorrizon fa10428b04 Fixed a bug where the "onAddTag" callback is fired before the input field is updated 2011-09-26 15:06:24 +02:00
David Haslem 37a4fb1e8e Working autosize 2011-08-22 12:31:11 -04:00
David Haslem 53ffc5f6ed Set up initial hooks 2011-08-22 11:24:19 -04:00
Chris Boden 1cc6f5b2f8 Fixed scope bug 2011-07-28 16:18:38 -04:00
Javier Toledo 1b461629fe Mark as not_valid the fake input on repeated tag
I have added some code to add a not_valid class to the fake input to be able to stylish it when some value is repeated and I have selected the unique option.
2011-07-12 21:20:16 +01:00
Javier Toledo ffac87c585 Fixed a bug when creating multiple tags-input in same page
Some variables used global scope, so each new instance of tagsInput overwrite them with its own values.
I have also added a preventDefault to the fake_input keypress event as I had some form submitting issues when using the plugin and "Optimized" the tagExist method.
2011-07-12 21:17:30 +01:00
5 changed files with 204 additions and 128 deletions
+51 -46
View File
@@ -1,18 +1,4 @@
jQuery Tags Input Plugin 1.3.1
Copyright (c) 2011 XOXCO, Inc
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
The git repository for this plugin can be found at:
https://github.com/xoxco/jQuery-Tags-Input
ben@xoxco.com
------------------------------------------------------
# jQuery Tags Input Plugin
Do you use tags to organize content on your site?
This plugin will turn your boring tag list into a
@@ -21,45 +7,65 @@ object with its own delete link. The plugin handles
all the data - your form just sees a comma-delimited
list of tags!
[Get it from Github](https://github.com/xoxco/jQuery-Tags-Input)
Instructions
[View Demo](http://xoxco.com/projects/code/tagsinput/)
[Test it yourself using this jsFiddle Demo](http://jsfiddle.net/7aDak/)
Created by [XOXCO](http://xoxco.com)
## Instructions
First, add the Javascript and CSS files to your <head> tag:
<script src="jquery.tagsinput.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.tagsinput.css" />
<script src="jquery.tagsinput.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.tagsinput.css" />
Create a real input in your form that will contain a comma-separated list of
tags. You can put any default or existing tags in the value attribute, and
they'll be handled properly.
<input name="tags" id="tags" value="foo,bar,baz" />
<input name="tags" id="tags" value="foo,bar,baz" />
Then, simply call the tagsInput function on any field that should be treated as
a list of tags.
$('#tags').tagsInput();
$('#tags').tagsInput();
If you want to use jQuery.autocomplete, you can pass in a parameter with the
autocomplete url.
$('#tags').tagsInput({
autocomplete_url:'http://myserver.com/api/autocomplete'
});
$('#tags').tagsInput({
autocomplete_url:'http://myserver.com/api/autocomplete'
});
If you're using the bassistance jQuery.autocomplete, which takes extra
parameters, you can also send in options to the autocomplete plugin, as
described here.
$('#tags').tagsInput({
autocomplete_url:'http://myserver.com/api/autocomplete',
autocomplete:{selectFirst:true,width:'100px',autoFill:true}
});
$('#tags').tagsInput({
autocomplete_url:'http://myserver.com/api/autocomplete',
autocomplete:{selectFirst:true,width:'100px',autoFill:true}
});
You can add and remove tags by calling the addTag() and removeTag() functions.
$('#tags').addTag('foo');
$('#tags').removeTag('bar');
$('#tags').addTag('foo');
$('#tags').removeTag('bar');
You can import a list of tags using the importTags() function...
$('#tags').importTags('foo,bar,baz');
You can also use importTags() to reset the tag list...
$('#tags').importTags('');
And you can check if a tag exists using tagExist()...
if ($('#tags').tagExist('foo')) { ... }
If additional functionality is required when a tag is added or removed, you may
specify callback functions via the onAddTag and onRemoveTag parameters. Both
@@ -77,21 +83,20 @@ By default, if the cursor is immediately after a tag, hitting backspace will
delete that tag. If you want to override this, set the 'removeWithBackspace'
option to false.
Options
$(selector).tagsInput({
'autocomplete_url': url_to_autocomplete_api,
'autocomplete': { option: value, option: value},
'height':'100px',
'width':'300px',
'interactive':true,
'defaultText':'add a tag',
'onAddTag':callback_function,
'onRemoveTag':callback_function,
'onChange' : callback_function,
'removeWithBackspace' : true,
'minChars' : 0,
'maxChars' : 0 //if not provided there is no limit,
'placeholderColor' : '#666666'
});
## Options
$(selector).tagsInput({
'autocomplete_url': url_to_autocomplete_api,
'autocomplete': { option: value, option: value},
'height':'100px',
'width':'300px',
'interactive':true,
'defaultText':'add a tag',
'onAddTag':callback_function,
'onRemoveTag':callback_function,
'onChange' : callback_function,
'removeWithBackspace' : true,
'minChars' : 0,
'maxChars' : 0 //if not provided there is no limit,
'placeholderColor' : '#666666'
});
+21 -26
View File
@@ -1,12 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery Tags Input Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="jquery.tagsinput.css" />
<link rel="stylesheet" type="text/css" href="http://xoxco.com/projects/code/tagsinput/jquery.tagsinput.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tagsinput.js"></script>
<script type="text/javascript" src="http://xoxco.com/projects/code/tagsinput/jquery.tagsinput.js"></script>
<!-- To test using the original jQuery.autocomplete, uncomment the following -->
<!--
<script type='text/javascript' src='http://xoxco.com/x/tagsinput/jquery-autocomplete/jquery.autocomplete.min.js'></script>
@@ -25,9 +20,15 @@
alert("Removed a tag: " + tag);
}
function onChangeTag(input,tag) {
alert("Changed a tag: " + tag);
}
$(function() {
$('#tags_1').tagsInput();
$('#tags_1').tagsInput({width:'auto'});
$('#tags_2').tagsInput({
width: 'auto',
onChange: function(elem, elem_tags)
{
var languages = ['php','ruby','javascript'];
@@ -39,35 +40,29 @@
}
});
$('#tags_3').tagsInput({
width: 'auto',
//autocomplete_url:'test/fake_plaintext_endpoint.html' //jquery.autocomplete (not jquery ui)
autocomplete_url:'test/fake_json_endpoint.html' // jquery ui autocomplete requires a json endpoint
});
// Uncomment this line to see the callback functions in action
// $('input.tags').tagsInput({onAddTag:onAddTag,onRemoveTag:onRemoveTag});
// $('input.tags').tagsInput({onAddTag:onAddTag,onRemoveTag:onRemoveTag,onChange: onChangeTag});
// Uncomment this line to see an input with no interface for adding new tags.
// $('input.tags').tagsInput({interactive:false});
});
</script>
</head>
<body>
<header>
jQuery Tags Input!
</header>
<section>
<form>
<input id="tags_1" type="text" class="tags" value="foo,bar,baz,roffle" />
<p><label>Defaults:</label>
<input id="tags_1" type="text" class="tags" value="foo,bar,baz,roffle" /></p>
<label>Technologies: (Programming languages in yellow)</label>
<input id="tags_2" type="text" class="tags" value="php,ios,javascript,ruby,android,kindle" />
<p><label>Technologies: (Programming languages in yellow)</label>
<input id="tags_2" type="text" class="tags" value="php,ios,javascript,ruby,android,kindle" /></p>
<label>Autocomplete:</label>
<input id='tags_3' type='text' class='tags'>
<p><label>Autocomplete:</label>
<input id='tags_3' type='text' class='tags'></p>
</form>
</section>
</body>
</form>
+1
View File
@@ -4,3 +4,4 @@ div.tagsinput span.tag a { font-weight: bold; color: #82ad2b; text-decoration:no
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;}
+130 -55
View File
@@ -1,6 +1,6 @@
/*
jQuery Tags Input Plugin 1.3.1
jQuery Tags Input Plugin 1.3.3
Copyright (c) 2011 XOXCO, Inc
@@ -18,11 +18,65 @@
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, '&amp;').replace(/\s/g,' ').replace(/</g, '&lt;').replace(/>/g, '&gt;');
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),
val = '',
input = $(this),
testSubject = $('<tester/>').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');
}
input.data('minwidth', minWidth);
input.data('maxwidth', maxWidth);
input.data('tester_id', testerId);
input.css('width', minWidth);
};
$.fn.addTag = function(value,options) {
var options = jQuery.extend({focus:false,callback:true},options);
options = jQuery.extend({focus:false,callback:true},options);
this.each(function() {
id = $(this).attr('id');
var id = $(this).attr('id');
var tagslist = $(this).val().split(delimiter[id]);
if (tagslist[0] == '') {
@@ -32,11 +86,15 @@
value = jQuery.trim(value);
if (options.unique) {
skipTag = $(tagslist).tagExist(value);
var skipTag = $(this).tagExist(value);
if(skipTag == true) {
//Marks fake input as not_valid to let styling it
$('#'+id+'_tag').addClass('not_valid');
}
} else {
skipTag = false;
var skipTag = false;
}
if (value !='' && skipTag != true) {
$('<span>').addClass('tag').append(
$('<span>').text(value).append('&nbsp;&nbsp;'),
@@ -58,18 +116,19 @@
$('#'+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(value);
f.call(this, value);
}
if(tags_callbacks[id] && tags_callbacks[id]['onChange'])
{
var i = tagslist.length;
var f = tags_callbacks[id]['onChange'];
f($(this), tagslist[i]);
f.call(this, $(this), tagslist[i-1]);
}
}
$.fn.tagsInput.updateTagsField(this,tagslist);
});
@@ -79,11 +138,10 @@
$.fn.removeTag = function(value) {
value = unescape(value);
this.each(function() {
id = $(this).attr('id');
var id = $(this).attr('id');
var old = $(this).val().split(delimiter[id]);
$('#'+id+'_tagsinput .tag').remove();
str = '';
for (i=0; i< old.length; i++) {
@@ -96,7 +154,7 @@
if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) {
var f = tags_callbacks[id]['onRemoveTag'];
f(value);
f.call(this, value);
}
});
@@ -104,15 +162,15 @@
};
$.fn.tagExist = function(val) {
if (jQuery.inArray(val, $(this)) == -1) {
return false; /* Cannot find value in array */
} else {
return true; /* Value found */
}
var id = $(this).attr('id');
val = val.toLowerCase();
var tagslist = $(this).val().toLowerCase().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) {
id = $(this).attr('id');
$('#'+id+'_tagsinput .tag').remove();
$.fn.tagsInput.importTags(this,str);
}
@@ -129,17 +187,22 @@
'delimiter':',',
'unique':true,
removeWithBackspace:true,
placeholderColor:'#666666'
placeholderColor:'#666666',
autosize: true,
comfortZone: 20,
inputPadding: 6*2
},options);
this.each(function() {
if (settings.hide) {
$(this).hide();
}
id = $(this).attr('id')
var id = $(this).attr('id');
if (!id || delimiter[$(this).attr('id')]) {
id = $(this).attr('id', 'tags' + new Date().getTime()).attr('id');
}
data = jQuery.extend({
var data = jQuery.extend({
pid:id,
real_input: '#'+id,
holder: '#'+id+'_tagsinput',
@@ -147,7 +210,6 @@
fake_input: '#'+id+'_tag'
},settings);
delimiter[id] = data.delimiter;
if (settings.onAddTag || settings.onRemoveTag || settings.onChange) {
@@ -168,14 +230,16 @@
$(markup).insertAfter(this);
$(data.holder).css('width',settings.width);
$(data.holder).css('height',settings.height);
$(data.holder).css('min-height',settings.height);
$(data.holder).css('height','100%');
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).css('color',settings.placeholderColor);
$(data.fake_input).resetAutosize(settings);
$(data.holder).bind('click',data,function(event) {
$(event.data.fake_input).focus();
@@ -189,27 +253,26 @@
});
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) {
d = data + "";
$(event.data.real_input).addTag(d,{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;
});
}
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
@@ -228,13 +291,17 @@
}
// if user types a comma, create a new tag
$(data.fake_input).bind('keypress',data,function(event) {
$(data.fake_input).bind('keypress',data,function(event) {
if (event.which==event.data.delimiter.charCodeAt(0) || event.which==13 ) {
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)
@@ -247,11 +314,19 @@
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');
}
});
}
} // if settings.interactive
return false;
});
return this;
@@ -259,13 +334,13 @@
};
$.fn.tagsInput.updateTagsField = function(obj,tagslist) {
id = $(obj).attr('id');
var id = $(obj).attr('id');
$(obj).val(tagslist.join(delimiter[id]));
};
$.fn.tagsInput.importTags = function(obj,val) {
$(obj).val('');
id = $(obj).attr('id');
var id = $(obj).attr('id');
var tags = val.split(delimiter[id]);
for (i=0; i<tags.length; i++) {
$(obj).addTag(tags[i],{focus:false,callback:false});
@@ -273,7 +348,7 @@
if(tags_callbacks[id] && tags_callbacks[id]['onChange'])
{
var f = tags_callbacks[id]['onChange'];
f(obj, tags[i]);
f.call(obj, obj, tags[i]);
}
};
+1 -1
View File
File diff suppressed because one or more lines are too long