import{NodeView}from"./base.mjs";import{NodeEditorView}from"./editor.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{isEmpty}from"../../base/helpers.mjs";const E=new ElementBuilder({windowsToolbar:"enfugue-windows-toolbar",toolbarItem:"enfugue-windows-toolbar-item",itemName:"enfugue-windows-toolbar-item-name",itemButton:"enfugue-windows-toolbar-item-button"});class WindowView extends NodeView{static snapSize=1;static canCopy=!1;static nodeButtons={minimize:{icon:"fas fa-window-minimize",callback:function(){this.editor.minimizeNode(this)}},maximize:{icon:"fas fa-window-maximize",callback:function(){this.editor.toggleMaximizeNode(this)}}};focus(){super.focus(),this.editor.unMinimizeNode(this)}minimize(){this.editor.minimizeNode(this)}maximize(){this.editor.toggleMaximizeNode(this)}}class WindowsView extends NodeEditorView{static disableCursor=!0;static canvasWidth="auto";static canvasHeight="auto";static className="windows";constructor(i){super(i),this.minimized=[]}minimizeNode(i){i.addClass("minimized"),this.minimized.push(i.addClass("minimized"));let e=0;if(void 0!==this.node){let t=this.node.find(E.getCustomTag("windowsToolbar")).findAll(E.getCustomTag("toolbarItem")).slice(-1).pop();isEmpty(t)||(e=t.element.offsetLeft);let o=E.toolbarItem().data("node-id",i.node.elementId).content(E.itemName().content(i.name),E.itemButton().content(E.i().class("fas fa-window-close")).on("click",(e=>{this.removeNode(i),i.closed()}))).on("click",(e=>{this.unMinimizeNode(i)}));this.node.find(E.getCustomTag("windowsToolbar")).append(o)}i.setDimension(e,this.height,0,0,!1)}unMinimizeNode(i){this.minimized=this.minimized.filter((e=>e!==i)),i.resetDimension();let e=this.node.find(E.getCustomTag("windowsToolbar")),t=e.find(`[data-node-id='${i.node.elementId}`);isEmpty(t)||e.remove(t),setTimeout((()=>i.removeClass("minimized")),250)}toggleMaximizeNode(i){1==i.maximized?(i.setDimension(i.lastLeft,i.lastTop,i.lastWidth,i.lastHeight),i.maximized=!1):(i.lastLeft=i.left,i.lastTop=i.top,i.lastWidth=i.width,i.lastHeight=i.height,i.maximized=!0,i.setDimension(-i.constructor.padding,-i.constructor.padding,this.width+2*i.constructor.padding,this.height+2*i.constructor.padding),i.addClass("focused")),i.resized()}async spawnWindow(i,e,t,o,n,d){isEmpty(t)&&(t=this.width-2*WindowView.padding),isEmpty(o)&&(o=this.height-2*WindowView.padding),void 0===n&&(n=WindowView.padding+document.scrollingElement.scrollLeft),void 0===d&&(d=WindowView.padding+document.scrollingElement.scrollTop);for(let i of this.nodes)i.left==n-WindowView.padding&&i.top==d-WindowView.padding&&(n+=WindowView.padding,d+=WindowView.padding);let s=await this.addNode(WindowView,i,e,n,d,t,o);return s.focus(),s}async windowResized(i){if(await super.windowResized(i),void 0!==this.node){let i=this.node.element.clientWidth,e=this.node.element.clientHeight,t=!0;for(let o of this.nodes){let n=o.left+o.width-i,d=o.top+o.height-e;n>0?d>0?o.setDimension(o.left,o.top,o.width-n,o.height-d,t):o.setDimension(o.left,o.top,o.width-n,o.height,t):d>0&&o.setDimension(o.left,o.top,o.width,o.height-d,t)}}}async build(){let i=await super.build(),e=E.windowsToolbar();for(let i of this.minimized){let t=E.toolbarItem().content(E.itemName().content(i.name),E.itemButton().content(E.i().class("fas fa-window-close")).on("click",(e=>{this.removeNode(i),i.closed()}))).on("click",(o=>{this.unMinimizeNode(i),e.remove(t)}));e.append(t)}return i.append(e)}}export{WindowsView,WindowView};
