eventman/static/js/nya-bs-select.min.js
2016-06-18 20:01:17 +02:00

6 рядки
No EOL
13 КіБ
JavaScript

/**
* nya-bootstrap-select v2.0.10
* Copyright 2014 Nyasoft
* Licensed under MIT license
*/
!function(){"use strict";function a(){return++j}function b(a){return a&&a.window===a}function c(a){return"string"==typeof a}function d(a){if(null==a||b(a))return!1;var d=a.length;return 1===a.nodeType&&d?!0:c(a)||Array.isArray(a)||0===d||"number"==typeof d&&d>0&&d-1 in a}function e(){return Object.create(null)}function f(b,c){var d,e=typeof b;return"function"==e||"object"==e&&null!==b?"function"==typeof(d=b.$$hashKey)?d=b.$$hashKey():void 0===d&&(d=b.$$hashKey=(c||a)()):d=b,e+":"+d}function g(a,b,c){var d,e,f=[],g=[];for(d=0;d<b.length;d++)for(e=0;e<a.length;e++)a[e][c]?a[e][c]===b[d]&&g.push(a[e]):f.push(a[e]);return g=g.concat(f)}function h(a){var b=a[0],c=a[a.length-1],d=[b];do{if(b=b.nextSibling,!b)break;d.push(b)}while(b!==c);return angular.element(d)}function i(a,b){for(;(a=a[b])&&1!==a.nodeType;);return a}var j=0,k=function(a){return a.clone[0]},l=function(a){return a.clone[a.clone.length-1]},m=function(a,b,c,d,e,f,g,h){a[c]=d,e&&(a[e]=f),a.$index=b,a.$first=0===b,a.$last=b===g-1,a.$middle=!(a.$first||a.$last),a.$odd=!(a.$even=0===(1&b)),h&&(a.$group=h)},n=function(a,b){var c,d=a.length;if(0===d)return!1;for(c=0;d>c;c++)if(v(b,a[c]))return!0;return!1},o=function(a,b){var c,d=a.length;if(0===d)return-1;for(c=0;d>c;c++)if(v(b,a[c]))return c;return-1},p=function(a,b,c){var d,e=a,f=typeof c;if(a==b)return null;do if("string"===f){if(d=" "+e.className+" ",1===e.nodeType&&d.replace(/[\t\r\n\f]/g," ").indexOf(c)>=0)return e}else if(e==c)return e;while((e=e.parentNode)&&e!=b&&9!==e.nodeType);return null},q=function(a){var b,c=a.className.replace(/[\t\r\n\f]/g," ").trim();b=c.split(" ");for(var d=0;d<b.length;d++)/\s+/.test(b[d])&&(b.splice(d,1),d--);return b},r=function(a,b){var c=q(a);return-1!==c.indexOf(b)},s=function(a,b){var c,d,e,f=a.children(),g=f.length;if(g>0)for(var h=0;g>h;h++){if(c=f.eq(h),d=!0,e=q(c[0]),e.length>0)for(var i=0;i<b.length;i++)if(-1===e.indexOf(b[i])){d=!1;break}if(d)return c}return[]},t=function(a,b){var c,d,e;if(-1!==a.text().toLowerCase().indexOf(b.toLowerCase()))return!0;for(c=a.children(),e=c.length,d=0;e>d;d++)if(-1!==c.eq(d).text().toLowerCase().indexOf(b.toLowerCase()))return!0;return!1},u=angular.element,v=angular.equals,w=angular.copy,x=angular.extend,y=angular.module("nya.bootstrap.select",[]);y.provider("nyaBsConfig",function(){var a=null,b={"en-us":{defaultNoneSelection:"Nothing selected",noSearchResult:"NO SEARCH RESULT",numberItemSelected:"%d item selected"}},c=w(b);this.setLocalizedText=function(a,b){if(!a)throw new Error("localeId must be a string formatted as languageId-countryId");c[a]||(c[a]={}),c[a]=x(c[a],b)},this.useLocale=function(b){a=b},this.$get=["$locale",function(d){var e;return e=a?c[a]:c[d.id],e||(e=b["en-us"]),e}]}),y.controller("nyaBsSelectCtrl",function(){var a=this;a.keyIdentifier=null,a.valueIdentifier=null,a.isMultiple=!1,a.onCollectionChange=function(){},a.setId=function(b){a.id=b||"id#"+Math.floor(1e4*Math.random())}}),y.directive("nyaBsSelect",["$parse","$document","$timeout","nyaBsConfig",function(a,b,c,d){var e="Nothing selected",f='<button class="btn btn-default dropdown-toggle" type="button"><span class="pull-left filter-option"></span>&nbsp;<span class="caret"></span></button>',g='<div class="dropdown-menu open"></div>',h='<div class="bs-searchbox"><input type="text" class="form-control"></div>',j='<ul class="dropdown-menu inner"></ul>',k='<li class="no-search-result"><span>NO SEARCH RESULT</span></li>';return{restrict:"ECA",require:["ngModel","nyaBsSelect"],controller:"nyaBsSelectCtrl",compile:function(l,m){l.addClass("btn-group");var x,y,z,A,B,C,D=function(){var a;return a=m.titleTpl?u(m.titleTpl):m.title?document.createTextNode(m.title):I.defaultNoneSelectionTpl?u(I.defaultNoneSelectionTpl):document.createTextNode(I.defaultNoneSelection?I.defaultNoneSelection:e)},E=l.children(),F=u(f),G=u(g),H=u(j),I=d;for(z=q(l[0]),z.forEach(function(a){/btn-(?:primary|info|success|warning|danger|inverse)/.test(a)&&(l.removeClass(a),F.removeClass("btn-default"),F.addClass(a)),"form-control"===a&&F.addClass(a)}),H.append(E),A=E.length,B=0;A>B;B++)C=E.eq(B),(C.hasClass("nya-bs-option")||C.attr("nya-bs-option"))&&C.find("a").attr("tabindex","0");return"true"===m.liveSearch&&(x=u(h),I.noSearchResultTpl?k=k.replace("NO SEARCH RESULT",I.noSearchResultTpl):I.noSearchResult&&(k=k.replace("NO SEARCH RESULT",I.noSearchResult)),y=u(k),G.append(x),H.append(y)),F.children().eq(0).append(D()),G.append(H),l.append(F),l.append(G),function(d,e,f,g){function h(){var a,b,c=M.children(),d=c.length;for(a=0;d>a;a++)if(b=c.eq(a),b.hasClass("active")&&b.hasClass("nya-bs-option"))return b;return null}function j(a){for(var b,c=a.childNodes,d=c.length,e=0;d>e;e++)if(b=c[e],1===b.nodeType&&"a"===b.tagName.toLowerCase()){b.focus();break}}function k(a){var b;b=M.children().eq(a?0:M.children().length-1);for(var c=0;c<M.children().length;c++)if(M.children().eq(c).hasClass("selected"))return M.children().eq(c)[0];return!b.hasClass("nya-bs-option")||b.hasClass("disabled")||b.hasClass("not-match")?a?l(b[0],"nextSibling"):l(b[0],"previousSibling"):b[0]}function l(a,b){if(!a||r(a,"nya-bs-option")){for(var c=a;(c=i(c,b))&&c.nodeType;)if(r(c,"nya-bs-option")&&!r(c,"disabled")&&!r(c,"not-match"))return c;return null}}function m(a){var b,c,f,g=E.$modelValue;b=q(a),"undefined"!=typeof b&&(J?(c=Array.isArray(g)?w(g):[],f=o(c,b),-1===f?(c.push(b),a.addClass("selected")):(c.splice(f,1),a.removeClass("selected"))):(M.children().removeClass("selected"),c=b,a.addClass("selected"))),E.$setViewValue(c),d.$digest(),J||(e.hasClass("open")&&e.triggerHandler("blur"),e.removeClass("open")),y()}function q(a){var b;return C?(b=a.scope(),C(b)):F.valueIdentifier||F.keyIdentifier?(b=a.scope(),b[F.valueIdentifier]||b[F.keyIdentifier]):a.attr("value")}function x(a){var b=a.find("a");return 0===b.children().length||b.children().eq(0).hasClass("check-mark")?b[0].firstChild.cloneNode(!1):b.children().eq(0)[0].cloneNode(!0)}function y(){var a=E.$modelValue;e.triggerHandler("change");var b=K.children().eq(0);return"undefined"==typeof a?(b.empty(),void b.append(D())):void(J&&0===a.length?(b.empty(),b.append(D())):c(function(){var c,d,e,g,h,i,j=M.children(),k=j.length,l=[];if(J&&"count"===f.selectedTextFormat?i=1:J&&f.selectedTextFormat&&(h=f.selectedTextFormat.match(/\s*count\s*>\s*(\d+)\s*/))&&(i=parseInt(h[1],10)),"undefined"!=typeof i&&a.length>i)return b.empty(),void b.append(I.numberItemSelectedTpl?u(I.numberItemSelectedTpl.replace("%d",a.length)):I.numberItemSelected?document.createTextNode(I.numberItemSelected.replace("%d",a.length)):document.createTextNode(a.length+" items selected"));for(e=0;k>e;e++)d=j.eq(e),d.hasClass("nya-bs-option")&&(c=q(d),J?Array.isArray(a)&&n(a,c)&&(g=d.attr("title"),l.push(g?document.createTextNode(g):x(d))):v(a,c)&&(g=d.attr("title"),l.push(g?document.createTextNode(g):x(d))));if(0===l.length)b.empty(),b.append(D());else if(1===l.length)b.empty(),b.append(l[0]);else for(b.empty(),e=0;e<l.length;e++)b.append(l[e]),e<l.length-1&&b.append(document.createTextNode(", "))}))}function z(){var a,b,c=M.find("li"),d=c.length;for(b=0;d>b;b++)if(a=c.eq(b),a.hasClass("nya-bs-option")||a.attr("nya-bs-option")){A=a[0].clientHeight;break}if(/\d+/.test(f.size)){var e=parseInt(f.size,10);M.css("max-height",e*A+"px"),M.css("overflow-y","auto")}}var A,B,C,E=g[0],F=g[1],G=!1,H=a(F.valueExp),J="undefined"!=typeof f.multiple,K=s(e,["dropdown-toggle"]),L=K.next(),M=s(L,["dropdown-menu","inner"]),N=s(L,["bs-searchbox"]),O=s(M,["no-search-result"]);F.valueExp&&(C=function(a,b){return H(a,b)}),F.setId(e.attr("id")),J&&(F.isMultiple=!0,E.$isEmpty=function(a){return!a||0===a.length}),"undefined"!=typeof f.disabled&&d.$watch(f.disabled,function(a){a?(K.addClass("disabled"),B=K.attr("tabindex"),K.attr("tabindex","-1"),G=!0):(K.removeClass("disabled"),B?K.attr("tabindex",B):K.removeAttr("tabindex"),G=!1)}),F.onCollectionChange=function(a){var b,c,e=[],f=E.$modelValue;if(f){if(a&&0!==a.length){if(C)for(b=0;b<a.length;b++)e.push(C(d,a[b]));else for(b=0;b<a.length;b++)F.valueIdentifier?e.push(a[b][F.valueIdentifier]):F.keyIdentifier&&e.push(a[b][F.keyIdentifier]);if(J){for(c=f.length,b=0;b<f.length;b++)n(e,f[b])||(f.splice(b,1),b--);c!==f.length&&(f=w(f))}else n(e,f)||(f=e[0])}else f=J?[]:null;E.$setViewValue(f),y()}},M.on("click",function(a){if(!G&&!u(a.target).hasClass("dropdown-header")){var b,c=p(a.target,M[0],"nya-bs-option");if(null!==c){if(b=u(c),b.hasClass("disabled"))return;m(b)}}}),b.on("click",function(a){null===p(a.target,e.parent()[0],e[0])&&(e.hasClass("open")&&e.triggerHandler("blur"),e.removeClass("open"))}),K.on("blur",function(){e.hasClass("open")||e.triggerHandler("blur")}),K.on("click",function(){var a;e.toggleClass("open"),e.hasClass("open")&&"undefined"==typeof A&&z(),"true"===f.liveSearch&&e.hasClass("open")?(N.children().eq(0)[0].focus(),a=k(!0),a&&(M.children().removeClass("active"),u(a).addClass("active"))):e.hasClass("open")&&(a=k(!0),a&&j(a))}),"true"===f.liveSearch&&N.children().on("input",function(){var a,b,c,d=N.children().val(),e=0,f=M.children(),g=f.length;if(d){for(a=0;g>a;a++)b=f.eq(a),b.hasClass("nya-bs-option")&&(t(b.find("a"),d)?(b.removeClass("not-match"),e++):b.addClass("not-match"));0===e?O.addClass("show"):O.removeClass("show")}else{for(a=0;g>a;a++)b=f.eq(a),b.hasClass("nya-bs-option")&&b.removeClass("not-match");O.removeClass("show")}c=k(!0),c&&(f.removeClass("active"),u(c).addClass("active"))}),E.$render=function(){var a,b,c=E.$modelValue,d=M.children(),e=d.length;if("undefined"==typeof c)for(a=0;e>a;a++)d.eq(a).hasClass("nya-bs-option")&&d.eq(a).removeClass("selected");else for(a=0;e>a;a++)d.eq(a).hasClass("nya-bs-option")&&(b=q(d.eq(a)),J?n(c,b)?d.eq(a).addClass("selected"):d.eq(a).removeClass("selected"):v(c,b)?d.eq(a).addClass("selected"):d.eq(a).removeClass("selected"));y()},e.on("keydown",function(a){var b=a.keyCode;if(27===b||13===b||38===b||40===b){if(a.preventDefault(),G)return void a.stopPropagation();var c,d,g,i,n=p(a.target,e[0],K[0]);"true"===f.liveSearch?d=p(a.target,e[0],N[0]):c=p(a.target,e[0],L[0]),n?13!==b&&38!==b&&40!==b||e.hasClass("open")||(a.stopPropagation(),e.addClass("open"),"undefined"==typeof A&&z(),"true"===f.liveSearch?(N.children().eq(0)[0].focus(),i=k(!0),i&&(M.children().removeClass("active"),u(i).addClass("active"))):(i=k(!0),i&&j(i))):c?27===b?(K[0].focus(),e.hasClass("open")&&e.triggerHandler("blur"),e.removeClass("open"),a.stopPropagation()):38===b?(a.stopPropagation(),i=l(a.target.parentNode,"previousSibling"),i?j(i):(i=k(!1),i&&j(i))):40===b?(a.stopPropagation(),i=l(a.target.parentNode,"nextSibling"),i?j(i):(i=k(!0),i&&j(i))):13===b&&(a.stopPropagation(),g=u(a.target.parentNode),g.hasClass("nya-bs-option")&&(m(g),J||K[0].focus())):d&&(27===b?(K[0].focus(),e.removeClass("open"),a.stopPropagation()):38===b?(a.stopPropagation(),g=h(),g&&(i=l(g[0],"previousSibling"),i?(g.removeClass("active"),u(i).addClass("active")):(i=k(!1),i&&(g.removeClass("active"),u(i).addClass("active"))))):40===b?(a.stopPropagation(),g=h(),g&&(i=l(g[0],"nextSibling"),i?(g.removeClass("active"),u(i).addClass("active")):(i=k(!0),i&&(g.removeClass("active"),u(i).addClass("active"))))):13===b&&(g=h(),g&&(m(g),J||K[0].focus())))}})}}}}]),y.directive("nyaBsOption",["$parse",function(a){var b=/^\s*(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+group\s+by\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/;return{restrict:"A",transclude:"element",priority:1e3,terminal:!0,require:["^nyaBsSelect","^ngModel"],compile:function(c,i){var j=i.nyaBsOption,o=document.createComment(" end nyaBsOption: "+j+" "),p=j.match(b);if(!p)throw new Error("invalid expression");var q,r,s,t,w,x=i.value,y=x?a(x):null,z=p[3]||p[1],A=p[2],B=p[4],C=p[5]?a(p[5]):null,D=p[6],E={$id:f},F={};return D?t=a(D):(q=function(a,b){return f(b)},r=function(a){return a}),function(a,b,c,f,i){function j(a){var c,f,j,t,x,y,B,C,E,F,H,J,K,L,M=b[0],N=e(),O=[];if(w&&(J=[]),d(a))y=a,x=s||q;else{x=s||r,y=[];for(var P in a)a.hasOwnProperty(P)&&"$"!=P.charAt(0)&&y.push(P);y.sort()}for(B=y.length,C=new Array(B),c=0;B>c;c++)if(f=a===y?c:y[c],j=a[f],t=x(f,j,c),L={},A&&(L[A]=f),L[z]=j,O.push(L),w&&(F=w(f,j),-1===J.indexOf(F)&&F&&J.push(F)),I[t])E=I[t],delete I[t],w&&(E.group=F),E.key=f,E.value=j,N[t]=E,C[c]=E;else{if(N[t])throw C.forEach(function(a){a&&a.scope&&(I[a.id]=a)}),new Error("Duplicates in a select are not allowed. Use 'track by' expression to specify unique keys.");C[c]={id:t,scope:void 0,clone:void 0,key:f,value:j},N[t]=!0,F&&(C[c].group=F)}J&&J.length>0&&(C=g(C,J,"group"));for(var Q in I)E=I[Q],h(E.clone).remove(),E.scope.$destroy();for(c=0;B>c;c++)E=C[c],E.scope?(H=M,k(E)!=H&&u(M).after(E.clone),M=l(E),m(E.scope,c,z,E.value,A,E.key,B,E.group)):i(function(a,b){E.scope=b;var d=o.cloneNode(!1);a[a.length++]=d,u(M).after(a),a.addClass("nya-bs-option"),j=p?p(E.key,E.value):E.value||E.key,D.isMultiple?Array.isArray(G.$modelValue)&&n(G.$modelValue,j)&&a.addClass("selected"):v(j,G.$modelValue)&&a.addClass("selected"),M=d,E.clone=a,N[E.id]=E,m(E.scope,c,z,E.value,A,E.key,B,E.group)}),J&&(K&&K===E.group?E.clone.removeClass("first-in-group"):E.clone.addClass("first-in-group"),K=E.group,E.clone.addClass("group-item"));I=N,D.onCollectionChange(O)}var p,D=f[0],G=f[1],H={};t&&(s=function(b,c,d){return A&&(E[A]=b),E[z]=c,E.$index=d,t(a,E)}),C&&(w=function(b,c){return A&&(F[A]=b),F[z]=c,C(a,F)}),A&&(D.keyIdentifier=A),z&&(D.valueIdentifier=z),y&&(D.valueExp=x,p=function(b,c){return A&&(H[A]=b),H[z]=c,y(a,H)});var I=e();"true"===c.deepWatch?a.$watch(B,j,!0):a.$watchCollection(B,j)}}}}])}();