-
Notifications
You must be signed in to change notification settings - Fork 31
/
bottom_bar.ts
286 lines (246 loc) · 8.69 KB
/
bottom_bar.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { Component, onWillUpdateProps, useRef, useState } from "@odoo/owl";
import { BACKGROUND_GRAY_COLOR, HEADER_WIDTH, TEXT_HEADER_COLOR } from "../../constants";
import { deepEquals } from "../../helpers";
import { MenuItemRegistry } from "../../registries/menu_items_registry";
import { _t } from "../../translation";
import { MenuMouseEvent, Pixel, Rect, SpreadsheetChildEnv, UID } from "../../types";
import { Ripple } from "../animation/ripple";
import { BottomBarSheet } from "../bottom_bar_sheet/bottom_bar_sheet";
import { BottomBarStatistic } from "../bottom_bar_statistic/bottom_bar_statistic";
import { css } from "../helpers/css";
import { useDragAndDropListItems } from "../helpers/drag_and_drop_hook";
import { Menu, MenuState } from "../menu/menu";
// -----------------------------------------------------------------------------
// SpreadSheet
// -----------------------------------------------------------------------------
const MENU_MAX_HEIGHT = 250;
css/* scss */ `
.o-spreadsheet-bottom-bar {
color: ${TEXT_HEADER_COLOR};
background-color: ${BACKGROUND_GRAY_COLOR};
padding-left: ${HEADER_WIDTH}px;
font-size: 15px;
border-top: 1px solid lightgrey;
.o-sheet-item {
cursor: pointer;
&:hover {
background-color: rgba(0, 0, 0, 0.08);
}
}
.o-all-sheets {
max-width: 70%;
.o-bottom-bar-fade-out {
background-image: linear-gradient(-90deg, #cfcfcf, transparent 1%);
}
.o-bottom-bar-fade-in {
background-image: linear-gradient(90deg, #cfcfcf, transparent 1%);
}
}
.o-bottom-bar-arrows {
.o-bottom-bar-arrow {
cursor: pointer;
&:hover:not([class*="o-disabled"]) {
.o-icon {
opacity: 0.9;
}
}
.o-icon {
height: 18px;
width: 18px;
}
}
}
}
`;
interface BottomBarSheetItem {
id: UID;
name: string;
}
interface Props {
onClick: () => void;
}
interface BottomBarMenuState extends MenuState {
menuId: UID | undefined;
}
export class BottomBar extends Component<Props, SpreadsheetChildEnv> {
static template = "o-spreadsheet-BottomBar";
static props = {
onClick: Function,
};
static components = { Menu, Ripple, BottomBarSheet, BottomBarStatistic };
private bottomBarRef = useRef("bottomBar");
private sheetListRef = useRef("sheetList");
private dragAndDrop = useDragAndDropListItems();
private targetScroll: number | undefined = undefined;
private state = useState({
isSheetListScrollableLeft: false,
isSheetListScrollableRight: false,
});
menuMaxHeight = MENU_MAX_HEIGHT;
menuState: BottomBarMenuState = useState({
isOpen: false,
menuId: undefined,
position: null,
menuItems: [],
});
sheetList = this.getVisibleSheets();
setup() {
onWillUpdateProps(() => {
this.updateScrollState();
const visibleSheets = this.getVisibleSheets();
// Cancel sheet dragging when there is a change in the sheets
if (!deepEquals(this.sheetList, visibleSheets)) {
this.dragAndDrop.cancel();
}
this.sheetList = visibleSheets;
});
}
clickAddSheet(ev: MouseEvent) {
const activeSheetId = this.env.model.getters.getActiveSheetId();
const position =
this.env.model.getters.getSheetIds().findIndex((sheetId) => sheetId === activeSheetId) + 1;
const sheetId = this.env.model.uuidGenerator.uuidv4();
const name = this.env.model.getters.getNextSheetName(_t("Sheet"));
this.env.model.dispatch("CREATE_SHEET", { sheetId, position, name });
this.env.model.dispatch("ACTIVATE_SHEET", { sheetIdFrom: activeSheetId, sheetIdTo: sheetId });
}
getVisibleSheets(): BottomBarSheetItem[] {
return this.env.model.getters.getVisibleSheetIds().map((sheetId) => {
const sheet = this.env.model.getters.getSheet(sheetId);
return { id: sheet.id, name: sheet.name };
});
}
clickListSheets(ev: MouseEvent) {
const registry = new MenuItemRegistry();
const from = this.env.model.getters.getActiveSheetId();
let i = 0;
for (const sheetId of this.env.model.getters.getSheetIds()) {
const sheet = this.env.model.getters.getSheet(sheetId);
registry.add(sheetId, {
name: sheet.name,
sequence: i,
isReadonlyAllowed: true,
textColor: sheet.isVisible ? undefined : "grey",
execute: (env) => {
env.model.dispatch("ACTIVATE_SHEET", { sheetIdFrom: from, sheetIdTo: sheetId });
},
});
i++;
}
const target = ev.currentTarget as HTMLElement;
const { left } = target.getBoundingClientRect();
const top = this.bottomBarRef.el!.getBoundingClientRect().top;
this.openContextMenu(left, top, "listSheets", registry);
}
openContextMenu(x: Pixel, y: Pixel, menuId: UID, registry: MenuItemRegistry) {
this.menuState.isOpen = true;
this.menuState.menuId = menuId;
this.menuState.menuItems = registry.getMenuItems();
this.menuState.position = { x, y };
}
onSheetContextMenu(sheetId: UID, registry: MenuItemRegistry, ev: MenuMouseEvent) {
const target = ev.currentTarget as HTMLElement;
const { top, left } = target.getBoundingClientRect();
if (ev.closedMenuId === sheetId) {
this.closeMenu();
return;
}
this.openContextMenu(left, top, sheetId, registry);
}
closeMenu() {
this.menuState.isOpen = false;
this.menuState.menuId = undefined;
this.menuState.menuItems = [];
this.menuState.position = null;
}
closeContextMenuWithId(menuId: UID) {
if (this.menuState.menuId === menuId) {
this.closeMenu();
}
}
onWheel(ev: WheelEvent) {
this.targetScroll = undefined;
const target = ev.currentTarget as HTMLElement;
target.scrollLeft += ev.deltaY * 0.5;
}
onScroll() {
this.updateScrollState();
if (this.targetScroll === this.sheetListCurrentScroll) {
this.targetScroll = undefined;
}
}
onArrowLeft(ev: MouseEvent) {
if (!this.state.isSheetListScrollableLeft) return;
if (!this.targetScroll) this.targetScroll = this.sheetListCurrentScroll;
const newScroll = this.targetScroll - this.sheetListWidth;
this.scrollSheetListTo(Math.max(0, newScroll));
}
onArrowRight(ev: MouseEvent) {
if (!this.state.isSheetListScrollableRight) return;
if (!this.targetScroll) this.targetScroll = this.sheetListCurrentScroll;
const newScroll = this.targetScroll + this.sheetListWidth;
this.scrollSheetListTo(Math.min(this.sheetListMaxScroll, newScroll));
}
private updateScrollState() {
this.state.isSheetListScrollableLeft = this.sheetListCurrentScroll > 0;
this.state.isSheetListScrollableRight = this.sheetListCurrentScroll < this.sheetListMaxScroll;
}
private scrollSheetListTo(scroll: number) {
if (!this.sheetListRef.el) return;
this.targetScroll = scroll;
this.sheetListRef.el.scrollTo({ top: 0, left: scroll, behavior: "smooth" });
}
onSheetMouseDown(sheetId: UID, event: MouseEvent) {
if (event.button !== 0 || this.env.model.getters.isReadonly()) return;
this.closeMenu();
const visibleSheets = this.getVisibleSheets();
const sheetRects = this.getSheetItemRects();
const sheets = visibleSheets.map((sheet, index) => ({
id: sheet.id,
size: sheetRects[index].width,
position: sheetRects[index].x,
}));
this.dragAndDrop.start("horizontal", {
draggedItemId: sheetId,
initialMousePosition: event.clientX,
items: sheets,
containerEl: this.sheetListRef.el!,
onDragEnd: (sheetId: UID, finalIndex: number) => this.onDragEnd(sheetId, finalIndex),
});
}
private onDragEnd(sheetId: UID, finalIndex: number) {
const originalIndex = this.getVisibleSheets().findIndex((sheet) => sheet.id === sheetId);
const delta = finalIndex - originalIndex;
if (sheetId && delta !== 0) {
this.env.model.dispatch("MOVE_SHEET", {
sheetId: sheetId,
delta: delta,
});
}
}
getSheetStyle(sheetId: UID): string {
return this.dragAndDrop.itemsStyle[sheetId] || "";
}
private getSheetItemRects(): Rect[] {
return Array.from(this.bottomBarRef.el!.querySelectorAll<HTMLElement>(`.o-sheet`))
.map((sheetEl) => sheetEl.getBoundingClientRect())
.map((rect) => ({
x: rect.x,
width: rect.width - 1, // -1 to compensate negative margin
y: rect.y,
height: rect.height,
}));
}
get sheetListCurrentScroll() {
if (!this.sheetListRef.el) return 0;
return this.sheetListRef.el.scrollLeft;
}
get sheetListWidth() {
if (!this.sheetListRef.el) return 0;
return this.sheetListRef.el.clientWidth;
}
get sheetListMaxScroll() {
if (!this.sheetListRef.el) return 0;
return this.sheetListRef.el.scrollWidth - this.sheetListRef.el.clientWidth;
}
}