-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalised-css.js
73 lines (62 loc) · 2.13 KB
/
localised-css.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
;(function () {
"use strict";
function init() {
var css = getLocalisedCss();
applyLocalisedCss(css);
window.setInterval(function () {
applyLocalisedCss(css);
}, 100);
}
function getLocalisedCss() {
var stylesheets = document.styleSheets,
rules,
rule,
localisedCss = [];
for (var i = 0; i < stylesheets.length; i++) {
rules = stylesheets[i].cssRules;
for(var j in rules) {
rule = rules[j].cssText;
if (typeof rule !== 'undefined') {
console.log(rule);
if (rule.indexOf('@media only local') !== -1) {
localisedCss.push(processLocalisedCssRule(rule));
}
}
}
}
return localisedCss;
}
function applyLocalisedCss(css) {
for (var i = 0; i < css.length; i++) {
$(css[i].container).each(function () {
var container = $(this),
child = container.find(css[i].child),
containerWidth;
if(child.length > 0) {
// only do the expensive width calculation if there is actually a child element to apply rules to
containerWidth = container.outerWidth();
if ( (css[i].maxWidth) && (containerWidth > css[i].condition) || (css[i].minWidth) && (containerWidth < css[i].condition)) {
child.attr('style', css[i].style);
}
else {
child.attr('style', '');
}
}
});
}
}
function processLocalisedCssRule(rule) {
// need to process the rule
console.log(rule);
// for now, we hardcode it
return {
container: '.list',
child: 'li',
style: 'display: inline;',
maxWidth: true,
minWidth: false,
condition: '400'
};
}
$(document).ready(init);
}());