import $ from"jquery";const NAME="Treeview",DATA_KEY="lte.treeview",EVENT_KEY=`.${DATA_KEY}`,JQUERY_NO_CONFLICT=$.fn[NAME],EVENT_EXPANDED=`expanded${EVENT_KEY}`,EVENT_COLLAPSED=`collapsed${EVENT_KEY}`,EVENT_LOAD_DATA_API=`load${EVENT_KEY}`,SELECTOR_LI=".nav-item",SELECTOR_LINK=".nav-link",SELECTOR_TREEVIEW_MENU=".nav-treeview",SELECTOR_OPEN=".menu-open",SELECTOR_DATA_WIDGET='[data-widget="treeview"]',CLASS_NAME_OPEN="menu-open",CLASS_NAME_IS_OPENING="menu-is-opening",CLASS_NAME_SIDEBAR_COLLAPSED="sidebar-collapse",Default={trigger:`${SELECTOR_DATA_WIDGET} .nav-link`,animationSpeed:300,accordion:!0,expandSidebar:!1,sidebarButtonSelector:'[data-widget="pushmenu"]'};class Treeview{constructor(e,n){this._config=n,this._element=e}init(){$(".nav-item.menu-open .nav-treeview.menu-open").css("display","block"),this._setupListeners()}expand(e,n){const t=$.Event(EVENT_EXPANDED);if(this._config.accordion){const e=n.siblings(".menu-open").first(),t=e.find(".nav-treeview").first();this.collapse(t,e)}n.addClass("menu-is-opening"),e.stop().slideDown(this._config.animationSpeed,(()=>{n.addClass("menu-open"),$(this._element).trigger(t)})),this._config.expandSidebar&&this._expandSidebar()}collapse(e,n){const t=$.Event(EVENT_COLLAPSED);n.removeClass("menu-is-opening menu-open"),e.stop().slideUp(this._config.animationSpeed,(()=>{$(this._element).trigger(t),e.find(".menu-open > .nav-treeview").slideUp(),e.find(".menu-open").removeClass("menu-open")}))}toggle(e){const n=$(e.currentTarget),t=n.parent();let i=t.find("> .nav-treeview");if(!i.is(".nav-treeview")&&(t.is(".nav-item")||(i=t.parent().find("> .nav-treeview")),!i.is(".nav-treeview")))return;e.preventDefault();const s=n.parents(".nav-item").first();s.hasClass("menu-open")?this.collapse($(i),s):this.expand($(i),s)}_setupListeners(){const e=void 0!==this._element.attr("id")?`#${this._element.attr("id")}`:"";$(document).on("click",`${e}${this._config.trigger}`,(e=>{this.toggle(e)}))}_expandSidebar(){$("body").hasClass("sidebar-collapse")&&$(this._config.sidebarButtonSelector).PushMenu("expand")}static _jQueryInterface(e){return this.each((function(){let n=$(this).data(DATA_KEY);const t=$.extend({},Default,$(this).data());n||(n=new Treeview($(this),t),$(this).data(DATA_KEY,n)),"init"===e&&n[e]()}))}}$(window).on(EVENT_LOAD_DATA_API,(()=>{$(SELECTOR_DATA_WIDGET).each((function(){Treeview._jQueryInterface.call($(this),"init")}))})),$.fn[NAME]=Treeview._jQueryInterface,$.fn[NAME].Constructor=Treeview,$.fn[NAME].noConflict=function(){return $.fn[NAME]=JQUERY_NO_CONFLICT,Treeview._jQueryInterface};export default Treeview;
