px-jspreadsheet-ce 0.0.1

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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +292 -0
  3. package/dist/index.d.ts +2382 -0
  4. package/dist/index.js +11286 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/jspreadsheet.css +723 -0
  7. package/dist/jspreadsheet.themes.css +104 -0
  8. package/package.json +57 -0
  9. package/src/index.js +95 -0
  10. package/src/test.js +50 -0
  11. package/src/utils/cells.js +36 -0
  12. package/src/utils/columns.js +742 -0
  13. package/src/utils/comments.js +87 -0
  14. package/src/utils/config.js +46 -0
  15. package/src/utils/copyPaste.js +438 -0
  16. package/src/utils/data.js +419 -0
  17. package/src/utils/dispatch.js +115 -0
  18. package/src/utils/download.js +38 -0
  19. package/src/utils/editor.js +430 -0
  20. package/src/utils/events.js +1639 -0
  21. package/src/utils/factory.js +216 -0
  22. package/src/utils/filter.js +128 -0
  23. package/src/utils/footer.js +51 -0
  24. package/src/utils/freeze.js +19 -0
  25. package/src/utils/headers.js +74 -0
  26. package/src/utils/helpers.js +409 -0
  27. package/src/utils/history.js +336 -0
  28. package/src/utils/internal.js +1299 -0
  29. package/src/utils/internalHelpers.js +96 -0
  30. package/src/utils/keys.js +406 -0
  31. package/src/utils/lazyLoading.js +143 -0
  32. package/src/utils/libraryBase.js +5 -0
  33. package/src/utils/merges.js +275 -0
  34. package/src/utils/meta.js +81 -0
  35. package/src/utils/orderBy.js +185 -0
  36. package/src/utils/pagination.js +181 -0
  37. package/src/utils/rows.js +624 -0
  38. package/src/utils/search.js +83 -0
  39. package/src/utils/selection.js +744 -0
  40. package/src/utils/style.js +147 -0
  41. package/src/utils/toolbar.js +566 -0
  42. package/src/utils/version.js +9 -0
  43. package/src/utils/worksheets.js +731 -0
  44. package/src/webcomponent.js +59 -0
@@ -0,0 +1,336 @@
1
+ import dispatch from './dispatch.js';
2
+ import { injectArray } from './internalHelpers.js';
3
+ import { updateTableReferences } from './internal.js';
4
+ import { setMerge } from './merges.js';
5
+ import { updateOrder, updateOrderArrow } from './orderBy.js';
6
+ import { conditionalSelectionUpdate } from './selection.js';
7
+
8
+ /**
9
+ * Initializes a new history record for undo/redo
10
+ *
11
+ * @return null
12
+ */
13
+ export const setHistory = function (changes) {
14
+ const obj = this;
15
+
16
+ if (obj.ignoreHistory != true) {
17
+ // Increment and get the current history index
18
+ const index = ++obj.historyIndex;
19
+
20
+ // Slice the array to discard undone changes
21
+ obj.history = obj.history = obj.history.slice(0, index + 1);
22
+
23
+ // Keep history
24
+ obj.history[index] = changes;
25
+ }
26
+ };
27
+
28
+ /**
29
+ * Process row
30
+ */
31
+ const historyProcessRow = function (type, historyRecord) {
32
+ const obj = this;
33
+
34
+ const rowIndex = !historyRecord.insertBefore ? historyRecord.rowNumber + 1 : +historyRecord.rowNumber;
35
+
36
+ if (obj.options.search == true) {
37
+ if (obj.results && obj.results.length != obj.rows.length) {
38
+ obj.resetSearch();
39
+ }
40
+ }
41
+
42
+ // Remove row
43
+ if (type == 1) {
44
+ const numOfRows = historyRecord.numOfRows;
45
+ // Remove nodes
46
+ for (let j = rowIndex; j < numOfRows + rowIndex; j++) {
47
+ obj.rows[j].element.parentNode.removeChild(obj.rows[j].element);
48
+ }
49
+ // Remove references
50
+ obj.records.splice(rowIndex, numOfRows);
51
+ obj.options.data.splice(rowIndex, numOfRows);
52
+ obj.rows.splice(rowIndex, numOfRows);
53
+
54
+ conditionalSelectionUpdate.call(obj, 1, rowIndex, numOfRows + rowIndex - 1);
55
+ } else {
56
+ // Insert data
57
+ const records = historyRecord.rowRecords.map((row) => {
58
+ return [...row];
59
+ });
60
+ obj.records = injectArray(obj.records, rowIndex, records);
61
+
62
+ const data = historyRecord.rowData.map((row) => {
63
+ return [...row];
64
+ });
65
+ obj.options.data = injectArray(obj.options.data, rowIndex, data);
66
+
67
+ obj.rows = injectArray(obj.rows, rowIndex, historyRecord.rowNode);
68
+ // Insert nodes
69
+ let index = 0;
70
+ for (let j = rowIndex; j < historyRecord.numOfRows + rowIndex; j++) {
71
+ obj.tbody.insertBefore(historyRecord.rowNode[index].element, obj.tbody.children[j]);
72
+ index++;
73
+ }
74
+ }
75
+
76
+ for (let j = rowIndex; j < obj.rows.length; j++) {
77
+ obj.rows[j].y = j;
78
+ }
79
+
80
+ for (let j = rowIndex; j < obj.records.length; j++) {
81
+ for (let i = 0; i < obj.records[j].length; i++) {
82
+ obj.records[j][i].y = j;
83
+ }
84
+ }
85
+
86
+ // Respect pagination
87
+ if (obj.options.pagination > 0) {
88
+ obj.page(obj.pageNumber);
89
+ }
90
+
91
+ updateTableReferences.call(obj);
92
+ };
93
+
94
+ /**
95
+ * Process column
96
+ */
97
+ const historyProcessColumn = function (type, historyRecord) {
98
+ const obj = this;
99
+
100
+ const columnIndex = !historyRecord.insertBefore ? historyRecord.columnNumber + 1 : historyRecord.columnNumber;
101
+
102
+ // Remove column
103
+ if (type == 1) {
104
+ const numOfColumns = historyRecord.numOfColumns;
105
+
106
+ obj.options.columns.splice(columnIndex, numOfColumns);
107
+ for (let i = columnIndex; i < numOfColumns + columnIndex; i++) {
108
+ obj.headers[i].parentNode.removeChild(obj.headers[i]);
109
+ obj.cols[i].colElement.parentNode.removeChild(obj.cols[i].colElement);
110
+ }
111
+ obj.headers.splice(columnIndex, numOfColumns);
112
+ obj.cols.splice(columnIndex, numOfColumns);
113
+ for (let j = 0; j < historyRecord.data.length; j++) {
114
+ for (let i = columnIndex; i < numOfColumns + columnIndex; i++) {
115
+ obj.records[j][i].element.parentNode.removeChild(obj.records[j][i].element);
116
+ }
117
+ obj.records[j].splice(columnIndex, numOfColumns);
118
+ obj.options.data[j].splice(columnIndex, numOfColumns);
119
+ }
120
+ // Process footers
121
+ if (obj.options.footers) {
122
+ for (let j = 0; j < obj.options.footers.length; j++) {
123
+ obj.options.footers[j].splice(columnIndex, numOfColumns);
124
+ }
125
+ }
126
+ } else {
127
+ // Insert data
128
+ obj.options.columns = injectArray(obj.options.columns, columnIndex, historyRecord.columns);
129
+ obj.headers = injectArray(obj.headers, columnIndex, historyRecord.headers);
130
+ obj.cols = injectArray(obj.cols, columnIndex, historyRecord.cols);
131
+
132
+ let index = 0;
133
+ for (let i = columnIndex; i < historyRecord.numOfColumns + columnIndex; i++) {
134
+ obj.headerContainer.insertBefore(historyRecord.headers[index], obj.headerContainer.children[i + 1]);
135
+ obj.colgroupContainer.insertBefore(historyRecord.cols[index].colElement, obj.colgroupContainer.children[i + 1]);
136
+ index++;
137
+ }
138
+
139
+ for (let j = 0; j < historyRecord.data.length; j++) {
140
+ obj.options.data[j] = injectArray(obj.options.data[j], columnIndex, historyRecord.data[j]);
141
+ obj.records[j] = injectArray(obj.records[j], columnIndex, historyRecord.records[j]);
142
+ let index = 0;
143
+ for (let i = columnIndex; i < historyRecord.numOfColumns + columnIndex; i++) {
144
+ obj.rows[j].element.insertBefore(historyRecord.records[j][index].element, obj.rows[j].element.children[i + 1]);
145
+ index++;
146
+ }
147
+ }
148
+ // Process footers
149
+ if (obj.options.footers) {
150
+ for (let j = 0; j < obj.options.footers.length; j++) {
151
+ obj.options.footers[j] = injectArray(obj.options.footers[j], columnIndex, historyRecord.footers[j]);
152
+ }
153
+ }
154
+ }
155
+
156
+ for (let i = columnIndex; i < obj.cols.length; i++) {
157
+ obj.cols[i].x = i;
158
+ }
159
+
160
+ for (let j = 0; j < obj.records.length; j++) {
161
+ for (let i = columnIndex; i < obj.records[j].length; i++) {
162
+ obj.records[j][i].x = i;
163
+ }
164
+ }
165
+
166
+ // Adjust nested headers
167
+ if (obj.options.nestedHeaders && obj.options.nestedHeaders.length > 0 && obj.options.nestedHeaders[0] && obj.options.nestedHeaders[0][0]) {
168
+ for (let j = 0; j < obj.options.nestedHeaders.length; j++) {
169
+ let colspan;
170
+
171
+ if (type == 1) {
172
+ colspan = parseInt(obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan) - historyRecord.numOfColumns;
173
+ } else {
174
+ colspan = parseInt(obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan) + historyRecord.numOfColumns;
175
+ }
176
+ obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan = colspan;
177
+ obj.thead.children[j].children[obj.thead.children[j].children.length - 1].setAttribute('colspan', colspan);
178
+ }
179
+ }
180
+
181
+ updateTableReferences.call(obj);
182
+ };
183
+
184
+ /**
185
+ * Undo last action
186
+ */
187
+ export const undo = function () {
188
+ const obj = this;
189
+
190
+ // Ignore events and history
191
+ const ignoreEvents = obj.parent.ignoreEvents ? true : false;
192
+ const ignoreHistory = obj.ignoreHistory ? true : false;
193
+
194
+ obj.parent.ignoreEvents = true;
195
+ obj.ignoreHistory = true;
196
+
197
+ // Records
198
+ const records = [];
199
+
200
+ // Update cells
201
+ let historyRecord;
202
+
203
+ if (obj.historyIndex >= 0) {
204
+ // History
205
+ historyRecord = obj.history[obj.historyIndex--];
206
+
207
+ if (historyRecord.action == 'insertRow') {
208
+ historyProcessRow.call(obj, 1, historyRecord);
209
+ } else if (historyRecord.action == 'deleteRow') {
210
+ historyProcessRow.call(obj, 0, historyRecord);
211
+ } else if (historyRecord.action == 'insertColumn') {
212
+ historyProcessColumn.call(obj, 1, historyRecord);
213
+ } else if (historyRecord.action == 'deleteColumn') {
214
+ historyProcessColumn.call(obj, 0, historyRecord);
215
+ } else if (historyRecord.action == 'moveRow') {
216
+ obj.moveRow(historyRecord.newValue, historyRecord.oldValue);
217
+ } else if (historyRecord.action == 'moveColumn') {
218
+ obj.moveColumn(historyRecord.newValue, historyRecord.oldValue);
219
+ } else if (historyRecord.action == 'setMerge') {
220
+ obj.removeMerge(historyRecord.column, historyRecord.data);
221
+ } else if (historyRecord.action == 'setStyle') {
222
+ obj.setStyle(historyRecord.oldValue, null, null, 1);
223
+ } else if (historyRecord.action == 'setWidth') {
224
+ obj.setWidth(historyRecord.column, historyRecord.oldValue);
225
+ } else if (historyRecord.action == 'setHeight') {
226
+ obj.setHeight(historyRecord.row, historyRecord.oldValue);
227
+ } else if (historyRecord.action == 'setHeader') {
228
+ obj.setHeader(historyRecord.column, historyRecord.oldValue);
229
+ } else if (historyRecord.action == 'setComments') {
230
+ obj.setComments(historyRecord.oldValue);
231
+ } else if (historyRecord.action == 'orderBy') {
232
+ let rows = [];
233
+ for (let j = 0; j < historyRecord.rows.length; j++) {
234
+ rows[historyRecord.rows[j]] = j;
235
+ }
236
+ updateOrderArrow.call(obj, historyRecord.column, historyRecord.order ? 0 : 1);
237
+ updateOrder.call(obj, rows);
238
+ } else if (historyRecord.action == 'setValue') {
239
+ // Redo for changes in cells
240
+ for (let i = 0; i < historyRecord.records.length; i++) {
241
+ records.push({
242
+ x: historyRecord.records[i].x,
243
+ y: historyRecord.records[i].y,
244
+ value: historyRecord.records[i].oldValue,
245
+ });
246
+
247
+ if (historyRecord.oldStyle) {
248
+ obj.resetStyle(historyRecord.oldStyle);
249
+ }
250
+ }
251
+ // Update records
252
+ obj.setValue(records);
253
+
254
+ // Update selection
255
+ if (historyRecord.selection) {
256
+ obj.updateSelectionFromCoords(historyRecord.selection[0], historyRecord.selection[1], historyRecord.selection[2], historyRecord.selection[3]);
257
+ }
258
+ }
259
+ }
260
+ obj.parent.ignoreEvents = ignoreEvents;
261
+ obj.ignoreHistory = ignoreHistory;
262
+
263
+ // Events
264
+ dispatch.call(obj, 'onundo', obj, historyRecord);
265
+ };
266
+
267
+ /**
268
+ * Redo previously undone action
269
+ */
270
+ export const redo = function () {
271
+ const obj = this;
272
+
273
+ // Ignore events and history
274
+ const ignoreEvents = obj.parent.ignoreEvents ? true : false;
275
+ const ignoreHistory = obj.ignoreHistory ? true : false;
276
+
277
+ obj.parent.ignoreEvents = true;
278
+ obj.ignoreHistory = true;
279
+
280
+ // Records
281
+ var records = [];
282
+
283
+ // Update cells
284
+ let historyRecord;
285
+
286
+ if (obj.historyIndex < obj.history.length - 1) {
287
+ // History
288
+ historyRecord = obj.history[++obj.historyIndex];
289
+
290
+ if (historyRecord.action == 'insertRow') {
291
+ historyProcessRow.call(obj, 0, historyRecord);
292
+ } else if (historyRecord.action == 'deleteRow') {
293
+ historyProcessRow.call(obj, 1, historyRecord);
294
+ } else if (historyRecord.action == 'insertColumn') {
295
+ historyProcessColumn.call(obj, 0, historyRecord);
296
+ } else if (historyRecord.action == 'deleteColumn') {
297
+ historyProcessColumn.call(obj, 1, historyRecord);
298
+ } else if (historyRecord.action == 'moveRow') {
299
+ obj.moveRow(historyRecord.oldValue, historyRecord.newValue);
300
+ } else if (historyRecord.action == 'moveColumn') {
301
+ obj.moveColumn(historyRecord.oldValue, historyRecord.newValue);
302
+ } else if (historyRecord.action == 'setMerge') {
303
+ setMerge.call(obj, historyRecord.column, historyRecord.colspan, historyRecord.rowspan, 1);
304
+ } else if (historyRecord.action == 'setStyle') {
305
+ obj.setStyle(historyRecord.newValue, null, null, 1);
306
+ } else if (historyRecord.action == 'setWidth') {
307
+ obj.setWidth(historyRecord.column, historyRecord.newValue);
308
+ } else if (historyRecord.action == 'setHeight') {
309
+ obj.setHeight(historyRecord.row, historyRecord.newValue);
310
+ } else if (historyRecord.action == 'setHeader') {
311
+ obj.setHeader(historyRecord.column, historyRecord.newValue);
312
+ } else if (historyRecord.action == 'setComments') {
313
+ obj.setComments(historyRecord.newValue);
314
+ } else if (historyRecord.action == 'orderBy') {
315
+ updateOrderArrow.call(obj, historyRecord.column, historyRecord.order);
316
+ updateOrder.call(obj, historyRecord.rows);
317
+ } else if (historyRecord.action == 'setValue') {
318
+ obj.setValue(historyRecord.records);
319
+ // Redo for changes in cells
320
+ for (let i = 0; i < historyRecord.records.length; i++) {
321
+ if (historyRecord.oldStyle) {
322
+ obj.resetStyle(historyRecord.newStyle);
323
+ }
324
+ }
325
+ // Update selection
326
+ if (historyRecord.selection) {
327
+ obj.updateSelectionFromCoords(historyRecord.selection[0], historyRecord.selection[1], historyRecord.selection[2], historyRecord.selection[3]);
328
+ }
329
+ }
330
+ }
331
+ obj.parent.ignoreEvents = ignoreEvents;
332
+ obj.ignoreHistory = ignoreHistory;
333
+
334
+ // Events
335
+ dispatch.call(obj, 'onredo', obj, historyRecord);
336
+ };