-
Notifications
You must be signed in to change notification settings - Fork 0
/
jexcel.js
12306 lines (10711 loc) · 413 KB
/
jexcel.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* (c) jExcel v3.3.1
*
* Author: Paul Hodel <[email protected]>
* Website: https://bossanova.uk/jexcel/
* Description: Create amazing web based spreadsheets.
*
* This software is distribute under MIT License
*
* Downloaded master on 2019-07-15 09:58.
* Includes https://github.com/paulhodel/jexcel/pull/445
*/
'use strict';
if (! jSuites && typeof(require) === 'function') {
var jSuites = require('jsuites');
require('jsuites/dist/jsuites.css');
}
var jexcel = (function(el, options) {
// Create jexcel object
var obj = {};
obj.options = {};
// Loading default configuration
var defaults = {
// External data
url:null,
// Data
data:[[]],
// Copy behavior
copyCompatibility:false,
// Rows and columns definitions
rows:[],
columns:[],
// Deprected legacy options
colHeaders:[],
colWidths:[],
colAlignments:[],
nestedHeaders:null,
// Column width that is used by default
defaultColWidth:50,
// Spare rows and columns
minSpareRows:0,
minSpareCols:0,
// Minimal table dimensions
minDimensions:[0,0],
// Allow Export
allowExport:true,
// Allow column sorting
columnSorting:true,
// Allow column resizing
columnDrag:false,
// Allow column resizing
columnResize:true,
// Allow row resizing
rowResize:false,
// Allow row dragging
rowDrag:true,
// Allow table edition
editable:true,
// Allow new rows
allowInsertRow:true,
// Allow new rows
allowManualInsertRow:true,
// Allow new columns
allowInsertColumn:true,
// Allow new rows
allowManualInsertColumn:true,
// Allow row delete
allowDeleteRow:true,
// Allow column delete
allowDeleteColumn:true,
// Allow rename column
allowRenameColumn:true,
// Allow comments
allowComments:false,
// Global wrap
wordWrap:false,
// CSV source
csv:null,
// Filename
csvFileName:'jexcel',
// Consider first line as header
csvHeaders:true,
// Delimiters
csvDelimiter:',',
// Disable corner selection
selectionCopy:true,
// Merged cells
mergeCells:[],
// Create toolbar
toolbar:null,
// Allow search
search:false,
// Create pagination
pagination:false,
paginationOptions:null,
// Full screen
fullscreen:false,
// Lazy loading
lazyLoading:false,
loadingSpin:false,
// Table overflow
tableOverflow:false,
tableHeight:'300px',
tableWidth:null,
// Style
style:null,
// Event handles
onload:null,
onchange:null,
onbeforechange:null,
oninsertrow:null,
oninsertcolumn:null,
ondeleterow:null,
ondeletecolumn:null,
onmoverow:null,
onmovecolumn:null,
onresizerow:null,
onresizecolumn:null,
onsort:null,
onselection:null,
onpaste:null,
onmerge:null,
onfocus:null,
onblur:null,
// Customize any cell behavior
updateTable:null,
// Texts
text:{
noRecordsFound: 'No records found',
showingPage: 'Showing page {0} of {1} entries',
show: 'Show ',
search: 'Search',
entries: ' entries',
insertANewColumnBefore: 'Insert a new column before',
insertANewColumnAfter: 'Insert a new column after',
deleteSelectedColumns: 'Delete selected columns',
renameThisColumn: 'Rename this column',
orderAscending: 'Order ascending',
orderDescending: 'Order descending',
insertANewRowBefore: 'Insert a new row before',
insertANewRowAfter: 'Insert a new row after',
deleteSelectedRows: 'Delete selected rows',
editComments: 'Edit comments',
addComments: 'Add comments',
comments: 'Comments',
clearComments: 'Clear comments',
copy: 'Copy...',
paste: 'Paste...',
saveAs: 'Save as...',
about: 'About',
areYouSureToDeleteTheSelectedRows: 'Are you sure to delete the selected rows?',
areYouSureToDeleteTheSelectedColumns: 'Are you sure to delete the selected columns?',
thisActionWillDestroyAnyExistingMergedCellsAreYouSure: 'This action will destroy any existing merged cells. Are you sure?',
thisActionWillClearYourSearchResultsAreYouSure: 'This action will clear your search results. Are you sure?',
thereIsAConflictWithAnotherMergedCell: 'There is a conflict with another merged cell',
invalidMergeProperties: 'Invalid merged properties',
cellAlreadyMerged: 'Cell already merged',
noCellsSelected: 'No cells selected',
},
// About message
about:"jExcel CE Spreadsheet\nVersion 3.3.1\nAuthor: Paul Hodel <[email protected]>\nWebsite: https://jexcel.net/v3",
};
// Loading initial configuration from user
for (var property in defaults) {
if (options && options.hasOwnProperty(property)) {
obj.options[property] = (property == 'text') ? Object.assign(defaults[property], options[property]) : options[property];
} else {
obj.options[property] = defaults[property];
}
}
// Global elements
obj.el = el;
obj.corner = null;
obj.contextMenu = null;
obj.textarea = null;
obj.ads = null;
obj.content = null;
obj.table = null;
obj.thead = null;
obj.tbody = null;
obj.rows = [];
obj.results = null;
obj.searchInput = null;
obj.toolbar = null;
obj.pagination = null;
obj.pageNumber = null;
obj.headerContainer = null;
obj.colgroupContainer = null;
// Containers
obj.headers = [];
obj.records = [];
obj.history = [];
obj.formula = [];
obj.formulaStack = 0;
obj.colgroup = [];
obj.selection = [];
obj.highlighted = [];
obj.selectedCell = null;
obj.selectedContainer = null;
obj.style = [];
obj.meta = [];
obj.data = null;
// Internal controllers
obj.cursor = null;
obj.historyIndex = -1;
obj.ignoreEvents = false;
obj.ignoreHistory = false;
obj.edition = null;
obj.hashString = null;
obj.resizing = null;
obj.dragging = null;
// Lazy loading
if (obj.options.lazyLoading == true && (obj.options.tableOverflow == false && obj.options.fullscreen == false)) {
console.error('JEXCEL: The lazyloading only works when tableOverflow = yes or fullscreen = yes');
obj.options.lazyLoading = false;
}
/**
* Prepare the jexcel table
*
* @Param config
*/
obj.prepareTable = function() {
// Loading initial data from remote sources
var results = [];
// Number of columns
var size = obj.options.columns.length;
if (typeof obj.options.data[0] !== 'undefined' &&
obj.options.data[0].length > size) {
size = obj.options.data[0].length;
}
// Minimal dimensions
if (obj.options.minDimensions[0] > size) {
size = obj.options.minDimensions[0];
}
// Requests
var requests = [];
var requestsIndex = [];
// Preparations
for (var i = 0; i < size; i++) {
// Deprected options. You should use only columns
if (! obj.options.colHeaders[i]) {
obj.options.colHeaders[i] = '';
}
if (! obj.options.colWidths[i]) {
obj.options.colWidths[i] = obj.options.defaultColWidth || '50';
}
if (! obj.options.colAlignments[i]) {
obj.options.colAlignments[i] = 'center';
}
// Default column description
if (! obj.options.columns[i]) {
obj.options.columns[i] = { type:'text' };
} else if (! obj.options.columns[i]) {
obj.options.columns[i].type = 'text';
}
if (! obj.options.columns[i].source) {
obj.options.columns[i].source = [];
}
if (! obj.options.columns[i].options) {
obj.options.columns[i].options = [];
}
if (! obj.options.columns[i].editor) {
obj.options.columns[i].editor = null;
}
if (! obj.options.columns[i].allowEmpty) {
obj.options.columns[i].allowEmpty = false;
}
if (! obj.options.columns[i].title) {
obj.options.columns[i].title = obj.options.colHeaders[i] ? obj.options.colHeaders[i] : '';
}
if (! obj.options.columns[i].width) {
obj.options.columns[i].width = obj.options.colWidths[i] ? obj.options.colWidths[i] : '50';
}
if (! obj.options.columns[i].align) {
obj.options.columns[i].align = obj.options.colAlignments[i] ? obj.options.colAlignments[i] : 'center';
}
// Pre-load initial source for json autocomplete
if (obj.options.columns[i].type == 'autocomplete' || obj.options.columns[i].type == 'dropdown') {
// if remote content
if (obj.options.columns[i].url) {
requestsIndex.push(i);
requests.push(fetch(obj.options.columns[i].url, { headers: new Headers({ 'content-type': 'text/json' }) })
.then(function(data) {
return data.json();
}));
}
} else if (obj.options.columns[i].type == 'calendar') {
// Default format for date columns
if (! obj.options.columns[i].options.format) {
obj.options.columns[i].options.format = 'DD/MM/YYYY';
}
}
}
if (requests.length) {
Promise.all(requests).then(function(data) {
for (var i = 0; i < data.length; i++) {
obj.options.columns[i].source = data[i];
}
obj.createTable();
});
} else {
// Create table
obj.createTable();
}
}
obj.createTable = function() {
// Elements
obj.table = document.createElement('table');
obj.thead = document.createElement('thead');
obj.tbody = document.createElement('tbody');
// Create headers controllers
obj.headers = [];
obj.colgroup = [];
// Create table container
obj.content = document.createElement('div');
obj.content.classList.add('jexcel_content');
// Create toolbar object
obj.toolbar = document.createElement('div');
obj.toolbar.classList.add('jexcel_toolbar');
// Search
var searchContainer = document.createElement('div');
var searchText = document.createTextNode((obj.options.text.search) + ': ');
obj.searchInput = document.createElement('input');
obj.searchInput.classList.add('jexcel_search');
searchContainer.appendChild(searchText);
searchContainer.appendChild(obj.searchInput);
obj.searchInput.onfocus = function() {
obj.resetSelection();
}
// Pagination select option
var paginationUpdateContainer = document.createElement('div');
if (obj.options.pagination > 0 && obj.options.paginationOptions && obj.options.paginationOptions.length > 0) {
obj.paginationDropdown = document.createElement('select');
obj.paginationDropdown.classList.add('jexcel_pagination_dropdown');
obj.paginationDropdown.onchange = function() {
obj.options.pagination = parseInt(this.value);
obj.page(0);
}
for (var i = 0; i < obj.options.paginationOptions.length; i++) {
var temp = document.createElement('option');
temp.value = obj.options.paginationOptions[i];
temp.innerHTML = obj.options.paginationOptions[i];
obj.paginationDropdown.appendChild(temp);
}
paginationUpdateContainer.appendChild(document.createTextNode(obj.options.text.show));
paginationUpdateContainer.appendChild(obj.paginationDropdown);
paginationUpdateContainer.appendChild(document.createTextNode(obj.options.text.entries));
}
// Filter and pagination container
obj.filter = document.createElement('div');
obj.filter.classList.add('jexcel_filter');
obj.filter.appendChild(paginationUpdateContainer);
obj.filter.appendChild(searchContainer);
// Colsgroup
obj.colgroupContainer = document.createElement('colgroup');
var tempCol = document.createElement('col');
tempCol.setAttribute('width', 50);
obj.colgroupContainer.appendChild(tempCol);
// Nested
if (obj.options.nestedHeaders && obj.options.nestedHeaders.length > 0) {
// Flexible way to handle nestedheaders
if (obj.options.nestedHeaders[0] && obj.options.nestedHeaders[0][0]) {
for (var j = 0; j < obj.options.nestedHeaders.length; j++) {
obj.thead.appendChild(obj.createNestedHeader(obj.options.nestedHeaders[j]));
}
} else {
obj.thead.appendChild(obj.createNestedHeader(obj.options.nestedHeaders));
}
}
// Row
obj.headerContainer = document.createElement('tr');
var tempCol = document.createElement('td');
tempCol.classList.add('jexcel_selectall');
obj.headerContainer.appendChild(tempCol);
for (var i = 0; i < obj.options.columns.length; i++) {
// Create header
obj.createCellHeader(i);
// Append cell to the container
obj.headerContainer.appendChild(obj.headers[i]);
obj.colgroupContainer.appendChild(obj.colgroup[i]);
}
obj.thead.appendChild(obj.headerContainer);
// Content table
obj.table = document.createElement('table');
obj.table.classList.add('jexcel');
obj.table.setAttribute('cellpadding', '0');
obj.table.setAttribute('cellspacing', '0');
obj.table.setAttribute('unselectable', 'yes');
obj.table.setAttribute('onselectstart', 'return false');
obj.table.appendChild(obj.colgroupContainer);
obj.table.appendChild(obj.thead);
obj.table.appendChild(obj.tbody);
// Spreadsheet corner
obj.corner = document.createElement('div');
obj.corner.className = 'jexcel_corner';
obj.corner.setAttribute('unselectable', 'on');
obj.corner.setAttribute('onselectstart', 'return false');
if (obj.options.selectionCopy == false) {
obj.corner.style.display = 'none';
}
// Textarea helper
obj.textarea = document.createElement('textarea');
obj.textarea.className = 'jexcel_textarea';
obj.textarea.id = 'jexcel_textarea';
// Contextmenu container
obj.contextMenu = document.createElement('div');
obj.contextMenu.className = 'jexcel_contextmenu';
// Create element
jSuites.contextmenu(obj.contextMenu, {
onclick:function() {
obj.contextMenu.contextmenu.close(false);
}
});
// Powered by jExcel
var ads = '<a href="https://bossanova.uk/jexcel/"><img src="//bossanova.uk/jexcel/logo.png">jExcel Spreadsheet</a>';
obj.ads = document.createElement('div');
obj.ads.className = 'jexcel_about';
if (typeof(sessionStorage) !== "undefined") {
if (! sessionStorage.getItem('jexcel')) {
sessionStorage.setItem('jexcel', true);
obj.ads.innerHTML = ads;
}
} else {
obj.ads.innerHTML = ads;
}
// Create table container TODO: frozen columns
var container = document.createElement('div');
container.classList.add('jexcel_table');
// Pagination
obj.pagination = document.createElement('div');
obj.pagination.classList.add('jexcel_pagination');
var paginationInfo = document.createElement('div');
var paginationPages = document.createElement('div');
obj.pagination.appendChild(paginationInfo);
obj.pagination.appendChild(paginationPages);
// Append containers to the table
if (obj.options.search == true) {
el.appendChild(obj.filter);
}
// Elements
obj.content.appendChild(obj.table);
obj.content.appendChild(obj.corner);
obj.content.appendChild(obj.textarea);
el.appendChild(obj.toolbar);
el.appendChild(obj.content);
el.appendChild(obj.pagination);
el.appendChild(obj.contextMenu);
el.appendChild(obj.ads);
el.classList.add('jexcel_container');
// Create toolbar
if (obj.options.toolbar && obj.options.toolbar.length) {
obj.createToolbar();
}
// Fullscreen
if (obj.options.fullscreen == true) {
el.classList.add('fullscreen');
if (obj.options.toolbar) {
el.classList.add('with-toolbar');
}
} else {
// Overflow
if (obj.options.tableOverflow == true) {
if (obj.options.tableHeight) {
obj.content.style['overflow-y'] = 'auto';
obj.content.style.height = obj.options.tableHeight;
}
if (obj.options.tableWidth) {
el.content.style['overflow-x'] = 'auto';
el.content.width = obj.options.tableWidth;
}
}
}
// Actions
if (obj.options.columnDrag == true) {
obj.thead.classList.add('draggable');
}
if (obj.options.columnResize == true) {
obj.thead.classList.add('resizable');
}
if (obj.options.rowDrag == true) {
obj.tbody.classList.add('draggable');
}
if (obj.options.rowResize == true) {
obj.tbody.classList.add('resizable');
}
// Load data
obj.setData();
// Style
if (obj.options.style) {
obj.setStyle(obj.options.style, null, null, 1, 1);
}
}
/**
* Set data
*
* @param array data In case no data is sent, default is reloaded
* @return void
*/
obj.setData = function(data) {
// Update data
if (data) {
if (typeof(data) == 'string') {
data = JSON.parse(data);
}
obj.options.data = data;
}
// Adjust minimal dimensions
var j = 0;
var i = 0;
var size_i = obj.options.columns.length;
var size_j = obj.options.data.length;
var min_i = obj.options.minDimensions[0];
var min_j = obj.options.minDimensions[1];
var max_i = min_i > size_i ? min_i : size_i;
var max_j = min_j > size_j ? min_j : size_j;
for (j = 0; j < max_j; j++) {
for (i = 0; i < max_i; i++) {
if (obj.options.data[j] == undefined) {
obj.options.data[j] = [];
}
if (obj.options.data[j][i] == undefined) {
obj.options.data[j][i] = '';
}
}
}
// Reset containers
obj.rows = [];
obj.results = null;
obj.records = [];
obj.history = [];
// Reset internal controllers
obj.historyIndex = -1;
// Reset data
obj.tbody.innerHTML = '';
// Lazy loading
if (obj.options.lazyLoading == true) {
// Load only 100 records
var startNumber = 0
var finalNumber = obj.options.data.length < 100 ? obj.options.data.length : 100;
if (obj.options.pagination) {
obj.options.pagination = false;
console.error('JEXCEL: Pagination will be disable due the lazyLoading');
}
} else if (obj.options.pagination) {
// Pagination
if (! obj.pageNumber) {
obj.pageNumber = 0;
}
var quantityPerPage = obj.options.pagination;
startNumber = (obj.options.pagination * obj.pageNumber);
finalNumber = (obj.options.pagination * obj.pageNumber) + obj.options.pagination;
if (obj.options.data.length < finalNumber) {
finalNumber = obj.options.data.length;
}
} else {
var startNumber = 0;
var finalNumber = obj.options.data.length;
}
// Append nodes to the HTML
for (j = 0; j < obj.options.data.length; j++) {
// Create row
var tr = obj.createRow(j, obj.options.data[j]);
// Append line to the table
if (j >= startNumber && j < finalNumber) {
obj.tbody.appendChild(tr);
}
}
if (obj.options.lazyLoading == true) {
// Do not create pagination with lazyloading activated
} else if (obj.options.pagination) {
obj.updatePagination();
}
// Merge cells
if (obj.options.mergeCells) {
var keys = Object.keys(obj.options.mergeCells);
for (var i = 0; i < keys.length; i++) {
var num = obj.options.mergeCells[keys[i]];
obj.setMerge(keys[i], num[0], num[1], 1);
}
}
// Updata table with custom configurations if applicable
obj.updateTable();
// Onload
if (! obj.ignoreEvents) {
if (typeof(obj.options.onload) == 'function') {
obj.options.onload(el);
}
}
}
/**
* Get the whole table data
*
* @param integer row number
* @return string value
*/
obj.getData = function(highlighted) {
// Control vars
var dataset = [];
var px = 0;
var py = 0;
// Column and row length
var x = obj.options.data[0].length
var y = obj.options.data.length
// Go through the columns to get the data
for (var j = 0; j < y; j++) {
px = 0;
for (var i = 0; i < x; i++) {
// Cell selected or fullset
if (! highlighted || obj.records[j][i].classList.contains('highlight')) {
// Get value
if (! dataset[py]) {
dataset[py] = [];
}
dataset[py][px] = obj.options.data[j][i];
px++;
}
}
if (px > 0) {
py++;
}
}
return dataset;
}
/**
* Get a row data by rowNumber
*/
obj.getRowData = function(rowNumber) {
return obj.options.data[rowNumber];
}
/**
* Set a row data by rowNumber
*/
obj.setRowData = function(rowNumber, data) {
for (var i = 0; i < obj.headers.length; i++) {
// Update cell
var columnName = jexcel.getColumnNameFromId([ i, rowNumber ]);
// Set value
obj.setValue(columnName, data[i]);
}
}
/**
* Get a column data by columnNumber
*/
obj.getColumnData = function(columnNumber) {
var dataset = [];
// Go through the rows to get the data
for (var j = 0; j < obj.options.data.length; j++) {
dataset.push(obj.options.data[j][columnNumber]);
}
return dataset;
}
/**
* Create row
*/
obj.createRow = function(j, data) {
// Create container
if (! obj.records[j]) {
obj.records[j] = [];
}
// New line of data to be append in the table
obj.rows[j] = document.createElement('tr');
obj.rows[j].setAttribute('data-y', j);
// Definitions
if (obj.options.rows[j]) {
if (obj.options.rows[j].height) {
obj.rows[j].style.height = obj.options.rows[j].height;
}
}
// Row number label
var td = document.createElement('td');
td.innerHTML = parseInt(j + 1);
td.setAttribute('data-y', j);
td.className = 'jexcel_row';
obj.rows[j].appendChild(td);
// Data columns
for (i = 0; i < obj.options.columns.length; i++) {
// New column of data to be append in the line
obj.records[j][i] = obj.createCell(i, j, data[i]);
// Add column to the row
obj.rows[j].appendChild(obj.records[j][i]);
}
// Add row to the table body
return obj.rows[j];
}
/**
* Create cell
*/
obj.createCell = function(i, j, value) {
// Create cell and properties
var td = document.createElement('td');
td.setAttribute('data-x', i);
td.setAttribute('data-y', j);
// Hidden column
if (obj.options.columns[i].type == 'hidden') {
td.style.display = 'none';
td.innerHTML = value;
} else if (obj.options.columns[i].type == 'checkbox' || obj.options.columns[i].type == 'radio') {
// Create input
var element = document.createElement('input');
element.type = obj.options.columns[i].type;
element.name = 'c' + i;
element.checked = (value == 1 || value == true || value == 'true') ? true : false;
element.onclick = function() {
obj.setValue(td, this.checked);
}
if (obj.options.columns[i].readOnly == true) {
element.setAttribute('disabled', 'disabled');
}
// Append to the table
td.appendChild(element);
// Make sure the values are correct
obj.options.data[j][i] = element.checked;
} else if (obj.options.columns[i].type == 'calendar') {
// Try formatted date
var formatted = jSuites.calendar.extractDateFromString(value, obj.options.columns[i].options.format);
// Create calendar cell
td.innerHTML = jSuites.calendar.getDateString(formatted ? formatted : value, obj.options.columns[i].options.format);
} else if (obj.options.columns[i].type == 'dropdown' || obj.options.columns[i].type == 'autocomplete') {
// Create dropdown cell
td.classList.add('dropdown');
td.innerHTML = obj.getDropDownValue(i, value);
} else if (obj.options.columns[i].type == 'color') {
if (obj.options.columns[i].render == 'square') {
var color = document.createElement('div');
color.className = 'color';
color.style.backgroundColor = value;
td.appendChild(color);
} else {
td.style.color = value;
td.innerHTML = value;
}
} else if (obj.options.columns[i].type == 'image') {
if (value && value.substr(0, 10) == 'data:image') {
var img = document.createElement('img');
img.src = value;
td.appendChild(img);
}
} else {
if ((''+value).substr(0,1) == '=') {
value = obj.executeFormula(value, i, j)
}
if (obj.options.columns[i].mask) {
var decimal = obj.options.columns[i].decimal || '.';
value = '' + jSuites.mask.run(value, obj.options.columns[i].mask, decimal);
}
td.innerHTML = value;
}
// Readonly
if (obj.options.columns[i].readOnly == true) {
td.className = 'readonly';
}
// Text align
var colAlign = obj.options.columns[i].align ? obj.options.columns[i].align : 'center';
td.style.textAlign = colAlign;
// Wrap option
if (obj.options.wordWrap == true || obj.options.columns[i].wordWrap == true || td.innerHTML.length > 200) {
td.style.whiteSpace = 'pre-wrap';
}
// Overflow
if (i > 0) {
if (value || td.innerHTML) {
obj.records[j][i-1].style.overflow = 'hidden';
} else {
if (i == obj.options.columns.length - 1) {
td.style.overflow = 'hidden';
}
}
}
return td;
}
obj.createCellHeader = function(colNumber) {
// Create col global control
var colWidth = obj.options.columns[colNumber].width ? obj.options.columns[colNumber].width : obj.options.defaultColWidth;
var colAlign = obj.options.columns[colNumber].align ? obj.options.columns[colNumber].align : 'center';
// Create header cell
obj.headers[colNumber] = document.createElement('td');
obj.headers[colNumber].innerHTML = obj.options.columns[colNumber].title ? obj.options.columns[colNumber].title : jexcel.getColumnName(colNumber);
obj.headers[colNumber].setAttribute('data-x', colNumber);
obj.headers[colNumber].style.textAlign = colAlign;
if (obj.options.columns[colNumber].title) {
obj.headers[colNumber].setAttribute('title', obj.options.columns[colNumber].title);
}
// Width control
obj.colgroup[colNumber] = document.createElement('col');
obj.colgroup[colNumber].setAttribute('width', colWidth);
// Hidden column
if (obj.options.columns[colNumber].type == 'hidden') {
obj.headers[colNumber].style.display = 'none';
obj.colgroup[colNumber].style.display = 'none';
}
}
obj.createNestedHeader = function(nestedInformation) {
var tr = document.createElement('tr');
tr.classList.add('jexcel_nested');
var td = document.createElement('td');
tr.appendChild(td);
var headerIndex = 0;
for (var i = 0; i < nestedInformation.length; i++) {
// Default values
if (! nestedInformation[i].colspan) {
nestedInformation[i].colspan = 1;
}
if (! nestedInformation[i].align) {
nestedInformation[i].align = 'center';
}
if (! nestedInformation[i].title) {
nestedInformation[i].title = '';
}
// Classes container
var column = [];
// Header classes for this cell
for (var x = 0; x < nestedInformation[i].colspan; x++) {
column.push(headerIndex);
headerIndex++;
}
// Created the nested cell
var td = document.createElement('td');
td.setAttribute('data-column', column.join(','));
td.setAttribute('colspan', nestedInformation[i].colspan);
td.setAttribute('align', nestedInformation[i].align);
td.innerHTML = nestedInformation[i].title;
tr.appendChild(td);
}
return tr;
}
/**
* Create toolbar
*/
obj.createToolbar = function(toolbar) {
if (toolbar) {
obj.options.toolbar = toolbar;
} else {
var toolbar = obj.options.toolbar;
}
for (var i = 0; i < toolbar.length; i++) {
if (toolbar[i].type == 'i') {
var toolbarItem = document.createElement('i');
toolbarItem.classList.add('jexcel_toolbar_item');
toolbarItem.classList.add('material-icons');
toolbarItem.setAttribute('data-k', toolbar[i].k);
toolbarItem.setAttribute('data-v', toolbar[i].v);
// Handle click
if (toolbar[i].onclick && typeof(toolbar[i].onclick)) {
toolbarItem.onclick = toolbar[i].onclick;
} else {
toolbarItem.onclick = function() {
var k = this.getAttribute('data-k');
var v = this.getAttribute('data-v');
obj.setStyle(obj.highlighted, k, v);
}
}
// Append element
toolbarItem.innerHTML = toolbar[i].content;
obj.toolbar.appendChild(toolbarItem);
} else if (toolbar[i].type == 'select') {
var toolbarItem = document.createElement('select');
toolbarItem.classList.add('jexcel_toolbar_item');
toolbarItem.setAttribute('data-k', toolbar[i].k);
// Handle onchange
if (toolbar[i].onchange && typeof(toolbar[i].onchange)) {
toolbarItem.onchange = toolbar[i].onchange;
} else {
toolbarItem.onchange = function() {
var k = this.getAttribute('data-k');
obj.setStyle(obj.highlighted, k, this.value);
}
}
// Add options to the dropdown
for(var j = 0; j < toolbar[i].v.length; j++) {
var toolbarDropdownOption = document.createElement('option');
toolbarDropdownOption.value = toolbar[i].v[j];
toolbarDropdownOption.innerHTML = toolbar[i].v[j];
toolbarItem.appendChild(toolbarDropdownOption);
}
obj.toolbar.appendChild(toolbarItem);
} else if (toolbar[i].type == 'color') {
var toolbarItem = document.createElement('i');
toolbarItem.classList.add('jexcel_toolbar_item');
toolbarItem.classList.add('material-icons');
toolbarItem.setAttribute('data-k', toolbar[i].k);
toolbarItem.setAttribute('data-v', '');
obj.toolbar.appendChild(toolbarItem);
toolbarItem.onclick = function() {
this.color.open();
}
toolbarItem.innerHTML = toolbar[i].content;
jSuites.color(toolbarItem, {
onchange:function(o, v) {
var k = o.getAttribute('data-k');
obj.setStyle(obj.highlighted, k, v);
}
});
}
}
}
/**
* Merge cells
* @param cellName
* @param colspan
* @param rowspan
* @param ignoreHistoryAndEvents
*/
obj.setMerge = function(cellName, colspan, rowspan, ignoreHistoryAndEvents) {