-
Notifications
You must be signed in to change notification settings - Fork 0
/
sp-grid.js
144 lines (113 loc) · 3.78 KB
/
sp-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
(function(document) {
var columnCount = 12, // Columns count int
columnCoefficient = 100 / columnCount, // Columns coefficient float
spaceSizes = [0, 2, 5, 10, 20, 30, 40, 50, 60, 80], // Margin & padding sizes
mediaSizes = [0, 1279, 1023, 959, 767, 479, 319], // Screen media sizes
mediaCount = (mediaSizes.length - 1); // Screen media count
function css(selector, options, important) {
var str = selector + '{';
for (var k in options) {
str += k + ':' + options[k] + ((important) ? ' !important' : '') + ';';
}
str += '}';
return str;
}
function cssSpaceModeProperty(k, p, q, l) {
var o = {}, v = spaceSizes[k];
if (l) {
for (var j in l) {
o[q + '-' + l[j]] = v + 'px';
}
} else {
o[q] = v + 'px';
}
return css('.' + p + v, o, !0);
}
function cssSpaceProperty(p, q) {
var r = '';
for (var k in spaceSizes) {
r += cssSpaceModeProperty(k, p, q) +
cssSpaceModeProperty(k, p + 'h', q, ['left', 'right']) +
cssSpaceModeProperty(k, p + 'v', q, ['top', 'bottom']) +
cssSpaceModeProperty(k, p + 't', q, ['top']) +
cssSpaceModeProperty(k, p + 'r', q, ['right']) +
cssSpaceModeProperty(k, p + 'b', q, ['bottom']) +
cssSpaceModeProperty(k, p + 'l', q, ['left']);
}
return r;
}
function sizeToString(s) {
s = parseInt(s);
return (s) ? 's' + s : '';
}
function cssGrids(s) {
var r = '', i;
s = sizeToString(s);
for (i = 1; i <= columnCount; i++) {
r += css('.' + s + 'g' + i, {width: i * columnCoefficient + '%'});
}
return r;
}
function cssFloat(s) {
s = sizeToString(s);
return css('.' + s + 'fN', {float: 'none'}) +
css('.' + s + 'fL', {float: 'left'}) +
css('.' + s + 'fR', {float: 'right'});
}
function cssTextAlign(s) {
s = sizeToString(s);
return css('.' + s + 'tC', {'text-align': 'center'}) +
css('.' + s + 'tJ', {'text-align': 'justify'}) +
css('.' + s + 'tL', {'text-align': 'left'}) +
css('.' + s + 'tR', {'text-align': 'right'});
}
function cssSpace(s) {
s = sizeToString(s);
return cssSpaceProperty(s + 'p', 'padding') +
cssSpaceProperty(s + 'm', 'margin');
}
function selectorGrids() {
var r = '', p = '', i;
for (i = 1; i <= columnCount; i++) {
r += p + '.g' + i;
p = ',';
}
return r;
}
function selectorMediaSizes() {
var r = '', p = '', i;
for (i = 1; i <= mediaCount; i++) {
r += p + '.s' + i;
p = ',';
}
return r;
}
var s = css(selectorGrids(), {
display: 'block',
float: 'left',
position: 'relative'
}) +
css(selectorMediaSizes(), {display: 'none'}) +
cssGrids() +
cssFloat() +
cssTextAlign() +
cssSpace();
for (var i = 1; i <= mediaCount; i++) {
s += '@media screen and (max-width:' + mediaSizes[i] + 'px) {' +
css('.s' + i, {display: 'inherit'}) +
css('.s' + i + 'H', {display: 'none'}) +
cssGrids(i) +
cssFloat(i) +
cssTextAlign(i) +
cssSpace(i) +
'}';
}
console.log(s);
var f = document.createElement('link');
f.href = 'data:text/css;base64,' + btoa(s);
f.rel = 'stylesheet';
f.type = 'text/css';
f.media = 'screen';
var w = document.getElementById('sp-grid');
w.parentNode.insertBefore(f, w);
})(document);