mirror of
https://github.com/SickGear/SickGear.git
synced 2024-12-27 05:03:37 +00:00
40 lines
14 KiB
JavaScript
40 lines
14 KiB
JavaScript
|
/*
|
||
|
* jQuery Plugin: Tokenizing Autocomplete Text Entry
|
||
|
* Version 1.6.2
|
||
|
*
|
||
|
* Copyright (c) 2009 James Smith (http://loopj.com)
|
||
|
* Licensed jointly under the GPL and MIT licenses,
|
||
|
* choose which one suits your project best
|
||
|
*
|
||
|
*/
|
||
|
var $jscomp={scope:{},findInternal:function(a,h,g){a instanceof String&&(a=String(a));for(var q=a.length,l=0;l<q;l++){var A=a[l];if(h.call(g,A,l,a))return{i:l,v:A}}return{i:-1,v:void 0}}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(a,h,g){if(g.get||g.set)throw new TypeError("ES3 does not support getters and setters.");a!=Array.prototype&&a!=Object.prototype&&(a[h]=g.value)};
|
||
|
$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(a,h,g,q){if(h){g=$jscomp.global;a=a.split(".");for(q=0;q<a.length-1;q++){var l=a[q];l in g||(g[l]={});g=g[l]}a=a[a.length-1];q=g[a];h=h(q);h!=q&&null!=h&&$jscomp.defineProperty(g,a,{configurable:!0,writable:!0,value:h})}};
|
||
|
$jscomp.polyfill("Array.prototype.find",function(a){return a?a:function(a,g){return $jscomp.findInternal(this,a,g).v}},"es6-impl","es3");
|
||
|
(function(a){function h(a){return String(null===a||void 0===a?"":a).replace(A,function(a){return l[a]})}var g={method:"GET",queryParam:"q",searchDelay:300,minChars:1,propertyToSearch:"name",jsonContainer:null,contentType:"json",excludeCurrent:!1,excludeCurrentParameter:"x",prePopulate:null,processPrePopulate:!1,hintText:"Type in a search term",noResultsText:"No results",searchingText:"Searching...",deleteText:"×",animateDropdown:!0,placeholder:null,theme:null,zindex:999,resultsLimit:null,enableHTML:!1,
|
||
|
resultsFormatter:function(a){a=a[this.propertyToSearch];return"<li>"+(this.enableHTML?a:h(a))+"</li>"},tokenFormatter:function(a){a=a[this.propertyToSearch];return"<li><p>"+(this.enableHTML?a:h(a))+"</p></li>"},tokenLimit:null,tokenDelimiter:",",preventDuplicates:!1,tokenValue:"id",allowFreeTagging:!1,allowTabOut:!1,autoSelectFirstResult:!1,onResult:null,onCachedResult:null,onAdd:null,onFreeTaggingAdd:null,onDelete:null,onReady:null,idPrefix:"token-input-",disabled:!1},q={tokenList:"token-input-list",
|
||
|
token:"token-input-token",tokenReadOnly:"token-input-token-readonly",tokenDelete:"token-input-delete-token",selectedToken:"token-input-selected-token",highlightedToken:"token-input-highlighted-token",dropdown:"token-input-dropdown",dropdownItem:"token-input-dropdown-item",dropdownItem2:"token-input-dropdown-item2",selectedDropdownItem:"token-input-selected-dropdown-item",inputToken:"token-input-input-token",focused:"token-input-focused",disabled:"token-input-disabled"},l={"&":"&","<":"<",">":">",
|
||
|
'"':""","'":"'","/":"/"},A=/[&<>"'\/]/g,C={init:function(b,p){var h=a.extend({},g,p||{});return this.each(function(){a(this).data("settings",h);a(this).data("tokenInputObject",new a.TokenList(this,b,h))})},clear:function(){this.data("tokenInputObject").clear();return this},add:function(a){this.data("tokenInputObject").add(a);return this},remove:function(a){this.data("tokenInputObject").remove(a);return this},get:function(){return this.data("tokenInputObject").getTokens()},toggleDisabled:function(a){this.data("tokenInputObject").toggleDisabled(a);
|
||
|
return this},setOptions:function(b){a(this).data("settings",a.extend({},a(this).data("settings"),b||{}));return this},destroy:function(){if(this.data("tokenInputObject")){this.data("tokenInputObject").clear();var a=this.parent();a.empty();this.show();a.append(this);return this}}};a.fn.tokenInput=function(a){return C[a]?C[a].apply(this,Array.prototype.slice.call(arguments,1)):C.init.apply(this,arguments)};a.TokenList=function(b,p,g){function l(c){return a(b).data("settings").enableHTML?c:h(c)}function O(c){"boolean"===
|
||
|
typeof c?a(b).data("settings").disabled=c:a(b).data("settings").disabled=!a(b).data("settings").disabled;f.attr("disabled",a(b).data("settings").disabled);r.toggleClass(a(b).data("settings").classes.disabled,a(b).data("settings").disabled);m&&B(a(m),2);n.attr("disabled",a(b).data("settings").disabled)}function P(){null!==a(b).data("settings").tokenLimit&&x>=a(b).data("settings").tokenLimit&&(f.hide(),y())}function A(){if(I!==(I=f.val())){var a=r.width()-f.offset().left-r.offset().left;Q.html(h(I)||
|
||
|
h(g.placeholder));f.width(Math.min(r.width(),Math.max(a,Q.width()+30)))}}function N(){var c=a.trim(f.val()).split(a(b).data("settings").tokenDelimiter);a.each(c,function(c,d){if(d){a.isFunction(a(b).data("settings").onFreeTaggingAdd)&&(d=a(b).data("settings").onFreeTaggingAdd.call(n,d));var e={};e[a(b).data("settings").tokenValue]=e[a(b).data("settings").propertyToSearch]=d;F(e)}})}function C(c){var e=a(a(b).data("settings").tokenFormatter(c)),d=!0===c.readonly;d&&e.addClass(a(b).data("settings").classes.tokenReadOnly);
|
||
|
e.addClass(a(b).data("settings").classes.token).insertBefore(z);d||a("<span>"+a(b).data("settings").deleteText+"</span>").addClass(a(b).data("settings").classes.tokenDelete).appendTo(e).click(function(){if(!a(b).data("settings").disabled)return G(a(this).parent()),n.change(),!1});a.data(e.get(0),"tokeninput",c);u=u.slice(0,v).concat([c]).concat(u.slice(v));v++;R(u,n);x+=1;null!==a(b).data("settings").tokenLimit&&x>=a(b).data("settings").tokenLimit&&(f.hide(),y());return e}function F(c){var e=a(b).data("settings").onAdd;
|
||
|
if(0<x&&a(b).data("settings").preventDuplicates){var d=null;r.children().each(function(){var b=a(this),e=a.data(b.get(0),"tokeninput");if(e&&e[g.tokenValue]===c[g.tokenValue])return d=b,!1});if(d){E(d);z.insertAfter(d);D(f);return}}f.width(1);if(null==a(b).data("settings").tokenLimit||x<a(b).data("settings").tokenLimit)C(c),f.attr("placeholder",null),P();f.val("");y();a.isFunction(e)&&e.call(n,c)}function E(c){a(b).data("settings").disabled||(c.addClass(a(b).data("settings").classes.selectedToken),
|
||
|
m=c.get(0),f.val(""),y())}function B(c,e){c.removeClass(a(b).data("settings").classes.selectedToken);m=null;0===e?(z.insertBefore(c),v--):1===e?(z.insertAfter(c),v++):(z.appendTo(r),v=x);D(f)}function G(c){var e=a.data(c.get(0),"tokeninput"),d=a(b).data("settings").onDelete,k=c.prevAll().length;k>v&&k--;c.remove();m=null;D(f);u=u.slice(0,k).concat(u.slice(k+1));0==u.length&&f.attr("placeholder",g.placeholder);k<v&&v--;R(u,n);--x;null!==a(b).data("settings").tokenLimit&&(f.show().val(""),D(f));a.isFunction(d)&&
|
||
|
d.call(n,e)}function R(c,e){var d=a.map(c,function(c){return"function"==typeof a(b).data("settings").tokenValue?a(b).data("settings").tokenValue.call(this,c):c[a(b).data("settings").tokenValue]});e.val(d.join(a(b).data("settings").tokenDelimiter))}function y(){w.hide().empty();t=null}function H(){w.css({position:"absolute",top:r.offset().top+r.outerHeight(!0),left:r.offset().left,width:r.width(),"z-index":a(b).data("settings").zindex}).show()}function V(a,b){return a.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+
|
||
|
b.replace(S,"\\$&")+")(?![^<>]*>)(?![^&;]+;)","gi"),function(a,b){return"<b>"+l(b)+"</b>"})}function W(c){if(a(b).data("settings").excludeCurrent){var e=a(b).data("tokenInputObject").getTokens(),d=[];e.length&&(a.each(c,function(c,f){var k=!0;a.each(e,function(c,e){if(f[a(b).data("settings").propertyToSearch]==e[a(b).data("settings").propertyToSearch])return k=!1});k&&d.push(f)}),c=d)}return c}function J(c,e){if((e=W(e))&&e.length){w.empty();var d=a("<ul/>").appendTo(w).mouseover(function(b){K(a(b.target).closest("li"))}).mousedown(function(b){F(a(b.target).closest("li").data("tokeninput"));
|
||
|
n.change();return!1}).hide();a(b).data("settings").resultsLimit&&e.length>a(b).data("settings").resultsLimit&&(e=e.slice(0,a(b).data("settings").resultsLimit));a.each(e,function(e,f){var g=a(b).data("settings").resultsFormatter(f),k=f[a(b).data("settings").propertyToSearch],g=g.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)("+k.replace(S,"\\$&")+")(?![^<>]*>)(?![^&;]+;)","g"),V(k,c)),g=a(g).appendTo(d);e%2?g.addClass(a(b).data("settings").classes.dropdownItem):g.addClass(a(b).data("settings").classes.dropdownItem2);
|
||
|
0===e&&a(b).data("settings").autoSelectFirstResult&&K(g);a.data(g.get(0),"tokeninput",f)});H();a(b).data("settings").animateDropdown?d.slideDown("fast"):d.show()}else a(b).data("settings").noResultsText&&(w.html("<p>"+l(a(b).data("settings").noResultsText)+"</p>"),H())}function K(c){c&&(t&&(a(t).removeClass(a(b).data("settings").classes.selectedDropdownItem),t=null),c.addClass(a(b).data("settings").classes.selectedDropdownItem),t=c.get(0))}function T(){var c=f.val();c&&c.length&&(m&&B(a(m),1),c.length>=
|
||
|
a(b).data("settings").minChars?(a(b).data("settings").searchingText&&(w.html("<p>"+l(a(b).data("settings").searchingText)+"</p>"),H()),clearTimeout(U),U=setTimeout(function(){X(c)},a(b).data("settings").searchDelay)):y())}function X(c){var e=c+L(),d=M.get(e);if(d)a.isFunction(a(b).data("settings").onCachedResult)&&(d=a(b).data("settings").onCachedResult.call(n,d)),J(c,d);else if(a(b).data("settings").url){var d=L(),k={data:{}};-1<d.indexOf("?")?(d=d.split("?"),k.url=d[0],d=d[1].split("&"),a.each(d,
|
||
|
function(a,b){var c=b.split("=");k.data[c[0]]=c[1]})):k.url=d;k.data[a(b).data("settings").queryParam]=c;k.type=a(b).data("settings").method;k.dataType=a(b).data("settings").contentType;a(b).data("settings").crossDomain&&(k.dataType="jsonp");a(b).data("settings").excludeCurrent&&(d=a(b).data("tokenInputObject").getTokens(),d=a.map(d,function(c){return"function"==typeof a(b).data("settings").tokenValue?a(b).data("settings").tokenValue.call(this,c):c[a(b).data("settings").tokenValue]}),k.data[a(b).data("settings").excludeCurrentParameter]=
|
||
|
d.join(a(b).data("settings").tokenDelimiter));k.success=function(d){M.add(e,a(b).data("settings").jsonContainer?d[a(b).data("settings").jsonContainer]:d);a.isFunction(a(b).data("settings").onResult)&&(d=a(b).data("settings").onResult.call(n,d));f.val()===c&&J(c,a(b).data("settings").jsonContainer?d[a(b).data("settings").jsonContainer]:d)};if(g.onSend)g.onSend(k);a.ajax(k)}else a(b).data("settings").local_data&&(d=a.grep(a(b).data("settings").local_data,function(e){return-1<e[a(b).data("settings").propertyToSearch].toLowerCase().indexOf(c.toLowerCase())}),
|
||
|
M.add(e,d),a.isFunction(a(b).data("settings").onResult)&&(d=a(b).data("settings").onResult.call(n,d)),J(c,d))}function L(){var c=a(b).data("settings");return"function"==typeof c.url?c.url.call(c):c.url}function D(a){setTimeout(function(){a.focus()},50)}"string"===typeof p||"function"===typeof p?(a(b).data("settings").url=p,p=L(),void 0===a(b).data("settings").crossDomain&&"string"===typeof p&&(-1===p.indexOf("://")?a(b).data("settings").crossDomain=!1:a(b).data("settings").crossDomain=location.href.split(/\/+/g)[1]!==
|
||
|
p.split(/\/+/g)[1])):"object"===typeof p&&(a(b).data("settings").local_data=p);a(b).data("settings").classes?a(b).data("settings").classes=a.extend({},q,a(b).data("settings").classes):a(b).data("settings").theme?(a(b).data("settings").classes={},a.each(q,function(c,e){a(b).data("settings").classes[c]=e+"-"+a(b).data("settings").theme})):a(b).data("settings").classes=q;var u=[],x=0,M=new a.TokenList.Cache,U,I,f=a('<input type="text" autocomplete="off" autocapitalize="off"/>').css({outline:"none"}).attr("id",
|
||
|
a(b).data("settings").idPrefix+b.id).focus(function(){if(a(b).data("settings").disabled)return!1;null!==a(b).data("settings").tokenLimit&&a(b).data("settings").tokenLimit===x||!a(b).data("settings").hintText||(w.html("<p>"+l(a(b).data("settings").hintText)+"</p>"),H());r.addClass(a(b).data("settings").classes.focused)}).blur(function(){y();a(b).data("settings").allowFreeTagging&&N();a(this).val("");r.removeClass(a(b).data("settings").classes.focused)}).bind("keyup keydown blur update",A).keydown(function(c){var e,
|
||
|
d;switch(c.keyCode){case 37:case 39:case 38:case 40:0===this.value.length?(e=z.prev(),d=z.next(),e.length&&e.get(0)===m||d.length&&d.get(0)===m?37===c.keyCode||38===c.keyCode?B(a(m),0):B(a(m),1):37!==c.keyCode&&38!==c.keyCode||!e.length?39!==c.keyCode&&40!==c.keyCode||!d.length||E(a(d.get(0))):E(a(e.get(0)))):(e=null,40===c.keyCode||39===c.keyCode?(e=a(w).find("li").first(),t&&(e=a(t).next())):(e=a(w).find("li").last(),t&&(e=a(t).prev())),K(e));break;case 8:e=z.prev();if(0===this.value.length)return m?
|
||
|
(G(a(m)),n.change()):e.length&&E(a(e.get(0))),!1;1===a(this).val().length?y():setTimeout(function(){T()},5);break;case 9:case 13:case 108:case 188:if(t)F(a(t).data("tokeninput")),n.change();else{if(a(b).data("settings").allowFreeTagging){if(a(b).data("settings").allowTabOut&&""===a(this).val())return!0;N()}else if(a(this).val(""),a(b).data("settings").allowTabOut)return!0;c.stopPropagation();c.preventDefault()}return!1;case 27:return y(),!0;default:String.fromCharCode(c.which)&&setTimeout(function(){T()},
|
||
|
5)}});g.placeholder&&f.attr("placeholder",g.placeholder);var n=a(b).hide().val("").focus(function(){D(f)}).blur(function(){f.blur();return n}),m=null,v=0,t=null,r=a("<ul />").addClass(a(b).data("settings").classes.tokenList).click(function(b){if((b=a(b.target).closest("li"))&&b.get(0)&&a.data(b.get(0),"tokeninput")){var c=m;m&&B(a(m),2);c===b.get(0)?B(b,2):E(b)}else m&&B(a(m),2),D(f)}).mouseover(function(c){(c=a(c.target).closest("li"))&&m!==this&&c.addClass(a(b).data("settings").classes.highlightedToken)}).mouseout(function(c){(c=
|
||
|
a(c.target).closest("li"))&&m!==this&&c.removeClass(a(b).data("settings").classes.highlightedToken)}).insertBefore(n),z=a("<li />").addClass(a(b).data("settings").classes.inputToken).appendTo(r).append(f),w=a("<div/>").addClass(a(b).data("settings").classes.dropdown).appendTo("body").hide(),Q=a("<tester/>").insertAfter(f).css({position:"absolute",top:-9999,left:-9999,width:"auto",fontSize:f.css("fontSize"),fontFamily:f.css("fontFamily"),fontWeight:f.css("fontWeight"),letterSpacing:f.css("letterSpacing"),
|
||
|
whiteSpace:"nowrap"});n.val("");p=a(b).data("settings").prePopulate||n.data("pre");a(b).data("settings").processPrePopulate&&a.isFunction(a(b).data("settings").onResult)&&(p=a(b).data("settings").onResult.call(n,p));p&&p.length&&a.each(p,function(a,b){C(b);P();f.attr("placeholder",null)});a(b).data("settings").disabled&&O(!0);"function"===typeof a(b).data("settings").onReady&&a(b).data("settings").onReady.call();this.clear=function(){r.children("li").each(function(){0===a(this).children("input").length&&
|
||
|
G(a(this))})};this.add=function(a){F(a)};this.remove=function(b){r.children("li").each(function(){if(0===a(this).children("input").length){var c=a(this).data("tokeninput"),d=!0,f;for(f in b)if(b[f]!==c[f]){d=!1;break}d&&G(a(this))}})};this.getTokens=function(){return u};this.toggleDisabled=function(a){O(a)};A();var S=RegExp("[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]","g")};a.TokenList.Cache=function(b){var g,h={},l=0,q;g=a.extend({max_size:500},b);q=function(){h={};l=0};this.add=function(a,b){l>g.max_size&&
|
||
|
q();h[a]||(l+=1);h[a]=b};this.get=function(a){return h[a]}}})(jQuery);
|