-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathgridster.js
More file actions
276 lines (227 loc) · 8.13 KB
/
gridster.js
File metadata and controls
276 lines (227 loc) · 8.13 KB
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
'use strict';
describe('GridsterCtrl', function() {
// load the controller's module
beforeEach(module('gridster'));
var GridsterCtrl,
item1x1,
item2x1,
item1x2,
item2x2;
// Initialize the controller
beforeEach(inject(function($controller) {
item1x1 = {
sizeX: 1,
sizeY: 1,
id: '1x1'
};
item2x1 = {
sizeX: 2,
sizeY: 1,
id: '2x1'
};
item2x2 = {
sizeX: 2,
sizeY: 2,
id: '2x2'
};
item1x2 = {
sizeX: 1,
sizeY: 2,
id: '1x2'
};
var config = [item1x1, item2x1, item2x2, item1x2];
GridsterCtrl = $controller('GridsterCtrl');
GridsterCtrl.setOptions(config);
}));
it('should have a grid Array', function() {
expect(GridsterCtrl.grid.constructor).toBe(Array);
});
describe('options', function() {
it('should set default options', function() {
expect(GridsterCtrl.columns).toBe(6);
expect(GridsterCtrl.width).toBe('auto');
expect(GridsterCtrl.colWidth).toBe('auto');
expect(GridsterCtrl.rowHeight).toBe('match');
expect(GridsterCtrl.margins).toEqual([10, 10]);
expect(GridsterCtrl.isMobile).toBe(false);
expect(GridsterCtrl.minColumns).toEqual(1);
expect(GridsterCtrl.minRows).toBe(1);
expect(GridsterCtrl.maxRows).toBe(100);
expect(GridsterCtrl.defaultSizeX).toBe(2);
expect(GridsterCtrl.defaultSizeY).toBe(1);
expect(GridsterCtrl.mobileBreakPoint).toBe(600);
expect(GridsterCtrl.resizable.enabled).toBe(true);
expect(GridsterCtrl.draggable.enabled).toBe(true);
expect(GridsterCtrl.dynamicColumns).toBe(false);
});
// todo: move these to e2e test
// it('should resolve smart options', function() {
// expect(GridsterCtrl.curWidth).toBe(400); // inherit element width
// expect(GridsterCtrl.curColWidth).toBe(65); // (400 - 10) / 6
// expect(GridsterCtrl.curRowHeight).toBe(65); // match curColWidth
// });
it('should update options', function() {
GridsterCtrl.setOptions({
width: 1200,
colWidth: 120,
rowHeight: 140,
columns: 7,
margins: [15, 15]
});
expect(GridsterCtrl.width).toBe(1200);
expect(GridsterCtrl.colWidth).toBe(120);
expect(GridsterCtrl.rowHeight).toBe(140);
expect(GridsterCtrl.columns).toBe(7);
expect(GridsterCtrl.margins).toEqual([15, 15]);
// todo: move these to e2e test
// expect(GridsterCtrl.curColWidth).toBe(120);
// expect(GridsterCtrl.curRowHeight).toBe(140);
});
});
describe('autoSetItemPosition', function() {
it('should place an item in the first available space', function() {
GridsterCtrl.putItem(item2x1, 0, 1);
GridsterCtrl.autoSetItemPosition(item1x1);
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
GridsterCtrl.autoSetItemPosition(item2x2);
expect(GridsterCtrl.getItem(0, 3)).toBe(item2x2);
});
it('should respect item size', function() {
GridsterCtrl.putItem(item2x1, 0, 1);
GridsterCtrl.autoSetItemPosition(item2x2);
expect(GridsterCtrl.getItem(0, 3)).toBe(item2x2);
});
});
describe('putItem', function() {
it('should be able to place an item with coordinates', function() {
GridsterCtrl.putItem(item1x1, 2, 3);
expect(GridsterCtrl.getItem(2, 3)).toBe(item1x1);
});
it('should place an item without coordinates into empty grid', function() {
GridsterCtrl.putItem(item1x1);
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
});
it('should place item into without coordinates into the next available position', function() {
// place 1x1 at 0x0
GridsterCtrl.putItem(item1x1);
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
// place 2x1 at 0x2
item2x1.row = 0;
item2x1.col = 2;
GridsterCtrl.putItem(item2x1);
expect(GridsterCtrl.getItem(0, 2)).toBe(item2x1);
// place 1x2 in without coordinates
GridsterCtrl.putItem(item1x2);
expect(GridsterCtrl.getItem(0, 1)).toBe(item1x2); // should stick it at 0x1
// place 2x2 without coordinates
GridsterCtrl.putItem(item2x2);
expect(GridsterCtrl.getItem(0, 4)).toBe(item2x2); // should stick it at 0x4
});
it('should not allow items to be placed with negative indices', function() {
GridsterCtrl.putItem(item1x1, -1, -1);
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
expect(item1x1.row).toBe(0);
expect(item1x1.col).toBe(0);
});
it('should not float items until told to', function() {
GridsterCtrl.putItem(item1x1, 3, 0);
expect(GridsterCtrl.getItem(0, 0)).toBe(null);
expect(GridsterCtrl.getItem(3, 0)).toBe(item1x1);
});
it('should not create two references to the same item', function() {
GridsterCtrl.putItem(item1x1, 0, 0);
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
GridsterCtrl.putItem(item1x1, 0, 4);
expect(GridsterCtrl.getItem(0, 4)).toBe(item1x1);
expect(GridsterCtrl.getItem(0, 0)).toBe(null);
});
});
describe('getItem', function() {
it('should match any column of a multi-column item', function() {
GridsterCtrl.putItem(item2x2, 0, 2);
// all 4 corners should return the same item
expect(GridsterCtrl.getItem(0, 2)).toBe(item2x2);
expect(GridsterCtrl.getItem(1, 2)).toBe(item2x2);
expect(GridsterCtrl.getItem(0, 3)).toBe(item2x2);
expect(GridsterCtrl.getItem(1, 3)).toBe(item2x2);
});
});
describe('getItems', function() {
it('should get items within an area', function() {
GridsterCtrl.putItem(item2x2, 0, 1);
GridsterCtrl.putItem(item2x1, 2, 0);
// verify they are still where we put them
expect(GridsterCtrl.getItem(0, 1)).toBe(item2x2);
expect(GridsterCtrl.getItem(2, 0)).toBe(item2x1);
var items = GridsterCtrl.getItems(1, 0, 2, 1);
expect(items.length).toBe(1);
expect(items[0]).toBe(item2x2);
});
});
describe('floatItemsUp', function() {
it('should float an item up', function() {
GridsterCtrl.putItem(item1x1, 3, 0);
GridsterCtrl.floatItemsUp();
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
});
it('should stack items when they float up', function() {
GridsterCtrl.putItem(item1x1, 3, 0);
GridsterCtrl.floatItemsUp();
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
GridsterCtrl.putItem(item2x1, 3, 0);
GridsterCtrl.floatItemsUp();
expect(GridsterCtrl.getItem(1, 0)).toBe(item2x1);
GridsterCtrl.putItem(item1x1, 3, 1);
GridsterCtrl.floatItemsUp();
expect(GridsterCtrl.getItem(1, 1)).toBe(item1x1);
});
it('should correctly stack multi-column items when their primary coordinates do not stack', function() {
GridsterCtrl.putItem(item2x2, 0, 2);
GridsterCtrl.putItem(item2x1, 2, 1);
// verify they are still where we put them
expect(GridsterCtrl.getItem(0, 2)).toBe(item2x2);
expect(GridsterCtrl.getItem(2, 1)).toBe(item2x1);
// allow them to float up
GridsterCtrl.floatItemsUp();
// verify they are still where we put them
expect(GridsterCtrl.getItem(0, 2)).toBe(item2x2);
expect(GridsterCtrl.getItem(2, 1)).toBe(item2x1);
});
});
describe('moveOverlappingItems', function() {
it('should correctly stack items on resize when their primary coordinates do not stack', function() {
GridsterCtrl.putItem(item1x1, 0, 0);
GridsterCtrl.putItem(item2x2, 0, 2);
GridsterCtrl.putItem(item2x1, 1, 0);
// verify they are still where we put them
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x1);
expect(GridsterCtrl.getItem(0, 2)).toBe(item2x2);
expect(GridsterCtrl.getItem(1, 0)).toBe(item2x1);
item2x1.sizeX = 3;
GridsterCtrl.moveOverlappingItems(item2x1);
expect(GridsterCtrl.getItem(1, 2)).toBe(item2x1);
expect(item2x2.row).toBe(2);
});
it('should correctly push items down', function() {
GridsterCtrl.putItem(item2x2, 0, 0);
GridsterCtrl.putItem(item1x1, 2, 0);
GridsterCtrl.putItem(item1x2, 1, 1);
GridsterCtrl.floatItemsUp();
expect(item2x2.row).toBe(2);
expect(item2x2.col).toBe(0);
expect(GridsterCtrl.getItem(4, 0)).toBe(item1x1);
expect(item1x2.row).toBe(0);
expect(item1x2.col).toBe(1);
});
it('should correctly push items down', function() {
GridsterCtrl.putItem(item1x2, 0, 0);
GridsterCtrl.putItem(item2x1, 0, 1);
GridsterCtrl.putItem(item1x1, 1, 2);
item1x2.sizeX = 2;
GridsterCtrl.moveOverlappingItems(item1x2);
expect(GridsterCtrl.getItem(0, 0)).toBe(item1x2);
expect(GridsterCtrl.getItem(2, 1)).toBe(item2x1);
expect(GridsterCtrl.getItem(3, 2)).toBe(item1x1);
});
});
});