-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablemagic.js
425 lines (401 loc) · 14.5 KB
/
tablemagic.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
/**
* @license TableMagic v0.8.1
* (c) 2011-2014 tweeeety, Ryuichi Murata http://tweeeety.com
* License: MIT
*/
//;(function(global, $) {
var TableMagic = ( function(TableMagic, undefined ) {
// SimpleDomManipulator
var SimpleDomManipulator = function() {
var Constructor = function() {
this.simpleDom = null;
};
Constructor.create = function(elm) {
var _this = new this();
var dom = document.createElement(elm);
_this.simpleDom = dom;
return _this;
}
Constructor.prototype.attr = function(attr, val) {
this.simpleDom.setAttribute(attr,val);
return this;
}
Constructor.prototype.text = function(text) {
var textNode = document.createTextNode(text)
this.simpleDom.appendChild(textNode);
return this;
}
Constructor.prototype.addClass = function(className) {
if( !className ) return this;
this.simpleDom.setAttribute("class",className);
this.simpleDom.setAttribute("className",className);
return this;
}
Constructor.prototype.append = function(content) {
var dom = ( content instanceof SimpleDomManipulator ) ? content.get() : content;
this.simpleDom.appendChild(dom);
return this;
}
Constructor.prototype.get = function() {
return this.simpleDom;
}
return Constructor;
}();
// Renderers
var Renderers = function() {
var Constructor = function() {
var _this = this;
_this._arr = null;
this.baseRenderers = {
// normal orientation
// ok
normal: function(){
var array = _this.ddm.addTitleRow().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
sumUpCol: function(){
var array = _this.ddm.addTitleRow().sumUpCol().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
aveCol: function(){
var array = _this.ddm.addTitleRow().aveCol().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
aveColExceptZero: function(){
var array = _this.ddm.addTitleRow().aveCol(true,true,true).get();
var tbl = _this.arrayToTable(array);
return tbl;
},
// rotate orientation
// ok
rotateNormal: function(){
var array = _this.ddm.addTitleRow().rowColRotate().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
sumUpColRotate: function(){
var array = _this.ddm.addTitleRow().sumUpCol().rowColRotate().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
rotateSumUpCol: function(){
var array = _this.ddm.addTitleRow().rowColRotate().sumUpCol().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
aveColRotate: function(){
var array = _this.ddm.addTitleRow().aveCol().rowColRotate().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
aveColExceptZeroRotate: function(){
var array = _this.ddm.addTitleRow().aveCol(true,true,true).rowColRotate().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
rotateAveCol: function(){
var array = _this.ddm.addTitleRow().rowColRotate().aveCol().get();
var tbl = _this.arrayToTable(array);
return tbl;
},
rotateAveColExceptZero: function(){
var array = _this.ddm.addTitleRow().rowColRotate().aveCol(true,true,true).get();
var tbl = _this.arrayToTable(array);
return tbl;
}
};
};
Constructor.create = function(ddm, opt){
var _this = new this;
_this.ddm = ddm;
if( typeof opt !== 'object' ) opt = {};
_this.tableClassName = opt.tableClassName ? opt.tableClassName : "" ;
_this.addThead = opt.addThead ? opt.addThead : false ;
_this.trOddClassName = opt.trOddClassName ? opt.trOddClassName : "" ;
_this.trEvenClassName = opt.trEvenClassName ? opt.trEvenClassName : "" ;
_this.trHeaderClassName = opt.trHeaderClassName ? opt.trHeaderClassName : "" ;
_this.tdHeaderClassName = opt.tdHeaderClassName ? opt.tdHeaderClassName : "" ;
_this.firstRowTd2Th = opt.firstRowTd2Th ? opt.firstRowTd2Th : "" ;
_this.firstColTd2Th = opt.firstColTd2Th ? opt.firstColTd2Th : "" ;
_this.rendereCallback = opt.rendereCallback ? opt.rendereCallback : undefined ;
// normal, sumUp,
_this.renderName = opt.renderName ? opt.renderName : 'normal';
return _this;
};
Constructor.prototype.render = function(){
if( !this.renderName ){
throw "renderName is not defined.";
}
if( this.baseRenderers && !this.baseRenderers[this.renderName] ){
throw "renderName:" + this.renderName + ' is not defined;';
}
this.tbl = this.baseRenderers[this.renderName]();
return this;
};
Constructor.prototype.get = function(){
return this.getTableTag();
};
Constructor.prototype.getTableObj = function(){
return this.tbl.get();;
};
Constructor.prototype.getTableTag = function(){
return this.tbl.get().outerHTML;
};
Constructor.prototype.getInner = function(){
var tbl = this.tbl.get().innerHTML;
console.log(tbl);
return tbl;
};
Constructor.prototype.addRenderer = function(name, fn){
this.baseRenderers[name] = fn;
};
Constructor.prototype.addProp = function(name, prop){
this[name] = prop;
};
// 配列からtableに変換して表示
Constructor.prototype.arrayToTable = function(array){
var _this = this;
var tbodyObj = SimpleDomManipulator.create("tbody");
var tblObj = SimpleDomManipulator.create("table");
for( var i=0, iLen=array.length; i<iLen; i++ ){
var trObj = SimpleDomManipulator.create("tr");
var trClassName = '';
trClassName = (this.trEvenClassName && this.trOddClassName && i%2==0)? this.trEvenClassName : this.trOddClassName;
if( i==0 && this.trHeaderClassName && typeof this.trHeaderClassName == 'string') trClassName += ' ' + this.trHeaderClassName;
trObj.addClass(trClassName);
for( var j=0, jLen=array[i].length; j<jLen; j++ ){
var tdObj;
if( i==0 ){
tdObj = ( this.firstRowTd2Th ) ? SimpleDomManipulator.create("th") : SimpleDomManipulator.create("td");
}else{
tdObj = ( this.firstColTd2Th && j==0 ) ? SimpleDomManipulator.create("th") : SimpleDomManipulator.create("td");
if( j==0 && typeof this.tdHeaderClassName == 'string') tdObj.addClass(_this.tdHeaderClassName);
}
tdObj.text( array[i][j] );
trObj.append(tdObj);
}
// trObj append to thead or tbody
if(this.addThead && i==0){
var theadObj = SimpleDomManipulator.create("thead");
tblObj.append(theadObj.append(trObj));
}else{
tbodyObj.append(trObj);
}
}
// tbody append to table
tblObj.append(tbodyObj).addClass(this.tableClassName);
return tblObj;
};
return Constructor;
}();
// DbDataManipulator
var DbDataManipulator = function() {
var Constructor = function() {
this._dbData = [];
this._orderArr = [];
this._titleHash = {};
this._baseTableArray = [];
this._newTableArray = null;
};
Constructor.create = function(data, orderArr, titleHash, opt){
var _this = new this;
if( typeof opt == 'undefined' ) opt = {};
if( typeof opt.label == 'undefined' ) opt.label = {
'sum' : 'sum',
'ave' : 'ave'
};
_this.label = opt.label;
if( typeof opt.noTitleRow == 'undefined' ) _this.noTitleRow = false;
_this.noTitleRow = opt.noTitleRow;
if( typeof opt.rotate == 'undefined' ) opt.rotate = false;
_this.rotate = opt.rotate;
_this._dbData = data;
if(!orderArr) orderArr = _this._dbData2OrderArr(data);
_this._orderArr = orderArr;
_this._titleHash = titleHash;
// default
_this.addedTitle = false;
_this.toArray();
//if( opt.title )_this.addTitle();
//if( opt.rotate ) _this.rowColRotate();
return _this;
};
Constructor.prototype._dbData2OrderArr = function(data){
if( !data || data.length == 0 ) return [];
var row = data[0];
var arr = [];
for( var key in row ){
arr.push(key);
}
return arr;
};
Constructor.prototype.toArray = function(){
var array = [];
//if(!this._orderArr) this._orderArr = this._dbData2OrderArr(data);
array = this._dbData2Array(this._dbData, this._orderArr );
//var titleRowArray = this.makeTitleRowArray(orderArr, titleHash);
//array.unshift(titleRowArray);
this.set_baseTableArray(array);
this.set_newTableArray(array);
return this;
};
Constructor.prototype._dbData2Array = function(data, orderArr){
var arr = [];
for ( var n in data ){
var colArray = [];
for ( var i=0, len=orderArr.length; i < len ; i++ )
{
if(data[n][orderArr[i]] == undefined) data[n][orderArr[i]] = 0 ;
colArray.push(data[n][orderArr[i]]);
}
arr.push(colArray);
}
return arr;
};
Constructor.prototype.addTitleRow = function(){
if( this.noTitleRow ) return this;
var arr = this.get_newTableArray();
var titleRowArray = this.makeTitleRowArray();
arr.unshift(titleRowArray);
this.set_newTableArray(arr);
this.addedTitle = true;
return this;
};
Constructor.prototype.makeTitleRowArray = function(){
if( typeof this._titleHash === 'undefined' ) return this._orderArr;
var titleRowArray = [];
for ( var i=0, len = this._orderArr.length; i < len ; i++ ){
titleRowArray.push(this._titleHash[this._orderArr[i]]);
}
return titleRowArray;
};
Constructor.prototype.get = function(){
return this.getArray();
};
Constructor.prototype.getArray = function(){
return this._newTableArray ? this._newTableArray : this._baseTableArray;
};
Constructor.prototype.get_baseTableArray = function(){
return this._baseTableArray;
};
Constructor.prototype.set_baseTableArray = function(array){
this._baseTableArray = array;
};
Constructor.prototype.get_newTableArray = function(){
return this._newTableArray;
};
Constructor.prototype.set_newTableArray = function(array){
this._newTableArray = array;
};
Constructor.prototype.rowColRotate = function(){
var array = this.get_newTableArray();
//tblRowArray
var newArray = [];
for(var i=0,iLen = array.length; i<iLen; i++){
for(var j=0,jLen = array[i].length; j<jLen; j++){
if(newArray[j] == undefined)newArray[j] = [];
newArray[j].push(array[i][j]);
//newArray[j].unshift(array[i][j]) //unshiftだとさらに行列を反転
//newArray[j][iLen-i] = array[i][j];
}
}
this.set_newTableArray(newArray);
return this;
};
Constructor.prototype.sumUpCol = function(exceptRowTitle, exceptColTitle){
if( typeof exceptRowTitle == 'undefined' ) exceptRowTitle = true;
if( typeof exceptColTitle == 'undefined' ) exceptColTitle = true;
var array = this.get_newTableArray();
var sumUpArray = [];
for(var row=0, rowLen = array.length; row<rowLen; row++){
for(var col=0, colLen = array[row].length; col<colLen; col++){
if(exceptRowTitle && row == 0){
continue;
}
if(exceptColTitle && col == 0){
sumUpArray[col] = this.label.sum;
continue
}
if(sumUpArray[col] == undefined) sumUpArray[col] = 0;
sumUpArray[col] = sumUpArray[col] + parseInt(array[row][col],10);
}
}
array.push(sumUpArray);
this.set_newTableArray(array);
return this;
};
Constructor.prototype.aveCol = function(exceptRowTitle, exceptColTitle, exceptZero){
if( typeof exceptRowTitle == 'undefined' ) exceptRowTitle = true;
if( typeof exceptColTitle == 'undefined' ) exceptColTitle = true;
if( typeof exceptZero == 'undefined' ) exceptZero = false;
var array = this.get_newTableArray();
var aveArray = [];
var sumUpArray = [];
//var rowLen = array.length;
var intCntArray = []; // 有効数字の個数配列
for(var row=0, rowLen=array.length; row<rowLen; row++){
for(var col=0, colLen = array[row].length; col<colLen; col++){
if(exceptRowTitle && row == 0){
continue;
}
if(exceptColTitle && col == 0){
sumUpArray[col] = this.label.ave;
continue;
}
if(sumUpArray[col] == undefined) sumUpArray[col] = 0;
sumUpArray[col] = sumUpArray[col] + parseInt(array[row][col],10);
if(intCntArray[col] == undefined) intCntArray[col] = 0;
if(exceptZero && parseInt(array[row][col],10) == 0 ) continue;
intCntArray[col]++;
}
}
for (var idx=0; idx<sumUpArray.length; idx++){
if(idx == 0)
{
aveArray[idx] = sumUpArray[idx];
continue;
}
aveArray[idx] = ( !intCntArray[idx] ) ? 0 : (sumUpArray[idx] / intCntArray[idx]).toFixed(1);
}
array.push(aveArray);
this.set_newTableArray(array);
return this;
};
return Constructor;
}();
// TableMagic
var TableMagic = function(data, opt){
this.data = data;
if( !(opt != null && opt instanceof Object && !(opt instanceof Array)) ) opt = {};
this.orderArr = opt.titleOrderArr || undefined;
this.titleHash = opt.titleHash || undefined;
this.opt = opt;
this.initialize();
};
/*----------------
* initialize
----------------*/
TableMagic.prototype.initialize = function(){
var ddm = DbDataManipulator.create(this.data, this.orderArr, this.titleHash, {label:this.opt.label, noTitleRow: this.opt.noTitleRow||false});
this.r = Renderers.create(ddm, this.opt);
return this;
};
/*----------------
* accessor
----------------*/
TableMagic.prototype.get = function(){
return this.getTableTag();
};
TableMagic.prototype.getTableTag = function(){
if( !this.r ) return false;
return this.r.render().get();
};
TableMagic.prototype.getInner = function(){
if( !this.r ) return false;
return this.r.render().getInner();
};
return TableMagic;
})({});