forked from xoxco/jQuery-Tags-Input
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.tagsinput.min.js
15 lines (9 loc) · 7.51 KB
/
jquery.tagsinput.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
jQuery Tags Input Plugin 1.3.3
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
*/(function(e){var t=new Array,n=new Array;e.fn.doAutosize=function(t){var n=e(this).data("minwidth"),r=e(this).data("maxwidth"),i="",s=e(this),o=e("#"+e(this).data("tester_id"));if(i===(i=s.val()))return;var u=i.replace(/&/g,"&").replace(/\s/g," ").replace(/</g,"<").replace(/>/g,">");o.html(u);var a=o.width(),f=a+t.comfortZone>=n?a+t.comfortZone:n,l=s.width(),c=f<l&&f>=n||f>n&&f<r;c&&s.width(f)},e.fn.resetAutosize=function(t){var n=e(this).data("minwidth")||t.minInputWidth||e(this).width(),r=e(this).data("maxwidth")||t.maxInputWidth||e(this).closest(".tagsinput").width()-t.inputPadding,i="",s=e(this),o=e("<tester/>").css({position:"absolute",top:-9999,left:-9999,width:"auto",fontSize:s.css("fontSize"),fontFamily:s.css("fontFamily"),fontWeight:s.css("fontWeight"),letterSpacing:s.css("letterSpacing"),whiteSpace:"nowrap"}),u=e(this).attr("id")+"_autosize_tester";!e("#"+u).length>0&&(o.attr("id",u),o.appendTo("body")),s.data("minwidth",n),s.data("maxwidth",r),s.data("tester_id",u),s.css("width",n)},e.fn.addTag=function(r,i){return i=jQuery.extend({focus:!1,callback:!0},i),this.each(function(){var s=e(this).attr("id"),o=e(this).val().split(t[s]);o[0]==""&&(o=new Array),r=jQuery.trim(r);if(i.unique){var u=e(this).tagExist(r);u==1&&e("#"+s+"_tag").addClass("not_valid")}else var u=!1;if(r!=""&&u!=1){e("<span>").addClass("tag").append(e("<span>").text(r).append(" "),e("<a>",{href:"#",title:"Removing tag",text:"x"}).click(function(){return e("#"+s).removeTag(escape(r))})).insertBefore("#"+s+"_addTag"),o.push(r),e("#"+s+"_tag").val(""),i.focus?e("#"+s+"_tag").focus():e("#"+s+"_tag").blur(),e.fn.tagsInput.updateTagsField(this,o);if(i.callback&&n[s]&&n[s].onAddTag){var a=n[s].onAddTag;i.obj?a.call(this,i.obj):a.call(this,r)}if(n[s]&&n[s].onChange){var f=o.length,a=n[s].onChange;a.call(this,e(this),o[f-1])}}}),!1},e.fn.removeTag=function(r){return r=unescape(r),this.each(function(){var s=e(this).attr("id"),o=e(this).val().split(t[s]);e("#"+s+"_tagsinput .tag").remove(),str="";for(i=0;i<o.length;i++)o[i]!=r&&(str=str+t[s]+o[i]);e.fn.tagsInput.importTags(this,str);if(n[s]&&n[s].onRemoveTag){var u=n[s].onRemoveTag;u.call(this,r)}}),!1},e.fn.tagExist=function(n){var r=e(this).attr("id"),i=e(this).val().split(t[r]);return jQuery.inArray(n,i)>=0},e.fn.importTags=function(t){id=e(this).attr("id"),e("#"+id+"_tagsinput .tag").remove(),e.fn.tagsInput.importTags(this,t)},e.fn.tagsInput=function(r){var i=jQuery.extend({interactive:!0,defaultText:"add a tag",minChars:0,autocomplete:{selectFirst:!1},hide:!0,delimiter:",",unique:!0,removeWithBackspace:!0,placeholderColor:"#666666",autosize:!0,comfortZone:80,inputPadding:12},r);return e('<style type="text/css">div.tagsinput { border:1px solid #CCC; background: #FFF; padding:2px; overflow-y: auto; display: inline-table}div.tagsinput span.tag { border: 1px solid #CAD8F3;; -moz-border-radius:2px; -webkit-border-radius:2px; display: block; float: left; padding: 2px; text-decoration:none; background: #DEE7F8; margin-right: 2px; margin-bottom:1px;font-family: helvetica; font-size:13px;}div.tagsinput span.tag a { font-weight: bold;; text-decoration:none; font-size: 11px; } div.tagsinput input { margin:0px; font-family: helvetica; font-size: 13px; border:1px solid transparent; padding:1px; background: transparent; color: #000; outline:0px; margin-right:1px; margin-bottom:1px; }div.tagsinput div { display:block; float: left; }.tags_clear { clear: both; width: 100%; height: 0px; } .not_valid {background: #FBD8DB !important; color: #90111A !important;}</style>').appendTo(e("head")),this.each(function(){i.hide&&e(this).hide();var r=e(this).attr("id");if(!r||t[e(this).attr("id")])r=e(this).attr("id","tags"+(new Date).getTime()).attr("id");var s=jQuery.extend({pid:r,real_input:"#"+r,holder:"#"+r+"_tagsinput",input_wrapper:"#"+r+"_addTag",fake_input:"#"+r+"_tag"},i);t[r]=s.delimiter;if(i.onAddTag||i.onRemoveTag||i.onChange)n[r]=new Array,n[r].onAddTag=i.onAddTag,n[r].onRemoveTag=i.onRemoveTag,n[r].onChange=i.onChange;var o='<div id="'+r+'_tagsinput" class="tagsinput"><div id="'+r+'_addTag">';i.interactive&&(o=o+'<input id="'+r+'_tag" value="" placeholder="'+i.defaultText+'" data-default="'+i.defaultText+'" />'),o=e(o+'</div><div class="tags_clear"></div></div>'),o.insertAfter(this);var u=o.find(s.fake_input),a=o,f=e(this);a.css("width",i.width),a.css("min-height",i.min_height||i.height),a.css("height",i.height),f.val()!=""&&e.fn.tagsInput.importTags(f,f.val());if(i.interactive){u.val(u.attr("data-default")),u.css("color",i.placeholderColor),u.resetAutosize(i),a.bind("click",s,function(t){e(t.data.fake_input).focus()}),u.bind("focus",s,function(t){e(t.data.fake_input).val()==e(t.data.fake_input).attr("data-default")&&e(t.data.fake_input).val(""),e(t.data.fake_input).css("color","#000000")});if(i.autocomplete_url!=undefined){autocomplete_options={source:i.autocomplete_url};for(attrname in i.autocomplete)autocomplete_options[attrname]=i.autocomplete[attrname];jQuery.Autocompleter!==undefined?(u.autocomplete(i.autocomplete_url,i.autocomplete),u.bind("result",s,function(t,n,s){n&&e("#"+r).addTag(n[0]+"",{focus:!0,unique:i.unique})})):jQuery.ui.autocomplete!==undefined&&(u.autocomplete(autocomplete_options),u.bind("autocompleteselect",s,function(t,n){return e(t.data.real_input).addTag(n.item.value,{focus:!0,unique:i.unique}),!1}))}else i.typeahead!=undefined?(u.attr("data-provide","typeahead"),u.typeahead({onselect:function(e){f.addTag(e[i.typeahead.property],{focus:!0,unique:i.unique,obj:e}),i.typeahead.onselect&&i.typeahead.onselect(e)},source:function(t,n){n!=""&&e.ajax({url:i.typeahead.url+"/"+n,success:function(e){t.process(e)}})},property:i.typeahead.property,items:i.typeahead.items||8,up:i.typeahead.up})):u.bind("blur",s,function(t){var n=e(this).attr("data-default");return e(t.data.fake_input).val()!=""&&e(t.data.fake_input).val()!=n?t.data.minChars<=e(t.data.fake_input).val().length&&(!t.data.maxChars||t.data.maxChars>=e(t.data.fake_input).val().length)&&e(t.data.real_input).addTag(e(t.data.fake_input).val(),{focus:!0,unique:i.unique}):(e(t.data.fake_input).val(e(t.data.fake_input).attr("data-default")),e(t.data.fake_input).css("color",i.placeholderColor)),!1});u.bind("keypress",s,function(t){if(t.which==t.data.delimiter.charCodeAt(0)||t.which==13)return t.preventDefault(),t.data.minChars<=e(t.data.fake_input).val().length&&(!t.data.maxChars||t.data.maxChars>=e(t.data.fake_input).val().length)&&e(t.data.real_input).addTag(e(t.data.fake_input).val(),{focus:!0,unique:i.unique}),e(t.data.fake_input).resetAutosize(i),!1;t.data.autosize&&e(t.data.fake_input).doAutosize(i)}),s.removeWithBackspace&&u.bind("keydown",function(t){if(t.keyCode==8&&e(this).val()==""){t.preventDefault();var n=e(this).closest(".tagsinput").find(".tag:last").text(),r=e(this).attr("id").replace(/_tag$/,"");n=n.replace(/[\s]+x$/,""),e("#"+r).removeTag(escape(n)),e(this).trigger("focus")}}),u.blur(),s.unique&&u.keydown(function(t){(t.keyCode==8||String.fromCharCode(t.which).match(/\w+|[áéíóúÁÉÍÓÚñÑ,/]+/))&&e(this).removeClass("not_valid")})}}),this},e.fn.tagsInput.updateTagsField=function(n,r){var i=e(n).attr("id");e(n).val(r.join(t[i]))},e.fn.tagsInput.importTags=function(r,s){e(r).val("");var o=e(r).attr("id"),u=s?s.split(t[o]):[];for(i=0;i<u.length;i++)e(r).addTag(u[i],{focus:!1,callback:!1});if(n[o]&&n[o].onChange){var a=n[o].onChange;a.call(r,r,u[i])}}})(jQuery);