-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.table.js
1796 lines (1545 loc) · 57.9 KB
/
parser.table.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
/*!
* Web Experience Toolkit (WET) / Boîte à outils de l'expérience Web (BOEW)
* wet-boew.github.com/wet-boew/License-eng.txt / wet-boew.github.com/wet-boew/Licence-fra.txt
*/
/**
* Table Parser - Table usability - Core plugin
*
* @author: Pierre Dubois
*
*/
/*global jQuery: false*/
(function ($) {
"use strict";
var _pe = window.pe || {
fn : {}
};
_pe.fn.parsertable = {
onParserError: undefined,
parse : function (elm) {
var obj = elm,
// Event handler for issue error found durring the table parsing process
errorTrigger = function (numerr, obj) {
// FYI - 31 Type of Error can be raised
if (typeof _pe.fn.parsertable.onParserError === "function") {
_pe.fn.parsertable.onParserError(numerr, obj);
}
// $(obj).trigger('parser.table.error', err, obj);
// console.log("Trigger ERROR: " + err); // Debug Mode
},
groupZero = {
allParserObj: [],
nbDescriptionRow: 0 // To remove ??
},
colgroupFrame = [],
columnFrame = [],
uidElem = 0,
currentRowLevel = 0,
currentRowPos = 0,
spannedRow = [],
tableCellWidth = 0,
headerRowGroupCompleted = false,
summaryRowGroupEligible = false,
currentRowHeader = [],
currentTbodyID,
theadRowStack = [],
stackRowHeader = false,
// Row Group Variable
rowgroupHeaderRowStack = [],
currentRowGroup,
currentRowGroupElement,
lstRowGroup = [],
rowgroupheadercalled = false,
hasTfoot = $(obj).has('tfoot'),
lastHeadingSummaryColPos,
previousDataHeadingColPos,
tfootOnProcess = false,
hassumMode = false;
// elm need to be a table
if ($(elm).get(0).nodeName.toLowerCase() !== 'table') {
errorTrigger(1, elm);
return;
}
// Check if this table was already parsed, if yes we exit by throwing an error
if ($(obj).tblparser) {
errorTrigger(2, obj);
return;
}
// Check for hassum mode
hassumMode = $(elm).hasClass('hassum');
/*
+-----------------------------------------------------+
| FYI - Here the value and signification of each type |
+-------+---------------+-----------------------------+
| Type | Signification | Technicality
+-------+---------------+------------------------------
| 1 | Header | TH element only
+-------+---------------+------------------------------
| 2 | Data | TD element only
+-------+---------------+------------------------------
| 3 | Summary | TD element and TD of type 2 exist
+-------+---------------+------------------------------
| 4 | Key | TD element applicable to right TH, Only available on row
+-------+---------------+------------------------------
| 5 | Description | TD element applicable to left or top TH
+-------+---------------+------------------------------
| 6 | Layout | Can be only: Top Left cell or/and Summmary group intersection
+-------+---------------+------------------------------
| 7 | Header Group | TH element only, visual heading grouping, this type are an extension of the type 1
+-------+---------------+------------------------------
*/
$(obj).data().tblparser = groupZero;
groupZero.colgroup = colgroupFrame;
if (!groupZero.rowgroup) {
groupZero.rowgroup = [];
}
if (!groupZero.lstrowgroup) {
// groupZero.lstrowgroup = [];
groupZero.lstrowgroup = lstRowGroup;
}
groupZero.elem = obj;
groupZero.uid = uidElem;
uidElem += 1; // Set the uid for the groupZero
groupZero.colcaption = {}; // Group Cell Header at level 0, scope=col
groupZero.colcaption.uid = uidElem;
uidElem += 1;
groupZero.colcaption.elem = undefined;
groupZero.colcaption.type = 7;
groupZero.colcaption.dataset = [];
groupZero.colcaption.summaryset = [];
groupZero.rowcaption = {}; // Group Cell Header at level 0, scope=row
groupZero.rowcaption.uid = uidElem;
uidElem += 1;
groupZero.rowcaption.elem = undefined;
groupZero.rowcaption.type = 7;
groupZero.rowcaption.dataset = [];
groupZero.rowcaption.summaryset = [];
groupZero.col = [];
function processCaption(elem) {
groupZero.colcaption.elem = elem;
groupZero.rowcaption.elem = elem;
var groupheadercell = {
colcaption: groupZero.colcaption,
rowcaption: groupZero.rowcaption,
elem: elem
},
caption,
captionFound,
description = [];
// Extract the caption vs the description
// There are 2 techniques,
// Recommanded is encapsulate the caption with "strong"
// Use Details/Summary element
// Use a simple paragraph
if ($(elem).children().length > 0) {
// Use the contents function to retreive the caption
$(elem).contents().filter(function () {
if (!caption && this.nodeType === 3) { // Text Node
// Doesn't matter what it is, but this will be considerated as the caption
// if is not empty
caption = $(this).text().replace(/^\s+|\s+$/g, "");
if (caption.length !== 0) {
caption = this;
captionFound = true;
return;
}
caption = false;
} else if (!caption && this.nodeType === 1) {
// Doesn't matter what it is, the first children element will be considerated as the caption
caption = this;
return;
}
});
// Use the children function to retreive the description
$(elem).children().filter(function () {
// if the caption are an element, we should ignore the first one
if (captionFound) {
description.push(this);
} else {
captionFound = true;
}
});
} else {
caption = elem;
}
// console.log(caption);
// Move the descriptin in a wrapper if there is more than one element
if (description.length > 1) {
groupheadercell.description = $(description);
} else if (description.length === 1) {
groupheadercell.description = description[0];
}
if (caption) {
groupheadercell.caption = caption;
}
groupheadercell.groupZero = groupZero;
groupheadercell.type = 1;
groupZero.groupheadercell = groupheadercell;
$(elem).data().tblparser = groupheadercell;
}
function processColgroup(elem, nbvirtualcol) {
// if elem is undefined, this mean that is an big empty colgroup
// nbvirtualcol if defined is used to create the virtual colgroup
var colgroup = {
elem: {},
start: 0,
end: 0,
col: [],
groupZero: groupZero
},
colgroupspan = 0,
width,
i,
_ilen,
col;
colgroup.elem = elem;
if (elem) {
$(elem).data().tblparser = colgroup;
}
colgroup.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(colgroup);
if (colgroupFrame.length !== 0) {
colgroup.start = colgroupFrame[colgroupFrame.length - 1].end + 1;
} else {
colgroup.start = 1;
}
// Add any exist structural col element
if (elem) {
$('col', elem).each(function () {
var $this = $(this),
width = $this.attr('span') !== undefined ? parseInt($this.attr('span'), 10) : 1,
col = {
elem: {},
start: 0,
end: 0,
groupZero: groupZero
};
col.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(col);
col.start = colgroup.start + colgroupspan;
col.end = colgroup.start + colgroupspan + width - 1; // Minus one because the default value was already calculated
col.elem = this;
col.groupZero = groupZero;
$this.data().tblparser = col;
colgroup.col.push(col);
columnFrame.push(col);
colgroupspan += width;
});
}
// If no col element check for the span attribute
if (colgroup.col.length === 0) {
if (elem) {
width = $(elem).attr('span') !== undefined ? parseInt($(elem).attr('span'), 10) : 1;
} else if (typeof nbvirtualcol === "number") {
width = nbvirtualcol;
} else {
errorTrigger(31);
return;
}
colgroupspan += width;
// Create virtual column
for (i = colgroup.start, _ilen = (colgroup.start + colgroupspan); i < _ilen; i += 1) {
col = {
start: 0,
end: 0,
groupZero: groupZero,
elem: undefined
};
col.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(col);
col.start = i;
col.end = i;
colgroup.col.push(col);
columnFrame.push(col);
}
}
colgroup.end = colgroup.start + colgroupspan - 1;
colgroupFrame.push(colgroup);
}
function processRowgroupHeader(colgroupHeaderColEnd) { // thead row group processing
var i, _ilen,
j, _jlen,
m, _mlen,
tmpStack = [],
tmpStackCurr,
tmpStackCell,
dataColgroup,
dataColumns,
colgroup,
col,
hcolgroup,
lstRealColgroup,
currColPos,
currColgroupStructure,
colFrmId,
bigTotalColgroupFound,
theadRSNext,
theadRSNextCell,
cell,
gzCol,
theadRS;
if (groupZero.colgrouphead || rowgroupheadercalled) {
return; // Prevent multiple call
}
rowgroupheadercalled = true;
if (colgroupHeaderColEnd && colgroupHeaderColEnd > 0) {
// The first colgroup must match the colgroupHeaderColEnd
if (colgroupFrame.length > 0 && (colgroupFrame[0].start !== 1 || (colgroupFrame[0].end !== colgroupHeaderColEnd && colgroupFrame[0].end !== (colgroupHeaderColEnd + 1)))) {
errorTrigger(3);
// Destroy any existing colgroup, because they are not valid
colgroupFrame = [];
}
} else {
colgroupHeaderColEnd = 0; // This mean that are no colgroup designated to be a colgroup header
}
// console.log('Call ProcessRowGroupHeader');
// Associate any descriptive cell to his top header
for (i = 0, _ilen = theadRowStack.length; i < _ilen; i += 1) {
theadRS = theadRowStack[i];
if (!theadRS.type) {
theadRS.type = 1;
}
for (j = 0, _jlen = theadRS.cell.length; j < _jlen; j += 1) {
cell = theadRowStack[i].cell[j];
cell.scope = "col";
// check if we have a layout cell at the top, left
if (i === 0 && j === 0 && $(cell.elem).html().length === 0) {
// That is a layout cell
cell.type = 6; // Layout cell
if (!groupZero.layoutCell) {
groupZero.layoutCell = [];
}
groupZero.layoutCell.push(cell);
j = cell.width - 1;
if (j >= _jlen) {
break;
}
}
// Check the next row to see if they have a corresponding description cell
theadRSNext = theadRowStack[i + 1];
theadRSNextCell = (theadRSNext ? theadRSNext.cell[j] : "");
if (!cell.descCell &&
cell.elem.nodeName.toLowerCase() === 'th' &&
!cell.type &&
theadRSNext &&
theadRSNext.uid !== cell.uid &&
theadRSNextCell &&
!theadRSNextCell.type &&
theadRSNextCell.elem.nodeName.toLowerCase() === 'td' &&
theadRSNextCell.width === cell.width &&
theadRSNextCell.height === 1) {
theadRSNext.type = 5; // Mark the next row as a row description
theadRSNextCell.type = 5; // Mark the cell as a cell description
theadRSNextCell.row = theadRS;
cell.descCell = theadRSNextCell;
// Add the description cell to the complete listing
if (!groupZero.desccell) {
groupZero.desccell = [];
}
groupZero.desccell.push(theadRSNextCell);
j = cell.width - 1;
if (j >= _jlen) {
break;
}
}
if (!cell.type) {
cell.type = 1;
}
}
}
// Clean the theadRowStack by removing any descriptive row
for (i = 0, _ilen = theadRowStack.length; i < _ilen; i += 1) {
theadRS = theadRowStack[i];
if (theadRS.type === 5) {
// Check if all the cell in it are set to the type 5
for (j = 0, _jlen = theadRS.cell.length; j < _jlen; j += 1) {
cell = theadRS.cell[j];
if (cell.type !== 5 && cell.type !== 6 && cell.height === 1) {
errorTrigger(4, cell.elem);
}
// Check the row before and modify their height value
if (cell.uid === theadRowStack[i - 1].cell[j].uid) {
cell.height -= 1;
}
}
groupZero.nbDescriptionRow += 1;
} else {
tmpStack.push(theadRS);
}
}
groupZero.colgrp = []; // Array based on level as indexes for columns and group headers
// Parser any cell in the colgroup header
if (colgroupHeaderColEnd > 0 && (colgroupFrame.length === 1 || colgroupFrame.length === 0)) {
// There are no colgroup element defined, All the cell will be considerated to be a data cell
// Data Colgroup
dataColgroup = {};
dataColumns = [];
colgroup = {
start: (colgroupHeaderColEnd + 1),
end: tableCellWidth,
col: [],
groupZero: groupZero,
elem: undefined,
type: 2 // Set colgroup data type
};
colgroup.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(colgroup);
if (colgroup.start > colgroup.end) {
errorTrigger(5);
}
dataColgroup = colgroup;
// Create the column
// Create virtual column
for (i = colgroup.start, _ilen = colgroup.end; i <= _ilen; i += 1) {
col = {
start: 0,
end: 0,
groupZero: groupZero,
elem: undefined
};
col.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(col);
if (!groupZero.col) {
groupZero.col = [];
}
dataColumns.push(col);
col.start = i;
col.end = i;
col.groupstruct = colgroup;
colgroup.col.push(col);
columnFrame.push(col); // Check to remove "columnFrame"
}
// Default Level => 1
groupZero.colgrp[1] = [];
groupZero.colgrp[1].push(groupZero.colcaption);
// Header Colgroup
if (colgroupHeaderColEnd > 0) {
hcolgroup = {
start: 1,
elem: undefined,
end: colgroupHeaderColEnd,
col: [],
groupZero: groupZero,
type: 1 // Set colgroup data type
};
hcolgroup.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(hcolgroup);
colgroupFrame.push(hcolgroup);
colgroupFrame.push(dataColgroup);
groupZero.colcaption.dataset = dataColgroup.col;
// Create the column
// Create virtual column
for (i = hcolgroup.start, _ilen = hcolgroup.end; i <= _ilen; i += 1) {
col = {
start: 0,
end: 0,
groupZero: groupZero,
elem: undefined
};
col.uid = uidElem;
uidElem += 1;
groupZero.allParserObj.push(col);
if (!groupZero.col) {
groupZero.col = [];
}
groupZero.col.push(col);
col.start = i;
col.end = i;
col.groupstruct = hcolgroup;
hcolgroup.col.push(col);
columnFrame.push(col);
}
for (i = 0, _ilen = dataColumns.length; i < _ilen; i += 1) {
groupZero.col.push(dataColumns[i]);
}
}
if (colgroupFrame.length === 0) {
colgroupFrame.push(dataColgroup);
groupZero.colcaption.dataset = dataColgroup.col;
}
// Set the header for each column
for (i = 0, _ilen = groupZero.col.length; i < _ilen; i += 1) {
gzCol = groupZero.col[i];
gzCol.header = [];
for (j = 0, _jlen = tmpStack.length; j < _jlen; j += 1) {
for (m = gzCol.start, _mlen = gzCol.end; m <= _mlen; m += 1) {
if ((j === 0 || (j > 0 && tmpStack[j].cell[m - 1].uid !== tmpStack[j - 1].cell[m - 1].uid)) && tmpStack[j].cell[m - 1].type === 1) {
gzCol.header.push(tmpStack[j].cell[m - 1]);
}
}
}
}
} else {
// They exist colgroup element,
//
// -----------------------------------------------------
//
// Build data column group based on the data column group and summary column group.
//
// Suggestion: In the future, may be allow the use of a HTML5 data or CSS Option to force a colgroup to be a data group instead of a summary group
//
// -----------------------------------------------------
//
lstRealColgroup = []; // List of real colgroup
currColPos = (colgroupHeaderColEnd === 0 ? 1 : colgroupFrame[0].end + 1); // Set the current column position
colgroup = {
start: currColPos,
end: undefined,
col: [],
row: [],
type: 2 // Set colgroup data type, that is the initial colgroup type
};
currColgroupStructure = [];
colFrmId = 0;
bigTotalColgroupFound = false;
$.each(colgroupFrame, function () {
var curColgroupFrame = this,
groupLevel,
cgrp,
parentHeader,
summaryAttached;
colFrmId += 1;
if (bigTotalColgroupFound || groupZero.colgrp[0]) {
errorTrigger(6, curColgroupFrame);
return;
}
$.each(curColgroupFrame.col, function () {
var column = this;
if (!groupZero.col) {
groupZero.col = [];
}
groupZero.col.push(column);
column.type = 1;
column.groupstruct = curColgroupFrame;
});
if (curColgroupFrame.start < currColPos) {
if (colgroupHeaderColEnd !== curColgroupFrame.end) {
errorTrigger(7, curColgroupFrame);
}
// Skip this colgroup, this should happened only once and should represent the header colgroup
// Assign the headers for this group
for (i = 0, _ilen = curColgroupFrame.col.length; i < _ilen; i += 1) {
gzCol = curColgroupFrame.col[i];
gzCol.header = [];
for (j = 0, _jlen = tmpStack.length; j < _jlen; j += 1) {
for (m = gzCol.start, _mlen = gzCol.end; m <= _mlen; m += 1) {
if ((j === 0 || (j > 0 && tmpStack[j].cell[m - 1].uid !== tmpStack[j - 1].cell[m - 1].uid)) && tmpStack[j].cell[m - 1].type === 1) {
gzCol.header.push(tmpStack[j].cell[m - 1]);
}
}
}
}
return;
}
groupLevel = undefined;
// Get the colgroup level
for (i = 0, _ilen = tmpStack.length; i < _ilen; i += 1) {
tmpStackCell = tmpStack[i].cell[curColgroupFrame.end - 1];
if (!tmpStackCell && curColgroupFrame.end > tmpStack[i].cell.length) {
errorTrigger(7); // Number of column are not corresponding to the table width
break;
}
if ((tmpStackCell.colpos + tmpStackCell.width - 1) === curColgroupFrame.end && (tmpStackCell.colpos >= curColgroupFrame.start)) {
if (!groupLevel || groupLevel > (i + 1)) {
groupLevel = (i + 1); // would equal at the current data cell level. The lowest row level win
}
}
}
if (!groupLevel) {
groupLevel = 1; // Default colgroup data Level, this happen when there is no column header, (same as no thead)
// errorTrigger(8, "Impossible to find the colgroup level, Check you colgroup definition or/and your table structure"); // That happened if we don't able to find an ending cell at the ending colgroup position.
}
// All the cells at higher level (Bellow the group level found) of witch one found, need to be inside the colgroup
for (i = (groupLevel - 1), _ilen = tmpStack.length; i < _ilen; i += 1) {
tmpStackCurr = tmpStack[i];
// Test each cell in that group
for (j = curColgroupFrame.start - 1, _jlen = curColgroupFrame.end; j < _jlen; j += 1) {
tmpStackCell = tmpStackCurr.cell[j];
if (tmpStackCell.colpos < curColgroupFrame.start || (tmpStackCell.colpos + tmpStackCell.width - 1) > curColgroupFrame.end) {
errorTrigger(9);
return;
}
}
}
// Add virtual colgroup Based on the top header
for (i = currColgroupStructure.length, _ilen = (groupLevel - 1); i < _ilen; i += 1) {
tmpStackCell = tmpStack[i].cell[curColgroupFrame.start - 1];
// Use the top cell at level minus 1, that cell must be larger
if (tmpStackCell.uid !== tmpStack[i].cell[curColgroupFrame.end - 1].uid ||
tmpStackCell.colpos > curColgroupFrame.start ||
tmpStackCell.colpos + tmpStackCell.width - 1 < curColgroupFrame.end) {
errorTrigger(10);
return;
}
// Convert the header in a group header cell
cgrp = tmpStackCell;
cgrp.level = (i + 1);
cgrp.start = cgrp.colpos;
cgrp.end = cgrp.colpos + cgrp.width - 1;
cgrp.type = 7; // Group Header Cell
currColgroupStructure.push(cgrp);
if (!groupZero.virtualColgroup) {
groupZero.virtualColgroup = [];
}
groupZero.virtualColgroup.push(cgrp);
// Add the group into the level colgroup perspective
if (!groupZero.colgrp[(i + 1)]) {
groupZero.colgrp[(i + 1)] = [];
}
groupZero.colgrp[(i + 1)].push(cgrp);
}
// Set the header list for the current group
curColgroupFrame.header = [];
for (i = groupLevel - (groupLevel >= 2 ? 2 : 1); i < tmpStack.length; i += 1) {
for (j = curColgroupFrame.start; j <= curColgroupFrame.end; j += 1) {
if (tmpStack[i].cell[j - 1].rowpos === i + 1) {
curColgroupFrame.header.push(tmpStack[i].cell[j - 1]);
// Attach the current colgroup to this header
tmpStack[i].cell[j - 1].colgroup = curColgroupFrame;
}
j += tmpStack[i].cell[j - 1].width - 1;
}
}
// Assign the parent header to the current header
parentHeader = [];
for (i = 0; i < currColgroupStructure.length - 1; i += 1) {
parentHeader.push(currColgroupStructure[i]);
}
curColgroupFrame.parentHeader = parentHeader;
// Check to set if this group are a data group
if (currColgroupStructure.length < groupLevel) {
// This colgroup are a data colgroup
// The current colgroup are a data colgroup
if (!curColgroupFrame.type) {
curColgroupFrame.type = 2; // Set Data group type
curColgroupFrame.level = groupLevel;
}
currColgroupStructure.push(curColgroupFrame);
// Add the group into the level colgroup perspective
if (!groupZero.colgrp[groupLevel]) {
groupZero.colgrp[groupLevel] = [];
}
groupZero.colgrp[groupLevel].push(curColgroupFrame);
}
//
// Preparing the current stack for the next colgroup and set if the current are a summary group
//
// Check if we need to pop out the current header colgroup
summaryAttached = false;
for (i = currColgroupStructure.length - 1; i >= 0; i -= 1) {
if (currColgroupStructure[i].end <= curColgroupFrame.end) {
if (currColgroupStructure[i].level < groupLevel && theadRowStack.length > 0) {
curColgroupFrame.type = 3;
}
// Attach the Summary group to the colgroup poped if current colgroup are type 3
if (curColgroupFrame.type === 3 && !summaryAttached) {
currColgroupStructure[currColgroupStructure.length - 1].summary = curColgroupFrame;
summaryAttached = true; // This are used to do not attach a summary of level 4 to an inapropriate level 1 for exampled
}
currColgroupStructure.pop();
}
}
if (!hassumMode) {
curColgroupFrame.type = 2;
}
// Catch the second and the third possible grouping at level 1
if (groupLevel === 1 && groupZero.colgrp[1] && groupZero.colgrp[1].length > 1 && theadRowStack.length > 0) {
// Check if in the group at level 1 if we don't already have a summary colgroup
for (i = 0; i < groupZero.colgrp[1].length; i += 1) {
if (groupZero.colgrp[1][i].type === 3) {
// Congrat, we found the last possible colgroup,
curColgroupFrame.level = 0;
if (!groupZero.colgrp[0]) {
groupZero.colgrp[0] = [];
}
groupZero.colgrp[0].push(curColgroupFrame);
groupZero.colgrp[1].pop();
bigTotalColgroupFound = true;
break;
}
}
if (hassumMode) {
curColgroupFrame.type = 3;
}
}
// Set the representative header "caption" element for a group at level 0
if (curColgroupFrame.level === 1 && curColgroupFrame.type === 2) {
curColgroupFrame.repheader = 'caption';
}
if (!groupZero.col) {
groupZero.col = [];
}
$.each(curColgroupFrame.col, function () {
var column = this;
column.type = curColgroupFrame.type;
column.level = curColgroupFrame.level;
column.groupstruct = curColgroupFrame;
column.header = [];
// Find the lowest header that would represent this column
for (j = (groupLevel - 1); j < tmpStack.length; j += 1) {
for (i = (curColgroupFrame.start - 1); i < curColgroupFrame.end; i += 1) {
if ((tmpStack[j].cell[i].colpos >= column.start &&
tmpStack[j].cell[i].colpos <= column.end) ||
(tmpStack[j].cell[i].colpos <= column.start &&
(tmpStack[j].cell[i].colpos + tmpStack[j].cell[i].width - 1) >= column.end) ||
((tmpStack[j].cell[i].colpos + tmpStack[j].cell[i].width - 1) <= column.start &&
(tmpStack[j].cell[i].colpos + tmpStack[j].cell[i].width - 1) >= column.end)) {
if (column.header.length === 0 || (column.header.length > 0 && column.header[column.header.length - 1].uid !== tmpStack[j].cell[i].uid)) {
// This are the header that would represent this column
column.header.push(tmpStack[j].cell[i]);
tmpStack[j].cell[i].level = curColgroupFrame.level;
}
}
}
}
});
});
if (!groupZero.virtualColgroup) {
groupZero.virtualColgroup = [];
}
// Set the Virtual Group Header Cell, if any
$.each(groupZero.virtualColgroup, function () {
var vGroupHeaderCell = this;
// Set the headerLevel at the appropriate column
for (i = (vGroupHeaderCell.start - 1); i < vGroupHeaderCell.end; i += 1) {
if (!groupZero.col[i].headerLevel) {
groupZero.col[i].headerLevel = [];
}
groupZero.col[i].headerLevel.push(vGroupHeaderCell);
}
});
}
// Associate the colgroup Header in the group Zero
if (colgroupFrame.length > 0 && colgroupHeaderColEnd > 0) {
groupZero.colgrouphead = colgroupFrame[0];
groupZero.colgrouphead.type = 1; // Set the first colgroup type :-)
}
}
function finalizeRowGroup() {
// Check if the current rowgroup has been go in the rowgroup setup, if not we do
if (!currentRowGroup.type || !currentRowGroup.level) {
// Colgroup Setup,
rowgroupSetup();
}
// console.log('Row Group Finalization');
// If the current row group are a data group, check each row if we can found a pattern about to increment the data level for this row group
// Update, if needed, each row and cell to take in consideration the new row group level
// Add the row group in the groupZero Collection
lstRowGroup.push(currentRowGroup);
currentRowGroup = {};
}
function initiateRowGroup() {
// console.log('Row Group Initialization');
// Finalisation of any exisiting row group
if (currentRowGroup && currentRowGroup.type) {
finalizeRowGroup();
}
// Initialisation of the a new row group
currentRowGroup = {};
currentRowGroup.elem = currentRowGroupElement;
currentRowGroup.row = [];
currentRowGroup.headerlevel = [];
currentRowGroup.groupZero = groupZero;
currentRowGroup.uid = uidElem;
uidElem += 1;
//currentRowGroup.type = 2 // (1 if elem is a thead or if detected in the table, 2 default, 3 if summary data) // FYI Here the existance of the "type" property is used to determined the real type of row group
}
function rowgroupSetup(forceDataGroup) {
var i,
previousRowGroup,
tmpHeaderLevel;
if (tfootOnProcess) {
currentRowGroup.type = 3;
currentRowGroup.level = 0;
rowgroupHeaderRowStack = [];
return;
}
// Check if the current row group, already have some row, if yes this is a new row group
if (rowgroupHeaderRowStack.length > 0) {
// if more than 0 cell in the stack, mark this row group as a data row group and create the new row group (can be only virtual)
if (currentRowGroup && currentRowGroup.type && currentRowGroup.row.length > 0) {
currentRowGroupElement = {};
initiateRowGroup();
}
// We have a data row group
currentRowGroup.type = 2;
// Set the group header cell
currentRowGroup.row = rowgroupHeaderRowStack;
for (i = 0; i < rowgroupHeaderRowStack.length; i += 1) {
// if (rowgroupHeaderRowStack[i].cell.length !== 1) {
// errorTrigger(11, "Seem to have a row header for the data that have 2 or more cell inside it");
// }
rowgroupHeaderRowStack[i].cell[0].type = 7;
rowgroupHeaderRowStack[i].cell[0].scope = "row";
rowgroupHeaderRowStack[i].cell[0].row = rowgroupHeaderRowStack[i];
currentRowGroup.headerlevel.push(rowgroupHeaderRowStack[i].cell[0]);
}
}
// if no cell in the stack but first row group, mark this row group as a data row group
// console.log('rowgroupHeaderRowStack.length: ' + rowgroupHeaderRowStack.length);
// console.log(currentRowGroup);
// console.log('lstRowGroup.length: ' + lstRowGroup.length);
if (rowgroupHeaderRowStack.length === 0 &&
// (!currentRowGroup || (currentRowGroup.type && currentRowGroup.type === 1)) &&
lstRowGroup.length === 0) {
if (currentRowGroup.type && currentRowGroup.type === 1) {
currentRowGroupElement = {};
initiateRowGroup();
}
// This is the first data row group at level 1
currentRowGroup.type = 2;
currentRowGroup.level = 1; // Default Row Group Level
}
// if no cell in the stack and not the first row group, this are a summary group
// This is only valid if the first colgroup is a header colgroup.
//console.log('rowgroupHeaderRowStack.length: ' + rowgroupHeaderRowStack.length + ' lstRowGroup.length: ' + lstRowGroup.length + ' currentRowGroup.type: ' + currentRowGroup.type);
// console.log(' colgroupFrame[0]: ' + colgroupFrame[0] + ' colgroupFrame[0].type: ' + colgroupFrame[0].type + ' forceDataGroup: ' + forceDataGroup);
if (rowgroupHeaderRowStack.length === 0 && lstRowGroup.length > 0 && !currentRowGroup.type && colgroupFrame[0] && (colgroupFrame[0].type === 1 || (!colgroupFrame[0].type && colgroupFrame.length > 0)) && !forceDataGroup) {
currentRowGroup.type = 3;
} else {
currentRowGroup.type = 2;
// currentRowGroup.level = 1; // Default Row Group Level
}
if (currentRowGroup.type === 3 && !hassumMode) {
currentRowGroup.type = 2;
currentRowGroup.level = lstRowGroup[lstRowGroup.length - 1].level;
}
// Set the Data Level for this row group
// Calculate the appropriate row group level based on the previous rowgroup
// * a Summary Group decrease the row group level
// * a Data Group increase the row group level based of his number of row group header and the previous row group level
// * Dont forget to set the appropriate level to each group header cell inside this row group.
if (!currentRowGroup.level) {
// Get the level of the previous group
if (lstRowGroup.length > 0) {
previousRowGroup = lstRowGroup[lstRowGroup.length - 1];
if (currentRowGroup.type === 2) {
// Data Group
if (currentRowGroup.headerlevel.length === previousRowGroup.headerlevel.length) {
// Same Level as the previous one
currentRowGroup.level = previousRowGroup.level;
} else if (currentRowGroup.headerlevel.length < previousRowGroup.headerlevel.length) {
// add the missing group heading cell
tmpHeaderLevel = currentRowGroup.headerlevel;
currentRowGroup.headerlevel = [];
for (i = 0; i < (previousRowGroup.headerlevel.length - currentRowGroup.headerlevel.length); i += 1) {
currentRowGroup.headerlevel.push(previousRowGroup.headerlevel[i]);
}
for (i = 0; i < tmpHeaderLevel.length; i += 1) {
currentRowGroup.headerlevel.push(tmpHeaderLevel[i]);
}
currentRowGroup.level = previousRowGroup.level;
} else if (currentRowGroup.headerlevel.length > previousRowGroup.headerlevel.length) {
// This are a new set of heading, the level equal the number of group header cell found
currentRowGroup.level = currentRowGroup.headerlevel.length + 1;
}
} else if (currentRowGroup.type === 3) {
// Summary Group
if (previousRowGroup.type === 3) {
currentRowGroup.level = previousRowGroup.level - 1;
} else {
currentRowGroup.level = previousRowGroup.level;
}
if (currentRowGroup.level < 0) {
// This is an error, Last summary row group was already found.
errorTrigger(12);
}
// Set the header level with the previous row group
for (i = 0; i < previousRowGroup.headerlevel.length; i += 1) {
if (previousRowGroup.headerlevel[i].level < currentRowGroup.level) {
currentRowGroup.headerlevel.push(previousRowGroup.headerlevel[i]);
}
}
} else {
// Error
currentRowGroup.level = "Error, not calculated";
errorTrigger(13);
}
} else {
currentRowGroup.level = 1 + rowgroupHeaderRowStack.length;
}
}
// Ensure that each row group cell heading have their level set
for (i = 0; i < currentRowGroup.headerlevel.length; i += 1) {
currentRowGroup.headerlevel[i].level = i + 1;
currentRowGroup.headerlevel[i].rowlevel = currentRowGroup.headerlevel[i].level;
}
rowgroupHeaderRowStack = []; // reset the row header stack
if (currentRowGroup.level === undefined || currentRowGroup.level < 0) {
errorTrigger(14, currentRowGroup.elem);
}
}
function processRow(elem) {
// In this function there are a possible confusion about the colgroup variable name used here vs the real colgroup table, In this function the colgroup is used when there are no header cell.
currentRowPos += 1;
var columnPos = 1,
lastCellType = "",
lastHeadingColPos = false,
cells = $(elem).children(),
row = {
colgroup: [], // === Build from colgroup object ==
cell: [], // === Build from Cell Object ==
elem: elem, // Row Structure jQuery element
rowpos: currentRowPos
},
colgroup,
fnPreProcessGroupHeaderCell,
fnPreProcessGroupDataCell,
fnParseSpannedRowCell,
headingRowCell,
rowheader,