-
Notifications
You must be signed in to change notification settings - Fork 1
/
maketable.js
112 lines (89 loc) · 2.66 KB
/
maketable.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
MakeTable = {
create: function( object, options ){
if( this.constructor != MakeTable.create )
return new MakeTable.create( object, options);
// Default modifiers
options = _.extend({
id: "",
tableClass: "",
theadClass: "",
tbodyClass: "",
thClass: "",
tdClass: "",
trClass: ""
}, options);
var self = this;
_.each( options, function( value, key ){
self[key] = value;
});
return typeof object == 'object' ? this.init( object ) : new Error("First parameter in MakeTable.create is not a object");
},
createdTables:[]
};
MakeTable.create.prototype.init = function( object ){
var table = document.createElement('table'),
tableHead = document.createElement('thead'),
tableBody = document.createElement('tbody');
// Apply modifiers
table.id = this.id;
table.className = this.tableClass;
tableHead.className = this.theadClass;
tableBody.className = this.tbodyClass;
// Insert parts in table
table.appendChild( tableHead );
table.appendChild( tableBody );
this.table = table;
this.tableHead = tableHead;
this.tableBody = tableBody;
return this.mount( object );
}
MakeTable.create.prototype.mount = function( object ){
// Count size table
var self = this,
cells = _.size(object),
lines = _.max(object, function( list ){ return list.length }).length;
// Create void spaces in table
for( var i = 0; i < lines; i++ ){
var tableRow = self.tableBody.insertRow(i);
for( var j = 0; j < cells; j++ ){
var voidCell = tableRow.insertCell(j);
voidCell.innerHTML = " ";
}
}
return this.seed(object);
};
MakeTable.create.prototype.seed = function( object ){
// Makes row in thead
var self = this,
tableHeadRow = self.tableHead.insertRow(0),
indexCell = 0;
_.each( object, function( list, column ){
// Make title head of table
var th = document.createElement('th');
th.innerHTML = column;
th.className = self.thClass;
tableHeadRow.appendChild(th);
_.each(list, function( element, indexRow ){
var tableRow = self.tableBody.childNodes[indexRow],
cell = tableRow.childNodes[ indexCell ],
text = '';
if( typeof element == 'object' ){
text = element.text;
if( element.hasOwnProperty('colspan') )
console.dir(cell);
cell.colSpan = element.colspan;
if( element.hasOwnProperty('rowspan') )
cell.rowSpan = element.rowspan;
} else text = element;
cell.innerHTML = text;
cell.className = self.tdClass;
});
indexCell++;
});
// Save to memoize
// MakeTable.createdTables.push([ object, this.table]);
return this.table;
}
UI.registerHelper('makeTable', function( object, options){
return Spacebars.SafeString( MakeTable.create( object, options.hash ).outerHTML );
});