forked from pulsir/pulsir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagbox.min.js
18 lines (18 loc) · 8.29 KB
/
tagbox.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var q=!0,t=!1;
(function(m,F,y){function z(f,e){function d(){if(i.visible){var a=i.m();a?g(a):e.allowNew&&l.val()&&g(e.createNew(l.val()))}}function g(b){b=new G(b,e);b.b.insertBefore(l);b.b.css("maxWidth",a.c.width());a.a.push(b);z&&l.val("");c(q);i.hide();l.focus();s()}function h(b,e){b.remove();var d=a.a.indexOf(b);a.a.splice(d,1);b===n&&(n=y);s();c(q);e&&(-1===e?o(a.a[d-1]):1===e&&o(a.a[d]));u()}function s(){for(var c=[],d=0;d<a.a.length;d+=1)c.push(a.a[d].value);b.val(c.join(e.delimiter))}function o(a){n&&n.e();
n!==a?(n=a,a.select()):n=y}function p(){n&&(n.e(),n=y)}function j(){var a=e.searchIn;"string"==typeof a&&(a=[a]);if(m.isArray(a)){for(var c={},b=0;b<a.length;b+=1)c[a[b]]=10;return c}return a}function r(c){for(var b=0;b<a.a.length;b+=1)if(a.a[b].item===c)return q;return t}function u(){var c=e.items,b=[],d=l.val(),f=j();a.c.removeClass("invalid");if(a.a.length===e.maxItems)i.hide(),l.removeAttr("placeholder");else if(l.attr("placeholder",D),""===d&&!e.autoShow)i.hide();else{if(""!==d){for(var k=0;k<
c.length;k+=1){var h={item:c[k],i:H(c[k],d,f)};0<h.i&&(e.allowDuplicates||!r(h.item))&&b.push(h)}b=b.sort(function(a,b){return b.i-a.i});for(k=0;k<b.length;k+=1)b[k]=b[k].item}else for(k=0;k<c.length;k+=1)(e.allowDuplicates||!r(c[k]))&&b.push(c[k]);i.q(b);i.p(d);i.l(v)}}function c(b){a.k==l.val()&&b!==q||(p(),a.k=l.val(),E.text(a.k),l.width(0),l.width(Math.min(a.c.width()-16,Math.max(a.c.width()-l.offset().left+a.c.offset().left+16,E.width(),1))),i.l(v))}var a=this;a.a=[];var b=a.input=m(f).hide();
a.options=e;var v=a.c=m('<div class="tagbox-wrapper" />').click(function(a){var b=m(a.target).closest("input, .tagbox-token, .tagbox-wrapper");b.is(".tagbox-token")?m(a.target).is("a")?h(b.data("token")):o(b.data("token")):p();l.focus()}).insertBefore(a.input);b.on("invalid",function(b){b.preventDefault();a.c.addClass("invalid")});var D=b.attr("placeholder"),l=m('<input type="text" />').attr({autocomplete:b.attr("autocomplete"),spellcheck:b.attr("spellcheck"),autocapitalize:b.attr("autocapitalize"),
placeholder:D}).on("keyup keydown blur update change",c).on("keypress",function(a){if(13===a.keyCode&&l.val())return a.preventDefault(),t}).on("blur",function(){d();setTimeout(function(){a.f||i.hide();v.removeClass("focus")},50)}).on("keydown",function(b){var c=l.val().length==l[0].selectionStart,f=0===l[0].selectionEnd,g=b.keyCode,k=t;if(b.ctrlKey||b.metaKey)k=q;if(37===g){if(n)return n===a.a[0]?p():o(a.a[a.a.indexOf(n)-1]),k;f&&a.a.length&&o(a.a[a.a.length-1])}if(39===g){if(n)return n===a.a[a.a.length-
1]?p():o(a.a[a.a.indexOf(n)+1]),k;c&&a.a.length&&o(a.a[0])}if(n&&(46===g||8===g))return h(n,8===g?-1:1),k;if(8===g&&f&&a.a.length)return o(a.a[a.a.length-1]),k;if(i.visible){if(38===g)return i.o(),k;if(40===g)return i.n(),k;if(39==g&&c||13==g||9==g)return d(),k}else if(l.val()&&13==g)return k;if(a.a.length===e.maxItems&&9!=b.keyCode)return l.val(""),k;setTimeout(u,10)}).on("focus",function(){v.addClass("focus");u()}).appendTo(v),E=m("<span />").appendTo(v).css({position:"absolute",left:-1E5,width:"auto",
display:"inline-block",whiteSpace:"nowrap",fontSize:b.css("fontSize"),fontFamily:b.css("fontFamily"),fontWeight:b.css("fontWeight"),fontVariant:b.css("fontVariant"),letterSpacing:b.css("letterSpacing")}),i=a.r=new I(a,e);i.b.appendTo(e.dropdownContainer);if(b.val())for(var A=e.items,B=b.val().split(e.delimiter),C,w=0;w<B.length;w+=1){C=t;for(var x=0;x<A.length;x+=1)if(A[x][e.valueField]==B[w]){g(A[x]);C=q;break}!C&&e.allowNew&&g(e.createNew(B[w]))}var z=q;c(q);m(F).on("resize",function(){c(q)});setTimeout(function(){c(q)},
500);a.j=g;var n}function G(f,e){var d=this,g=d.b=m('<div class="tagbox-token"><span></span><a>×</a></div>').data("token",d),h=e.tokenFormat;d.value="string"==typeof f?f:f[e.valueField];d.item=f;"string"==typeof h?("string"==typeof f&&(f={value:f}),g.children("span").html(h.replace(/\{\{([^}]*)\}\}/g,function(d,e){return f[e]}))):g.children("span").html(h(f));d.remove=function(){g.data("token",null);g.remove();d.item=null;d.b=null};d.select=function(){g.addClass("selected")};d.e=function(){g.removeClass("selected")}}
function I(f,e){function d(c,a){s&&s.e();r&&r.removeClass("selected");"number"!==typeof c&&(c=p.indexOf(c));0<=c?(s=p[c],s.select(),clearTimeout(u),a?u=setTimeout(function(){g(s.b)},80):g(s.b)):(s=t,r&&r.addClass("selected"));o=c}function g(c){var a=c.offset().top-j.offset().top;0>a?j.scrollTop(a+j.scrollTop()):a>j.innerHeight()-c.outerHeight()&&j.scrollTop(a+j.scrollTop()-j.innerHeight()+c.outerHeight())}var h=this,s,o,p,j=h.b=m('<div class="tagbox-dropdown"><div class="list"></div></div>').css({maxHeight:e.maxHeight}).hide();
if(e.allowNew)var r=m('<div class="tagbox-item new-item" />').prependTo(j).on("mousedown",function(){f.f=q}).on("mouseup",function(){f.f=t}).on("click",function(){f.j(e.createNew(h.h))});h.l=function(c){j.offset();var a=c.offset(),b=j.parent().offset();j.parent().is("body")&&(b={top:0,left:0});j.css({top:a.top-b.top+c.height()+1,left:a.left-b.left,width:c.outerWidth()})};h.show=function(){j.show();h.visible=q};h.hide=function(){j.hide();h.visible=t};h.m=function(){if(s)return s.item};h.n=function(){o===
p.length-1?e.allowNew&&h.h||0===p.length?d(-1):d(0):d(o+1)};h.o=function(){0===o?e.allowNew&&h.h?d(-1):d(p.length-1):(-1===o&&(o=p.length),d(o-1))};var u;h.p=function(c){r&&((h.h=c)?r.show().text(e.newText.replace(/\{\{txt\}\}/g,c)):r.hide())};h.q=function(c){j.find(".list").empty();p=[];if(0<c.length){for(var a=0;a<Math.min(c.length,e.maxListItems);a+=1){var b=new J(c[a],e);b.b.appendTo(j.find(".list"));b.b.on("mouseover",function(a){return function(){d(a,q)}}(b));b.b.on("mousedown",function(){f.f=
q}).on("mouseup",function(){f.f=t});b.b.on("click",function(a){return function(){f.j(a)}}(c[a]));p.push(b)}d(0)}else e.allowNew||m('<div class="tagbox-item empty"/>').text(e.emptyText).appendTo(j.find(".list")),d(-1);h.show()}}function J(f,e){var d=this.b=m('<div class="tagbox-item" />');e.itemClass&&d.addClass(e.itemClass);this.item=f;var g=e.rowFormat;"string"==typeof g?("string"==typeof f&&(f={value:f}),d.html(g.replace(/\{\{([^}]*)\}\}/g,function(e,d){return f[d]}))):d.html(g(f));this.select=
function(){d.addClass("selected")};this.e=function(){d.removeClass("selected")}}var H=function(){function f(c){return c.replace(/[\[\]\{\}\(\)\^\$\.\*\+\|]/g,function(a){return"\\"+a})}function e(c){return/[a-z]/.test(c)?m:/[A-Z]/.test(c)?h:/[0-9]/.test(c)?o:/[\/\-_\.]/.test(c)?p:j}function d(c,a,b){c=e(c);return r[c][e(a)]+0.4*r[c][e(b)]}function g(c,a,b){if(0===a.length)return 100*b/c.length;if(0===c.length||!c.slice(b).match(RegExp(("^.*"+f(a.split("").join("~~K~~"))+".*$").split("~~K~~").join(".*"),
"i")))return-1;for(var e=a.charAt(0),h=-1,l;b<c.length;b+=1)if(c.charAt(b).toLowerCase()===e.toLowerCase()){var j=g(c.substr(b),a.slice(1),1);if(-1!=j){var i=400/Math.max(1,b);0===c.substr(b).toLowerCase().indexOf(a.toLowerCase())&&(i+=3*a.length*a.length);i+=d(c.charAt(b),0===b?"/":c.charAt(b-1),b===c.length-1?"/":c.charAt(b+1));i+=j;if(i>h){h=i;l=[b];j=j.g||[];for(i=0;i<j.length;i+=1)l.push(j[i]+b)}}}return{d:h,valueOf:function(){return this.d},g:l}}var h=0,m=1,o=2,p=3,j=4,r=[[0,240,120,240,220],
[20,0,20,120,120],[140,140,0,140,140],[120,120,120,0,120],[120,120,120,160,0]],u=function(c,a){var b=RegExp("["+c+"]","g"),e={},d;lookup=function(a){return e[a]||a};for(d=0;d<c.length;d+=1)e[c.charAt(d)]=a.charAt(d);return function(a){return a.replace(b,lookup)}}("\u00e0\u00e1\u00e2\u00e3\u00e4\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee\u00ef\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f9\u00fa\u00fb\u00fc\u00fd\u00ff\u00c0\u00c1\u00c2\u00c3\u00c4\u00c7\u00c8\u00c9\u00ca\u00cb\u00cc\u00cd\u00ce\u00cf\u00d1\u00d2\u00d3\u00d4\u00d5\u00d6\u00d9\u00da\u00db\u00dc\u00dd",
"aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY");return function(c,a,b){if("string"==typeof c)return g(u(c),u(a),0);var d={d:0,valueOf:function(){return this.d},g:{}},e;for(e in b)if(b.hasOwnProperty(e)&&c.hasOwnProperty(e)){var f=g(u(c[e]),u(a),0);d.d+=b[e]*f;d.g[e]=0<f?f.g:[]}return d}}();m.fn.tagbox=function(f){var e=m.extend({},{maxHeight:200,maxListItems:20,rowFormat:"{{value}}",tokenFormat:"{{value}}",valueField:"value",delimiter:",",allowDuplicates:t,createNew:function(d){return{value:d}},
allowNew:t,newText:"{{txt}}",emptyText:"Not Found",dropdownContainer:"body",maxItems:-1,autoShow:t},f);return this.each(function(){if(!m(this).data("tagbox")){var d=new z(this,e);m(this).data("tagbox",d)}})}})(jQuery,window);