@dynamix-layout/core 0.0.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +109 -0
- package/dist/core.iife.js +1 -0
- package/dist/core.js +1 -0
- package/dist/core.mjs +244 -0
- package/dist/core.umd.js +1 -0
- package/dist/types/app/index.d.ts +64 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/lib/Constant.d.ts +14 -0
- package/dist/types/lib/Element.d.ts +22 -0
- package/dist/types/lib/Helper.d.ts +21 -0
- package/dist/types/lib/Method.d.ts +1 -0
- package/dist/types/lib/index.d.ts +4 -0
- package/dist/types/types/Element.d.ts +103 -0
- package/dist/types/types/Method.d.ts +5 -0
- package/dist/types/types/index.d.ts +2 -0
- package/package.json +45 -0
- package/src/app/index.ts +291 -0
- package/src/index.ts +3 -0
- package/src/lib/Constant.ts +17 -0
- package/src/lib/Element.ts +66 -0
- package/src/lib/Helper.ts +259 -0
- package/src/lib/Method.ts +12 -0
- package/src/lib/index.ts +4 -0
- package/src/types/Element.ts +116 -0
- package/src/types/Method.ts +4 -0
- package/src/types/index.ts +2 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Akash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Dynamix Layout/Core
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
**Dynamix Layout/Core** is a versatile JavaScript package for creating dynamic layouts with ease. This package provides a set of options and methods to manage and manipulate layouts within a container. With Dynamix Layout/Core, you can efficiently handle layouts, dimensions, coordinates, tabs, and more.
|
|
8
|
+
|
|
9
|
+
## Options Object Parameters
|
|
10
|
+
|
|
11
|
+
When initializing the package, you can provide an options object with the following parameters:
|
|
12
|
+
|
|
13
|
+
1. **layout** *(JSON)*: This parameter allows you to provide a serialized JSON representation of a layout. It can be used to recreate the same layout in the future. If not provided, it defaults to `null`.
|
|
14
|
+
|
|
15
|
+
2. **range** *(Number)*: Use this parameter to specify the number of tabs you want to create and get their sizes on layout shift. If not provided, it defaults to `1`.
|
|
16
|
+
|
|
17
|
+
3. **dimension** *(Array)*: This parameter is used to set the width and height of the main container in which child layouts exist. It should be an array of two numbers, e.g., `[width, height]`. The default value is `[0, 0]`.
|
|
18
|
+
|
|
19
|
+
4. **coordinate** *(Array)*: Specify the x and y positions within the window of the container element using this parameter. It should be an array of two numbers, e.g., `[x, y]`. The default value is `[0, 0]`.
|
|
20
|
+
|
|
21
|
+
5. **tabs** *(Array)*: This parameter allows you to provide an array of names for the tabs. These names are used as IDs and key names. The default value is an array created based on the `range` parameter, such as `['tab-1', 'tab-2', ...]`.
|
|
22
|
+
|
|
23
|
+
## Allowed Parameters
|
|
24
|
+
|
|
25
|
+
- **layout**: Represents a serialized JSON layout for recreating layouts.
|
|
26
|
+
- **dimension**: Specifies the width and height of the main container.
|
|
27
|
+
- **coordinate**: Sets the x and y positions within the container element's window.
|
|
28
|
+
- **tabs**: An array of names for tabs, used as IDs and key names.
|
|
29
|
+
- **range**: Number of tabs to create and get their sizes on layout shift.
|
|
30
|
+
|
|
31
|
+
## Methods
|
|
32
|
+
|
|
33
|
+
### 1. shiftTree(srcId, destId)
|
|
34
|
+
|
|
35
|
+
The `shiftTree` method is used to shift the layout from a source element with the given `srcId` to a destination element with the `destId`. It returns the sizes of the elements involved in the shift.
|
|
36
|
+
|
|
37
|
+
Example:
|
|
38
|
+
```javascript
|
|
39
|
+
const sizes = dynamixLayout.shiftTree('tab-1', 'tab-2');
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. updateTree()
|
|
43
|
+
|
|
44
|
+
The `updateTree` method is used to recalculate the sizes in case of a window resize. It ensures that the layout remains consistent after a container size change.
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
```javascript
|
|
48
|
+
dynamixLayout.updateTree();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. getElement()
|
|
52
|
+
|
|
53
|
+
The `getElement` method returns information about all the tabs and their sizes. It is useful for retrieving the current state of the layout.
|
|
54
|
+
|
|
55
|
+
Example:
|
|
56
|
+
```javascript
|
|
57
|
+
const tabInfo = dynamixLayout.getElement();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 4. jsonify()
|
|
61
|
+
|
|
62
|
+
The `jsonify` method is used to generate a JSON representation of the current layout. You can store this JSON in local storage and use it to recreate the same layout at a later time.
|
|
63
|
+
|
|
64
|
+
Example:
|
|
65
|
+
```javascript
|
|
66
|
+
const layoutJSON = dynamixLayout.jsonify();
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Emojis
|
|
70
|
+
|
|
71
|
+
🔀 - Used to shift the layout with the `shiftTree` method.
|
|
72
|
+
|
|
73
|
+
🔄 - Triggered when updating the layout using the `updateTree` method.
|
|
74
|
+
|
|
75
|
+
📜 - Represents the information retrieval with the `getElement` method.
|
|
76
|
+
|
|
77
|
+
💾 - Indicates the ability to save and recreate layouts with the `jsonify` method.
|
|
78
|
+
|
|
79
|
+
## Example Usage
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
// Initialize Dynamix Layout/Core
|
|
83
|
+
const dynamixLayout = new DynamixLayoutCore({
|
|
84
|
+
layout: { /* Your serialized layout JSON here */ },
|
|
85
|
+
dimension: [800, 600],
|
|
86
|
+
coordinate: [100, 100],
|
|
87
|
+
tabs: ['tab-1', 'tab-2', 'tab-3'],
|
|
88
|
+
range: 3,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Perform layout shifts
|
|
92
|
+
const sizes = dynamixLayout.shiftTree('tab-1', 'tab-2');
|
|
93
|
+
|
|
94
|
+
// Handle window resize
|
|
95
|
+
dynamixLayout.updateTree();
|
|
96
|
+
|
|
97
|
+
// Retrieve tab information
|
|
98
|
+
const tabInfo = dynamixLayout.getElement();
|
|
99
|
+
|
|
100
|
+
// Generate JSON for storage and future use
|
|
101
|
+
const layoutJSON = dynamixLayout.jsonify();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Dynamix Layout/Core is a powerful tool for managing layouts and tabbed content, making it easier to build dynamic and responsive user interfaces.
|
|
105
|
+
|
|
106
|
+
For more details and advanced usage, please refer to the official documentation.
|
|
107
|
+
|
|
108
|
+
**Thank you for choosing Dynamix Layout/Core!** 🚀
|
|
109
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var layout=function(){"use strict";const g={Row:0,Column:1,Tabset:2,Tab:3};class m{constructor(e,s,t){this.type=e,this.name=null,this.parent=t,this.weight=s,this.children=[],this.dimension=[0,0],this.coordinate=[0,0],this.prev=null,this.next=null}}class y{constructor(e,s,t){this.type=e,this.prev=s,this.next=t,this.name=`${s.name}-${t.name}`,this.dimension=[0,0],this.coordinate=[0,0],s.next=this,t.prev=this}}const E={top:g.Row,left:g.Column,right:g.Column,bottom:g.Row,cover:g.Tab},v={top:0,left:0,right:1,bottom:1,cover:0};class l{static detach(e,s){var h;const t=e.parent;if(!t)return;const n=(h=e.parent)==null?void 0:h.children.indexOf(e),o=t==null?void 0:t.children.length,a=8*100/t.dimension[t.type];if(!s)return l.fixSrc(e);if(n>0&&n<o-1){const r=(e.weight+a)/2,c=t.children[n+1],i=t.children[n-1];c.weight+=r,i.weight+=r,new y(e.type,i,c)}if(n===o-1){const r=t.children[n-1];r.weight+=e.weight+a,r.next=null}if(n===0){const r=t.children[n+1];r.weight+=e.weight+a,r.prev=null}t.children.splice(n,1)}static attach(e,s,t){const n=e.parent;if(!n)return;const o=n.children.indexOf(e);switch(E[t]){case 1-n.type:const a=o+v[t],h=8*100/n.dimension[n.type];s.weight=e.weight=(e.weight-h)/2,s.parent=n,n.children.splice(a,0,s),l.fixBond(s,n.type);break;case n.type:const r=e.weight/2,c=v[t]==0?[s,e]:[e,s],i=new m(1-n.type,e.weight,n);i.children=c,n.children[o]=i,c.forEach(u=>{u.weight=r,u.parent=i}),this.fixBond(i,n.type),this.fixBond(s,n.type),this.fixBond(e,n.type);break;case g.Tab:e.children.push(...s.children),s.children.forEach(u=>{u.parent=e});break}}static getTotalWeight(e){return e.children.reduce((s,t)=>s+t.weight,0)??100}static getTotalNodes(e){return e.children.length}static getSize(e){return e.dimension[e.type]-(this.getTotalNodes(e)-1)*8}}l.createBinaryTree=(f,e)=>{var c;const{range:s}=e,t=[...e.tabs];if(s==1){const i=new m(g.Column,100,f);f.children=[i],l.addTab(i,t);return}const n=[f],o=[];let a=1,h=1;for(;h<s||n.length;){const i=[],u=n.length;for(let w=0;w<u;w++){const d=n.shift(),T=((c=d.parent)==null?void 0:c.children.indexOf(d))??0;if(d.name=`r${o.length}/c${w}/tb${T}`,d&&(i.push(d),h<s&&(d.children[0]=new m(a,50,d),n.push(d.children[0])),h<s)){d.children[1]=new m(a,50,d),n.push(d.children[1]),h++;const b=new y(a,d.children[0],d.children[1]);b.name=`r${o.length}/c${w}/b${T}-${T+1}`}}o.push(i),a=1-a}[...o[o.length-1],...o[o.length-2]].forEach(i=>{l.addTab(i,t)})},l.fixSrc=f=>{const e=f.parent;if(!e)return;const s=e.children.indexOf(f),t=e.children[1-s],n=e.parent,o=n==null?void 0:n.children.indexOf(e);switch(t.type){case g.Tabset:const a=e.weight;n==null||n.children.splice(o,1),n==null||n.children.splice(o,0,t),t.weight=a,t.parent=n,l.fixBond(t,t.type);break;default:if(!n){e.children.splice(s,1);break}const h=t.children.length,r=8*(h-1)*100/n.dimension[t.type];t.children.forEach(u=>{u.weight=(e.weight-r)*u.weight/100});const c=t.children[0],i=t.children[h-1];n.children=[...n.children.slice(0,o),...t.children,...n.children.slice(o+1)],t.children.forEach(u=>{u.parent=n}),l.fixBond(c,t.type),l.fixBond(i,t.type);break}},l.fixBond=(f,e)=>{const s=f.parent;if(!s)return;const t=s.children.indexOf(f),n=s.children.length;t>0&&t<n-1&&(new y(e,s.children[t-1],s.children[t]),new y(e,s.children[t],s.children[t+1])),t===n-1&&(new y(e,s.children[t-1],s.children[t]),s.children[t].next=null),t===0&&(new y(e,s.children[t],s.children[t+1]),s.children[t].prev=null)},l.addTab=(f,e)=>{if(f.children.length!==0)return;f.type=g.Tabset;const s=new m(g.Tab,100,f),t=e.shift();s.name=t,f.children=[s]},l.json=f=>{var e;return{type:f.type,dimension:f.dimension,coordinate:f.coordinate,name:f.name,children:(e=f.children)==null?void 0:e.map(s=>l.json(s))}};class B{constructor(e={}){this.shiftTree=(t,n,o)=>{let a,h;if(this._tabs.forEach(c=>{c.name==n&&(a=c),c.name==t&&(h=c),c.children.forEach(i=>{i.name==n&&(a=i),i.name==t&&(h=i)})}),h==null||a==null||h.parent===null)return;const r=h.parent.children.length;return a.type==g.Tabset&&h.type==g.Tab?(l.detach(h,r>1),l.attach(a,h,o)):a.type==g.Tabset&&h.type==g.Tabset&&(l.detach(h,r>2),l.attach(a,h,o)),this.calculate(this._root,0)},this.createTree=()=>{const{layout:t}=this._settings;if(!t)return l.createBinaryTree(this._root,this._settings),console.log("create tree",JSON.stringify(this.jsonify())),this.updateTree();const n=[t],o=[this._root];for(;n.length;){const a=n.length;for(let h=0;h<a;h++){const r=n.shift(),c=o.shift();r&&r.children.forEach((i,u)=>{n.push(i),o.push(new m(i.type,i.weight,c??null)),c==null||c.children.push(o[o.length-1]),u!=0&&new y(i.type,o[o.length-2],o[o.length-1])})}}return this.updateTree(),this},this.updateTree=(t,n,o)=>{t&&n&&(this._root.dimension=[t,n]);const a=[this._root];for(this._bond=[],this._tabs=[];a.length;){const h=[],r=a.length;for(let c=0;c<r;c++){const i=a.shift(),u=l.getSize(i),w=l.getTotalWeight(i);let d=0,T=0,b=i.children[0];const x=i.dimension,_=i.coordinate;i&&(h.push(i.coordinate),i.type==g.Tabset&&this._tabs.push(i),i.children.forEach((p,$)=>{if(a.push(p),p.dimension=[...x],p.coordinate=[..._],p.weight=p.weight/w*100,$===i.children.length-1)p.dimension[i.type]=u-T,p.weight=100-d;else{p.weight=p.weight/w*100;const j=u*(p.weight/100);p.dimension[i.type]=j,d+=p.weight,T+=p.dimension[i.type]}p.dimension[i.type]=p.dimension[i.type],p.prev&&(p.coordinate[i.type]=b.coordinate[i.type]+b.dimension[i.type]+8,p.prev.dimension=[...x],p.prev.coordinate=[..._],p.prev.dimension[i.type]=8,p.prev.coordinate[i.type]=b.coordinate[i.type]+b.dimension[i.type],p.prev.type=1-i.type,b=p,this._bond.push(p.prev))}))}}return o&&o(),this.getElements()},this.getElements=()=>({tabs:this._tabs.map(t=>l.json(t)),bonds:this._bond.map(t=>l.json(t))}),this.jsonify=()=>l.json(this._root);let s=Object.assign({layout:null,range:1,dimension:[0,0],coordinate:[0,0],tabs:Array.from({length:e.range??1},(t,n)=>`tab-${n+1}`)},e);this._tabs=[],this._bond=[],this._settings=s,this._root=new m(g.Row,100,null),this._root.coordinate=this._settings.coordinate,this._root.dimension=this._settings.dimension,this.createTree()}calculate(e,s=0){s==0&&(this._bond=[],this._tabs=[]);const t=l.getSize(e),n=l.getTotalWeight(e);let o=e.children[0],a=0,h=0;e.type==g.Tabset&&this._tabs.push(e),e.children.forEach((r,c)=>{r.dimension=[...e.dimension],r.coordinate=[...e.coordinate],c===e.children.length-1?(r.dimension[e.type]=t-a,r.weight=100-h):(r.weight=r.weight/n*100,r.dimension[e.type]=t*(r.weight/100),h+=r.weight,a+=r.dimension[e.type]),c!==0&&(r.coordinate[e.type]=o.coordinate[e.type]+o.dimension[e.type]+8,r.prev&&(r.prev.dimension=[...e.dimension],r.prev.coordinate=[...e.coordinate],r.prev.dimension[e.type]=8,r.prev.coordinate[e.type]=o.coordinate[e.type]+o.dimension[e.type],this._bond.push(r.prev)),o=r)}),e.type==g.Tabset&&this._tabs.push(e),e.children.forEach(r=>{r.children.length&&this.calculate(r,s+1)})}}return B}();
|
package/dist/core.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const g={Row:0,Column:1,Tabset:2,Tab:3};class b{constructor(e,s,t){this.type=e,this.name=null,this.parent=t,this.weight=s,this.children=[],this.dimension=[0,0],this.coordinate=[0,0],this.prev=null,this.next=null}}class w{constructor(e,s,t){this.type=e,this.prev=s,this.next=t,this.name=`${s.name}-${t.name}`,this.dimension=[0,0],this.coordinate=[0,0],s.next=this,t.prev=this}}const $={top:g.Row,left:g.Column,right:g.Column,bottom:g.Row,cover:g.Tab},v={top:0,left:0,right:1,bottom:1,cover:0};class l{static detach(e,s){var h;const t=e.parent;if(!t)return;const n=(h=e.parent)==null?void 0:h.children.indexOf(e),o=t==null?void 0:t.children.length,a=8*100/t.dimension[t.type];if(!s)return l.fixSrc(e);if(n>0&&n<o-1){const r=(e.weight+a)/2,c=t.children[n+1],i=t.children[n-1];c.weight+=r,i.weight+=r,new w(e.type,i,c)}if(n===o-1){const r=t.children[n-1];r.weight+=e.weight+a,r.next=null}if(n===0){const r=t.children[n+1];r.weight+=e.weight+a,r.prev=null}t.children.splice(n,1)}static attach(e,s,t){const n=e.parent;if(!n)return;const o=n.children.indexOf(e);switch($[t]){case 1-n.type:const a=o+v[t],h=8*100/n.dimension[n.type];s.weight=e.weight=(e.weight-h)/2,s.parent=n,n.children.splice(a,0,s),l.fixBond(s,n.type);break;case n.type:const r=e.weight/2,c=v[t]==0?[s,e]:[e,s],i=new b(1-n.type,e.weight,n);i.children=c,n.children[o]=i,c.forEach(u=>{u.weight=r,u.parent=i}),this.fixBond(i,n.type),this.fixBond(s,n.type),this.fixBond(e,n.type);break;case g.Tab:e.children.push(...s.children),s.children.forEach(u=>{u.parent=e});break}}static getTotalWeight(e){return e.children.reduce((s,t)=>s+t.weight,0)??100}static getTotalNodes(e){return e.children.length}static getSize(e){return e.dimension[e.type]-(this.getTotalNodes(e)-1)*8}}l.createBinaryTree=(f,e)=>{var c;const{range:s}=e,t=[...e.tabs];if(s==1){const i=new b(g.Column,100,f);f.children=[i],l.addTab(i,t);return}const n=[f],o=[];let a=1,h=1;for(;h<s||n.length;){const i=[],u=n.length;for(let m=0;m<u;m++){const d=n.shift(),T=((c=d.parent)==null?void 0:c.children.indexOf(d))??0;if(d.name=`r${o.length}/c${m}/tb${T}`,d&&(i.push(d),h<s&&(d.children[0]=new b(a,50,d),n.push(d.children[0])),h<s)){d.children[1]=new b(a,50,d),n.push(d.children[1]),h++;const y=new w(a,d.children[0],d.children[1]);y.name=`r${o.length}/c${m}/b${T}-${T+1}`}}o.push(i),a=1-a}[...o[o.length-1],...o[o.length-2]].forEach(i=>{l.addTab(i,t)})};l.fixSrc=f=>{const e=f.parent;if(!e)return;const s=e.children.indexOf(f),t=e.children[1-s],n=e.parent,o=n==null?void 0:n.children.indexOf(e);switch(t.type){case g.Tabset:const a=e.weight;n==null||n.children.splice(o,1),n==null||n.children.splice(o,0,t),t.weight=a,t.parent=n,l.fixBond(t,t.type);break;default:if(!n){e.children.splice(s,1);break}const h=t.children.length,r=8*(h-1)*100/n.dimension[t.type];t.children.forEach(u=>{u.weight=(e.weight-r)*u.weight/100});const c=t.children[0],i=t.children[h-1];n.children=[...n.children.slice(0,o),...t.children,...n.children.slice(o+1)],t.children.forEach(u=>{u.parent=n}),l.fixBond(c,t.type),l.fixBond(i,t.type);break}};l.fixBond=(f,e)=>{const s=f.parent;if(!s)return;const t=s.children.indexOf(f),n=s.children.length;t>0&&t<n-1&&(new w(e,s.children[t-1],s.children[t]),new w(e,s.children[t],s.children[t+1])),t===n-1&&(new w(e,s.children[t-1],s.children[t]),s.children[t].next=null),t===0&&(new w(e,s.children[t],s.children[t+1]),s.children[t].prev=null)};l.addTab=(f,e)=>{if(f.children.length!==0)return;f.type=g.Tabset;const s=new b(g.Tab,100,f),t=e.shift();s.name=t,f.children=[s]};l.json=f=>{var e;return{type:f.type,dimension:f.dimension,coordinate:f.coordinate,name:f.name,children:(e=f.children)==null?void 0:e.map(s=>l.json(s))}};class j{constructor(e={}){this.shiftTree=(t,n,o)=>{let a,h;if(this._tabs.forEach(c=>{c.name==n&&(a=c),c.name==t&&(h=c),c.children.forEach(i=>{i.name==n&&(a=i),i.name==t&&(h=i)})}),h==null||a==null||h.parent===null)return;const r=h.parent.children.length;return a.type==g.Tabset&&h.type==g.Tab?(l.detach(h,r>1),l.attach(a,h,o)):a.type==g.Tabset&&h.type==g.Tabset&&(l.detach(h,r>2),l.attach(a,h,o)),this.calculate(this._root,0)},this.createTree=()=>{const{layout:t}=this._settings;if(!t)return l.createBinaryTree(this._root,this._settings),console.log("create tree",JSON.stringify(this.jsonify())),this.updateTree();const n=[t],o=[this._root];for(;n.length;){const a=n.length;for(let h=0;h<a;h++){const r=n.shift(),c=o.shift();r&&r.children.forEach((i,u)=>{n.push(i),o.push(new b(i.type,i.weight,c??null)),c==null||c.children.push(o[o.length-1]),u!=0&&new w(i.type,o[o.length-2],o[o.length-1])})}}return this.updateTree(),this},this.updateTree=(t,n,o)=>{t&&n&&(this._root.dimension=[t,n]);const a=[this._root];for(this._bond=[],this._tabs=[];a.length;){const h=[],r=a.length;for(let c=0;c<r;c++){const i=a.shift(),u=l.getSize(i),m=l.getTotalWeight(i);let d=0,T=0,y=i.children[0];const x=i.dimension,_=i.coordinate;i&&(h.push(i.coordinate),i.type==g.Tabset&&this._tabs.push(i),i.children.forEach((p,E)=>{if(a.push(p),p.dimension=[...x],p.coordinate=[..._],p.weight=p.weight/m*100,E===i.children.length-1)p.dimension[i.type]=u-T,p.weight=100-d;else{p.weight=p.weight/m*100;const B=u*(p.weight/100);p.dimension[i.type]=B,d+=p.weight,T+=p.dimension[i.type]}p.dimension[i.type]=p.dimension[i.type],p.prev&&(p.coordinate[i.type]=y.coordinate[i.type]+y.dimension[i.type]+8,p.prev.dimension=[...x],p.prev.coordinate=[..._],p.prev.dimension[i.type]=8,p.prev.coordinate[i.type]=y.coordinate[i.type]+y.dimension[i.type],p.prev.type=1-i.type,y=p,this._bond.push(p.prev))}))}}return o&&o(),this.getElements()},this.getElements=()=>({tabs:this._tabs.map(t=>l.json(t)),bonds:this._bond.map(t=>l.json(t))}),this.jsonify=()=>l.json(this._root);let s=Object.assign({layout:null,range:1,dimension:[0,0],coordinate:[0,0],tabs:Array.from({length:e.range??1},(t,n)=>`tab-${n+1}`)},e);this._tabs=[],this._bond=[],this._settings=s,this._root=new b(g.Row,100,null),this._root.coordinate=this._settings.coordinate,this._root.dimension=this._settings.dimension,this.createTree()}calculate(e,s=0){s==0&&(this._bond=[],this._tabs=[]);const t=l.getSize(e),n=l.getTotalWeight(e);let o=e.children[0],a=0,h=0;e.type==g.Tabset&&this._tabs.push(e),e.children.forEach((r,c)=>{r.dimension=[...e.dimension],r.coordinate=[...e.coordinate],c===e.children.length-1?(r.dimension[e.type]=t-a,r.weight=100-h):(r.weight=r.weight/n*100,r.dimension[e.type]=t*(r.weight/100),h+=r.weight,a+=r.dimension[e.type]),c!==0&&(r.coordinate[e.type]=o.coordinate[e.type]+o.dimension[e.type]+8,r.prev&&(r.prev.dimension=[...e.dimension],r.prev.coordinate=[...e.coordinate],r.prev.dimension[e.type]=8,r.prev.coordinate[e.type]=o.coordinate[e.type]+o.dimension[e.type],this._bond.push(r.prev)),o=r)}),e.type==g.Tabset&&this._tabs.push(e),e.children.forEach(r=>{r.children.length&&this.calculate(r,s+1)})}}module.exports=j;
|
package/dist/core.mjs
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
const g = {
|
|
2
|
+
Row: 0,
|
|
3
|
+
Column: 1,
|
|
4
|
+
Tabset: 2,
|
|
5
|
+
Tab: 3
|
|
6
|
+
};
|
|
7
|
+
class b {
|
|
8
|
+
constructor(e, s, t) {
|
|
9
|
+
this.type = e, this.name = null, this.parent = t, this.weight = s, this.children = [], this.dimension = [0, 0], this.coordinate = [0, 0], this.prev = null, this.next = null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class w {
|
|
13
|
+
constructor(e, s, t) {
|
|
14
|
+
this.type = e, this.prev = s, this.next = t, this.name = `${s.name}-${t.name}`, this.dimension = [0, 0], this.coordinate = [0, 0], s.next = this, t.prev = this;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const $ = {
|
|
18
|
+
top: g.Row,
|
|
19
|
+
left: g.Column,
|
|
20
|
+
right: g.Column,
|
|
21
|
+
bottom: g.Row,
|
|
22
|
+
cover: g.Tab
|
|
23
|
+
}, v = {
|
|
24
|
+
top: 0,
|
|
25
|
+
left: 0,
|
|
26
|
+
right: 1,
|
|
27
|
+
bottom: 1,
|
|
28
|
+
cover: 0
|
|
29
|
+
};
|
|
30
|
+
class l {
|
|
31
|
+
static detach(e, s) {
|
|
32
|
+
var h;
|
|
33
|
+
const t = e.parent;
|
|
34
|
+
if (!t)
|
|
35
|
+
return;
|
|
36
|
+
const n = (h = e.parent) == null ? void 0 : h.children.indexOf(e), o = t == null ? void 0 : t.children.length, a = 8 * 100 / t.dimension[t.type];
|
|
37
|
+
if (!s)
|
|
38
|
+
return l.fixSrc(e);
|
|
39
|
+
if (n > 0 && n < o - 1) {
|
|
40
|
+
const r = (e.weight + a) / 2, c = t.children[n + 1], i = t.children[n - 1];
|
|
41
|
+
c.weight += r, i.weight += r, new w(e.type, i, c);
|
|
42
|
+
}
|
|
43
|
+
if (n === o - 1) {
|
|
44
|
+
const r = t.children[n - 1];
|
|
45
|
+
r.weight += e.weight + a, r.next = null;
|
|
46
|
+
}
|
|
47
|
+
if (n === 0) {
|
|
48
|
+
const r = t.children[n + 1];
|
|
49
|
+
r.weight += e.weight + a, r.prev = null;
|
|
50
|
+
}
|
|
51
|
+
t.children.splice(n, 1);
|
|
52
|
+
}
|
|
53
|
+
static attach(e, s, t) {
|
|
54
|
+
const n = e.parent;
|
|
55
|
+
if (!n)
|
|
56
|
+
return;
|
|
57
|
+
const o = n.children.indexOf(e);
|
|
58
|
+
switch ($[t]) {
|
|
59
|
+
case 1 - n.type:
|
|
60
|
+
const a = o + v[t], h = 8 * 100 / n.dimension[n.type];
|
|
61
|
+
s.weight = e.weight = (e.weight - h) / 2, s.parent = n, n.children.splice(a, 0, s), l.fixBond(s, n.type);
|
|
62
|
+
break;
|
|
63
|
+
case n.type:
|
|
64
|
+
const r = e.weight / 2, c = v[t] == 0 ? [s, e] : [e, s], i = new b(1 - n.type, e.weight, n);
|
|
65
|
+
i.children = c, n.children[o] = i, c.forEach((u) => {
|
|
66
|
+
u.weight = r, u.parent = i;
|
|
67
|
+
}), this.fixBond(i, n.type), this.fixBond(s, n.type), this.fixBond(e, n.type);
|
|
68
|
+
break;
|
|
69
|
+
case g.Tab:
|
|
70
|
+
e.children.push(...s.children), s.children.forEach((u) => {
|
|
71
|
+
u.parent = e;
|
|
72
|
+
});
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
static getTotalWeight(e) {
|
|
77
|
+
return e.children.reduce((s, t) => s + t.weight, 0) ?? 100;
|
|
78
|
+
}
|
|
79
|
+
static getTotalNodes(e) {
|
|
80
|
+
return e.children.length;
|
|
81
|
+
}
|
|
82
|
+
static getSize(e) {
|
|
83
|
+
return e.dimension[e.type] - (this.getTotalNodes(e) - 1) * 8;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
l.createBinaryTree = (f, e) => {
|
|
87
|
+
var c;
|
|
88
|
+
const { range: s } = e, t = [...e.tabs];
|
|
89
|
+
if (s == 1) {
|
|
90
|
+
const i = new b(g.Column, 100, f);
|
|
91
|
+
f.children = [i], l.addTab(i, t);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const n = [f], o = [];
|
|
95
|
+
let a = 1, h = 1;
|
|
96
|
+
for (; h < s || n.length; ) {
|
|
97
|
+
const i = [], u = n.length;
|
|
98
|
+
for (let m = 0; m < u; m++) {
|
|
99
|
+
const d = n.shift(), T = ((c = d.parent) == null ? void 0 : c.children.indexOf(d)) ?? 0;
|
|
100
|
+
if (d.name = `r${o.length}/c${m}/tb${T}`, d && (i.push(d), h < s && (d.children[0] = new b(a, 50, d), n.push(d.children[0])), h < s)) {
|
|
101
|
+
d.children[1] = new b(a, 50, d), n.push(d.children[1]), h++;
|
|
102
|
+
const y = new w(a, d.children[0], d.children[1]);
|
|
103
|
+
y.name = `r${o.length}/c${m}/b${T}-${T + 1}`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
o.push(i), a = 1 - a;
|
|
107
|
+
}
|
|
108
|
+
[
|
|
109
|
+
...o[o.length - 1],
|
|
110
|
+
...o[o.length - 2]
|
|
111
|
+
].forEach((i) => {
|
|
112
|
+
l.addTab(i, t);
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
l.fixSrc = (f) => {
|
|
116
|
+
const e = f.parent;
|
|
117
|
+
if (!e)
|
|
118
|
+
return;
|
|
119
|
+
const s = e.children.indexOf(f), t = e.children[1 - s], n = e.parent, o = n == null ? void 0 : n.children.indexOf(e);
|
|
120
|
+
switch (t.type) {
|
|
121
|
+
case g.Tabset:
|
|
122
|
+
const a = e.weight;
|
|
123
|
+
n == null || n.children.splice(o, 1), n == null || n.children.splice(o, 0, t), t.weight = a, t.parent = n, l.fixBond(t, t.type);
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
if (!n) {
|
|
127
|
+
e.children.splice(s, 1);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
const h = t.children.length, r = 8 * (h - 1) * 100 / n.dimension[t.type];
|
|
131
|
+
t.children.forEach((u) => {
|
|
132
|
+
u.weight = (e.weight - r) * u.weight / 100;
|
|
133
|
+
});
|
|
134
|
+
const c = t.children[0], i = t.children[h - 1];
|
|
135
|
+
n.children = [
|
|
136
|
+
...n.children.slice(0, o),
|
|
137
|
+
...t.children,
|
|
138
|
+
...n.children.slice(o + 1)
|
|
139
|
+
], t.children.forEach((u) => {
|
|
140
|
+
u.parent = n;
|
|
141
|
+
}), l.fixBond(c, t.type), l.fixBond(i, t.type);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
l.fixBond = (f, e) => {
|
|
146
|
+
const s = f.parent;
|
|
147
|
+
if (!s)
|
|
148
|
+
return;
|
|
149
|
+
const t = s.children.indexOf(f), n = s.children.length;
|
|
150
|
+
t > 0 && t < n - 1 && (new w(e, s.children[t - 1], s.children[t]), new w(e, s.children[t], s.children[t + 1])), t === n - 1 && (new w(e, s.children[t - 1], s.children[t]), s.children[t].next = null), t === 0 && (new w(e, s.children[t], s.children[t + 1]), s.children[t].prev = null);
|
|
151
|
+
};
|
|
152
|
+
l.addTab = (f, e) => {
|
|
153
|
+
if (f.children.length !== 0)
|
|
154
|
+
return;
|
|
155
|
+
f.type = g.Tabset;
|
|
156
|
+
const s = new b(g.Tab, 100, f), t = e.shift();
|
|
157
|
+
s.name = t, f.children = [s];
|
|
158
|
+
};
|
|
159
|
+
l.json = (f) => {
|
|
160
|
+
var e;
|
|
161
|
+
return {
|
|
162
|
+
type: f.type,
|
|
163
|
+
dimension: f.dimension,
|
|
164
|
+
coordinate: f.coordinate,
|
|
165
|
+
name: f.name,
|
|
166
|
+
children: (e = f.children) == null ? void 0 : e.map((s) => l.json(s))
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
class j {
|
|
170
|
+
constructor(e = {}) {
|
|
171
|
+
this.shiftTree = (t, n, o) => {
|
|
172
|
+
let a, h;
|
|
173
|
+
if (this._tabs.forEach((c) => {
|
|
174
|
+
c.name == n && (a = c), c.name == t && (h = c), c.children.forEach((i) => {
|
|
175
|
+
i.name == n && (a = i), i.name == t && (h = i);
|
|
176
|
+
});
|
|
177
|
+
}), h == null || a == null || h.parent === null)
|
|
178
|
+
return;
|
|
179
|
+
const r = h.parent.children.length;
|
|
180
|
+
return a.type == g.Tabset && h.type == g.Tab ? (l.detach(h, r > 1), l.attach(a, h, o)) : a.type == g.Tabset && h.type == g.Tabset && (l.detach(h, r > 2), l.attach(a, h, o)), this.calculate(this._root, 0);
|
|
181
|
+
}, this.createTree = () => {
|
|
182
|
+
const { layout: t } = this._settings;
|
|
183
|
+
if (!t)
|
|
184
|
+
return l.createBinaryTree(this._root, this._settings), console.log("create tree", JSON.stringify(this.jsonify())), this.updateTree();
|
|
185
|
+
const n = [t], o = [this._root];
|
|
186
|
+
for (; n.length; ) {
|
|
187
|
+
const a = n.length;
|
|
188
|
+
for (let h = 0; h < a; h++) {
|
|
189
|
+
const r = n.shift(), c = o.shift();
|
|
190
|
+
r && r.children.forEach((i, u) => {
|
|
191
|
+
n.push(i), o.push(new b(i.type, i.weight, c ?? null)), c == null || c.children.push(o[o.length - 1]), u != 0 && new w(i.type, o[o.length - 2], o[o.length - 1]);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return this.updateTree(), this;
|
|
196
|
+
}, this.updateTree = (t, n, o) => {
|
|
197
|
+
t && n && (this._root.dimension = [t, n]);
|
|
198
|
+
const a = [this._root];
|
|
199
|
+
for (this._bond = [], this._tabs = []; a.length; ) {
|
|
200
|
+
const h = [], r = a.length;
|
|
201
|
+
for (let c = 0; c < r; c++) {
|
|
202
|
+
const i = a.shift(), u = l.getSize(i), m = l.getTotalWeight(i);
|
|
203
|
+
let d = 0, T = 0, y = i.children[0];
|
|
204
|
+
const x = i.dimension, _ = i.coordinate;
|
|
205
|
+
i && (h.push(i.coordinate), i.type == g.Tabset && this._tabs.push(i), i.children.forEach((p, E) => {
|
|
206
|
+
if (a.push(p), p.dimension = [...x], p.coordinate = [..._], p.weight = p.weight / m * 100, E === i.children.length - 1)
|
|
207
|
+
p.dimension[i.type] = u - T, p.weight = 100 - d;
|
|
208
|
+
else {
|
|
209
|
+
p.weight = p.weight / m * 100;
|
|
210
|
+
const B = u * (p.weight / 100);
|
|
211
|
+
p.dimension[i.type] = B, d += p.weight, T += p.dimension[i.type];
|
|
212
|
+
}
|
|
213
|
+
p.dimension[i.type] = p.dimension[i.type], p.prev && (p.coordinate[i.type] = y.coordinate[i.type] + y.dimension[i.type] + 8, p.prev.dimension = [...x], p.prev.coordinate = [..._], p.prev.dimension[i.type] = 8, p.prev.coordinate[i.type] = y.coordinate[i.type] + y.dimension[i.type], p.prev.type = 1 - i.type, y = p, this._bond.push(p.prev));
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return o && o(), this.getElements();
|
|
218
|
+
}, this.getElements = () => ({
|
|
219
|
+
tabs: this._tabs.map((t) => l.json(t)),
|
|
220
|
+
bonds: this._bond.map((t) => l.json(t))
|
|
221
|
+
}), this.jsonify = () => l.json(this._root);
|
|
222
|
+
let s = Object.assign({
|
|
223
|
+
layout: null,
|
|
224
|
+
range: 1,
|
|
225
|
+
dimension: [0, 0],
|
|
226
|
+
coordinate: [0, 0],
|
|
227
|
+
tabs: Array.from({ length: e.range ?? 1 }, (t, n) => `tab-${n + 1}`)
|
|
228
|
+
}, e);
|
|
229
|
+
this._tabs = [], this._bond = [], this._settings = s, this._root = new b(g.Row, 100, null), this._root.coordinate = this._settings.coordinate, this._root.dimension = this._settings.dimension, this.createTree();
|
|
230
|
+
}
|
|
231
|
+
calculate(e, s = 0) {
|
|
232
|
+
s == 0 && (this._bond = [], this._tabs = []);
|
|
233
|
+
const t = l.getSize(e), n = l.getTotalWeight(e);
|
|
234
|
+
let o = e.children[0], a = 0, h = 0;
|
|
235
|
+
e.type == g.Tabset && this._tabs.push(e), e.children.forEach((r, c) => {
|
|
236
|
+
r.dimension = [...e.dimension], r.coordinate = [...e.coordinate], c === e.children.length - 1 ? (r.dimension[e.type] = t - a, r.weight = 100 - h) : (r.weight = r.weight / n * 100, r.dimension[e.type] = t * (r.weight / 100), h += r.weight, a += r.dimension[e.type]), c !== 0 && (r.coordinate[e.type] = o.coordinate[e.type] + o.dimension[e.type] + 8, r.prev && (r.prev.dimension = [...e.dimension], r.prev.coordinate = [...e.coordinate], r.prev.dimension[e.type] = 8, r.prev.coordinate[e.type] = o.coordinate[e.type] + o.dimension[e.type], this._bond.push(r.prev)), o = r);
|
|
237
|
+
}), e.type == g.Tabset && this._tabs.push(e), e.children.forEach((r) => {
|
|
238
|
+
r.children.length && this.calculate(r, s + 1);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
export {
|
|
243
|
+
j as default
|
|
244
|
+
};
|
package/dist/core.umd.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(u,m){typeof exports=="object"&&typeof module<"u"?module.exports=m():typeof define=="function"&&define.amd?define(m):(u=typeof globalThis<"u"?globalThis:u||self,u.layout=m())})(this,function(){"use strict";const u={Row:0,Column:1,Tabset:2,Tab:3};class m{constructor(e,s,t){this.type=e,this.name=null,this.parent=t,this.weight=s,this.children=[],this.dimension=[0,0],this.coordinate=[0,0],this.prev=null,this.next=null}}class y{constructor(e,s,t){this.type=e,this.prev=s,this.next=t,this.name=`${s.name}-${t.name}`,this.dimension=[0,0],this.coordinate=[0,0],s.next=this,t.prev=this}}const E={top:u.Row,left:u.Column,right:u.Column,bottom:u.Row,cover:u.Tab},x={top:0,left:0,right:1,bottom:1,cover:0};class l{static detach(e,s){var h;const t=e.parent;if(!t)return;const n=(h=e.parent)==null?void 0:h.children.indexOf(e),o=t==null?void 0:t.children.length,c=8*100/t.dimension[t.type];if(!s)return l.fixSrc(e);if(n>0&&n<o-1){const r=(e.weight+c)/2,a=t.children[n+1],i=t.children[n-1];a.weight+=r,i.weight+=r,new y(e.type,i,a)}if(n===o-1){const r=t.children[n-1];r.weight+=e.weight+c,r.next=null}if(n===0){const r=t.children[n+1];r.weight+=e.weight+c,r.prev=null}t.children.splice(n,1)}static attach(e,s,t){const n=e.parent;if(!n)return;const o=n.children.indexOf(e);switch(E[t]){case 1-n.type:const c=o+x[t],h=8*100/n.dimension[n.type];s.weight=e.weight=(e.weight-h)/2,s.parent=n,n.children.splice(c,0,s),l.fixBond(s,n.type);break;case n.type:const r=e.weight/2,a=x[t]==0?[s,e]:[e,s],i=new m(1-n.type,e.weight,n);i.children=a,n.children[o]=i,a.forEach(d=>{d.weight=r,d.parent=i}),this.fixBond(i,n.type),this.fixBond(s,n.type),this.fixBond(e,n.type);break;case u.Tab:e.children.push(...s.children),s.children.forEach(d=>{d.parent=e});break}}static getTotalWeight(e){return e.children.reduce((s,t)=>s+t.weight,0)??100}static getTotalNodes(e){return e.children.length}static getSize(e){return e.dimension[e.type]-(this.getTotalNodes(e)-1)*8}}l.createBinaryTree=(f,e)=>{var a;const{range:s}=e,t=[...e.tabs];if(s==1){const i=new m(u.Column,100,f);f.children=[i],l.addTab(i,t);return}const n=[f],o=[];let c=1,h=1;for(;h<s||n.length;){const i=[],d=n.length;for(let w=0;w<d;w++){const g=n.shift(),T=((a=g.parent)==null?void 0:a.children.indexOf(g))??0;if(g.name=`r${o.length}/c${w}/tb${T}`,g&&(i.push(g),h<s&&(g.children[0]=new m(c,50,g),n.push(g.children[0])),h<s)){g.children[1]=new m(c,50,g),n.push(g.children[1]),h++;const b=new y(c,g.children[0],g.children[1]);b.name=`r${o.length}/c${w}/b${T}-${T+1}`}}o.push(i),c=1-c}[...o[o.length-1],...o[o.length-2]].forEach(i=>{l.addTab(i,t)})},l.fixSrc=f=>{const e=f.parent;if(!e)return;const s=e.children.indexOf(f),t=e.children[1-s],n=e.parent,o=n==null?void 0:n.children.indexOf(e);switch(t.type){case u.Tabset:const c=e.weight;n==null||n.children.splice(o,1),n==null||n.children.splice(o,0,t),t.weight=c,t.parent=n,l.fixBond(t,t.type);break;default:if(!n){e.children.splice(s,1);break}const h=t.children.length,r=8*(h-1)*100/n.dimension[t.type];t.children.forEach(d=>{d.weight=(e.weight-r)*d.weight/100});const a=t.children[0],i=t.children[h-1];n.children=[...n.children.slice(0,o),...t.children,...n.children.slice(o+1)],t.children.forEach(d=>{d.parent=n}),l.fixBond(a,t.type),l.fixBond(i,t.type);break}},l.fixBond=(f,e)=>{const s=f.parent;if(!s)return;const t=s.children.indexOf(f),n=s.children.length;t>0&&t<n-1&&(new y(e,s.children[t-1],s.children[t]),new y(e,s.children[t],s.children[t+1])),t===n-1&&(new y(e,s.children[t-1],s.children[t]),s.children[t].next=null),t===0&&(new y(e,s.children[t],s.children[t+1]),s.children[t].prev=null)},l.addTab=(f,e)=>{if(f.children.length!==0)return;f.type=u.Tabset;const s=new m(u.Tab,100,f),t=e.shift();s.name=t,f.children=[s]},l.json=f=>{var e;return{type:f.type,dimension:f.dimension,coordinate:f.coordinate,name:f.name,children:(e=f.children)==null?void 0:e.map(s=>l.json(s))}};class B{constructor(e={}){this.shiftTree=(t,n,o)=>{let c,h;if(this._tabs.forEach(a=>{a.name==n&&(c=a),a.name==t&&(h=a),a.children.forEach(i=>{i.name==n&&(c=i),i.name==t&&(h=i)})}),h==null||c==null||h.parent===null)return;const r=h.parent.children.length;return c.type==u.Tabset&&h.type==u.Tab?(l.detach(h,r>1),l.attach(c,h,o)):c.type==u.Tabset&&h.type==u.Tabset&&(l.detach(h,r>2),l.attach(c,h,o)),this.calculate(this._root,0)},this.createTree=()=>{const{layout:t}=this._settings;if(!t)return l.createBinaryTree(this._root,this._settings),console.log("create tree",JSON.stringify(this.jsonify())),this.updateTree();const n=[t],o=[this._root];for(;n.length;){const c=n.length;for(let h=0;h<c;h++){const r=n.shift(),a=o.shift();r&&r.children.forEach((i,d)=>{n.push(i),o.push(new m(i.type,i.weight,a??null)),a==null||a.children.push(o[o.length-1]),d!=0&&new y(i.type,o[o.length-2],o[o.length-1])})}}return this.updateTree(),this},this.updateTree=(t,n,o)=>{t&&n&&(this._root.dimension=[t,n]);const c=[this._root];for(this._bond=[],this._tabs=[];c.length;){const h=[],r=c.length;for(let a=0;a<r;a++){const i=c.shift(),d=l.getSize(i),w=l.getTotalWeight(i);let g=0,T=0,b=i.children[0];const _=i.dimension,v=i.coordinate;i&&(h.push(i.coordinate),i.type==u.Tabset&&this._tabs.push(i),i.children.forEach((p,$)=>{if(c.push(p),p.dimension=[..._],p.coordinate=[...v],p.weight=p.weight/w*100,$===i.children.length-1)p.dimension[i.type]=d-T,p.weight=100-g;else{p.weight=p.weight/w*100;const j=d*(p.weight/100);p.dimension[i.type]=j,g+=p.weight,T+=p.dimension[i.type]}p.dimension[i.type]=p.dimension[i.type],p.prev&&(p.coordinate[i.type]=b.coordinate[i.type]+b.dimension[i.type]+8,p.prev.dimension=[..._],p.prev.coordinate=[...v],p.prev.dimension[i.type]=8,p.prev.coordinate[i.type]=b.coordinate[i.type]+b.dimension[i.type],p.prev.type=1-i.type,b=p,this._bond.push(p.prev))}))}}return o&&o(),this.getElements()},this.getElements=()=>({tabs:this._tabs.map(t=>l.json(t)),bonds:this._bond.map(t=>l.json(t))}),this.jsonify=()=>l.json(this._root);let s=Object.assign({layout:null,range:1,dimension:[0,0],coordinate:[0,0],tabs:Array.from({length:e.range??1},(t,n)=>`tab-${n+1}`)},e);this._tabs=[],this._bond=[],this._settings=s,this._root=new m(u.Row,100,null),this._root.coordinate=this._settings.coordinate,this._root.dimension=this._settings.dimension,this.createTree()}calculate(e,s=0){s==0&&(this._bond=[],this._tabs=[]);const t=l.getSize(e),n=l.getTotalWeight(e);let o=e.children[0],c=0,h=0;e.type==u.Tabset&&this._tabs.push(e),e.children.forEach((r,a)=>{r.dimension=[...e.dimension],r.coordinate=[...e.coordinate],a===e.children.length-1?(r.dimension[e.type]=t-c,r.weight=100-h):(r.weight=r.weight/n*100,r.dimension[e.type]=t*(r.weight/100),h+=r.weight,c+=r.dimension[e.type]),a!==0&&(r.coordinate[e.type]=o.coordinate[e.type]+o.dimension[e.type]+8,r.prev&&(r.prev.dimension=[...e.dimension],r.prev.coordinate=[...e.coordinate],r.prev.dimension[e.type]=8,r.prev.coordinate[e.type]=o.coordinate[e.type]+o.dimension[e.type],this._bond.push(r.prev)),o=r)}),e.type==u.Tabset&&this._tabs.push(e),e.children.forEach(r=>{r.children.length&&this.calculate(r,s+1)})}}return B});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ILayout, LayoutTree, AreaValue } from "../types";
|
|
2
|
+
import { Node, Bond } from "../lib";
|
|
3
|
+
export type LayoutOptions = {
|
|
4
|
+
layout?: LayoutTree;
|
|
5
|
+
range?: number;
|
|
6
|
+
dimension?: [number, number];
|
|
7
|
+
tabs?: string[];
|
|
8
|
+
};
|
|
9
|
+
export type Settings = {
|
|
10
|
+
layout: LayoutTree;
|
|
11
|
+
range: number;
|
|
12
|
+
dimension: [number, number];
|
|
13
|
+
coordinate: [number, number];
|
|
14
|
+
tabs: string[];
|
|
15
|
+
};
|
|
16
|
+
export default class Layout implements ILayout<Node, Bond> {
|
|
17
|
+
_root: Node;
|
|
18
|
+
_tabs: Node[];
|
|
19
|
+
_bond: Bond[];
|
|
20
|
+
_settings: Settings;
|
|
21
|
+
constructor(options?: LayoutOptions);
|
|
22
|
+
shiftTree: (srcId: string, desId: string, area: AreaValue) => void;
|
|
23
|
+
private createTree;
|
|
24
|
+
updateTree: (width?: number, height?: number, hook?: Function) => {
|
|
25
|
+
tabs: {
|
|
26
|
+
type: any;
|
|
27
|
+
dimension: any;
|
|
28
|
+
coordinate: any;
|
|
29
|
+
name: any;
|
|
30
|
+
children: any;
|
|
31
|
+
}[];
|
|
32
|
+
bonds: {
|
|
33
|
+
type: any;
|
|
34
|
+
dimension: any;
|
|
35
|
+
coordinate: any;
|
|
36
|
+
name: any;
|
|
37
|
+
children: any;
|
|
38
|
+
}[];
|
|
39
|
+
};
|
|
40
|
+
calculate(node: Node, level?: number): void;
|
|
41
|
+
getElements: () => {
|
|
42
|
+
tabs: {
|
|
43
|
+
type: any;
|
|
44
|
+
dimension: any;
|
|
45
|
+
coordinate: any;
|
|
46
|
+
name: any;
|
|
47
|
+
children: any;
|
|
48
|
+
}[];
|
|
49
|
+
bonds: {
|
|
50
|
+
type: any;
|
|
51
|
+
dimension: any;
|
|
52
|
+
coordinate: any;
|
|
53
|
+
name: any;
|
|
54
|
+
children: any;
|
|
55
|
+
}[];
|
|
56
|
+
};
|
|
57
|
+
jsonify: () => {
|
|
58
|
+
type: any;
|
|
59
|
+
dimension: any;
|
|
60
|
+
coordinate: any;
|
|
61
|
+
name: any;
|
|
62
|
+
children: any;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { INode, IBond, TypeValue } from '../types';
|
|
2
|
+
export declare class Node implements INode<Node, Bond> {
|
|
3
|
+
type: TypeValue;
|
|
4
|
+
name: string | null;
|
|
5
|
+
weight: number;
|
|
6
|
+
parent: Node | null;
|
|
7
|
+
children: Node[];
|
|
8
|
+
dimension: [number, number];
|
|
9
|
+
coordinate: [number, number];
|
|
10
|
+
prev: Bond | null;
|
|
11
|
+
next: Bond | null;
|
|
12
|
+
constructor(type: TypeValue, weight: number, parent: Node | null);
|
|
13
|
+
}
|
|
14
|
+
export declare class Bond implements IBond<Node> {
|
|
15
|
+
type: TypeValue;
|
|
16
|
+
name: string | null;
|
|
17
|
+
prev: Node;
|
|
18
|
+
next: Node;
|
|
19
|
+
dimension: [number, number];
|
|
20
|
+
coordinate: [number, number];
|
|
21
|
+
constructor(type: TypeValue, prev: Node, next: Node);
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { IMethods, AreaValue, TypeValue } from "../types";
|
|
2
|
+
import { Node } from "./Element";
|
|
3
|
+
import { Settings } from "../app";
|
|
4
|
+
export declare class Helper implements IMethods {
|
|
5
|
+
static createBinaryTree: (root: Node, settings: Settings) => void;
|
|
6
|
+
static fixSrc: (src: Node) => void;
|
|
7
|
+
static detach(src: Node, flag: Boolean): void;
|
|
8
|
+
static attach(des: Node, src: Node, area: AreaValue): void;
|
|
9
|
+
static fixBond: (node: Node, type: TypeValue) => void;
|
|
10
|
+
static getTotalWeight(node: Node): number;
|
|
11
|
+
static getTotalNodes(node: Node): number;
|
|
12
|
+
static getSize(node: Node): number;
|
|
13
|
+
static addTab: (child: Node, names: string[]) => void;
|
|
14
|
+
static json: (node: any) => {
|
|
15
|
+
type: any;
|
|
16
|
+
dimension: any;
|
|
17
|
+
coordinate: any;
|
|
18
|
+
name: any;
|
|
19
|
+
children: any;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function emitEvent(type: string, detail?: {}, elem?: Document): boolean | undefined;
|