]>
Commit | Line | Data |
---|---|---|
4dd199c3 CP |
1 | qwebirc.ui.style.ModifiableStylesheet = new Class({ |
2 | initialize: function(url) { | |
3 | var n = this.__parseStylesheet(this.__getStylesheet(url)); | |
4 | ||
5 | this.__cssText = n.cssText; | |
6 | this.rules = n.rules; | |
7 | ||
8 | this.__tag = this.__createTag(); | |
9 | }, | |
10 | __createTag: function() { | |
11 | var tag = document.createElement("style"); | |
12 | tag.type = "text/css"; | |
13 | tag.media = "all"; | |
14 | ||
15 | document.getElementsByTagName("head")[0].appendChild(tag); | |
16 | ||
17 | return tag; | |
18 | }, | |
19 | __getStylesheet: function(url) { | |
20 | var r = new Request({url: url, async: false}); | |
21 | var result; | |
22 | r.addEvent("complete", function(x) { | |
23 | result = x; | |
24 | }); | |
25 | r.get(); | |
26 | return result; | |
27 | }, | |
28 | __setStylesheet: function(stylesheet) { | |
29 | var node = this.__tag; | |
30 | ||
31 | if(node.styleSheet) { /* IE */ | |
32 | node.styleSheet.cssText = stylesheet; | |
33 | } else { | |
34 | var d = document.createTextNode(stylesheet); | |
35 | node.appendChild(d); | |
36 | while(node.childNodes.length > 1) | |
37 | node.removeChild(node.firstChild); | |
38 | } | |
39 | }, | |
40 | __parseStylesheet: function(data) { | |
41 | var lines = data.replace("\r\n", "\n").split("\n"); | |
42 | ||
43 | var rules = {}; | |
44 | var i; | |
45 | for(i=0;i<lines.length;i++) { | |
46 | var line = lines[i]; | |
47 | if(line.trim() === "") | |
48 | break; | |
49 | ||
50 | var tokens = line.splitMax("=", 2); | |
51 | if(tokens.length != 2) | |
52 | continue; | |
53 | ||
54 | rules[tokens[0]] = tokens[1]; | |
55 | } | |
56 | ||
57 | var cssLines = [] | |
58 | for(;i<lines.length;i++) | |
59 | cssLines.push(lines[i]); | |
60 | ||
61 | return {cssText: cssLines.join("\n"), rules: rules}; | |
62 | }, | |
63 | set: function(mutator) { | |
64 | if(!$defined(mutator)) | |
65 | mutator = function(x) { return x; }; | |
66 | ||
67 | var text = this.__cssText; | |
68 | for(var key in this.rules) { | |
69 | var value = mutator(new Color(this.rules[key])); | |
70 | ||
71 | if(value == "255,255,255") /* IE confuses white with transparent... */ | |
72 | value = "255,255,254"; | |
73 | ||
74 | text = text.replaceAll("$(" + key + ")", "rgb(" + value + ")"); | |
75 | } | |
76 | ||
77 | this.__setStylesheet(text); | |
78 | } | |
79 | }); |