wx-svelte-grid 1.3.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/license.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 XB Software Sp. z o.o.
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/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "wx-svelte-grid",
3
+ "version": "1.3.0",
4
+ "productTag": "grid",
5
+ "needsTrial": true,
6
+ "type": "module",
7
+ "scripts": {
8
+ "build": "vite build",
9
+ "build:tests": "vite build --mode test",
10
+ "lint": "yarn eslint ./demos ./src --ext .svelte,.ts,.js",
11
+ "start": "vite --open",
12
+ "start:tests": "vite --open=/tests/ --mode test --host 0.0.0.0 --port 5100",
13
+ "test": "true",
14
+ "test:cypress": "cypress run -P ./ --config \"baseUrl=http://localhost:5100/tests\""
15
+ },
16
+ "svelte": "src/index.js",
17
+ "exports": {
18
+ ".": {
19
+ "svelte": "./src/index.js"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/svar-widgets/grid.git"
27
+ },
28
+ "bugs": {
29
+ "url": "https://forum.svar.dev"
30
+ },
31
+ "homepage": "https://svar.dev/svelte/grid/",
32
+ "dependencies": {
33
+ "wx-lib-dom": "0.6.0",
34
+ "wx-lib-state": "1.9.0",
35
+ "wx-lib-svelte": "0.4.0",
36
+ "wx-svelte-menu": "1.3.0",
37
+ "wx-svelte-core": "1.3.0",
38
+ "wx-grid-data-provider": "1.3.0",
39
+ "wx-grid-locales": "1.3.0",
40
+ "wx-grid-store": "1.3.0"
41
+ },
42
+ "files": [
43
+ "src",
44
+ "readme.md",
45
+ "whatsnew.md",
46
+ "license.txt"
47
+ ]
48
+ }
package/readme.md ADDED
@@ -0,0 +1,69 @@
1
+ ### SVAR Grid for Svelte
2
+
3
+ SVAR Grid provides ready to use control for tabular data management
4
+
5
+ ### Useful Links
6
+
7
+ - [Documentation](https://docs.svar.dev/svelte/grid/overview)
8
+ - [How to start guide](https://docs.svar.dev/svelte/grid/getting_started/)
9
+ - [Demos](https://docs.svar.dev/svelte/grid/samples/#/base/willow)
10
+
11
+ ### License
12
+
13
+ SVAR Grid for Svelte is available under MIT license.
14
+
15
+ ### How to Use
16
+
17
+ To use the widget, simply import the package and include the component in your Svelte file:
18
+
19
+ ```svelte
20
+ <script>
21
+ import { Grid } from "wx-svelte-grid";
22
+
23
+ const data = [
24
+ {
25
+ id: 12,
26
+ name: "Alex Brown",
27
+ year: 1974,
28
+ },
29
+ ];
30
+ const columns = [
31
+ {
32
+ id: "name",
33
+ header: "Title",
34
+ flexgrow: 1,
35
+ sort: true,
36
+ editor: "text",
37
+ },
38
+ {
39
+ id: "year",
40
+ header: "Year",
41
+ width: 100,
42
+ sort: true,
43
+ editor: "text",
44
+ },
45
+ ];
46
+ </script>
47
+
48
+ <Grid {data} {columns} />
49
+ ```
50
+
51
+ ### How to Modify
52
+
53
+ Typically, you don't need to modify the code. However, if you wish to do so, follow these steps:
54
+
55
+ 1. Run `yarn` to install dependencies. Note that this project is a monorepo using `yarn` workspaces, so npm will not work
56
+ 2. Start the project in development mode with `yarn start`
57
+
58
+ ### Run Tests
59
+
60
+ To run the test:
61
+
62
+ 1. Start the test examples with:
63
+ ```sh
64
+ yarn start:tests
65
+ ```
66
+ 2. In a separate console, run the end-to-end tests with:
67
+ ```sh
68
+ yarn test:cypress
69
+ ```
@@ -0,0 +1,79 @@
1
+ <script>
2
+ import { getStyle } from "../helpers/columnWidth";
3
+ import { getRenderValue } from "wx-grid-store";
4
+
5
+ export let row;
6
+ export let col;
7
+ export let cellStyle = null;
8
+ export let columnStyle = null;
9
+
10
+ let style, css;
11
+ $: style = getStyle(col.width, col.flexgrow, col.fixed, col.left);
12
+
13
+ function buildCellCss(columnStyle, cellStyle) {
14
+ let css = "wx-cell";
15
+ css += columnStyle ? " " + columnStyle(col) : "";
16
+ css += cellStyle ? " " + cellStyle(row, col) : "";
17
+ return css;
18
+ }
19
+ // params are needed for css update
20
+ $: css = buildCellCss(columnStyle, cellStyle);
21
+ </script>
22
+
23
+ <div
24
+ class={css}
25
+ class:wx-shadow={col.fixed === -1}
26
+ {style}
27
+ data-row-id={row.id}
28
+ data-col-id={col.id}
29
+ >
30
+ {#if col.treetoggle}
31
+ <div class="wx-tree-cell">
32
+ <span style="margin-left:{row.$level * 28}px;" />
33
+ {#if row.$count}
34
+ <i
35
+ data-action="toggle-row"
36
+ class="wx-table-tree-toggle wxi-menu-{row.open !== false
37
+ ? 'down'
38
+ : 'right'}"
39
+ />
40
+ {/if}
41
+ <slot>{getRenderValue(row, col)}</slot>
42
+ </div>
43
+ {:else}
44
+ <slot>{getRenderValue(row, col)}</slot>
45
+ {/if}
46
+ </div>
47
+
48
+ <style>
49
+ :global(.wx-measure-cell-body),
50
+ .wx-cell {
51
+ background: inherit;
52
+ box-sizing: border-box;
53
+ padding: 8px;
54
+ overflow: hidden;
55
+ text-overflow: ellipsis;
56
+ white-space: nowrap;
57
+ }
58
+ .wx-tree-cell {
59
+ display: flex;
60
+ }
61
+
62
+ :global(.wx-measure-cell-body),
63
+ .wx-cell:not(:last-child) {
64
+ border-right: var(--wx-table-cell-border);
65
+ }
66
+
67
+ .wx-shadow.wx-cell {
68
+ border-right: var(--wx-table-fixed-column-right-border);
69
+ clip-path: inset(0px -15px 0px 0px);
70
+ z-index: 1;
71
+ }
72
+
73
+ .wx-table-tree-toggle {
74
+ font-size: 20px;
75
+ cursor: pointer;
76
+ margin: 0 8px 0 0;
77
+ display: inline-block;
78
+ }
79
+ </style>
@@ -0,0 +1,81 @@
1
+ <script>
2
+ import { getStyle, getCssName } from "../helpers/columnWidth";
3
+
4
+ export let cell;
5
+ export let column;
6
+ export let columnStyle;
7
+
8
+ let style;
9
+ $: style = getStyle(
10
+ cell.width,
11
+ cell.flexgrow,
12
+ column.fixed,
13
+ column.left,
14
+ cell.height
15
+ );
16
+
17
+ let css = "";
18
+ $: css = getCssName(column, cell, columnStyle);
19
+ </script>
20
+
21
+ <div class="wx-cell {css} {cell.css || ''}" {style}>
22
+ {#if !column.collapsed && !cell.collapsed}
23
+ <div class="wx-text">{cell.text || ""}</div>
24
+ {/if}
25
+ </div>
26
+
27
+ <style>
28
+ :global(.wx-measure-cell-footer),
29
+ .wx-cell {
30
+ padding: 8px;
31
+ display: flex;
32
+ align-items: center;
33
+ font-weight: var(--wx-header-font-weight);
34
+ background: var(--wx-table-header-background);
35
+ overflow: hidden;
36
+ }
37
+
38
+ .wx-cell:not(:last-child) {
39
+ border-right: var(--wx-table-footer-cell-border);
40
+ }
41
+
42
+ .wx-cell.wx-vertical {
43
+ align-items: flex-end;
44
+ }
45
+
46
+ :global(.wx-measure-cell-footer.wx-measure-vertical) {
47
+ padding: 8px;
48
+ }
49
+
50
+ .wx-text {
51
+ text-overflow: ellipsis;
52
+ white-space: nowrap;
53
+ overflow: hidden;
54
+ }
55
+
56
+ .wx-vertical .wx-text {
57
+ transform: rotate(-90deg) translateY(100%);
58
+ transform-origin: left bottom;
59
+ text-overflow: clip;
60
+ overflow: unset;
61
+ }
62
+
63
+ .wx-cell.wx-shadow {
64
+ clip-path: inset(0px -15px 0px 0px);
65
+ border-right: var(--wx-table-fixed-column-right-border);
66
+ }
67
+
68
+ .wx-shadow,
69
+ .wx-fixed {
70
+ z-index: 1;
71
+ }
72
+
73
+ .wx-rowspan {
74
+ z-index: 2;
75
+ }
76
+
77
+ .wx-rowspan.wx-shadow,
78
+ .wx-colspan.wx-shadow {
79
+ z-index: 3;
80
+ }
81
+ </style>
@@ -0,0 +1,138 @@
1
+ <script>
2
+ // svelte core
3
+ import { createEventDispatcher, getContext } from "svelte";
4
+ import { writable } from "svelte/store";
5
+ const dispatch = createEventDispatcher();
6
+
7
+ // core widgets lib
8
+ import { Locale } from "wx-svelte-core";
9
+ import { en } from "wx-grid-locales";
10
+
11
+ // stores
12
+ import { EventBusRouter } from "wx-lib-state";
13
+ import { DataStore } from "wx-grid-store";
14
+
15
+ // ui
16
+ import Layout from "./Layout.svelte";
17
+
18
+ // incoming parameters
19
+ export let data = [];
20
+ export let columns = [];
21
+
22
+ export let rowStyle = null;
23
+ export let columnStyle = null;
24
+ export let cellStyle = null;
25
+
26
+ export let selected = null;
27
+ export let selectedRows = [];
28
+ export let select = true;
29
+ export let multiselect = false;
30
+
31
+ export let header = true;
32
+ export let footer = false;
33
+ export let dynamic = null;
34
+ export let editor = null;
35
+ export let filter = null;
36
+ export let overlay = null;
37
+ export let autoRowHeight = false;
38
+ export let sizes = {};
39
+ export let split = { left: 0 };
40
+
41
+ export let tree = false;
42
+ export let autoConfig = false;
43
+
44
+ export let init = null;
45
+
46
+ // auto config columns
47
+ $: if (autoConfig && !columns.length && data.length) {
48
+ const test = data[0];
49
+
50
+ for (let key in test)
51
+ if (key != "id" && key[0] != "$") {
52
+ let col = {
53
+ id: key,
54
+ header: key[0].toUpperCase() + key.substr(1),
55
+ };
56
+
57
+ if (typeof autoConfig === "object")
58
+ col = { ...col, ...autoConfig };
59
+ columns.push(col);
60
+ }
61
+ }
62
+ //sync selection props
63
+ $: {
64
+ if (selectedRows.length) selected = selectedRows[0];
65
+ else if (selected) selectedRows.push(selected);
66
+ }
67
+
68
+ $: _skin = getContext("wx-theme");
69
+
70
+ // init stores
71
+ const dataStore = new DataStore(writable);
72
+ $: {
73
+ dataStore.init({
74
+ data,
75
+ editor,
76
+ columns,
77
+ split,
78
+ sizes,
79
+ selected,
80
+ selectedRows,
81
+ dynamic,
82
+ filter,
83
+ tree,
84
+ _skin,
85
+ });
86
+
87
+ if (init) {
88
+ init(api);
89
+ init = null;
90
+ }
91
+ }
92
+
93
+ // define event route
94
+ let firstInRoute = dataStore.in;
95
+ let lastInRoute = new EventBusRouter(dispatch);
96
+ firstInRoute.setNext(lastInRoute);
97
+
98
+ const actions = ev => {
99
+ firstInRoute.exec(ev.detail.action, ev.detail.data);
100
+ };
101
+
102
+ // public API
103
+ export const api = {
104
+ // state
105
+ getState: dataStore.getState.bind(dataStore),
106
+ getReactiveState: dataStore.getReactive.bind(dataStore),
107
+ getStores: () => ({ data: dataStore }),
108
+
109
+ // events
110
+ exec: firstInRoute.exec,
111
+ setNext: ev => (lastInRoute = lastInRoute.setNext(ev)),
112
+ intercept: firstInRoute.intercept.bind(firstInRoute),
113
+ on: firstInRoute.on.bind(firstInRoute),
114
+ detach: firstInRoute.detach.bind(firstInRoute),
115
+
116
+ // extra api
117
+ getRow: id => dataStore.getRow(id),
118
+ getColumn: id => dataStore.getColumn(id),
119
+ };
120
+ </script>
121
+
122
+ <Locale words={en} optional={true}>
123
+ <Layout
124
+ store={dataStore}
125
+ {api}
126
+ {header}
127
+ {footer}
128
+ {overlay}
129
+ {rowStyle}
130
+ {columnStyle}
131
+ {cellStyle}
132
+ {select}
133
+ {multiselect}
134
+ {autoRowHeight}
135
+ on:action={actions}
136
+ on:data-request
137
+ />
138
+ </Locale>
@@ -0,0 +1,239 @@
1
+ <script>
2
+ import { createEventDispatcher } from "svelte";
3
+ import { resize } from "../helpers/actions/resize";
4
+ import { getCssName, getStyle } from "../helpers/columnWidth";
5
+
6
+ export let cell;
7
+ export let column;
8
+ export let row;
9
+ export let lastRow;
10
+ export let columnStyle;
11
+
12
+ const dispatch = createEventDispatcher();
13
+ let start;
14
+
15
+ function down(node) {
16
+ start = cell.flexgrow ? node.parentNode.clientWidth : cell.width;
17
+ }
18
+
19
+ function move(dx) {
20
+ dispatch("action", {
21
+ action: "resize-column",
22
+ data: { id: cell.id, width: Math.max(1, start + dx) },
23
+ });
24
+ }
25
+
26
+ function sort(ev) {
27
+ dispatch("action", {
28
+ action: "sort-rows",
29
+ data: { key: cell.id, add: ev.ctrlKey },
30
+ });
31
+ }
32
+ function collapse() {
33
+ dispatch("action", {
34
+ action: "collapse-column",
35
+ data: { id: cell.id, row },
36
+ });
37
+ }
38
+
39
+ let style;
40
+ $: style = getStyle(
41
+ cell.width,
42
+ cell.flexgrow,
43
+ column.fixed,
44
+ column.left,
45
+ cell.height
46
+ );
47
+
48
+ let css = "";
49
+ $: css = getCssName(column, cell, columnStyle);
50
+ </script>
51
+
52
+ {#if cell.collapsed && column.collapsed}
53
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
54
+ <div
55
+ class="wx-cell {css} {cell.css || ''} wx-collapsed"
56
+ {style}
57
+ on:click|stopPropagation={collapse}
58
+ data-header-id={column.id}
59
+ >
60
+ <div class="wx-text">{cell.text || ""}</div>
61
+ </div>
62
+ {:else}
63
+ <div
64
+ class="wx-cell {css} {cell.css || ''}"
65
+ {style}
66
+ data-header-id={column.id}
67
+ >
68
+ {#if cell.collapsible}
69
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
70
+ <div class="wx-collapse" on:click|stopPropagation={collapse}>
71
+ <i class="wxi-angle-{cell.collapsed ? 'down' : 'right'}" />
72
+ </div>
73
+ {/if}
74
+
75
+ <div class="wx-text">{cell.text || ""}</div>
76
+
77
+ {#if column.resize && lastRow}
78
+ <div class="wx-grip" use:resize={{ down, move }} />
79
+ {/if}
80
+
81
+ {#if column.sort}
82
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
83
+ <div class="wx-sort" on:click={sort}>
84
+ {#if column.$sort && lastRow}
85
+ {#if column.$sort.index > 0}
86
+ <div class="wx-order">{column.$sort.index}</div>
87
+ {/if}
88
+ <i
89
+ class="wxi-arrow-{column.$sort.order === 'asc'
90
+ ? 'up'
91
+ : 'down'}"
92
+ />
93
+ {/if}
94
+ </div>
95
+ {/if}
96
+ </div>
97
+ {/if}
98
+
99
+ <style>
100
+ :global(.wx-measure-cell-header),
101
+ .wx-cell {
102
+ padding: 8px;
103
+ position: relative;
104
+ display: flex;
105
+ align-items: center;
106
+ font-weight: var(--wx-header-font-weight);
107
+ background: var(--wx-table-header-background);
108
+ overflow: hidden;
109
+ gap: 10px;
110
+ line-height: 20px;
111
+ }
112
+
113
+ .wx-cell.wx-vertical {
114
+ align-items: flex-end;
115
+ }
116
+
117
+ :global(.wx-measure-cell-header.wx-measure-vertical) {
118
+ padding: 8px;
119
+ }
120
+
121
+ :global(.wx-measure-cell-header),
122
+ .wx-cell:not(:last-child) {
123
+ border-right: var(--wx-table-header-cell-border);
124
+ }
125
+
126
+ .wx-cell:last-child {
127
+ overflow: hidden;
128
+ }
129
+
130
+ .wx-text {
131
+ text-overflow: ellipsis;
132
+ white-space: nowrap;
133
+ overflow: hidden;
134
+ }
135
+
136
+ .wx-vertical .wx-text {
137
+ height: 100%;
138
+ transform: rotate(-180deg);
139
+ writing-mode: vertical-lr;
140
+ text-overflow: ellipsis;
141
+ overflow: hidden;
142
+ }
143
+
144
+ .wx-cell.wx-shadow {
145
+ box-shadow: var(--wx-table-fixed-column-shadow);
146
+ clip-path: inset(0px -15px 0px 0px);
147
+ border-right: var(--wx-table-fixed-column-right-border);
148
+ }
149
+
150
+ .wx-shadow,
151
+ .wx-fixed {
152
+ z-index: 1;
153
+ }
154
+
155
+ .wx-grip {
156
+ box-sizing: border-box;
157
+ position: absolute;
158
+ top: 0;
159
+ bottom: 0;
160
+ right: -4px;
161
+ width: 9px;
162
+ border-left: 5px solid var(--wx-table-header-background);
163
+ border-right: 3px solid var(--wx-table-header-background);
164
+ background-color: var(--wx-color-primary);
165
+ opacity: 0;
166
+ cursor: ew-resize;
167
+ z-index: 5;
168
+ }
169
+
170
+ .wx-grip::before,
171
+ .wx-grip::after {
172
+ content: "";
173
+ position: absolute;
174
+ top: 0;
175
+ bottom: 0;
176
+ width: 0;
177
+ height: 0;
178
+ margin: auto;
179
+ }
180
+
181
+ .wx-grip::before {
182
+ border: 3px dashed transparent;
183
+ border-right: 3px solid var(--wx-color-primary);
184
+ right: 5px;
185
+ }
186
+
187
+ .wx-grip::after {
188
+ border: 3px dashed transparent;
189
+ border-left: 3px solid var(--wx-color-primary);
190
+ left: 5px;
191
+ }
192
+
193
+ .wx-grip:hover {
194
+ opacity: 1;
195
+ }
196
+
197
+ .wx-sort {
198
+ position: absolute;
199
+ top: 0;
200
+ bottom: 0;
201
+ left: 0;
202
+ right: 5px;
203
+ display: flex;
204
+ align-items: center;
205
+ justify-content: flex-end;
206
+ }
207
+
208
+ .wx-order {
209
+ width: 16px;
210
+ height: 16px;
211
+ line-height: 16px;
212
+ border-radius: 50%;
213
+ font-size: 12px;
214
+ text-align: center;
215
+ color: #fff;
216
+ background-color: #3498ff;
217
+ }
218
+
219
+ .wx-icon {
220
+ padding: 5px;
221
+ color: #3498ff;
222
+ cursor: pointer;
223
+ }
224
+
225
+ .wx-rowspan {
226
+ z-index: 6; /* because resize grips have z-index: 5 */
227
+ }
228
+
229
+ .wx-rowspan.wx-shadow,
230
+ .wx-colspan.wx-shadow {
231
+ z-index: 7;
232
+ }
233
+
234
+ .wx-collapse,
235
+ .wx-collapsed {
236
+ cursor: pointer;
237
+ z-index: 1;
238
+ }
239
+ </style>