-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgrid.js
341 lines (271 loc) · 8.85 KB
/
rgrid.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
;(function(window, undefined) {
/* Cache browser objects */
var document = window.document;
/* Cache internal objects */
var container = [];
/* Defaults */
var defaults = {
'1024': {
columns: 6,
columnUnit: 'px',
width: 1000,
gutter: 20,
offset: 20,
align: 'center',
baseline: 20 // Always in px
}
};
/* CSS Styles for lines */
var style = {
horizontal: {
position: 'absolute',
left: '0',
height: '1px',
width: '100%',
backgroundColor: '#00aaee'
},
vertical: {
position: 'absolute',
top: '0',
width: '1px',
backgroundColor: '#00aaee'
}
};
/* Internal vars */
var options = {};
var resolutions = [];
var currentResolution;
/* Grid Manager */
var grid = {
// Build grid
build : function() {
for (var containerResolution in options) { // Each resolution
if (options.hasOwnProperty(containerResolution)) {
// Create grid container
var gridContainer;
gridContainer = document.createElement("div");
gridContainer.id = 'grid_' + containerResolution;
gridContainer.style.display = 'none';
gridContainer.style = helper.merge(gridContainer.style, {
position: 'absolute',
width: '100%',
height: '1px',
top: '0',
left: '0',
overflow: 'visible'
});
// Append grid container to body
document.body.appendChild(gridContainer);
// Create vertical lines
print.verticalContainer(gridContainer);
// Create horizontal lines
print.horizontalContainer(gridContainer);
// Cache gridContainer reference
container[containerResolution] = document.getElementById('grid_' + containerResolution);
}
}
// Set first resolution
resolution.eval();
// @todo: Add debug window
},
// Change the grid depending on resolution
change : function(resolution) {
// Hide all grid containers
for (var containerResolution in options) {
if (options.hasOwnProperty(containerResolution)) {
helper.getContainer(containerResolution).style.display = 'none';
}
}
// Show current resolution container
helper.getContainer(resolution).style.display = 'block';
// Set the current resolution
currentResolution = resolution;
}
};
/* Resolution Manager object */
var resolution = {
// Get resolution from body width
eval : function() {
var bodyWidth = document.body.scrollWidth;
var bodyHeight = document.body.scrollHeight;
var resolution;
for (var i = resolutions.length - 1; i >= 0; i--) { // Loop inversely resolutions
if (bodyWidth >= resolutions[i]) {
resolution = resolutions[i]; // Set current break
break;
}
}
if (resolution !== null && resolution !== currentResolution) {
grid.change(resolution);
}
}
};
/* Print lines object */
var print = {
// Build vertical lines container
verticalContainer : function(gridContainer) {
// Get resolution name
var containerResolution = helper.getResolution(gridContainer.id);
// Get sizes
var bodyWidth = document.body.scrollWidth;
var gridWidth = options[containerResolution].width;
// @todo: Add calc to percentage columns
var columnWidth = (gridWidth - (options[containerResolution].offset * 2) - ((options[containerResolution].columns - 1) * options[containerResolution].gutter)) / options[containerResolution].columns;
var vlinesContainerStyle;
// Main container styles depending on align parameter
switch (options[containerResolution].align) {
case 'center':
vlinesContainerStyle = {
position: 'relative',
margin: '0 auto',
width: gridWidth + 'px',
height: '1px',
top: '0',
left: '0',
overflow: 'visible'
};
break;
case 'left':
vlinesContainerStyle = {
position: 'absolute',
margin: '0',
width: gridWidth + 'px',
height: '1px',
top: '0',
left: '0',
overflow: 'visible'
};
break;
case 'right':
vlinesContainerStyle = {
position: 'absolute',
margin: '0',
width: gridWidth + 'px',
height: '1px',
top: '0',
right: '0',
overflow: 'visible'
};
break;
}
// Draw vertical lines container
var vlinesContainer;
vlinesContainer = document.createElement("div");
vlinesContainer.className = 'vlines_container';
vlinesContainer.style = helper.merge(vlinesContainer.style, vlinesContainerStyle);
// Append vertical lines container container to grid container
gridContainer.appendChild(vlinesContainer);
// Draw left and right grid limits
print.vertical(0, vlinesContainer);
print.vertical(gridWidth, vlinesContainer);
// Draw offset
print.vertical(options[containerResolution].offset, vlinesContainer);
print.vertical(gridWidth - options[containerResolution].offset, vlinesContainer);
// Draw columns
for (var n = 1, columns = options[containerResolution].columns; n < columns; n++) {
var columnLimit = (n * columnWidth) + ((n-1) * options[containerResolution].gutter) + options[containerResolution].offset;
var columnLimitGutter = columnLimit + options[containerResolution].gutter;
print.vertical(columnLimit, vlinesContainer);
print.vertical(columnLimitGutter, vlinesContainer);
}
},
// Build horizontal lines container
horizontalContainer : function(gridContainer) {
// Get resolution name
var containerResolution = helper.getResolution(gridContainer.id);
// Get sizes
var baseline = parseInt(options[containerResolution].baseline, 10);
var bodyHeight = document.body.scrollHeight;
// Draw horizontal lines container
var hlinesContainer;
hlinesContainer = document.createElement("div");
hlinesContainer.className = 'hlines_container';
// Append horizontal lines container container to grid container
gridContainer.appendChild(hlinesContainer);
for (var n = baseline; n < bodyHeight; n += baseline) {
print.horizontal(n, hlinesContainer);
}
},
// Vertical line
vertical : function(left, container) {
// Create vertical line
var vline;
vline = document.createElement("div");
vline.className = 'vline';
vline.style.height = document.body.scrollHeight + 'px';
vline.style = helper.merge(vline.style, style.vertical); // Apply styles
vline.style.left = left + 'px';
// Add vertical line to the grid
container.appendChild(vline);
// @todo: Add line to cache
},
// Horizontal line
horizontal : function(top, container) {
// Create horizontal line
var hline;
hline = document.createElement("div");
hline.className = 'hline';
hline.style = helper.merge(hline.style, style.horizontal); // Apply styles
hline.style.top = top + 'px'; // Set top parameter
// Add horizontal line to the grid
container.appendChild(hline);
// @todo: Add line to cache
}
};
/* Events block */
var events = {
// Init function
init : function() {
events.resize(); // Bind resize event
},
// Bind resize event
resize : function() {
window.onresize = function(event) {
resolution.eval();
// @todo: Change height: draw less or more horizontal lines
};
}
};
/* Helpers object */
var helper = {
// Merge objects
merge : function(ob1, ob2){
for (var key in ob2) {
if (ob2.hasOwnProperty(key)) {
ob1[key] = ob2[key];
}
}
return ob1;
},
// Get resolution from id "grid_*"
getResolution : function(id) {
var regexp = /([A-Za-z0-9]+)(_)([\d]+)/;
var parts = regexp.exec(id);
return parts[3];
},
// Get container from cache
getContainer : function(resolution) {
return container[resolution];
}
};
/* Public object */
RGrid = (function() {
var RGrid = function(userOptions) {
//Merge default and user options
options = helper.merge(defaults, userOptions || {});
// Cache resolutions
for (var res in options) {
if (options.hasOwnProperty(res)) {
resolutions.push(parseInt(res, 10));
}
}
// Reverse the cache resolution array
resolutions.reverse();
// Draw all grids
grid.build();
// Add events
events.init();
};
return RGrid;
})();
})(window);