-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtagging.min.js
1 lines (1 loc) · 5.71 KB
/
tagging.min.js
1
/*! taggingJS - v1.3.3 - 2015-05-04 */(function(e,l,g,p){var n=function(b,a){this.elem=b;this.$elem=e(b);this.options=a;this.tags=[]};n.prototype={keys:{add:{comma:188,enter:13,spacebar:32},remove:{del:46,backspace:8}},defaults:{"case-sensitive":!1,"close-char":"×","close-class":"tag-i","edit-on-delete":!0,"forbidden-chars":[".","_","?"],"forbidden-chars-callback":l.alert,"forbidden-chars-text":"Forbidden character:","forbidden-words":[],"forbidden-words-callback":l.alert,"forbidden-words-text":"Forbidden word:","no-backspace":!1,"no-comma":!1,"no-del":!1,"no-duplicate":!0,"no-duplicate-callback":l.alert,"no-duplicate-text":"Duplicate tag:","no-enter":!1,"no-spacebar":!1,"pre-tags-separator":", ","tag-box-class":"tagging","tag-box-editable-class":"editable","tag-char":"#","tag-class":"tag","tags-input-name":"tag","tag-on-blur":!0,"tags-limit":0,"type-zone-class":"type-zone"},add:function(b){var a,d,c,f,m;c=this;if(e.isArray(b))return e.each(b,function(){c.add(this+"")});m=c.config["forbidden-words"];b||(b=c.valInput(),c.emptyInput());if(!b||!b.length)return!1;c.config["case-sensitive"]||(b=b.toLowerCase());for(d=m.length;d--;)if(f=b.indexOf(m[d]),0<=f)return c.emptyInput(),d=c.config["forbidden-words-callback"],f=c.config["forbidden-words-text"],c.throwError(d,f,b);if(c.config["no-duplicate"])for(d=c.tags.length;d--;)if(c.tags[d].pure_text===b)return c.emptyInput(),d=c.config["no-duplicate-callback"],f=c.config["no-duplicate-text"],c.throwError(d,f,b);a=e(g.createElement("div")).addClass(c.config["tag-class"]).html("<span>"+c.config["tag-char"]+"</span> "+b);e(g.createElement("input")).attr("type","hidden").attr("name",c.config["tags-input-name"]+"[]").val(b).appendTo(a);e(g.createElement("a")).attr("role","button").addClass(c.config["close-class"]).html(c.config["close-char"]).click(function(){c.remove(a)}).appendTo(a);a.pure_text=b;c.tags.push(a);c.$type_zone.before(a);0<c.config["tags-limit"]&&c.tags.length>=c.config["tags-limit"]&&(c.$elem.removeClass(c.config["tag-box-editable-class"]),c.$type_zone.hide());c.$elem.trigger("add:after",[b,c]);return!0},addSpecialKeys:function(b){var a,d,c,f;a=this;f=b[0];c=b[1];b={};if(e.isArray(c))return e.each(c,function(){a.addSpecialKeys([f,this])});if(!c&&c.constructor!==Object)return"Error -> The second argument is not an Object!";for(d in c)c.hasOwnProperty(d)&&c[d]===+c[d]&&c[d]===(c[d]|0)&&e.extend(b,c);a.keys[f]=e.extend({},b,a.keys[f]);return a.keys[f]},destroy:function(){this.$elem.find("."+this.config["type-zone-class"]).remove();this.$elem.find("."+this.config["tag-class"]).remove();this.$elem.data("tag-box",null);return!0},emptyInput:function(){this.focusInput();return this.valInput("")},focusInput:function(){return this.$type_zone.focus()},getDataOptions:function(){var b,a,d;d={};for(b in this.defaults)(a=this.$elem.data(b))&&(d[b]=a);return d},getSpecialKeys:function(){return e.extend({},this.keys.add,this.keys.remove)},getSpecialKeysD:function(){return this.keys},getTags:function(){var b,a,d;d=this.tags.length;b=[];for(a=0;a<d;a+=1)b.push(this.tags[a].pure_text);return b},getTagsObj:function(){return this.tags},init:function(){var b,a,d;a=this;a.config=e.extend({},a.defaults,a.options,a.getDataOptions());b=a.$elem.text();a.$elem.empty();a.$type_zone=e(g.createElement("input")).addClass(a.config["type-zone-class"]).attr("contenteditable",!0);a.$elem.addClass(a.config["tag-box-class"]).addClass(a.config["tag-box-editable-class"]).append(a.$type_zone);a.$type_zone.keydown(function(c){var b,d,e,g,k,h;e=a.getSpecialKeys();k=a.config["forbidden-chars"];h=a.valInput();g=c.which;if(h){for(e=k.length;e--;)if(d=h.indexOf(k[e]),0<=d)return c.preventDefault(),h=h.replace(k[e],""),a.focusInput(),a.valInput(h),c=a.config["forbidden-chars-callback"],b=a.config["forbidden-chars-text"],a.throwError(c,b,k[e]);for(b in a.keys.add)if(g===a.keys.add[b]&&!a.config["no-"+b])return c.preventDefault(),a.add()}else for(b in e)if(g===e[b]){if(a.keys.add[b]){c.preventDefault();break}if(a.keys.remove[b]&&!a.config["no-"+b])return c.preventDefault(),a.remove()}return!0});a.config["tag-on-blur"]&&a.$type_zone.focusout(function(){return(d=a.valInput())&&d.length?a.add():!1});a.$elem.on("click",function(){a.focusInput()});a.refresh(b);return a},refresh:function(b){var a,d;a=this;d=a.config["pre-tags-separator"];b=b||a.getTags().join(d);a.reset();e.each(b.split(d),function(){a.add(this+"")});return!0},remove:function(b){var a,d,c;a=this;if(e.isArray(b))return e.each(b,function(){a.remove(this+"")});if("string"===typeof b&&(d=b,b=a.$elem.find("input[value="+d+"]").parent(),!b.length))return"Error -> Tag not found";if(b)for(c=a.tags.length;c--;)a.tags[c][0].innerHTML===b[0].innerHTML&&a.tags.splice(c,1);else b=a.tags.pop();d=d||b.pure_text;b.remove();a.config["edit-on-delete"]&&(a.emptyInput(),a.valInput(b.pure_text));0<a.config["tags-limit"]&&a.$type_zone.not(":visible")&&a.tags.length<a.config["tags-limit"]&&(a.$elem.addClass(a.config["tag-box-editable-class"]),a.$type_zone.show());a.$elem.trigger("remove:after",[d,a]);return b},removeAll:function(){return this.reset()},removeSpecialKeys:function(b){var a,d,c;a=this;c=b[0];b=b[1];if(e.isArray(b))return e.each(b,function(){a.removeSpecialKeys([c,this])});for(d in a.keys[c])a.keys[c].hasOwnProperty(d)&&a.keys[c][d]===b&&(a.keys[c][d]=p);return a.keys[c]},reset:function(){for(;this.tags.length;)this.remove(this.tags[this.tags.length]);this.emptyInput();return this.tags},throwError:function(b,a,d){return b([a+" '"+d+"'."])},valInput:function(b){return null==b?this.$type_zone.val():this.$type_zone.val(b)}};e.fn.tagging=function(b,a){var d=[];this.each(function(){var c,f;c=e(this);(f=c.data("tag-box"))?(c=f[b](a))&&d.push(c):(f=new n(this,b),c.data("tag-box",f),f.init(),d.push(f.$elem))});return"string"===typeof b?1<d.length?d:d[0]:d}})(window.jQuery,window,document);