-
Notifications
You must be signed in to change notification settings - Fork 0
/
select2tree.min.js
10 lines (10 loc) · 4.3 KB
/
select2tree.min.js
1
2
3
4
5
6
7
8
9
10
/// (c) Andrey Savitsky <contact@qroc.pro>
(function(d){function h(a,b,c,d){this._id=a;this._parent=b;this._childs=c;this._title=d}function l(a,b){this._shadowRoot=a;this._nodes=b}function k(){this._rawElements=[]}function p(a){if(!a.getParent())return a.getTitle();for(var b=[];a;)b.push(a.getTitle()),a=a.getParent();return b.join(" / ")}function n(a,b){this.$searchContainer=a.dropdown.$searchContainer;this.handler=b;this.$backContainer=d('<span class="input-group-btn select2tree_back_container"> <button class="btn btn-default select2tree_back" type="button"> <i class="fa fa-angle-left"></i> </button> </span>');
this.$backContainer.find(".select2tree_back").on("click",b)}function f(a,b){var c=this;this.select2=a.data("select2");c.$titleBlock=c.select2.$selection.find(".select2-selection__rendered");c.$useBlock=null;b.useButtonLabel&&(c.$useBlock=d('<span class="btn btn-default pull-right select2tree-use_button">'+b.useButtonLabel+"</span>"));var h=new k;c.select2.$element.children().each(function(a,b){var c=d(b);h.push(+c.attr("value"),+c.attr("parent"),c.text())});c.tree=h.build();c.backButtonModifier=new n(c.select2,
function(){return c.triggerClickBack()});a.on("select2:open",function(a){return c.triggerOpen(a)});a.on("select2:selecting",function(a){return c.triggerSelecting(a)});a.on("select2:close",function(a){return c.triggerClose(a)});c.updateState()}h.prototype.getId=function(){return this._id};h.prototype.getParent=function(){return this._parent};h.prototype.getChilds=function(){return this._childs};h.prototype.hasChilds=function(){return 0<this._childs.length};h.prototype.getTitle=function(){return this._title};
l.prototype.getFirstLevelNodes=function(){return this._shadowRoot.getChilds()};l.prototype.getShadowRoot=function(){return this._shadowRoot};l.prototype.get=function(a){if("number"===typeof a)for(var b=0;b<this._nodes.length;b++)if(this._nodes[b].getId()===a)return this._nodes[b];return null};k.prototype.push=function(a,b,c){this._rawElements.push({id:a,parentId:b,title:c})};k.prototype.build=function(){for(var a=this._rawElements,b=[new h(0,null,[],"")],c=0;a.length;){if(1E4<++c)throw Error("Error: Too many iterations");
for(var d=[],f=0;f<a.length;f++){for(var e=a[f],g=!1,m=0;m<b.length;m++){var k=b[m];if(e.parentId===k.getId()){g=new h(e.id,0!==m?k:null,[],e.title);b.push(g);k.getChilds().push(g);g=!0;break}}g||d.push(e)}a=d}return new l(b.shift(),b)};n.prototype.attach=function(){this.$searchContainer.addClass("input-group select2tree");this.$searchContainer.prepend(this.$backContainer)};n.prototype.detach=function(){this.$searchContainer.hasClass("select2tree")&&(this.$searchContainer.removeClass("input-group select2tree"),
this.$backContainer.detach())};f.prototype.getSelectedNode=function(){var a=+this.select2.$element.val();return a?this.tree.get(a):this.tree.getShadowRoot()};f.prototype.updateState=function(){var a=this,b=a.getSelectedNode(),c=b.getParent();b=p(b);a.$titleBlock.text(b);a.$titleBlock.attr("title",b);c?a.backButtonModifier.attach():a.backButtonModifier.detach();a.select2.$results.hide();setTimeout(function(){a.select2.$results.find("li").each(function(b,f){var e=d(f),g=e.data("data");g&&(g=a.tree.get(+g.id),
c!==g.getParent()?(e.css("display","none"),e.css("visibility","hidden")):(e.attr("aria-selected","false"),g.hasChilds()&&!e.hasClass("select2tree-with_child")&&(e.addClass("select2tree-with_child"),a.$useBlock&&e.append(a.$useBlock.clone())),e.css("display","block"),e.css("visibility","visible")))});a.select2.$results.show()},0)};f.prototype.triggerOpen=function(a){this.updateState()};f.prototype.triggerSelecting=function(a){var b=this.tree.get(+a.params.args.data.id);if(b.hasChilds()){var c=d(a.params.args.originalEvent.target);
this.$useBlock&&c.hasClass("select2tree-use_button")||(a.preventDefault(),this.select2.$element.val(b.getChilds()[0].getId()).trigger("change"),this.updateState())}};f.prototype.triggerClose=function(a){this.backButtonModifier.detach();a=p(this.getSelectedNode());this.$titleBlock.text(a);this.$titleBlock.attr("title",a)};f.prototype.triggerClickBack=function(){var a=this.getSelectedNode().getParent();this.select2.$element.val(a.getId()).trigger("change");this.updateState()};d.fn.select2tree=function(a){a=
d.extend({useButtonLabel:""},a);d(this).each(function(){new f(d(this),a)})}})(jQuery);