forked from mleibman/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgrid.js
executable file
·329 lines (251 loc) · 9.7 KB
/
grid.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
(function ($) {
var grid;
var $container = $("#container");
var el, offsetBefore, offsetAfter, dragged;
var drag = function drag(handle, dx, dy) {
offsetBefore = el.offset();
$(handle).simulate("drag", {
dx: dx || 0,
dy: dy || 0
});
dragged = { dx: dx, dy: dy };
offsetAfter = el.offset();
};
var moved = function moved(dx, dy, msg) {
msg = msg ? msg + "." : "";
var actual = { left: offsetAfter.left, top: offsetAfter.top };
var expected = { left: offsetBefore.left + dx, top: offsetBefore.top + dy };
same(actual, expected, 'dragged[' + dragged.dx + ', ' + dragged.dy + '] ' + msg);
};
var ROWS = 500, COLS = 10;
var data = [], row;
for (var i = 0; i < ROWS; i++) {
row = { id: "id_" + i };
for (var j = 0; j < COLS; j++) {
row["col_" + j] = i + "." + j;
}
data.push(row);
}
var cols = buildColumns(COLS);
var col;
var treeColumns;
function buildColumns(size, current) {
var columns = [];
current = current || 0;
for (var i = 0; i < size; i++) {
var index = current + i;
columns.push({
id: "col" + index,
field: "col_" + index,
name: "col_" + index,
minWidth: 70
});
}
return columns;
}
function buildTreeColumns(sizeGroups, sizeColumnsPerGroup) {
var treeColumns = [];
for (var i = 0; i < sizeGroups; i++) {
treeColumns.push({
id: "group" + i,
name: "group_" + i,
columns: buildColumns(sizeColumnsPerGroup, sizeColumnsPerGroup*i)
});
}
return treeColumns;
}
function setupGrid() {
grid = new Slick.Grid($container, data, cols);
grid.render();
}
function setupGridWithTreeColumns() {
treeColumns = buildTreeColumns(2, 5);
grid = new Slick.Grid($container, data, treeColumns);
grid.render();
}
function teardownGrid() {
$container.empty();
}
module(
"grid - column resizing",
{
setup: setupGrid,
teardown: teardownGrid
}
);
test("minWidth is respected", function () {
var firstCol = $("#container .slick-header-column:first");
firstCol.find(".slick-resizable-handle:first").simulate("drag", { dx: 100, dy: 0 });
firstCol.find(".slick-resizable-handle:first").simulate("drag", { dx: -200, dy: 0 });
equal(firstCol.outerWidth(), 70, "width set to minWidth");
});
test("onColumnsResized is fired on column resize", function () {
expect(2);
grid.onColumnsResized.subscribe(function () {
ok(true, "onColumnsResized called");
});
var oldWidth = cols[0].width;
$("#container .slick-resizable-handle:first").simulate("drag", { dx: 100, dy: 0 });
equal(cols[0].width, oldWidth+100-1, "columns array is updated");
});
test("getData should return data", function () {
equal(grid.getData(), data);
});
module(
"grid - initial render",
{
setup: setupGrid,
teardown: teardownGrid
}
);
test("top-right canvas height equals top-left canvas height", function () {
var leftHeight = $("#container .grid-canvas.grid-canvas-top.grid-canvas-left").height();
var rightHeight = $("#container .grid-canvas.grid-canvas-top.grid-canvas-right").height();
equal(leftHeight, rightHeight);
});
module(
"grid - freeze options changing",
{
setup: setupGrid,
teardown: teardownGrid
}
);
test("setOptions 'frozenColumn' from frozen to unfrozen", function () {
var currentWidth,
width = $("#container").width(),
$paneHeaderL = $(".slick-pane.slick-pane-header.slick-pane-left"),
$paneTopL = $(".slick-pane.slick-pane-top.slick-pane-left"),
$viewportTopL = $(".slick-viewport.slick-viewport-top.slick-viewport-left");
grid.setOptions({ 'frozenColumn': 1 });
grid.setOptions({ 'frozenColumn': -1 });
currentWidth = $paneHeaderL.width();
equal(currentWidth, width);
currentWidth = $paneTopL.width();
equal(currentWidth, width);
currentWidth = $viewportTopL.width();
equal(currentWidth, width);
});
test("setOptions 'frozenColumn' from unfrozen to frozen", function () {
var i
, currentWidth
, width = 0
, frozenColumns = 1
, $paneHeaderL = $(".slick-pane.slick-pane-header.slick-pane-left")
, $paneTopL = $(".slick-pane.slick-pane-top.slick-pane-left")
, $viewportTopL = $(".slick-viewport.slick-viewport-top.slick-viewport-left");
for (i = 0; i <= frozenColumns; i++) {
width += cols[i].width;
}
grid.setOptions({ 'frozenColumn': frozenColumns });
currentWidth = $paneHeaderL.width();
equal(currentWidth, width);
currentWidth = $paneTopL.width();
equal(currentWidth, width);
currentWidth = $viewportTopL.width();
equal(currentWidth, width);
});
test("setOptions 'frozenRow' from frozen to unfrozen", function () {
var currentHeight
, height = $('#container').height()
, $paneTopL = $('.slick-pane.slick-pane-top.slick-pane-left')
, $headerScrollerL = $('.slick-header.slick-header-left')
, $viewportTopL = $('.slick-viewport.slick-viewport-top.slick-viewport-left');
grid.setOptions({ 'frozenRow': 5 });
grid.setOptions({ 'frozenRow': -1 });
// Subtract the columns height from the container because they're not included
// in the top pane
height -= $headerScrollerL.height();
currentHeight = $paneTopL.height();
equal(currentHeight, height);
currentHeight = $viewportTopL.height();
equal(currentHeight, height);
});
test("setOptions 'frozenRow' from unfrozen to frozen", function () {
var i
, currentHeight
, height = 0
, frozenRows = 4
, rowHeight = grid.getOptions().rowHeight
, $paneTopL = $('.slick-pane.slick-pane-top.slick-pane-left')
, $viewportTopL = $('.slick-viewport.slick-viewport-top.slick-viewport-left');
for (i = 0; i < frozenRows; i++) {
height += rowHeight;
}
grid.setOptions({ 'frozenRow': frozenRows });
currentHeight = $paneTopL.height();
equal(currentHeight, height);
currentHeight = $viewportTopL.height();
equal(currentHeight, height);
});
test("frozen columns should to have class .frozen", function() {
const FROZEN_COLUMNS = 2;
grid.setOptions({ frozenColumn: FROZEN_COLUMNS-1 });
equal($('.slick-row:visible:first .slick-cell.frozen').length, FROZEN_COLUMNS, 'should find '+FROZEN_COLUMNS+' frozen columns');
});
test("frozen rows should to have class .frozen", function() {
const FROZEN_ROWS = 2;
grid.setOptions({ frozenRow: FROZEN_ROWS-1 });
equal($('.slick-row.frozen').length, FROZEN_ROWS, 'should find '+FROZEN_ROWS+' frozen rows');
});
test("frozen header column should to have class .frozen", function() {
const FROZEN_COLUMNS = 2;
grid.setOptions({ frozenColumn: FROZEN_COLUMNS-1 });
equal($('.slick-header-column.frozen').length, FROZEN_COLUMNS, 'should find '+FROZEN_COLUMNS+' frozen header columns');
});
test("frozen footer row column should to have class .frozen", function() {
const FROZEN_COLUMNS = 2;
grid.setOptions({ frozenColumn: FROZEN_COLUMNS-1 });
equal($('.slick-footerrow-column.frozen').length, FROZEN_COLUMNS, 'should find '+FROZEN_COLUMNS+' frozen footer columns');
});
module(
"grid - tree columns",
{
setup: setupGridWithTreeColumns,
teardown: teardownGrid
}
);
test('header columns render correct', function() {
equal($container.find('.slick-group-header-column').length, 2, 'grid should have 2 columns group header');
equal($container.find('.slick-header-column').length, 10, 'grid should have 10 columns header');
});
test("columns can be reordered in your groups", function() {
grid.onColumnsReordered.subscribe(function() {
ok(true, "onColumnsReordered shouldn't be called");
});
var $firstCol = $(".slick-header-column:first", $container);
var currentPosition = $firstCol.offset().left;
var widthColumn = $firstCol.outerWidth();
var oneColumn = currentPosition + widthColumn;
$firstCol.simulate("drag", { dx: oneColumn, dy: 0 });
});
test("columns shouldn't be reordered out of your groups", function() {
grid.onColumnsReordered.subscribe(function() {
ok(false, "onColumnsReordered shouldn't be called");
});
var $firstCol = $(".slick-header-column:first", $container);
var currentPosition = $firstCol.offset().left;
var widthColumn = $firstCol.outerWidth();
var fiveColumns = currentPosition + (5 * widthColumn);
$firstCol.simulate("drag", { dx: fiveColumns, dy: 0 });
});
test("render with frozen columns", function() {
const FROZEN_COLUMNS = 4;
var widthColumnsLeft;
grid.setOptions({ 'frozenColumn': FROZEN_COLUMNS });
widthColumnsLeft = $('.slick-header-column:first', $container).outerWidth() * (FROZEN_COLUMNS + 1);
equal($(".slick-pane.slick-pane-header.slick-pane-left").outerWidth(), widthColumnsLeft);
equal($(".slick-pane.slick-pane-top.slick-pane-left").outerWidth(), widthColumnsLeft);
equal($(".slick-viewport.slick-viewport-top.slick-viewport-left").outerWidth(), widthColumnsLeft);
});
test("all columns of a group should be frozen, otherwise no group will be created", function() {
const FROZEN_COLUMNS = 3;
grid.setOptions({ 'frozenColumn': FROZEN_COLUMNS });
equal($(".slick-group-header-column").length, 0, 'no group will be found');
});
test("frozen group header column should to have class .frozen", function() {
const FROZEN_COLUMNS = 5;
grid.setOptions({ frozenColumn: FROZEN_COLUMNS-1 });
equal($('.slick-group-header-column.frozen').length, 1, 'should find '+1+' frozen group header columns');
equal($('.slick-header-column.frozen').length, FROZEN_COLUMNS, 'should find '+FROZEN_COLUMNS+' frozen header columns');
});
})(jQuery);