__PluginManager__.register("menu",function(t,e,i,n){t("Collection"),t("Action");var o=t("userAction"),s=t("ActionCollection"),a=t("support"),r=t("Viewer"),h=t("align"),d=t("lib"),t=t("dataStructures");let c="menu",p="menuItem";var u={parent:null,eventType:"hint",animation:"",hideDelay:200,alignOffset:[0,0],click:function(t,e){console.log("Menu click")},hover:function(t,e){console.log("Menu hover")},dblclick:function(t,e){console.log("Menu dblclick")},hide:function(){}},l=[],f=0,v=function(t={},e=[]){this.id=++f,(l[this.id]=this).itemList={},this.node=null,this.bInit=!1,this.options=u,this.createNode(),this.setOptions(t),this.addItemList(e)};function m(t){var e=this;this.parentId=Number(t),this.node=Element,this.bInit=!1,this.bActive=!1,this.child=null,this.options={id:"",icon:d.svg.options,arrow:d.svg.arrowMenu,label:"menu",href:"javascript:void(0);",class:"",sort:500,visible:!0,hover:this.defaultHoverHandler,click:this.defaultClickHandler,dblclick:this.defaultDblClickHandler},this.handleEvent=function(t){switch(t.stopPropagation(),t.type){case"mouseover":this.options.hover(t,e,l[e.parentId]);break;case"click":this.options.click(t,e,l[e.parentId]);break;case"dblclick":this.options.dblclick(t,e,l[e.parentId])}},this.createNode()}v.prototype.getItemById=function(t){return!!this.itemList.hasOwnProperty(t)&&this.itemList[t]},v.prototype.getNode=function(){return this.node},v.prototype.getParent=function(){return this.options.parent},v.prototype.createNode=function(){this.node=document.createElement("div"),this.node.classList.add("kit-menu"),this.node.style.display="none",this.node.setAttribute("data-menu-id",this.id),a.setNodeData(this.node,c,this)},v.prototype.isActive=function(){return!!this.viewer&&this.viewer.isShown()},v.prototype.hide=function(t={}){return this.viewer&&this.viewer.hide(t),this},v.prototype.show=function(t={}){return this.viewer&&this.viewer.show(t),this},v.prototype.setPosition=function(){if(!(this.getParent()instanceof Element))return console.warn("Menu.options.parent NOT instanceof Element"),!1;var t,e,i,n;a.isA(this.align,h.Class)&&(this.node.style.position="absolute",this.node.style.width="fit-content",t=this.getParent(),i=e="right",(n=this.getParentMenu())&&n instanceof v&&(i=n.align.condition.horizontal.direction,e=n.align.condition.horizontal.align),this.align.offset=this.options.alignOffset,this.align.setNode(this.node).setParentNode(t).set(e,"top",i,"bottom"))},v.prototype.setPositionRecursive=function(){console.log("setPositionRecursive",this.id),b(this.id).forEach(function(t){t.menu.setPosition()})},v.prototype.getParentMenu=function(){var t=this.getParent();if(t instanceof Element&&t.hasOwnProperty("kit")&&t.kit[p]instanceof m)return t.kit[p].getMenu()},v.prototype.getParentMenuNode=function(){var t,e=this.getParentMenu();return t=e?e.node:t},v.prototype.addItem=function(t={}){var e;return t instanceof m?(t.parentId=this.id,e=t.options.id,this.itemList[e]=t):t instanceof Object&&(t.id||(t.id=a.uniqueId(8)),e=t.id,this.itemList[e]=new m(this.id).setOptions(t).init()),this.itemList[e]},v.prototype.addItemList=function(t=[]){var n=[];return t.forEach(function(t,e,i){n.push(this.addItem(t))}.bind(this)),n},v.prototype.sortItems=function(){this.itemList=Object.entries(this.itemList).sort(function(t,e){return Number(t[1].options.sort)-Number(e[1].options.sort)}).reduce((t,[e,i])=>({...t,[e]:i}),{})},v.prototype.render=function(){for(var t in this.sortItems(),this.node.innerHTML="",n.config.debug&&(this.node.innerHTML='
'+this.id+"
"),this.itemList)this.itemList.hasOwnProperty(t)&&this.node.append(this.itemList[t].getNode());return this},v.prototype.setParent=function(t){if(!((t=a.getNode(t))instanceof Element))return!1;this.options.parent=t,this.bInit&&this.init(!0)},v.prototype.setEventType=function(t="hint"){this.options.eventType=t,this.bInit&&this.init()},v.prototype.init=function(){var e=this,t=this.isActive();if(!(this.options.parent instanceof Element))return console.warn("Menu.init: Не установлен parent"),!1;this.viewer instanceof r&&this.viewer.stop(),this.render(),this.node.parentElement!==this.options.parent&&(this.node.remove(),this.options.parent.prepend(this.node)),this.align instanceof h.Class&&this.align.unset(),this.align=new h.Class(this.node,this.options.parent),this.actions instanceof s&&this.actions.stop();var i=new o[a.str.ucFirst(this.options.eventType)](this.options.parent,this.node);i.viewer instanceof r&&(this.viewer=i.viewer,i.viewer.onHideBefore.push(function(){var t=b(e.id);t.shift(),t.reverse().forEach(function(t){t.menu.viewer&&t.menu.viewer.hide({onHideBefore:!1})})}),i.viewer.onHide.push(function(){}),i.viewer.onShow.push(function(t={}){e.setPosition.call(e),t.recursiveMode||((t=b(e.id)).shift(),t.forEach(function(t){t.menu.viewer&&t.menu.viewer.show({recursiveMode:!0,force:!0})}))})),this.actions=new s([i]),this.actions.run(),this.isActive()||t?this.show({force:!0}):this.hide(),this.node.setAttribute("data-kit-menu-init",1),this.bInit=!0},v.prototype.setOptions=function(t={}){return this.options=Object.assign({},this.options,t),this.setParent(this.options.parent),this},m.prototype.defaultHoverHandler=function(t,e,i){console.log("hover: "+e.options.label)},m.prototype.defaultClickHandler=function(t,e,i){console.log("click: "+e.options.label),i.hide()},m.prototype.defaultDblClickHandler=function(t,e,i){console.log("dblclick: "+e.options.label)},m.prototype.getMenu=function(){return l[this.parentId]},m.prototype.getParent=function(){return l[this.parentId]instanceof v?l[this.parentId].node:null},m.prototype.getId=function(){return this.options.id},m.prototype.getNode=function(){return this.node},m.prototype.createNode=function(){this.node=document.createElement("a"),this.node.style.position="relative",this.node.dataset.parentId=this.parentId,a.setNodeData(this.node,"menuItem",this)},m.prototype.setLabel=function(t=""){this.options.label=t;var e=this.getNodeScopeElementByClassName("kit-menu-item__label");return this.setContent(e,t),this},m.prototype.setHref=function(t="javascript:void(0);"){return this.options.href=t,this.getNode().setAttribute("href",t),this},m.prototype.setIcon=function(t=""){this.options.icon=t;var e=this.getNodeScopeElementByClassName("kit-menu-item__icon");return this.setContent(e,t),this},m.prototype.setSort=function(t=500){return this.options.sort=Number(t),this},m.prototype.setArrow=function(t=""){this.options.arrow=t;var e=this.getNodeScopeElementByClassName("kit-menu-item__arrow");return this.setContent(e,t),this},m.prototype.setId=function(t=""){return t&&(this.options.id=t),this.node.id=this.options.id,this},m.prototype.setVisible=function(t=!0){return this.options.visible=t,this.node.style.display=this.options.visible?"":"none",this},m.prototype.setChild=function(t){var i;return t instanceof v?(t.setParent(this.node),t.init()):Array.isArray(t)&&(i=new v,t.forEach(function(t,e){"object"==typeof t&&i.addItem(t)}),i.setParent(this.node),i.init()),this},m.prototype.addClass=function(t=""){return t&&this.node.classList.add(t),this},m.prototype.setOptions=function(t={}){return this.options=Object.assign({},this.options,t),this},m.prototype.render=function(){return this.setLabel(this.options.label),this.setHref(this.options.href),this.setId(this.options.id),this.addClass("kit-menu-item"),this.addClass(this.options.class),this.setChild(this.options.child),this.setIcon(this.options.icon),this.setArrow(this.options.arrow),this.setVisible(this.options.visible),this.setSort(this.options.sort),this},m.prototype.init=function(){return this.bInit||(this.render(),this.node.removeEventListener("click",this,!1),this.node.removeEventListener("dblclick",this,!1),this.node.addEventListener("click",this,!1),this.node.addEventListener("dblclick",this,!1),this.bInit=!0),this},m.prototype.reInit=function(){return this.render(),this.node.removeEventListener("click",this,!1),this.node.removeEventListener("dblclick",this,!1),this.node.addEventListener("click",this,!1),this.node.addEventListener("dblclick",this,!1),this},m.prototype.remove=function(){this.node.removeEventListener("click",this,!1),this.node.removeEventListener("dblclick",this,!1);var t,e;this.options.id;setTimeout((t=this.parentId,e=this.options.id,function(){l.hasOwnProperty(t)&&l[t].itemList.hasOwnProperty(e)&&(delete l[t].itemList[e],l[t].init())}),0)},m.prototype.getNodeScopeElementByClassName=function(t=""){var e;return(e=this.node?this.node.querySelector(":scope > ."+t):e)||((e=document.createElement("div")).classList.add(t),this.node.append(e)),e},m.prototype.setContent=function(t,e){return a.innerHtml(t,e),!0};function y(){console.log("reCalcAllMenu"),g().forEach(function(t){t.menu.setPosition()})}var g=function(){var o=[],s=[],r=[];function h(t){var e,i=!1,n=l[t];return r.hasOwnProperty(t)&&r[t].parentMenu?r[t].parentMenu.id:(n&&(r[t]=r[t]||{},e=r[t].parentMenu=n.getParentMenu()),e?e.id:i)}l.forEach(function(t){var e,i,n;t instanceof v&&t.isActive()&&(i=t.id,r[i]=r[i]||{},r[i].menu=t,!(t=function(e){return function(){var t=r[e].data.parentId;return 1+(r.hasOwnProperty(t)?a.valueEx(r[t].data.depth):0)}})!==(n=h(i))&&(-1!==(e=o.indexOf(n))?-1===o.indexOf(i)&&(o.splice(e+1,0,i),s.splice(e+1,0,r[i].data={id:i,depth:t(i),parentId:n,type:1})):(r[n]=r[n]||{},-1!==(e=o.indexOf(i))?(o.splice(e,0,n),s.splice(e,0,r[n].data={id:n,depth:t(n),parentId:h(n),type:5})):(o.push(n),s.push(r[n].data={id:n,depth:t(n),parentId:h(n),type:2}),o.push(i),s.push(r[i].data={id:i,depth:t(i),parentId:n,type:3})))),-1===o.indexOf(i))&&(o.push(i),s.push(r[i].data={id:i,depth:1,parentId:0,type:4}))});var e=[];return s.forEach(function(t){t.depth=a.valueEx(t.depth),t.menu=r[t.id].menu,t.menu&&e.push(t)}),e},b=function(e){var t=g(),i=[],n=0;return t.forEach(function(t){(n=(n=t.id===e?t.depth:n)===t.depth&&t.id!==e||n>t.depth?0:n)&&i.push(t)}),i},w=new t.StackExecutionLimiter(this,y,100),t=function(){w.exec()},I=function(){w.exec()},t=(window.addEventListener("resize",t),window.addEventListener("scroll",I),{MenuItem:m,Class:v,getInstance:function(t={},e=[]){return t instanceof v?t:("string"==typeof t&&(t=document.querySelector(t)),a.getNodeData(t,c)||new v(t,e))},reInitAllMenu:function(){g().forEach(function(t){t.menu.init()})},reCalcAllMenu:y,autoload:function(t=document,e=".kit-menu"){if(!(t instanceof Node))return!1;t.querySelectorAll(e).forEach(function(t,e,i){t.kit&&t.kit.hasOwnProperty(c)})},getActiveMenuDataList:g,getChainActiveMenuDataList:b,MenuList:l});return i.exports=t});