-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation.hashgrid.js
More file actions
392 lines (347 loc) · 12.6 KB
/
foundation.hashgrid.js
File metadata and controls
392 lines (347 loc) · 12.6 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* custom hashgrid for foundation inspired by http://github.com/dotjay/hashgrid
* Usage
*
* // The basic #grid setup looks like this
* $(document).hashgrid();
*
* // But there are a whole bunch of additional options you can set
* $(document).hashgrid({
* id: 'mygrid', // set a custom id for the grid container
* modifierKey: 'alt', // optional 'ctrl', 'alt' or 'shift'
* showGridKey: 's', // key to show the grid
* holdGridKey: 'enter', // key to hold the grid in place
* foregroundKey: 'f', // key to toggle foreground/background
* jumpGridsKey: 'd', // key to cycle through the grid classes
* numberOfGrids: 2, // number of grid classes used
* classPrefix: 'myclass', // prefix for the grid classes
* cookiePrefix: 'mygrid' // prefix for the cookie name
* });
*/
if (typeof jQuery == "undefined") {
alert("Hashgrid: jQuery not loaded. Make sure it's linked to your pages.");
}
(function($, window, document) {
'use strict';
/**
* hashgrid overlay
* @constructor
*/
$.fn.hashgrid = function(options) {
//set up default options
var defaults = {
id: 'grid', // id for the grid container
modifierKey: null, // optional 'ctrl', 'alt' or 'shift'
showGridKey: 'g', // key to show the grid
holdGridKey: 'h', // key to hold the grid in place
foregroundKey: 'f', // key to toggle foreground/background
jumpGridsKey: 'j', // key to cycle through the grid classes
numberOfGrids: 1, // number of grid classes used
classPrefix: 'grid-', // prefix for the grid classes
cookiePrefix: 'hashgrid' // prefix for the cookie name
};
var options = $.extend({}, defaults, options),
alreadyDown,
classNumber = 1,
gridLines,
i,
line,
lineHeight,
numGridLines,
overlay,
overlayCookie,
overlayEl,
overlayOn = false,
overlayVert,
overlayZState = 'B',
overlayZBackground = -1,
overlayZForeground = 9999,
pageHeight,
setKey,
state,
sticky = false,
top,
gridLinesHoriz = '',
gridLinesVert = '',
numGridColumns = 0;
// Remove any conflicting overlay
$('#' + options.id).remove();
// Override the default overlay height with the actual page height
pageHeight = parseFloat($(document).height());
// Create overlay, hidden before adding to DOM
overlay = $('<div>', {
id: options.id
})
.css({
width: "100%",
display: 'none',
pointerEvents: 'none'
})
.prependTo('body')
.height(pageHeight);
// Unless a custom z-index is set, ensure the overlay will be behind everything
if (overlay.css('z-index') == 'auto') overlay.css('z-index', overlayZBackground);
// Build the Vertical grid lines based on number of styled columns
function buildGridVert() {
var i = 0,
eW = 0,
ceW = 1,
colW = function(i) {
try {
var el = $('<div>', {
class: 'small-' + i + ' columns',
style: 'display:none'
})
.prependTo('body');
var isNew = (el.outerWidth() != ceW);
ceW = el.outerWidth();
el.remove();
return isNew;
} catch (e) {
return 0;
}
};
while (colW(i + 1)) {
gridLinesVert += '<div class="small-1 columns vert"><span> </span></div>';
i++;
}
numGridColumns = i;
}
buildGridVert();
// Calculate the number of grid lines needed
function getLineHeight() {
try {
var el = $('<p>', {
class: 'small-1 columns lineHightTest',
style: 'display:none;height:1em;font-size:1em;',
text: 'M'
})
.prependTo('body');
var lH = el.height();
el.remove();
return lH;
} catch (e) {
return 0;
}
};
lineHeight = getLineHeight();
// Set grid lines
numGridLines = Math.floor(pageHeight / lineHeight);
// Build the Horizontal grid lines
function buildGridHoriz() {
for (i = numGridLines; i > 0; i--) {
gridLinesHoriz += '<div class="small-' + numGridColumns + ' columns horiz"></div>';
}
}
buildGridHoriz();
// Add the horizontal grid to the dom
line = $('<div>', {
class: 'horiz-container'
})
.css({
'position': 'absolute'
})
.appendTo(overlay)
.append(gridLinesHoriz);
// Add the vertical grid to the dom
overlayVert = $('<div>', {
class: 'vert-container row'
})
.appendTo(overlay)
.css({
top: 0
})
.append(gridLinesVert)
.children().children('span')
.height(pageHeight)
.css({
display: 'inline-block',
width: '100%'
});
// Check for saved state
overlayCookie = readCookie(options.cookiePrefix + options.id);
if (typeof overlayCookie == 'string') {
state = overlayCookie.split('-');
state[2] = Number(state[2]);
if ((typeof state[2] == 'number') && !isNaN(state[2])) {
classNumber = state[2].toFixed(0);
overlay.addClass(options.classPrefix + classNumber);
}
if (state[1] == 'F') {
overlayZState = 'F';
overlay.css('z-index', overlayZForeground);
}
if (state[0] == '1') {
overlayOn = true;
sticky = true;
showOverlay();
}
} else {
overlay.addClass(options.classPrefix + classNumber);
}
// bind Keyboard controls
$(document)
.on('keydown', keydownHandler)
.on('keyup', keyupHandler);
/**
* Helpers
*/
function getModifier(e) {
if (options.modifierKey === null) return true; // Bypass by default
var m = true;
switch (options.modifierKey) {
case 'ctrl':
m = (e.ctrlKey ? e.ctrlKey : false);
break;
case 'alt':
m = (e.altKey ? e.altKey : false);
break;
case 'shift':
m = (e.shiftKey ? e.shiftKey : false);
break;
}
return m;
}
function getKey(e) {
var k = false,
c = (e.keyCode ? e.keyCode : e.which);
// Handle keywords
if (c == 13) k = 'enter';
// Handle letters
else k = String.fromCharCode(c).toLowerCase();
return k;
}
function saveState() {
createCookie(options.cookiePrefix + options.id, (sticky ? '1' : '0') + '-' + overlayZState + '-' + classNumber, 1);
}
function showOverlay() {
overlay.show();
overlayVert
// hide any vertical blocks that aren't at the top of the viewport
.children('.vert').each(function() {
var vCol = $(this);
vCol.css('display', 'inline-block');
if (vCol.offset().top > vCol.parent().offset().top) {
vCol.hide();
}
});
}
/**
* Event handlers
*/
alreadyDown = {};
function keydownHandler(e) {
var k = getKey(e) || null,
m = getModifier(e) || null,
source = e.target.tagName.toLowerCase();
if ((source == 'input') || (source == 'textarea') || (source == 'select')) {
return true;
}
if (!m || !k || alreadyDown[k]) return true;
alreadyDown[k] = true;
switch (k) {
case options.showGridKey:
if (!overlayOn) {
showOverlay();
overlayOn = true;
} else if (sticky) {
overlay.hide();
overlayOn = false;
sticky = false;
saveState();
}
break;
case options.holdGridKey:
if (overlayOn && !sticky) {
// Turn sticky overlay on
sticky = true;
saveState();
}
break;
case options.foregroundKey:
if (overlayOn) {
// Toggle sticky overlay z-index
if (overlay.css('z-index') == overlayZForeground) {
overlay.css('z-index', overlayZBackground);
overlayZState = 'B';
} else {
overlay.css('z-index', overlayZForeground);
overlayZState = 'F';
}
saveState();
}
break;
case options.jumpGridsKey:
if (overlayOn && (options.numberOfGrids > 1)) {
// Cycle through the available grids
overlay.removeClass(options.classPrefix + classNumber);
classNumber++;
if (classNumber > options.numberOfGrids) classNumber = 1;
overlay.addClass(options.classPrefix + classNumber);
showOverlay();
if (/webkit/.test(navigator.userAgent.toLowerCase())) {
forceRepaint();
}
saveState();
}
break;
}
return true;
}
function keyupHandler(e) {
var k = getKey(e) || null,
m = getModifier(e);
if (!m) return true;
alreadyDown[k] = false;
if (k && (k == options.showGridKey) && !sticky) overlay.hide(0, function() {
overlayOn = false;
});
return true;
}
/**
* Cookie functions
*
* By Peter-Paul Koch:
* http://www.quirksmode.org/js/cookies.html
*/
function createCookie(name, value, days) {
var date,
expires = "";
if (days) {
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var c,
ca = document.cookie.split(';'),
i = 0,
len = ca.length,
nameEQ = name + "=";
for (; i < len; i++) {
c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
/**
* Forces a repaint (because WebKit has issues)
* http://www.sitepoint.com/forums/showthread.php?p=4538763
* http://www.phpied.com/the-new-game-show-will-it-reflow/
*/
function forceRepaint() {
var ss = document.styleSheets[0];
try {
ss.addRule('.xxxxxx', 'position: relative');
ss.removeRule(ss.rules.length - 1);
} catch (e) {}
}
return {};
};
})(jQuery, window, document);