-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (59 loc) · 1.45 KB
/
index.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
/*
* require dependencies
*/
var template = require('./template'),
domify = require('domify'),
child = require('child');
/*
* create new instance of DomTable
* if no element is given use template as root el
*/
function DomTable (el) {
this.el = el;
if (arguments.length === 0)
this.el = domify(template);
this.el.__proto__.addRow = this._addRow;
this.el.__proto__.removeAllRows = this._removeAllRows;
return this.el;
};
DomTable.prototype._addRow = function (element, rowIndex) {
var rowEl;
/*
* add a new row to the table at given index
* if index is undefined, add it to the end
*/
rowEl = domify('<tr></tr>');
if (element !== null && element !== undefined)
if (typeof element === "object")
rowEl.appendChild(element);
else
rowEl.innerHTML = element;
/*
* append Row to table at rowIndex
*/
child(this)
.add(rowEl, rowIndex);
/*
* add addCell function to row
*/
rowEl.__proto__.addCell = function (element, cellIndex) {
var cellEl = domify('<td></td>');
if (element !== null)
if (typeof element === "object")
cellEl.appendChild(element);
else
cellEl.innerHTML = element;
child(rowEl)
.add(cellEl, cellIndex);
return cellEl;
};
return rowEl;
};
DomTable.prototype._removeAllRows = function() {
if (this.el !== undefined)
child(this.el)
.removeAll();
};
module.exports = function (table) {
return new DomTable(table);
};