-
Notifications
You must be signed in to change notification settings - Fork 1
/
TagInput.min.js
7 lines (7 loc) · 4.65 KB
/
TagInput.min.js
1
2
3
4
5
6
7
/*
Author : Divya Mamgai
TagInput.js
Version : 1.1
2016
*/
(function(f,b,e,g){if(!Array.prototype.find){Array.prototype.find=function(d){if(this==null){throw new TypeError("Array.prototype.find called on null or undefined")}if(typeof d!=="function"){throw new TypeError("predicate must be a function")}var m=Object(this),k=m.length>>>0,h=arguments[1],l;for(var j=0;j<k;j++){l=m[j];if(d.call(h,l,j,m)){return l}}return undefined}}var c=f('<div class="TagItem Hide"><span class="TagText"></span><span class="RemoveTag"></span></div>'),a=f('<input type="hidden">');b.TagInput=function(l){var n=f(l),o=n.attr("id")||Math.round(Math.random()*1000000000),k=f('<div class="TagInput">\n <input type="text" class="TagInputText">\n</div>').insertAfter(n),j=f("label[for="+o+"]",g),q=k.find(".TagInputText"),p={},h,m=[],d={ToString:function(){var r="",u=m.length,t=u-1;for(var s=0;s<u;s++){r+=m[s].Text+(s===t?"":p.Glue)}return r},UpdateValue:function(){var r=d.ToString();n.val(r);return r},AddTag:function(t){if((t!==undefined)&&(typeof t==="string")){var r=f.trim(t.replace(h," "));if(r.length>=p.MinLength){if(m.find(i.FindTag,r)===undefined){var s=c.clone();s.find(".TagText").html(r);s.find(".RemoveTag").addClass(p.RemoveIcon);m.push({Text:r,$Tag:s});q.before(s);setTimeout(function(){s.removeClass("Hide")},21);d.UpdateValue();n.trigger("TagInput:AddTag",[m,s,r]);return true}}}return false},RemoveTag:function(s,t){var v=t||(s.index()-1),r=m[v];if(r!==undefined){var u=r.Text;r.$Tag.remove();m.splice(v,1);d.UpdateValue();n.trigger("TagInput:RemoveTag",[m,u]);return true}return false},GetArray:function(){return m},GetTextArray:function(){var r=[],t=m.length;for(var s=0;s<t;s++){r.push(m[s].Text)}return r},GetTagCount:function(){return m.length},ConvertToHiddenInput:function(){var u=m.length,r=n.attr("name")+"[]",t=a.clone().attr("name",r);for(var s=0;s<u;s++){k.before(t.clone().val(m[s].Text))}n.attr("disabled","disabled");return r},Reset:function(){var s=m.length;for(var r=0;r<s;r++){m[r].$Tag.remove()}m=[];n.val("");q.val("");j.removeClass(p.LabelAnimationClass);return d},Private:undefined},i={FindTag:function(r){return r.Text==this},TagInputOnClick:function(){q.focus()},TagInputOnMouseEnter:function(){k.addClass("Focus")},TagInputOnMouseLeave:function(){if(q.is(":focus")===false){if(m.length===0){j.removeClass(p.LabelAnimationClass)}k.removeClass("Focus")}},RemoveTagOnClick:function(r){r.stopPropagation();r.preventDefault();d.RemoveTag(f(this).parent());if(m.length===0){j.removeClass(p.LabelAnimationClass)}},TagItemOnClick:function(s){s.stopPropagation();s.preventDefault();if(q.val().length===0){var u=f(this),t=u.index()-1,r=m[t].Text;q.val(r).focus();d.RemoveTag(u,t)}},TagInputTextOnFocus:function(){if(m.length===0&&q.val().length===0){j.addClass(p.LabelAnimationClass)}k.addClass("Focus")},TagInputTextOnBlur:function(){if(m.length===0&&q.val().length===0){j.removeClass(p.LabelAnimationClass)}k.removeClass("Focus")},TagInputTextOnKeyDown:function(r){if(r.keyCode===13){r.stopPropagation();r.preventDefault();if(d.AddTag(q.val())===true){q.val("")}}else{if(r.keyCode===8){if(m.length>0){if(q.val().length===0){d.RemoveTag(q.prev())}}}else{if(r.key===p.Glue){r.stopPropagation();r.preventDefault()}}}},ElementValidation:function(){k.val(String(k.val()).replace(h," "))},ProcessValue:function(){var u=n.val(),t=u.split(p.Glue),s=t.length;for(var r=0;r<s;r++){d.AddTag(t[r])}if(m.length>0){j.addClass(p.LabelAnimationClass);d.UpdateValue()}},Initialize:function(){p={MinLength:n.attr("data-MinLength")||1,MaxLength:n.attr("data-MaxLength"),Glue:n.attr("data-Glue")||",",RemoveIcon:n.attr("data-RemoveIcon")||"glyphicon glyphicon-remove",LabelAnimationClass:n.attr("data-LabelAnimationClass")||"Hide"};if(p.MinLength<=0){p.MinLength=1}if(p.Glue===" "){p.Glue=","}h=new RegExp(p.Glue,"g");if(j.length===0){n.before((j=f('<label for="'+o+'">Enter Tags</label>')))}else{j.prependTo(k)}k.on("click",i.TagInputOnClick).on("mouseenter",i.TagInputOnMouseEnter).on("mouseleave",i.TagInputOnMouseLeave).on("click",".RemoveTag",i.RemoveTagOnClick).on("click",".TagItem",i.TagItemOnClick);q.attr({minlength:p.MinLength,maxlength:p.MaxLength}).on("focus",i.TagInputTextOnFocus).on("blur",i.TagInputTextOnBlur).on("keydown",i.TagInputTextOnKeyDown).on("input",i.ElementValidation).on("change",i.ElementValidation);n.attr("tabindex",-1).addClass("TagInputElement").on("focus",function(r){r.stopPropagation();r.preventDefault();q.focus()}).appendTo(k);i.ProcessValue()}};d.Private=i;i.Initialize();return d};f.fn.TagInput=function(){return this.each(function(){if(f.data(this,"TagInput")===undefined){f.data(this,"TagInput",new TagInput(this))}})};f(function(){f("input[data-TagInput]",g).TagInput()})})(jQuery,window,jQuery(window),document);