]>
Commit | Line | Data |
---|---|---|
1 | qwebirc.ui.style.ModifiableStylesheet = new Class({ | |
2 | initialize: function(url) { | |
3 | var n = this.__parseStylesheet(this.__getStylesheet(url), 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, url) { | |
41 | var lines = data.replace("\r\n", "\n").split("\n"); | |
42 | var baseURL = new URI(".", {base: url}).toString(); | |
43 | ||
44 | var rules = {}; | |
45 | var i; | |
46 | for(i=0;i<lines.length;i++) { | |
47 | var line = lines[i]; | |
48 | if(line.trim() === "") | |
49 | break; | |
50 | ||
51 | var tokens = line.splitMax("=", 2); | |
52 | if(tokens.length != 2) | |
53 | continue; | |
54 | ||
55 | rules[tokens[0]] = tokens[1]; | |
56 | } | |
57 | ||
58 | var cssLines = [] | |
59 | for(;i<lines.length;i++) { | |
60 | var line = lines[i]; | |
61 | line = line.replaceAll("$(base_url)", baseURL); | |
62 | cssLines.push(line); | |
63 | } | |
64 | return {cssText: cssLines.join("\n"), rules: rules}; | |
65 | }, | |
66 | set: function(mutator) { | |
67 | if(!$defined(mutator)) | |
68 | mutator = function(x) { return x; }; | |
69 | ||
70 | var text = this.__cssText; | |
71 | for(var key in this.rules) { | |
72 | var s = this.rules[key].split(","); | |
73 | var value = mutator.pass(s); | |
74 | ||
75 | text = text.replaceAll("$(" + key + ")", value); | |
76 | } | |
77 | ||
78 | this.__setStylesheet(text); | |
79 | } | |
80 | }); |