This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gumby.tablereflow.js
146 lines (111 loc) · 3.51 KB
/
gumby.tablereflow.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
/**
* Gumby TableReflow
*/
!function($) {
'use strict';
function TableReflow($el) {
Gumby.debug('Initializing TableReflow', $el);
this.$el = $el;
this.cellLabels = "ui-table-cell-label";
var scope = this;
scope.refresh();
scope.updateReflow();
}
TableReflow.prototype.getAttribute = function( element, key ) {
var data;
element = element.jquery ? element[0] : element;
if ( element && element.getAttribute ) {
data = element.getAttribute( "gumby-" + key );
}
// Copied from core's src/data.js:dataAttr()
// Convert from a string to a proper data type
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test( data ) ? JSON.parse( data ) :
data;
} catch( err ) {}
return data;
}
TableReflow.prototype.setHeaders = function() {
var trs = this.$el.find( "thead tr" );
this.headers = this.$el.find( "tr:eq(0)" ).children();
this.allHeaders = this.headers.add( trs.children() );
}
TableReflow.prototype.refresh = function( /* create */ ) {
var table = this.$el,
trs = table.find( "thead tr" );
// updating headers on refresh (fixes #5880)
this.setHeaders();
// Iterate over the trs
trs.each( function() {
var columnCount = 0;
// Iterate over the children of the tr
$( this ).children().each( function() {
var span = parseInt( this.getAttribute( "colspan" ), 10 ),
selector = ":nth-child(" + ( columnCount + 1 ) + ")",
j;
this.setAttribute( "gumby-" + "colstart", columnCount + 1 );
if ( span ) {
for( j = 0; j < span - 1; j++ ) {
columnCount++;
selector += ", :nth-child(" + ( columnCount + 1 ) + ")";
}
}
// Store "cells" data on header as a reference to all cells in the
// same column as this TH
$( this ).data( "cells", table.find( "tr" ).not( trs.eq( 0 ) ).not( this ).children( selector ) );
columnCount++;
});
});
}
TableReflow.prototype.updateReflow = function() {
var table = this;
// get headers in reverse order so that top-level headers are appended last
$( table.allHeaders.get().reverse() ).each( function() {
var cells = $( this ).data( "cells" ),
colstart = table.getAttribute( this, "colstart" ),
hierarchyClass = cells.not( this ).filter( "thead th" ).length && " ui-table-cell-label-top",
text = $( this ).text(),
iteration, filter;
if ( text !== "" ) {
if ( hierarchyClass ) {
iteration = parseInt( this.getAttribute( "colspan" ), 10 );
filter = "";
if ( iteration ) {
filter = "td:nth-child("+ iteration +"n + " + ( colstart ) +")";
}
table.addLabels( cells.filter( filter ), table.cellLabels + hierarchyClass, text );
} else {
table.addLabels( cells, table.cellLabels, text );
}
}
});
}
TableReflow.prototype.addLabels = function( cells, label, text ) {
cells.not( ":has(b." + label + ")" ).prepend( "<b class='" + label + "'>" + text + "</b>" );
};
Gumby.addInitalisation('tablereflow', function() {
$('.reflow').each(function() {
var $this = $(this);
// this element has already been initialized
if($this.data('isReflow')) {
return true;
}
// mark element as initialized
$this.data('isReflow', true);
new TableReflow($this);
});
});
// register UI module
Gumby.UIModule({
module: 'tablereflow',
events: ['initialize'],
init: function() {
Gumby.initialize('tablereflow');
}
});
}(jQuery);