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.
- package/LICENSE +21 -0
- package/README.md +292 -0
- package/dist/index.d.ts +2382 -0
- package/dist/index.js +11286 -0
- package/dist/index.js.map +1 -0
- package/dist/jspreadsheet.css +723 -0
- package/dist/jspreadsheet.themes.css +104 -0
- package/package.json +57 -0
- package/src/index.js +95 -0
- package/src/test.js +50 -0
- package/src/utils/cells.js +36 -0
- package/src/utils/columns.js +742 -0
- package/src/utils/comments.js +87 -0
- package/src/utils/config.js +46 -0
- package/src/utils/copyPaste.js +438 -0
- package/src/utils/data.js +419 -0
- package/src/utils/dispatch.js +115 -0
- package/src/utils/download.js +38 -0
- package/src/utils/editor.js +430 -0
- package/src/utils/events.js +1639 -0
- package/src/utils/factory.js +216 -0
- package/src/utils/filter.js +128 -0
- package/src/utils/footer.js +51 -0
- package/src/utils/freeze.js +19 -0
- package/src/utils/headers.js +74 -0
- package/src/utils/helpers.js +409 -0
- package/src/utils/history.js +336 -0
- package/src/utils/internal.js +1299 -0
- package/src/utils/internalHelpers.js +96 -0
- package/src/utils/keys.js +406 -0
- package/src/utils/lazyLoading.js +143 -0
- package/src/utils/libraryBase.js +5 -0
- package/src/utils/merges.js +275 -0
- package/src/utils/meta.js +81 -0
- package/src/utils/orderBy.js +185 -0
- package/src/utils/pagination.js +181 -0
- package/src/utils/rows.js +624 -0
- package/src/utils/search.js +83 -0
- package/src/utils/selection.js +744 -0
- package/src/utils/style.js +147 -0
- package/src/utils/toolbar.js +566 -0
- package/src/utils/version.js +9 -0
- package/src/utils/worksheets.js +731 -0
- package/src/webcomponent.js +59 -0
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
import jSuites from 'jsuites';
|
|
2
|
+
import dispatch from './dispatch.js';
|
|
3
|
+
import { getColumnName } from './helpers.js';
|
|
4
|
+
import { setHistory } from './history.js';
|
|
5
|
+
import { isColMerged } from './merges.js';
|
|
6
|
+
import { createCell, updateTableReferences } from './internal.js';
|
|
7
|
+
import { conditionalSelectionUpdate, updateCornerPosition } from './selection.js';
|
|
8
|
+
import { setFooter } from './footer.js';
|
|
9
|
+
import { getColumnNameFromId, injectArray, getFreezeColumnLeft } from './internalHelpers.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 获取列数
|
|
13
|
+
* minDimensions > 第一行data长度 > columns长度
|
|
14
|
+
* @returns number numberOfColumns
|
|
15
|
+
*/
|
|
16
|
+
export const getNumberOfColumns = function () {
|
|
17
|
+
const obj = this;
|
|
18
|
+
|
|
19
|
+
let numberOfColumns = (obj.options.columns && obj.options.columns.length) || 0;
|
|
20
|
+
|
|
21
|
+
if (obj.options.data && typeof obj.options.data[0] !== 'undefined') {
|
|
22
|
+
// Data keys
|
|
23
|
+
const keys = Object.keys(obj.options.data[0]);
|
|
24
|
+
|
|
25
|
+
if (keys.length > numberOfColumns) {
|
|
26
|
+
numberOfColumns = keys.length;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (obj.options.minDimensions && obj.options.minDimensions[0] > numberOfColumns) {
|
|
31
|
+
numberOfColumns = obj.options.minDimensions[0];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return numberOfColumns;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const createCellHeader = function (colNumber) {
|
|
38
|
+
const obj = this;
|
|
39
|
+
|
|
40
|
+
// Create col global control
|
|
41
|
+
const colWidth = (obj.options.columns && obj.options.columns[colNumber] && obj.options.columns[colNumber].width) || obj.options.defaultColWidth || 100;
|
|
42
|
+
const colAlign = (obj.options.columns && obj.options.columns[colNumber] && obj.options.columns[colNumber].align) || obj.options.defaultColAlign || 'center';
|
|
43
|
+
|
|
44
|
+
// Create header cell
|
|
45
|
+
obj.headers[colNumber] = document.createElement('td');
|
|
46
|
+
obj.headers[colNumber].textContent =
|
|
47
|
+
(obj.options.columns && obj.options.columns[colNumber] && obj.options.columns[colNumber].title) || getColumnName(colNumber);
|
|
48
|
+
|
|
49
|
+
obj.headers[colNumber].setAttribute('data-x', colNumber);
|
|
50
|
+
obj.headers[colNumber].style.textAlign = colAlign;
|
|
51
|
+
if (obj.options.columns && obj.options.columns[colNumber] && obj.options.columns[colNumber].title) {
|
|
52
|
+
obj.headers[colNumber].setAttribute('title', obj.headers[colNumber].innerText);
|
|
53
|
+
}
|
|
54
|
+
if (obj.options.columns && obj.options.columns[colNumber] && obj.options.columns[colNumber].id) {
|
|
55
|
+
obj.headers[colNumber].setAttribute('id', obj.options.columns[colNumber].id);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 表头冻结
|
|
59
|
+
if (obj.options.freezeColumns > colNumber) {
|
|
60
|
+
obj.headers[colNumber].classList.add('jss_frozen');
|
|
61
|
+
obj.headers[colNumber].style.left = getFreezeColumnLeft(colNumber, obj.options.columns);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Width control
|
|
65
|
+
const colElement = document.createElement('col');
|
|
66
|
+
colElement.setAttribute('width', colWidth);
|
|
67
|
+
|
|
68
|
+
obj.cols[colNumber] = {
|
|
69
|
+
colElement,
|
|
70
|
+
x: colNumber,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Hidden column
|
|
74
|
+
if (obj.options.columns && obj.options.columns[colNumber] && obj.options.columns[colNumber].type == 'hidden') {
|
|
75
|
+
obj.headers[colNumber].style.display = 'none';
|
|
76
|
+
colElement.style.display = 'none';
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Insert a new column
|
|
82
|
+
*
|
|
83
|
+
* @param mixed - num of columns to be added or data to be added in one single column
|
|
84
|
+
* @param int columnNumber - number of columns to be created
|
|
85
|
+
* @param bool insertBefore
|
|
86
|
+
* @param object properties - column properties
|
|
87
|
+
* @return void
|
|
88
|
+
*/
|
|
89
|
+
export const insertColumn = function (mixed, columnNumber, insertBefore, properties) {
|
|
90
|
+
const obj = this;
|
|
91
|
+
|
|
92
|
+
// Configuration
|
|
93
|
+
if (obj.options.allowInsertColumn != false) {
|
|
94
|
+
// Records
|
|
95
|
+
var records = [];
|
|
96
|
+
|
|
97
|
+
// Data to be insert
|
|
98
|
+
let data = [];
|
|
99
|
+
|
|
100
|
+
// The insert could be lead by number of rows or the array of data
|
|
101
|
+
let numOfColumns;
|
|
102
|
+
if (!Array.isArray(mixed)) {
|
|
103
|
+
numOfColumns = typeof mixed === 'number' ? mixed : 1;
|
|
104
|
+
} else {
|
|
105
|
+
numOfColumns = 1;
|
|
106
|
+
|
|
107
|
+
if (mixed) {
|
|
108
|
+
data = mixed;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Direction
|
|
113
|
+
insertBefore = insertBefore ? true : false;
|
|
114
|
+
|
|
115
|
+
// Current column number
|
|
116
|
+
const currentNumOfColumns = Math.max(
|
|
117
|
+
obj.options.columns.length,
|
|
118
|
+
...obj.options.data.map(function (row) {
|
|
119
|
+
return row.length;
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const lastColumn = currentNumOfColumns - 1;
|
|
124
|
+
|
|
125
|
+
// Confirm position
|
|
126
|
+
if (columnNumber == undefined || columnNumber >= parseInt(lastColumn) || columnNumber < 0) {
|
|
127
|
+
columnNumber = lastColumn;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Create default properties
|
|
131
|
+
if (!properties) {
|
|
132
|
+
properties = [];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < numOfColumns; i++) {
|
|
136
|
+
if (!properties[i]) {
|
|
137
|
+
properties[i] = {};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const columns = [];
|
|
142
|
+
|
|
143
|
+
if (!Array.isArray(mixed)) {
|
|
144
|
+
for (let i = 0; i < mixed; i++) {
|
|
145
|
+
const column = {
|
|
146
|
+
column: columnNumber + i + (insertBefore ? 0 : 1),
|
|
147
|
+
options: Object.assign({}, properties[i]),
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
columns.push(column);
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
const data = [];
|
|
154
|
+
|
|
155
|
+
for (let i = 0; i < obj.options.data.length; i++) {
|
|
156
|
+
data.push(i < mixed.length ? mixed[i] : '');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const column = {
|
|
160
|
+
column: columnNumber + (insertBefore ? 0 : 1),
|
|
161
|
+
options: Object.assign({}, properties[0]),
|
|
162
|
+
data,
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
columns.push(column);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Onbeforeinsertcolumn
|
|
169
|
+
if (dispatch.call(obj, 'onbeforeinsertcolumn', obj, columns) === false) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Merged cells
|
|
174
|
+
if (obj.options.mergeCells && Object.keys(obj.options.mergeCells).length > 0) {
|
|
175
|
+
if (isColMerged.call(obj, columnNumber, insertBefore).length) {
|
|
176
|
+
if (!confirm(jSuites.translate('This action will destroy any existing merged cells. Are you sure?'))) {
|
|
177
|
+
return false;
|
|
178
|
+
} else {
|
|
179
|
+
obj.destroyMerge();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Insert before
|
|
185
|
+
const columnIndex = !insertBefore ? columnNumber + 1 : columnNumber;
|
|
186
|
+
obj.options.columns = injectArray(obj.options.columns, columnIndex, properties);
|
|
187
|
+
|
|
188
|
+
// Open space in the containers
|
|
189
|
+
const currentHeaders = obj.headers.splice(columnIndex);
|
|
190
|
+
const currentColgroup = obj.cols.splice(columnIndex);
|
|
191
|
+
|
|
192
|
+
// History
|
|
193
|
+
const historyHeaders = [];
|
|
194
|
+
const historyColgroup = [];
|
|
195
|
+
const historyRecords = [];
|
|
196
|
+
const historyData = [];
|
|
197
|
+
const historyFooters = [];
|
|
198
|
+
|
|
199
|
+
// Add new headers
|
|
200
|
+
for (let col = columnIndex; col < numOfColumns + columnIndex; col++) {
|
|
201
|
+
createCellHeader.call(obj, col);
|
|
202
|
+
obj.headerContainer.insertBefore(obj.headers[col], obj.headerContainer.children[col + 1]);
|
|
203
|
+
obj.colgroupContainer.insertBefore(obj.cols[col].colElement, obj.colgroupContainer.children[col + 1]);
|
|
204
|
+
|
|
205
|
+
historyHeaders.push(obj.headers[col]);
|
|
206
|
+
historyColgroup.push(obj.cols[col]);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Add new footer cells
|
|
210
|
+
if (obj.options.footers) {
|
|
211
|
+
for (let j = 0; j < obj.options.footers.length; j++) {
|
|
212
|
+
historyFooters[j] = [];
|
|
213
|
+
for (let i = 0; i < numOfColumns; i++) {
|
|
214
|
+
historyFooters[j].push('');
|
|
215
|
+
}
|
|
216
|
+
obj.options.footers[j].splice(columnIndex, 0, historyFooters[j]);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Adding visual columns
|
|
221
|
+
for (let row = 0; row < obj.options.data.length; row++) {
|
|
222
|
+
// Keep the current data
|
|
223
|
+
const currentData = obj.options.data[row].splice(columnIndex);
|
|
224
|
+
const currentRecord = obj.records[row].splice(columnIndex);
|
|
225
|
+
|
|
226
|
+
// History
|
|
227
|
+
historyData[row] = [];
|
|
228
|
+
historyRecords[row] = [];
|
|
229
|
+
|
|
230
|
+
for (let col = columnIndex; col < numOfColumns + columnIndex; col++) {
|
|
231
|
+
// New value
|
|
232
|
+
const value = data[row] ? data[row] : '';
|
|
233
|
+
obj.options.data[row][col] = value;
|
|
234
|
+
// New cell
|
|
235
|
+
const td = createCell.call(obj, col, row, obj.options.data[row][col]);
|
|
236
|
+
obj.records[row][col] = {
|
|
237
|
+
element: td,
|
|
238
|
+
y: row,
|
|
239
|
+
};
|
|
240
|
+
// Add cell to the row
|
|
241
|
+
if (obj.rows[row]) {
|
|
242
|
+
obj.rows[row].element.insertBefore(td, obj.rows[row].element.children[col + 1]);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (obj.options.columns && obj.options.columns[col] && typeof obj.options.columns[col].render === 'function') {
|
|
246
|
+
obj.options.columns[col].render(td, value, parseInt(col), parseInt(row), obj, obj.options.columns[col]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Record History
|
|
250
|
+
historyData[row].push(value);
|
|
251
|
+
historyRecords[row].push({ element: td, x: col, y: row });
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Copy the data back to the main data
|
|
255
|
+
Array.prototype.push.apply(obj.options.data[row], currentData);
|
|
256
|
+
Array.prototype.push.apply(obj.records[row], currentRecord);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
Array.prototype.push.apply(obj.headers, currentHeaders);
|
|
260
|
+
Array.prototype.push.apply(obj.cols, currentColgroup);
|
|
261
|
+
|
|
262
|
+
for (let i = columnIndex; i < obj.cols.length; i++) {
|
|
263
|
+
obj.cols[i].x = i;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
for (let j = 0; j < obj.records.length; j++) {
|
|
267
|
+
for (let i = 0; i < obj.records[j].length; i++) {
|
|
268
|
+
obj.records[j][i].x = i;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Adjust nested headers
|
|
273
|
+
if (obj.options.nestedHeaders && obj.options.nestedHeaders.length > 0 && obj.options.nestedHeaders[0] && obj.options.nestedHeaders[0][0]) {
|
|
274
|
+
for (let j = 0; j < obj.options.nestedHeaders.length; j++) {
|
|
275
|
+
const colspan = parseInt(obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan) + numOfColumns;
|
|
276
|
+
obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan = colspan;
|
|
277
|
+
obj.thead.children[j].children[obj.thead.children[j].children.length - 1].setAttribute('colspan', colspan);
|
|
278
|
+
let o = obj.thead.children[j].children[obj.thead.children[j].children.length - 1].getAttribute('data-column');
|
|
279
|
+
o = o.split(',');
|
|
280
|
+
for (let col = columnIndex; col < numOfColumns + columnIndex; col++) {
|
|
281
|
+
o.push(col);
|
|
282
|
+
}
|
|
283
|
+
obj.thead.children[j].children[obj.thead.children[j].children.length - 1].setAttribute('data-column', o);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Keep history
|
|
288
|
+
setHistory.call(obj, {
|
|
289
|
+
action: 'insertColumn',
|
|
290
|
+
columnNumber: columnNumber,
|
|
291
|
+
numOfColumns: numOfColumns,
|
|
292
|
+
insertBefore: insertBefore,
|
|
293
|
+
columns: properties,
|
|
294
|
+
headers: historyHeaders,
|
|
295
|
+
cols: historyColgroup,
|
|
296
|
+
records: historyRecords,
|
|
297
|
+
footers: historyFooters,
|
|
298
|
+
data: historyData,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// Remove table references
|
|
302
|
+
updateTableReferences.call(obj);
|
|
303
|
+
|
|
304
|
+
// Events
|
|
305
|
+
dispatch.call(obj, 'oninsertcolumn', obj, columns);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Move column
|
|
311
|
+
*
|
|
312
|
+
* @return void
|
|
313
|
+
*/
|
|
314
|
+
export const moveColumn = function (o, d) {
|
|
315
|
+
const obj = this;
|
|
316
|
+
|
|
317
|
+
if (obj.options.mergeCells && Object.keys(obj.options.mergeCells).length > 0) {
|
|
318
|
+
let insertBefore;
|
|
319
|
+
if (o > d) {
|
|
320
|
+
insertBefore = 1;
|
|
321
|
+
} else {
|
|
322
|
+
insertBefore = 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (isColMerged.call(obj, o).length || isColMerged.call(obj, d, insertBefore).length) {
|
|
326
|
+
if (!confirm(jSuites.translate('This action will destroy any existing merged cells. Are you sure?'))) {
|
|
327
|
+
return false;
|
|
328
|
+
} else {
|
|
329
|
+
obj.destroyMerge();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
o = parseInt(o);
|
|
335
|
+
d = parseInt(d);
|
|
336
|
+
|
|
337
|
+
if (o > d) {
|
|
338
|
+
obj.headerContainer.insertBefore(obj.headers[o], obj.headers[d]);
|
|
339
|
+
obj.colgroupContainer.insertBefore(obj.cols[o].colElement, obj.cols[d].colElement);
|
|
340
|
+
|
|
341
|
+
for (let j = 0; j < obj.rows.length; j++) {
|
|
342
|
+
obj.rows[j].element.insertBefore(obj.records[j][o].element, obj.records[j][d].element);
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
obj.headerContainer.insertBefore(obj.headers[o], obj.headers[d].nextSibling);
|
|
346
|
+
obj.colgroupContainer.insertBefore(obj.cols[o].colElement, obj.cols[d].colElement.nextSibling);
|
|
347
|
+
|
|
348
|
+
for (let j = 0; j < obj.rows.length; j++) {
|
|
349
|
+
obj.rows[j].element.insertBefore(obj.records[j][o].element, obj.records[j][d].element.nextSibling);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
obj.options.columns.splice(d, 0, obj.options.columns.splice(o, 1)[0]);
|
|
354
|
+
obj.headers.splice(d, 0, obj.headers.splice(o, 1)[0]);
|
|
355
|
+
obj.cols.splice(d, 0, obj.cols.splice(o, 1)[0]);
|
|
356
|
+
|
|
357
|
+
const firstAffectedIndex = Math.min(o, d);
|
|
358
|
+
const lastAffectedIndex = Math.max(o, d);
|
|
359
|
+
|
|
360
|
+
for (let j = 0; j < obj.rows.length; j++) {
|
|
361
|
+
obj.options.data[j].splice(d, 0, obj.options.data[j].splice(o, 1)[0]);
|
|
362
|
+
obj.records[j].splice(d, 0, obj.records[j].splice(o, 1)[0]);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
for (let i = firstAffectedIndex; i <= lastAffectedIndex; i++) {
|
|
366
|
+
obj.cols[i].x = i;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
for (let j = 0; j < obj.records.length; j++) {
|
|
370
|
+
for (let i = firstAffectedIndex; i <= lastAffectedIndex; i++) {
|
|
371
|
+
obj.records[j][i].x = i;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Update footers position
|
|
376
|
+
if (obj.options.footers) {
|
|
377
|
+
for (let j = 0; j < obj.options.footers.length; j++) {
|
|
378
|
+
obj.options.footers[j].splice(d, 0, obj.options.footers[j].splice(o, 1)[0]);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Keeping history of changes
|
|
383
|
+
setHistory.call(obj, {
|
|
384
|
+
action: 'moveColumn',
|
|
385
|
+
oldValue: o,
|
|
386
|
+
newValue: d,
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
// Update table references
|
|
390
|
+
updateTableReferences.call(obj);
|
|
391
|
+
|
|
392
|
+
// Events
|
|
393
|
+
dispatch.call(obj, 'onmovecolumn', obj, o, d, 1);
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Delete a column by number
|
|
398
|
+
*
|
|
399
|
+
* @param integer columnNumber - reference column to be excluded
|
|
400
|
+
* @param integer numOfColumns - number of columns to be excluded from the reference column
|
|
401
|
+
* @return void
|
|
402
|
+
*/
|
|
403
|
+
export const deleteColumn = function (columnNumber, numOfColumns) {
|
|
404
|
+
const obj = this;
|
|
405
|
+
|
|
406
|
+
// Global Configuration
|
|
407
|
+
if (obj.options.allowDeleteColumn != false) {
|
|
408
|
+
if (obj.headers.length > 1) {
|
|
409
|
+
// Delete column definitions
|
|
410
|
+
if (columnNumber == undefined) {
|
|
411
|
+
const number = obj.getSelectedColumns(true);
|
|
412
|
+
|
|
413
|
+
if (!number.length) {
|
|
414
|
+
// Remove last column
|
|
415
|
+
columnNumber = obj.headers.length - 1;
|
|
416
|
+
numOfColumns = 1;
|
|
417
|
+
} else {
|
|
418
|
+
// Remove selected
|
|
419
|
+
columnNumber = parseInt(number[0]);
|
|
420
|
+
numOfColumns = parseInt(number.length);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Lasat column
|
|
425
|
+
const lastColumn = obj.options.data[0].length - 1;
|
|
426
|
+
|
|
427
|
+
if (columnNumber == undefined || columnNumber > lastColumn || columnNumber < 0) {
|
|
428
|
+
columnNumber = lastColumn;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Minimum of columns to be delete is 1
|
|
432
|
+
if (!numOfColumns) {
|
|
433
|
+
numOfColumns = 1;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Can't delete more than the limit of the table
|
|
437
|
+
if (numOfColumns > obj.options.data[0].length - columnNumber) {
|
|
438
|
+
numOfColumns = obj.options.data[0].length - columnNumber;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const removedColumns = [];
|
|
442
|
+
for (let i = 0; i < numOfColumns; i++) {
|
|
443
|
+
removedColumns.push(i + columnNumber);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// onbeforedeletecolumn
|
|
447
|
+
if (dispatch.call(obj, 'onbeforedeletecolumn', obj, removedColumns) === false) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Can't remove the last column
|
|
452
|
+
if (parseInt(columnNumber) > -1) {
|
|
453
|
+
// Merged cells
|
|
454
|
+
let mergeExists = false;
|
|
455
|
+
if (obj.options.mergeCells && Object.keys(obj.options.mergeCells).length > 0) {
|
|
456
|
+
for (let col = columnNumber; col < columnNumber + numOfColumns; col++) {
|
|
457
|
+
if (isColMerged.call(obj, col, null).length) {
|
|
458
|
+
mergeExists = true;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (mergeExists) {
|
|
463
|
+
if (!confirm(jSuites.translate('This action will destroy any existing merged cells. Are you sure?'))) {
|
|
464
|
+
return false;
|
|
465
|
+
} else {
|
|
466
|
+
obj.destroyMerge();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Delete the column properties
|
|
471
|
+
const columns = obj.options.columns ? obj.options.columns.splice(columnNumber, numOfColumns) : undefined;
|
|
472
|
+
|
|
473
|
+
for (let col = columnNumber; col < columnNumber + numOfColumns; col++) {
|
|
474
|
+
obj.cols[col].colElement.className = '';
|
|
475
|
+
obj.headers[col].className = '';
|
|
476
|
+
obj.cols[col].colElement.parentNode.removeChild(obj.cols[col].colElement);
|
|
477
|
+
obj.headers[col].parentNode.removeChild(obj.headers[col]);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const historyHeaders = obj.headers.splice(columnNumber, numOfColumns);
|
|
481
|
+
const historyColgroup = obj.cols.splice(columnNumber, numOfColumns);
|
|
482
|
+
const historyRecords = [];
|
|
483
|
+
const historyData = [];
|
|
484
|
+
const historyFooters = [];
|
|
485
|
+
|
|
486
|
+
for (let row = 0; row < obj.options.data.length; row++) {
|
|
487
|
+
for (let col = columnNumber; col < columnNumber + numOfColumns; col++) {
|
|
488
|
+
obj.records[row][col].element.className = '';
|
|
489
|
+
obj.records[row][col].element.parentNode.removeChild(obj.records[row][col].element);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Delete headers
|
|
494
|
+
for (let row = 0; row < obj.options.data.length; row++) {
|
|
495
|
+
// History
|
|
496
|
+
historyData[row] = obj.options.data[row].splice(columnNumber, numOfColumns);
|
|
497
|
+
historyRecords[row] = obj.records[row].splice(columnNumber, numOfColumns);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
for (let i = columnNumber; i < obj.cols.length; i++) {
|
|
501
|
+
obj.cols[i].x = i;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
for (let j = 0; j < obj.records.length; j++) {
|
|
505
|
+
for (let i = columnNumber; i < obj.records[j].length; i++) {
|
|
506
|
+
obj.records[j][i].x = i;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Delete footers
|
|
511
|
+
if (obj.options.footers) {
|
|
512
|
+
for (let row = 0; row < obj.options.footers.length; row++) {
|
|
513
|
+
historyFooters[row] = obj.options.footers[row].splice(columnNumber, numOfColumns);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// Remove selection
|
|
518
|
+
conditionalSelectionUpdate.call(obj, 0, columnNumber, columnNumber + numOfColumns - 1);
|
|
519
|
+
|
|
520
|
+
// Adjust nested headers
|
|
521
|
+
if (obj.options.nestedHeaders && obj.options.nestedHeaders.length > 0 && obj.options.nestedHeaders[0] && obj.options.nestedHeaders[0][0]) {
|
|
522
|
+
for (let j = 0; j < obj.options.nestedHeaders.length; j++) {
|
|
523
|
+
const colspan = parseInt(obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan) - numOfColumns;
|
|
524
|
+
obj.options.nestedHeaders[j][obj.options.nestedHeaders[j].length - 1].colspan = colspan;
|
|
525
|
+
obj.thead.children[j].children[obj.thead.children[j].children.length - 1].setAttribute('colspan', colspan);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// Keeping history of changes
|
|
530
|
+
setHistory.call(obj, {
|
|
531
|
+
action: 'deleteColumn',
|
|
532
|
+
columnNumber: columnNumber,
|
|
533
|
+
numOfColumns: numOfColumns,
|
|
534
|
+
insertBefore: 1,
|
|
535
|
+
columns: columns,
|
|
536
|
+
headers: historyHeaders,
|
|
537
|
+
cols: historyColgroup,
|
|
538
|
+
records: historyRecords,
|
|
539
|
+
footers: historyFooters,
|
|
540
|
+
data: historyData,
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
// Update table references
|
|
544
|
+
updateTableReferences.call(obj);
|
|
545
|
+
|
|
546
|
+
// Delete
|
|
547
|
+
dispatch.call(obj, 'ondeletecolumn', obj, removedColumns);
|
|
548
|
+
}
|
|
549
|
+
} else {
|
|
550
|
+
console.error('Jspreadsheet: It is not possible to delete the last column');
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Get the column width
|
|
557
|
+
*
|
|
558
|
+
* @param int column column number (first column is: 0)
|
|
559
|
+
* @return int current width
|
|
560
|
+
*/
|
|
561
|
+
export const getWidth = function (column) {
|
|
562
|
+
const obj = this;
|
|
563
|
+
|
|
564
|
+
let data;
|
|
565
|
+
|
|
566
|
+
if (typeof column === 'undefined') {
|
|
567
|
+
// Get all headers
|
|
568
|
+
data = [];
|
|
569
|
+
for (let i = 0; i < obj.headers.length; i++) {
|
|
570
|
+
data.push((obj.options.columns && obj.options.columns[i] && obj.options.columns[i].width) || obj.options.defaultColWidth || 100);
|
|
571
|
+
}
|
|
572
|
+
} else {
|
|
573
|
+
data = parseInt(obj.cols[column].colElement.getAttribute('width'));
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
return data;
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Set the column width
|
|
581
|
+
*
|
|
582
|
+
* @param int column number (first column is: 0)
|
|
583
|
+
* @param int new column width
|
|
584
|
+
* @param int old column width
|
|
585
|
+
*/
|
|
586
|
+
export const setWidth = function (column, width, oldWidth) {
|
|
587
|
+
const obj = this;
|
|
588
|
+
|
|
589
|
+
if (width) {
|
|
590
|
+
if (Array.isArray(column)) {
|
|
591
|
+
// Oldwidth
|
|
592
|
+
if (!oldWidth) {
|
|
593
|
+
oldWidth = [];
|
|
594
|
+
}
|
|
595
|
+
// Set width
|
|
596
|
+
for (let i = 0; i < column.length; i++) {
|
|
597
|
+
if (!oldWidth[i]) {
|
|
598
|
+
oldWidth[i] = parseInt(obj.cols[column[i]].colElement.getAttribute('width'));
|
|
599
|
+
}
|
|
600
|
+
const w = Array.isArray(width) && width[i] ? width[i] : width;
|
|
601
|
+
obj.cols[column[i]].colElement.setAttribute('width', w);
|
|
602
|
+
|
|
603
|
+
if (!obj.options.columns) {
|
|
604
|
+
obj.options.columns = [];
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (!obj.options.columns[column[i]]) {
|
|
608
|
+
obj.options.columns[column[i]] = {};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
obj.options.columns[column[i]].width = w;
|
|
612
|
+
}
|
|
613
|
+
} else {
|
|
614
|
+
// Oldwidth
|
|
615
|
+
if (!oldWidth) {
|
|
616
|
+
oldWidth = parseInt(obj.cols[column].colElement.getAttribute('width'));
|
|
617
|
+
}
|
|
618
|
+
// Set width
|
|
619
|
+
obj.cols[column].colElement.setAttribute('width', width);
|
|
620
|
+
|
|
621
|
+
if (!obj.options.columns) {
|
|
622
|
+
obj.options.columns = [];
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (!obj.options.columns[column]) {
|
|
626
|
+
obj.options.columns[column] = {};
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
obj.options.columns[column].width = width;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Keeping history of changes
|
|
633
|
+
setHistory.call(obj, {
|
|
634
|
+
action: 'setWidth',
|
|
635
|
+
column: column,
|
|
636
|
+
oldValue: oldWidth,
|
|
637
|
+
newValue: width,
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// On resize column
|
|
641
|
+
dispatch.call(obj, 'onresizecolumn', obj, column, width, oldWidth);
|
|
642
|
+
|
|
643
|
+
// Update corner position
|
|
644
|
+
updateCornerPosition.call(obj);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Show column
|
|
650
|
+
*/
|
|
651
|
+
export const showColumn = function (colNumber) {
|
|
652
|
+
const obj = this;
|
|
653
|
+
|
|
654
|
+
if (!Array.isArray(colNumber)) {
|
|
655
|
+
colNumber = [colNumber];
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
for (let i = 0; i < colNumber.length; i++) {
|
|
659
|
+
const columnIndex = colNumber[i];
|
|
660
|
+
|
|
661
|
+
obj.headers[columnIndex].style.display = '';
|
|
662
|
+
obj.cols[columnIndex].colElement.style.display = '';
|
|
663
|
+
if (obj.filter && obj.filter.children.length > columnIndex + 1) {
|
|
664
|
+
obj.filter.children[columnIndex + 1].style.display = '';
|
|
665
|
+
}
|
|
666
|
+
for (let j = 0; j < obj.options.data.length; j++) {
|
|
667
|
+
obj.records[j][columnIndex].element.style.display = '';
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// Update footers
|
|
672
|
+
if (obj.options.footers) {
|
|
673
|
+
setFooter.call(obj);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
obj.resetSelection();
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Hide column
|
|
681
|
+
*/
|
|
682
|
+
export const hideColumn = function (colNumber) {
|
|
683
|
+
const obj = this;
|
|
684
|
+
|
|
685
|
+
if (!Array.isArray(colNumber)) {
|
|
686
|
+
colNumber = [colNumber];
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
for (let i = 0; i < colNumber.length; i++) {
|
|
690
|
+
const columnIndex = colNumber[i];
|
|
691
|
+
|
|
692
|
+
obj.headers[columnIndex].style.display = 'none';
|
|
693
|
+
obj.cols[columnIndex].colElement.style.display = 'none';
|
|
694
|
+
if (obj.filter && obj.filter.children.length > columnIndex + 1) {
|
|
695
|
+
obj.filter.children[columnIndex + 1].style.display = 'none';
|
|
696
|
+
}
|
|
697
|
+
for (let j = 0; j < obj.options.data.length; j++) {
|
|
698
|
+
obj.records[j][columnIndex].element.style.display = 'none';
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// Update footers
|
|
703
|
+
if (obj.options.footers) {
|
|
704
|
+
setFooter.call(obj);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
obj.resetSelection();
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Get a column data by columnNumber
|
|
712
|
+
*/
|
|
713
|
+
export const getColumnData = function (columnNumber, processed) {
|
|
714
|
+
const obj = this;
|
|
715
|
+
|
|
716
|
+
const dataset = [];
|
|
717
|
+
// Go through the rows to get the data
|
|
718
|
+
for (let j = 0; j < obj.options.data.length; j++) {
|
|
719
|
+
if (processed) {
|
|
720
|
+
dataset.push(obj.records[j][columnNumber].element.innerHTML);
|
|
721
|
+
} else {
|
|
722
|
+
dataset.push(obj.options.data[j][columnNumber]);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return dataset;
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Set a column data by colNumber
|
|
730
|
+
*/
|
|
731
|
+
export const setColumnData = function (colNumber, data, force) {
|
|
732
|
+
const obj = this;
|
|
733
|
+
|
|
734
|
+
for (let j = 0; j < obj.rows.length; j++) {
|
|
735
|
+
// Update cell
|
|
736
|
+
const columnName = getColumnNameFromId([colNumber, j]);
|
|
737
|
+
// Set value
|
|
738
|
+
if (data[j] != null) {
|
|
739
|
+
obj.setValue(columnName, data[j], force);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
};
|