@dynamix-layout/core 0.0.2-beta.0 → 0.0.6

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/src/lib/Helper.ts DELETED
@@ -1,259 +0,0 @@
1
- import { IMethods, Type, AreaValue, TypeValue } from "../types"
2
- import { Node, Bond } from "./Element";
3
- import { AreaIndex, AreaType } from "./Constant";
4
- import { Settings } from "../app";
5
- export class Helper implements IMethods {
6
-
7
- static createBinaryTree = (root: Node, settings: Settings) => {
8
- const { range } = settings;
9
- const tabs = [...settings.tabs]
10
- if (range == 1) {
11
-
12
- const col = new Node(Type.Column, 100, root)
13
- root.children = [col];
14
- Helper.addTab(col, tabs);
15
- return;
16
- };
17
-
18
- const queue: Node[] = [root];
19
-
20
- const levels: Node[][] = [];
21
-
22
- let toggle = 1;
23
- let count = 1;
24
-
25
- while (count < (range as number) || queue.length) {
26
- const level: Node[] = [];
27
- const length = queue.length;
28
-
29
- for (let i = 0; i < length; i++) {
30
-
31
- const parent = queue.shift() as Node;
32
- const pIndex = parent.parent?.children.indexOf(parent) ?? 0;
33
- parent.name = `r${levels.length}/c${i}/tb${pIndex}`;
34
-
35
- if (parent) {
36
- level.push(parent);
37
-
38
- if (count < (range as number)) {
39
- parent.children[0] = new Node(toggle, 50, parent);
40
- queue.push(parent.children[0]);
41
- }
42
-
43
- if (count < (range as number)) {
44
- parent.children[1] = new Node(toggle, 50, parent);
45
- queue.push(parent.children[1]);
46
-
47
- count++;
48
-
49
- const bond = new Bond(
50
- toggle,
51
- parent.children[0],
52
- parent.children[1]
53
- );
54
-
55
- bond.name = `r${levels.length}/c${i}/b${pIndex}-${pIndex + 1}`;
56
- }
57
- }
58
- }
59
-
60
- levels.push(level);
61
- toggle = 1 - toggle;
62
- }
63
-
64
- const last = [
65
- ...levels[levels.length - 1],
66
- ...levels[levels.length - 2]
67
- ]
68
-
69
- last.forEach((child: Node) => {
70
- Helper.addTab(child, tabs)
71
- })
72
-
73
- return;
74
- }
75
-
76
- static fixSrc = (src: Node) => {
77
- const parent = src.parent;
78
- if (!parent) return;
79
- const index = parent.children.indexOf(src);
80
- const peer = parent.children[1 - index];
81
- const grand = parent.parent;
82
- const pIndex = grand?.children.indexOf(parent) as number;
83
-
84
- switch (peer.type) {
85
- case Type.Tabset:
86
- const weight = parent.weight;
87
- grand?.children.splice(pIndex, 1);
88
- grand?.children.splice(pIndex, 0, peer);
89
- peer.weight = weight;
90
- peer.parent = grand;
91
- Helper.fixBond(peer, peer.type);
92
-
93
- break;
94
- default:
95
-
96
- if (!grand) {
97
- parent.children.splice(index, 1);
98
- break;
99
- }
100
- const count = peer.children.length;
101
- const affacted = (8 * (count - 1)) * 100 / grand.dimension[peer.type];
102
-
103
- peer.children.forEach((child: Node) => {
104
- child.weight = ((parent.weight - affacted) * child.weight) / 100;
105
- });
106
-
107
- const first = peer.children[0];
108
- const last = peer.children[count - 1];
109
-
110
- grand.children = [
111
- ...grand.children.slice(0, pIndex),
112
- ...peer.children,
113
- ...grand.children.slice(pIndex + 1)
114
- ]
115
-
116
- peer.children.forEach((child: Node) => {
117
- child.parent = grand;
118
- })
119
-
120
- Helper.fixBond(first, peer.type);
121
- Helper.fixBond(last, peer.type);
122
- break;
123
- }
124
- }
125
-
126
- static detach(src: Node, flag: Boolean) {
127
- const parent = src.parent;
128
- if (!parent) return;
129
-
130
- const index = src.parent?.children.indexOf(src) as number;
131
- const range = parent?.children.length as number;
132
- const affacted = 8 * 100 / parent.dimension[parent.type];
133
- if (!flag) return Helper.fixSrc(src);
134
-
135
-
136
-
137
- if (index > 0 && index < range - 1) {
138
- const weight = (src.weight + affacted) / 2;
139
- const next = parent.children[index + 1];
140
- const prev = parent.children[index - 1];
141
- next.weight += weight;
142
- prev.weight += weight;
143
- new Bond(src.type, prev, next);
144
- }
145
-
146
- if (index === range - 1) {
147
- const prev = parent.children[index - 1];
148
- prev.weight += src.weight + affacted;
149
- prev.next = null;
150
- }
151
-
152
- if (index === 0) {
153
- const next = parent.children[index + 1];
154
- next.weight += src.weight + affacted;
155
- next.prev = null;
156
- }
157
-
158
- parent.children.splice(index, 1);
159
- }
160
-
161
- static attach(des: Node, src: Node, area: AreaValue) {
162
- const parent = des.parent;
163
- if (!parent) return;
164
- const index = parent.children.indexOf(des) as number;
165
-
166
- switch (AreaType[area]) {
167
- case 1 - parent.type:
168
- const insert = index + AreaIndex[area];
169
- const affacted = 8 * 100 / parent.dimension[parent.type];
170
- src.weight = des.weight = (des.weight - affacted) / 2;
171
- src.parent = parent;
172
- parent.children.splice(insert, 0, src);
173
- Helper.fixBond(src, parent.type);
174
- break;
175
-
176
- case parent.type:
177
-
178
- const weight = des.weight / 2;
179
- const children = AreaIndex[area] == 0 ? [src, des] : [des, src];
180
- const rowcol = new Node(1 - parent.type, des.weight, parent);
181
- rowcol.children = children;
182
- parent.children[index] = rowcol;
183
-
184
- children.forEach((child: Node) => {
185
- child.weight = weight;
186
- child.parent = rowcol;
187
- })
188
-
189
- this.fixBond(rowcol, parent.type);
190
- this.fixBond(src, parent.type);
191
- this.fixBond(des, parent.type);
192
- break;
193
-
194
- case Type.Tab:
195
- des.children.push(...src.children);
196
- src.children.forEach((child: Node) => {
197
- child.parent = des;
198
- })
199
- break;
200
-
201
- }
202
- }
203
-
204
- static fixBond = (node: Node, type: TypeValue) => {
205
- const parent = node.parent;
206
- if (!parent) return;
207
- const index = parent.children.indexOf(node) as number;
208
- const range = parent.children.length;
209
-
210
- if (index > 0 && index < range - 1) {
211
- new Bond(type, parent.children[index - 1], parent.children[index]);
212
- new Bond(type, parent.children[index], parent.children[index + 1]);
213
- }
214
-
215
- if (index === range - 1) {
216
- new Bond(type, parent.children[index - 1], parent.children[index]);
217
- parent.children[index].next = null;
218
- }
219
-
220
- if (index === 0) {
221
- new Bond(type, parent.children[index], parent.children[index + 1]);
222
- parent.children[index].prev = null;
223
- }
224
- }
225
-
226
- static getTotalWeight(node: Node): number {
227
- return node.children.reduce((acc, cur) => acc + (cur.weight), 0) ?? 100;
228
- }
229
-
230
- static getTotalNodes(node: Node): number {
231
-
232
- return node.children.length;
233
- }
234
-
235
- static getSize(node: Node): number {
236
- return node.dimension[node.type] - ((this.getTotalNodes(node) - 1) * 8);
237
- }
238
-
239
- static addTab = (child: Node, names: string[]) => {
240
- if (child.children.length !== 0) return;
241
- child.type = Type.Tabset;
242
- const tab = new Node(Type.Tab, 100, child);
243
- const name = names.shift() as string;
244
- tab.name = name;
245
- child.children = [tab];
246
- return;
247
- }
248
-
249
- static json = (node: any) => {
250
- return {
251
- type: node.type,
252
- dimension: node.dimension,
253
- coordinate: node.coordinate,
254
- name: node.name,
255
- children: node.children?.map((child: any) => Helper.json(child))
256
- }
257
- }
258
- }
259
-
package/src/lib/Method.ts DELETED
@@ -1,12 +0,0 @@
1
- export function emitEvent (type:string, detail = {}, elem = document as Document) {
2
-
3
- if (!type) return;
4
-
5
- let event = new CustomEvent(type, {
6
- bubbles: true,
7
- cancelable: true,
8
- detail: detail
9
- });
10
-
11
- return elem.dispatchEvent(event);
12
- }
package/src/lib/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './Element';
2
- export * from './Helper';
3
- export * from './Constant';
4
- export * from './Method';
@@ -1,116 +0,0 @@
1
- /**
2
- * @file Element.ts
3
- * @description Interface to create concrete class.
4
- */
5
-
6
- /**
7
- * @interface INode
8
- * @description Interface for Node.
9
- * @property {Type} type : Type of Node (Row, Column, Tabset, Tab).
10
- * @property {number} weight : Weight of Node.
11
- * @property {[number,number]} dimension : Dimension of Node.
12
- * @property {[number,number]} coordinate : Coordinate of Node.
13
- * @property {Node | null} parent : Parent Node ( Node | null ).
14
- * @property {Node[]} children : Children Node ( Node[] ).
15
- * @property {Bond | null} prev : Previous Bond ( Bond | null ).
16
- * @property {Bond | null} next : Next Bond ( Bond | null ).
17
- *
18
- */
19
- export interface INode<T, K> {
20
- type: TypeValue;
21
- weight: number;
22
- dimension: [number, number];
23
- coordinate: [number, number];
24
- parent: T | null;
25
- children: T[];
26
- prev: K | null;
27
- next: K | null;
28
- }
29
-
30
- /**
31
- * @interface IBond
32
- * @description Interface for Bond.
33
- * @property {Type} type : Type of Bond (Row, Column, Tabset, Tab).
34
- * @property {Node} prev : Previous Node ( Node | null ).
35
- * @property {Node} next : Next Node ( Node | null ).
36
- * @property {[number,number]} dimension : Dimension of Bond.
37
- * @property {[number,number]} coordinate : Coordinate of Bond.
38
- *
39
- */
40
- export interface IBond<T> {
41
- type: TypeValue;
42
- prev: T;
43
- next: T;
44
- dimension: [number, number];
45
- coordinate: [number, number];
46
- }
47
-
48
- /**
49
- * @interface ILayout
50
- * @description Interface for Layout.
51
- * @property {Node} root : Root Node.
52
- * @property {Node[]} tabs : Tabs Node.
53
- * @property {Bond[]} bond : Bond between two components.
54
- *
55
- */
56
- export interface ILayout<T, K> {
57
- _root: T;
58
- _tabs: T[];
59
- _bond: K[];
60
-
61
- }
62
-
63
- /**
64
- * @enum Type
65
- * @description Enum for Type.
66
- * @property {Row} Row : Row Type.
67
- * @property {Column} Column : Column Type.
68
- * @property {Tabset} Tabset : Tabset Type.
69
- * @property {Tab} Tab : Tab Type.
70
- */
71
- export const Type = {
72
- Row: 0,
73
- Column: 1,
74
- Tabset: 2,
75
- Tab: 3
76
- }
77
-
78
-
79
-
80
- /**
81
- * @enum TypeValue : Value of Type.
82
- */
83
- export type TypeValue = number;
84
-
85
- /**
86
- * @enum Area
87
- * @description Enum for Area.
88
- * @property {Left} Left : Left Area.
89
- * @property {Right} Right : Right Area.
90
- * @property {Top} Top : Top Area.
91
- * @property {Bottom} Bottom : Bottom Area.
92
- * @property {Cover} Cover : Cover Area.
93
- */
94
- export const Area = {
95
- Left: "left" as Left,
96
- Right: "right" as Right,
97
- Top: "top" as Top,
98
- Bottom: "bottom" as Bottom,
99
- Cover: "cover" as Cover
100
- }
101
-
102
- export type Left = "left";
103
- export type Right = "right";
104
- export type Top = "top";
105
- export type Bottom = "bottom";
106
- export type Cover = "cover";
107
-
108
- export type AreaValue = typeof Area[keyof typeof Area];
109
-
110
- export type LayoutTree = {
111
- type: TypeValue;
112
- weight: number;
113
- children: LayoutTree[];
114
- name: string;
115
- }
116
-
@@ -1,4 +0,0 @@
1
- /**
2
- * Methods
3
- */
4
- export interface IMethods {}
@@ -1,2 +0,0 @@
1
- export * from './Element';
2
- export * from './Method';