adore-datatable 2.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2018 Frappe Technologies Pvt. Ltd. <developers@frappe.io>
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ <div align="center" markdown="1">
2
+
3
+ <img width="80" alt="datatable-logo" src="https://github.com/user-attachments/assets/8235f4b9-993a-4329-97de-9431dcf63aae" >
4
+
5
+ <h1>Frappe DataTable</h1>
6
+
7
+ **A modern datatable library for the web**
8
+
9
+ [![Test and Release](https://github.com/frappe/datatable/workflows/Test%20and%20Release/badge.svg)](https://github.com/frappe/datatable/actions?query=workflow%3A%22Test+and+Release%22)
10
+ [![npm version](https://badge.fury.io/js/frappe-datatable.svg)](https://badge.fury.io/js/frappe-datatable)
11
+ ![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/frappe-datatable.svg)
12
+
13
+
14
+ ![datatable-demo-2](https://user-images.githubusercontent.com/9355208/40740030-5412aa40-6465-11e8-8542-b0247ab1daac.gif)
15
+
16
+ </div>
17
+
18
+ ## Frappe Datatable
19
+
20
+ Frappe DataTable is a simple, modern and interactive datatable library for displaying tabular data. Originally built for [ERPNext](https://github.com/frappe/erpnext), it can be used to render large amount of rows without sacrificing performance and has the basic data grid features like inline editing and keyboard navigation. It does not require jQuery, unlike most data grids out there.
21
+
22
+ ### Motivation
23
+
24
+ I was trying to remove all legacy UI components from the [frappe](https://github.com/frappe/frappe) codebase. We were using [SlickGrid](https://github.com/mleibman/SlickGrid) for rendering tables. It was unmaintained and UI was dated. Other datatable solutions either didn't have the features we needed or were closed source. So we built our own.
25
+
26
+
27
+ ### Key Features
28
+
29
+ - **Cell**: Enable editing within individual cells and features like custom formatters, inline editing, and mouse selection. Users can easily copy cell content, navigate through cells using the keyboard, and take advantage of a custom cell editor for advanced functionality.
30
+ - **Column**: Columns are highly flexible, allowing users to reorder, resize, and sort them with ease. Additional features include hiding/removing columns and adding custom actions.
31
+ - **Row**: Rows support advanced interactions, including row selection, tree-structured organization, and inline filters for precise control. They handle large datasets efficiently with dynamic row heights.
32
+
33
+
34
+ ## Usage
35
+
36
+ ```bash
37
+ yarn add frappe-datatable
38
+ # or
39
+ npm install frappe-datatable
40
+ ```
41
+
42
+ > Note: [`sortablejs`](https://github.com/RubaXa/Sortable) is required to be installed as well.
43
+
44
+
45
+ ```js
46
+ const datatable = new DataTable('#datatable', {
47
+ columns: [ 'First Name', 'Last Name', 'Position' ],
48
+ data: [
49
+ [ 'Don', 'Joe', 'Designer' ],
50
+ [ 'Mary', 'Jane', 'Software Developer' ]
51
+ ]
52
+ });
53
+ ```
54
+
55
+ ## Development Setup
56
+
57
+ * `yarn start` - Start dev server
58
+ * Open `index.html` located in the root folder, and start development.
59
+ * Run `yarn lint` before committing changes
60
+ * This project uses [commitizen](https://github.com/commitizen/cz-cli) for conventional commit messages, use `yarn commit` command instead of `git commit`
61
+
62
+ ## Links
63
+
64
+ - [Making a new datatable for the web](https://medium.com/frapp%C3%A9-thoughts/things-i-learned-building-a-library-for-the-web-6846a588bf53)
65
+
66
+ <br>
67
+ <br>
68
+ <div align="center" style="padding-top: 0.75rem;">
69
+ <a href="https://frappe.io" target="_blank">
70
+ <picture>
71
+ <source media="(prefers-color-scheme: dark)" srcset="https://frappe.io/files/Frappe-white.png">
72
+ <img src="https://frappe.io/files/Frappe-black.png" alt="Frappe Technologies" height="28"/>
73
+ </picture>
74
+ </a>
75
+ </div>
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "adore-datatable",
3
+ "version": "2.0.0",
4
+ "description": "A modern datatable library for the web",
5
+ "main": "dist/adore-datatable.cjs.js",
6
+ "unpkg": "dist/adore-datatable.min.js",
7
+ "jsdelivr": "dist/adore-datatable.min.js",
8
+ "scripts": {
9
+ "start": "yarn run dev",
10
+ "build": "rollup -c && NODE_ENV=production rollup -c",
11
+ "dev": "rollup -c -w",
12
+ "cy:server": "http-server -p 8989",
13
+ "cy:open": "cypress open",
14
+ "cy:run": "cypress run",
15
+ "test": "start-server-and-test cy:server http://localhost:8989 cy:run",
16
+ "test-local": "start-server-and-test cy:server http://localhost:8989 cy:open",
17
+ "travis-deploy-once": "travis-deploy-once",
18
+ "semantic-release": "semantic-release",
19
+ "lint": "eslint src",
20
+ "lint-and-build": "yarn lint && yarn build",
21
+ "commit": "npx git-cz"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "src"
26
+ ],
27
+ "devDependencies": {
28
+ "autoprefixer": "^9.0.0",
29
+ "chai": "3.5.0",
30
+ "cypress": "^9.2.0",
31
+ "cz-conventional-changelog": "^2.1.0",
32
+ "deepmerge": "^2.0.1",
33
+ "eslint": "^5.0.1",
34
+ "eslint-config-airbnb": "^16.1.0",
35
+ "eslint-config-airbnb-base": "^12.1.0",
36
+ "eslint-plugin-import": "^2.11.0",
37
+ "http-server": "^0.11.1",
38
+ "mocha": "3.3.0",
39
+ "postcss-custom-properties": "^7.0.0",
40
+ "postcss-nested": "^3.0.0",
41
+ "rollup": "^0.59.4",
42
+ "rollup-plugin-commonjs": "^8.3.0",
43
+ "rollup-plugin-eslint": "^4.0.0",
44
+ "rollup-plugin-json": "^2.3.0",
45
+ "rollup-plugin-node-resolve": "^3.0.3",
46
+ "rollup-plugin-postcss": "^1.2.8",
47
+ "rollup-plugin-uglify-es": "^0.0.1",
48
+ "semantic-release": "^17.1.1",
49
+ "start-server-and-test": "^1.4.1",
50
+ "travis-deploy-once": "^5.0.1"
51
+ },
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "https://github.com/frappe/datatable.git"
55
+ },
56
+ "keywords": [
57
+ "datatable",
58
+ "data",
59
+ "grid",
60
+ "table"
61
+ ],
62
+ "author": "Faris Ansari",
63
+ "license": "MIT",
64
+ "bugs": {
65
+ "url": "https://github.com/frappe/datatable/issues"
66
+ },
67
+ "homepage": "https://frappe.io/datatable",
68
+ "dependencies": {
69
+ "hyperlist": "^1.0.0-beta",
70
+ "lodash": "^4.17.5",
71
+ "sortablejs": "^1.7.0"
72
+ },
73
+ "config": {
74
+ "commitizen": {
75
+ "path": "cz-conventional-changelog"
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,163 @@
1
+ import HyperList from 'hyperlist';
2
+
3
+ export default class BodyRenderer {
4
+ constructor(instance) {
5
+ this.instance = instance;
6
+ this.options = instance.options;
7
+ this.datamanager = instance.datamanager;
8
+ this.rowmanager = instance.rowmanager;
9
+ this.cellmanager = instance.cellmanager;
10
+ this.bodyScrollable = instance.bodyScrollable;
11
+ this.footer = this.instance.footer;
12
+ this.log = instance.log;
13
+ }
14
+
15
+ renderRows(rows) {
16
+ this.visibleRows = rows;
17
+ this.visibleRowIndices = rows.map(row => row.meta.rowIndex);
18
+ this.instance.noData = false;
19
+ if (rows.length === 0) {
20
+ this.bodyScrollable.innerHTML = this.getNoDataHTML();
21
+ this.instance.noData = true;
22
+ this.footer.innerHTML = '';
23
+ return;
24
+ }
25
+
26
+ // Create a temporary set for faster lookups.
27
+ // We can't change this.visibleRowIndices as it would be breaking for users.
28
+ let visibleRowIndicesSet = new Set(this.visibleRowIndices);
29
+ const rowViewOrder = this.datamanager.rowViewOrder.map(index => {
30
+ if (visibleRowIndicesSet.has(index)) {
31
+ return index;
32
+ }
33
+ return null;
34
+ }).filter(index => index !== null);
35
+
36
+ const computedStyle = getComputedStyle(this.bodyScrollable);
37
+
38
+ let config = {
39
+ width: computedStyle.width,
40
+ height: computedStyle.height,
41
+ itemHeight: this.options.cellHeight,
42
+ total: rows.length,
43
+ generate: (index) => {
44
+ const el = document.createElement('div');
45
+ const rowIndex = rowViewOrder[index];
46
+ const row = this.datamanager.getRow(rowIndex);
47
+ const rowHTML = this.rowmanager.getRowHTML(row, row.meta);
48
+ el.innerHTML = rowHTML;
49
+ return el.children[0];
50
+ },
51
+ afterRender: () => {
52
+ this.restoreState();
53
+ }
54
+ };
55
+
56
+ if (!this.hyperlist) {
57
+ this.hyperlist = new HyperList(this.bodyScrollable, config);
58
+ } else {
59
+ this.hyperlist.refresh(this.bodyScrollable, config);
60
+ }
61
+
62
+ this.renderFooter();
63
+ }
64
+
65
+ render() {
66
+ const rows = this.datamanager.getRowsForView();
67
+ this.renderRows(rows);
68
+ // setDimensions requires atleast 1 row to exist in dom
69
+ this.instance.setDimensions();
70
+ }
71
+
72
+ renderFooter() {
73
+ if (!this.options.showTotalRow) return;
74
+
75
+ const totalRow = this.getTotalRow();
76
+ let html = this.rowmanager.getRowHTML(totalRow, { isTotalRow: 1, rowIndex: 'totalRow' });
77
+
78
+ this.footer.innerHTML = html;
79
+ }
80
+
81
+ getTotalRow() {
82
+ const columns = this.datamanager.getColumns();
83
+ const totalRowTemplate = columns.map(col => {
84
+ let content = null;
85
+ if (['_rowIndex', '_checkbox'].includes(col.id)) {
86
+ content = '';
87
+ }
88
+ return {
89
+ content,
90
+ isTotalRow: 1,
91
+ colIndex: col.colIndex,
92
+ column: col
93
+ };
94
+ });
95
+
96
+ const totalRow = totalRowTemplate.map((cell, i) => {
97
+ if (cell.content === '') return cell;
98
+
99
+ if (this.options.hooks.columnTotal) {
100
+ const columnValues = this.visibleRows.map(row => row[i].content);
101
+ const result = this.options.hooks.columnTotal.call(this.instance, columnValues, cell);
102
+ if (result != null) {
103
+ cell.content = result;
104
+ return cell;
105
+ }
106
+ }
107
+
108
+ cell.content = this.visibleRows.reduce((acc, prevRow) => {
109
+ const prevCell = prevRow[i];
110
+ if (typeof prevCell.content === 'number') {
111
+ if (acc == null) acc = 0;
112
+ return acc + prevCell.content;
113
+ }
114
+ return acc;
115
+ }, cell.content);
116
+
117
+ return cell;
118
+ });
119
+
120
+ return totalRow;
121
+ }
122
+
123
+ restoreState() {
124
+ this.rowmanager.highlightCheckedRows();
125
+ this.cellmanager.selectAreaOnClusterChanged();
126
+ this.cellmanager.focusCellOnClusterChanged();
127
+ }
128
+
129
+ showToastMessage(message, hideAfter) {
130
+ this.instance.toastMessage.innerHTML = this.getToastMessageHTML(message);
131
+
132
+ if (hideAfter) {
133
+ setTimeout(() => {
134
+ this.clearToastMessage();
135
+ }, hideAfter * 1000);
136
+ }
137
+ }
138
+
139
+ clearToastMessage() {
140
+ this.instance.toastMessage.innerHTML = '';
141
+ }
142
+
143
+ getNoDataHTML() {
144
+ const style = window.getComputedStyle(this.instance.header);
145
+ const matrix = new DOMMatrixReadOnly(style.transform);
146
+ const width = (-matrix.m41) + this.instance.header.clientWidth;
147
+ const height = this.bodyScrollable.clientHeight;
148
+ return `
149
+ <div
150
+ class="dt-scrollable__no-data"
151
+ style="width: ${width}px; height: ${height}px"
152
+ >
153
+ <div class="dt-scrollable__no-data no-data-message">
154
+ ${this.options.noDataMessage}
155
+ </div>
156
+ </div>
157
+ `;
158
+ }
159
+
160
+ getToastMessageHTML(message) {
161
+ return `<span class="dt-toast__message">${message}</span>`;
162
+ }
163
+ }