forked from mootools/mootools-more
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlTable.js
168 lines (140 loc) · 3.96 KB
/
HtmlTable.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
/*
---
name: HtmlTable Tests
requires: [More/HtmlTable]
provides: [HtmlTable.Tests]
...
*/
describe('HtmlTable', function(){
it('should create a new HtmlTable instance', function(){
var properties = {
border: '1',
cellspacing: '2',
cellpadding: '3',
'class': 'simple-table'
};
var t = new HtmlTable({
properties: properties,
headers: ['fruit', 'colors'],
footers: ['fruit', 'colors'],
rows: [
['lemon', 'yellow'],
['apple', 'red']
]
});
expect(t.thead.getElements('th').length).toEqual(2);
expect(t.body.getElements('td').length).toEqual(4);
expect(t.body.rows.length).toEqual(2);
expect($(t).get('border', 'cellspacing', 'cellpadding', 'class')).toEqual(properties);
});
describe('HtmlTable:push', function(){
var t = new HtmlTable(),
tds;
it('should push an multidimensional array', function(){
t.push([
'apple',
[
new Element('span', {'html': 'red'}),
new Element('span', {'html': '(green)'})
]
]);
tds = t.body.rows[0].getElements('td');
expect(tds.length).toEqual(2);
expect(tds[0].get('text')).toEqual('apple');
expect(tds[1].get('text')).toEqual('red(green)');
});
it('should push a simple array with text', function(){
t.push(['lemon', 'yellow']);
tds = t.body.rows[1].getElements('td');
expect(tds[0].get('text')).toEqual('lemon');
expect(tds[1].get('text')).toEqual('yellow');
});
it('should push an array with options and properties', function(){
t.push([{
content: 'grapes',
properties: {'class': 'someClass'}
},
'purple'
]);
tds = t.body.rows[2].getElements('td');
expect(tds[0].get('text', 'class')).toEqual({
text: 'grapes',
'class': 'someClass'
});
expect(tds[1].get('text')).toEqual('purple');
});
it('should complement the spanned row with a new td', function(){
t.push(['green']);
expect(t.body.rows[3].getElements('td').length).toEqual(1);
});
it('should push an tr element', function(){
var tr = new Element('tr').adopt(
new Element('td'),
new Element('td')
);
t.push(tr);
expect(t.body.rows[4]).toEqual(tr);
});
it('should return an object {tr: ..., tds: ...}', function(){
var value = t.push(['banana', 'yellow']);
expect(value.tr.get('tag')).toEqual('tr');
expect(value.tds.length).toEqual(2);
});
it('should push a row to the designated location', function(){
var t = new HtmlTable();
t.push(['red', 'blue']);
var rows = t.body.rows;
var value = t.push(['kiwi', 'green'], {}, rows[0], 'td', 'before');
expect(value.tr).toEqual(t.body.rows[0]);
});
});
describe('HtmlTable:pushMany', function(){
it('should push many rows', function(){
var t = new HtmlTable();
var rows = t.pushMany([
[1, 'one'],
[2, 'two'],
[3, 'three']
], {
'class': 'tableRowClass'
});
expect(rows.length).toEqual(3);
expect(t.body.rows.length).toEqual(3);
expect(rows[0].tr.hasClass).toBeTruthy();
tds = t.body.rows[0].getElements('td');
expect(tds.length).toEqual(2);
expect(tds[0].get('text')).toEqual('1');
expect(tds[1].get('text')).toEqual('one');
});
});
describe('HtmlTable:update', function(){
it('should update a table row', function(){
var table = new HtmlTable();
var row = table.push(['hello','world']);
var newrow = table.update(row.tr, ['I','work']);
expect(newrow.tds.get('text')).toEqual(['I', 'work']);
});
});
describe('HtmlTable cloned Element methods', function(){
var t = new HtmlTable({
rows: [
['lemon', 'yellow'],
['apple', 'red']
]
});
it('should inject the table in another element', function(){
var wrapper = new Element('div');
t.inject(wrapper);
expect(wrapper.contains($(t))).toBeTruthy();
});
it('should adopt another element into the table', function(){
var tr = new Element('tr').adopt(
new Element('td'),
new Element('td')
);
var ret = t.adopt(tr);
expect($(t).contains(tr)).toBeTruthy();
expect(ret).toEqual(t);
});
});
});