-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathCellMenu.svelte
180 lines (162 loc) · 4.73 KB
/
CellMenu.svelte
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
<script lang="ts">
import { onMount } from "svelte";
import CellMenuIcons from "./CellMenuIcons.svelte";
import type { I18nFormatter } from "js/utils/src";
import type { SortDirection } from "./context/table_context";
export let x: number;
export let y: number;
export let on_add_row_above: () => void;
export let on_add_row_below: () => void;
export let on_add_column_left: () => void;
export let on_add_column_right: () => void;
export let row: number;
export let col_count: [number, "fixed" | "dynamic"];
export let row_count: [number, "fixed" | "dynamic"];
export let on_delete_row: () => void;
export let on_delete_col: () => void;
export let can_delete_rows: boolean;
export let can_delete_cols: boolean;
export let on_sort: (direction: SortDirection) => void = () => {};
export let on_clear_sort: () => void = () => {};
export let sort_direction: SortDirection | null = null;
export let sort_priority: number | null = null;
export let i18n: I18nFormatter;
let menu_element: HTMLDivElement;
$: is_header = row === -1;
$: can_add_rows = row_count[1] === "dynamic";
$: can_add_columns = col_count[1] === "dynamic";
onMount(() => {
position_menu();
});
function position_menu(): void {
if (!menu_element) return;
const viewport_width = window.innerWidth;
const viewport_height = window.innerHeight;
const menu_rect = menu_element.getBoundingClientRect();
let new_x = x - 30;
let new_y = y - 20;
if (new_x + menu_rect.width > viewport_width) {
new_x = x - menu_rect.width + 10;
}
if (new_y + menu_rect.height > viewport_height) {
new_y = y - menu_rect.height + 10;
}
menu_element.style.left = `${new_x}px`;
menu_element.style.top = `${new_y}px`;
}
</script>
<div bind:this={menu_element} class="cell-menu">
{#if is_header}
<button
on:click={() => on_sort("asc")}
class:active={sort_direction === "asc"}
>
<CellMenuIcons icon="sort-asc" />
{i18n("dataframe.sort_ascending")}
{#if sort_direction === "asc" && sort_priority !== null}
<span class="priority">{sort_priority}</span>
{/if}
</button>
<button
on:click={() => on_sort("desc")}
class:active={sort_direction === "desc"}
>
<CellMenuIcons icon="sort-desc" />
{i18n("dataframe.sort_descending")}
{#if sort_direction === "desc" && sort_priority !== null}
<span class="priority">{sort_priority}</span>
{/if}
</button>
<button on:click={on_clear_sort}>
<CellMenuIcons icon="clear-sort" />
{i18n("dataframe.clear_sort")}
</button>
{/if}
{#if !is_header && can_add_rows}
<button on:click={() => on_add_row_above()}>
<CellMenuIcons icon="add-row-above" />
{i18n("dataframe.add_row_above")}
</button>
<button on:click={() => on_add_row_below()}>
<CellMenuIcons icon="add-row-below" />
{i18n("dataframe.add_row_below")}
</button>
{#if can_delete_rows}
<button on:click={on_delete_row} class="delete">
<CellMenuIcons icon="delete-row" />
{i18n("dataframe.delete_row")}
</button>
{/if}
{/if}
{#if can_add_columns}
<button on:click={() => on_add_column_left()}>
<CellMenuIcons icon="add-column-left" />
{i18n("dataframe.add_column_left")}
</button>
<button on:click={() => on_add_column_right()}>
<CellMenuIcons icon="add-column-right" />
{i18n("dataframe.add_column_right")}
</button>
{#if can_delete_cols}
<button on:click={on_delete_col} class="delete">
<CellMenuIcons icon="delete-column" />
{i18n("dataframe.delete_column")}
</button>
{/if}
{/if}
</div>
<style>
.cell-menu {
position: fixed;
z-index: 9;
background: var(--background-fill-primary);
border: 1px solid var(--border-color-primary);
border-radius: var(--radius-sm);
padding: var(--size-1);
display: flex;
flex-direction: column;
gap: var(--size-1);
box-shadow: var(--shadow-drop-lg);
min-width: 150px;
z-index: var(--layer-1);
}
.cell-menu button {
background: none;
border: none;
cursor: pointer;
text-align: left;
padding: var(--size-1) var(--size-2);
border-radius: var(--radius-sm);
color: var(--body-text-color);
font-size: var(--text-sm);
transition:
background-color 0.2s,
color 0.2s;
display: flex;
align-items: center;
gap: var(--size-2);
position: relative;
}
.cell-menu button.active {
background-color: var(--background-fill-secondary);
}
.cell-menu button:hover {
background-color: var(--background-fill-secondary);
}
.cell-menu button :global(svg) {
fill: currentColor;
transition: fill 0.2s;
}
.priority {
display: flex;
align-items: center;
justify-content: center;
margin-left: auto;
font-size: var(--size-2);
background-color: var(--button-secondary-background-fill);
color: var(--body-text-color);
border-radius: var(--radius-sm);
width: var(--size-2-5);
height: var(--size-2-5);
}
</style>