forked from runjuu/column-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn-resizer.ts
212 lines (176 loc) · 5.36 KB
/
column-resizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { BarActionType, ItemType, SizeRelatedInfo } from './types';
import { Resizer } from './resizer';
import {
ColumnBar,
ColumnBarConfig,
ColumnSection,
ColumnSectionConfig,
DispatchBarAction,
} from './column-items';
import {
BarActionScanResult,
calculateCoordinateOffset,
collectSizeRelatedInfo,
ColumnItemsCache,
createBarStore,
dispatchResizerEvent,
isDisabledResponsive,
isSolidItem,
parseResizerItems,
resizerItemAttributes,
ResizerItems,
ResizerEventHub,
} from './utils';
export type { ColumnSectionConfig, ColumnBarConfig };
export type ColumnResizerConfig = {
vertical: boolean;
beforeApplyResizer?: (resizer: Resizer) => void;
};
export class ColumnResizer {
styles = {
container: <T>(style?: T) =>
({
...style,
display: 'flex',
}) as const,
section: <T>(config: ColumnSectionConfig, style?: T) => ({
...style,
...ColumnSection.getStyle(config, this.config.vertical),
}),
bar: <T>(config: ColumnBarConfig, style?: T) => ({
...style,
...ColumnBar.getStyle(config),
}),
};
attributes = {
bar: resizerItemAttributes(ItemType.BAR),
section: resizerItemAttributes(ItemType.SECTION),
};
private itemsCache = new ColumnItemsCache();
private eventHub = new ResizerEventHub();
private container: HTMLElement | null = null;
private barStore: ReturnType<typeof createBarStore>;
private get axis() {
return this.config.vertical ? 'y' : 'x';
}
private get dimension() {
return this.config.vertical ? 'height' : 'width';
}
private get direction() {
return this.config.vertical ? 'column' : 'row';
}
get on() {
return this.eventHub.watchResizerEvent;
}
constructor(public readonly config: Readonly<ColumnResizerConfig>) {
this.barStore = createBarStore({
calculateOffset: (current, original) =>
calculateCoordinateOffset(current, original)[this.axis],
getSizeRelatedInfo: () => this.makeSizeInfos(),
});
}
init(container: HTMLElement | null) {
this.dispose();
this.container = container;
if (container) {
this.itemsCache.update(
parseResizerItems(container).map((item) => {
switch (item.type) {
case ItemType.BAR:
return new ColumnBar(item, this.dispatchBarAction);
case ItemType.SECTION:
return new ColumnSection(item);
}
}),
);
this.initStyles(container, this.itemsCache.getItems());
this.sizeRelatedInfoChange(this.makeSizeInfos());
this.barStore.subscribe((state) => {
this.monitorBarStatusChanges(state);
this.sizeRelatedInfoChange(state);
});
}
}
dispose() {
this.container = null;
this.itemsCache.reset();
this.barStore.unsubscribeAll();
this.eventHub.reset();
}
getResizer(): Resizer {
return new Resizer(this.makeSizeInfos());
}
applyResizer(resizer: Resizer): void {
this.sizeRelatedInfoChange(resizer.getResult());
}
private dispatchBarAction: DispatchBarAction = (elm, action) => {
const barIndex = this.itemsCache.getItemIndex(elm);
if (barIndex) {
this.barStore.dispatch({ ...action, barIndex });
}
};
private sizeRelatedInfoChange(info: SizeRelatedInfo | BarActionScanResult) {
if (info.discard) return;
info = (() => {
if (typeof this.config.beforeApplyResizer === 'function') {
const resizer = new Resizer(info);
this.config.beforeApplyResizer(resizer);
return resizer.getResult();
} else {
return info;
}
})();
if (info.discard) return;
info.sizeInfoArray.forEach((sizeInfo) => {
const item = this.itemsCache.getItem(sizeInfo.elm);
if (item instanceof ColumnSection) {
item.update({ sizeInfo, flexGrowRatio: info.flexGrowRatio });
}
});
}
private monitorBarStatusChanges({ type }: BarActionScanResult) {
switch (type) {
case BarActionType.ACTIVATE:
return dispatchResizerEvent(this.container, 'column:activate', null);
case BarActionType.DEACTIVATE:
return dispatchResizerEvent(this.container, 'column:after-resizing', null);
default:
return;
}
}
private makeSizeInfos(): SizeRelatedInfo {
const { collect, getResult } = collectSizeRelatedInfo();
this.itemsCache.getItems().forEach((item) => {
if (item instanceof ColumnBar) {
collect({
elm: item.elm,
disableResponsive: true,
isSolid: true,
currentSize: item.elm.getBoundingClientRect()[this.dimension],
});
}
if (item instanceof ColumnSection) {
collect({
elm: item.elm,
maxSize: item.config.maxSize,
minSize: item.config.minSize,
disableResponsive: isDisabledResponsive(item.config),
isSolid: isSolidItem(item.config),
currentSize: item.elm.getBoundingClientRect()[this.dimension],
});
}
});
return getResult();
}
private initStyles(container: HTMLElement, items: ResizerItems) {
Object.assign(container.style, this.styles.container());
items.forEach((item) => {
if (item instanceof ColumnBar) {
Object.assign(item.elm.style, this.styles.bar(item.config));
}
if (item instanceof ColumnSection) {
Object.assign(item.elm.style, this.styles.section(item.config));
}
});
}
}