Support Debian's upcoming xulrunner-1.9.1 packages
[conkeror.git] / modules / theme.js
blob81a01c8d993618c1eb2b0ced4607a72ed96219bb
1 /**
2  * (C) Copyright 2008 John J. Foerch
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
9     let themes = {};
10     let loaded_themes = {};
12     define_variable('theme_load_paths', ['chrome://conkeror-gui/skin/'],
13                     "List of directories and chrome urls within which themes "+
14                     "may be found.  The items can be represented as strings, "+
15                     "nsIURI's, or nsIFile's. Chrome urls must be given as "+
16                     "strings.");
18     function theme_cssfile_module (cssfile) {
19         cssfile = cssfile.substring(0, cssfile.indexOf('.'));
20         var sep = cssfile.indexOf('--');
21         if (sep == -1)
22             return cssfile;
23         else
24             return cssfile.substring(0, sep);
25     }
27     function theme (location, sheets) {
28         this.location = location;
29         this.sheets = sheets;
30     }
31     theme.prototype = {
32         register: function (cssfile) {
33             register_agent_stylesheet(this.location+cssfile);
34         },
35         unregister: function (cssfile) {
36             unregister_agent_stylesheet(this.location+cssfile);
37         },
38         registered_p: function (cssfile) {
39             return agent_stylesheet_registered_p(this.location+cssfile);
40         }
41     };
43     function theme_find (name) {
44         if (loaded_themes[name])
45             return loaded_themes[name];
46         for each (var path in theme_load_paths) {
47             var url;
48             if (path instanceof Ci.nsIURI) {
49                 url = path.spec;
50             } else if (path instanceof Ci.nsIFile) {
51                 url = make_uri(path).spec;
52             } else if (typeof(path) == "string") {
53                 if (path.substr(0, 7) == 'chrome:')
54                     url = path;
55                 else
56                     url = make_uri(make_file(path)).spec;
57             }
58             if (url.substr(-1) != '/')
59                 url += '/';
60             url += name + '/';
61             let def = get_contents_synchronously(url+'theme.json');
62             if (def === null)
63                 continue;
64             def = eval('('+def+')');
65             loaded_themes[name] = new theme(url, def.sheets);
66             return loaded_themes[name];
67         }
68         return null;
69     }
71     function theme_load (name) {
72         var th = theme_find(name);
73         if (! th) throw new Error("theme "+name+" not found.");
74         for each (var cssfile in th.sheets) {
75             let module = theme_cssfile_module(cssfile);
76             if (! themes[module]) {
77                 themes[module] = {};
78                 if (! loaded(module+".js"))
79                     call_after_load(module+".js",
80                                     function () { theme_module(module); });
81             }
82             if (! themes[module][cssfile])
83                 themes[module][cssfile] = [];
84             if (themes[module][cssfile].length > 0)
85                 themes[module][cssfile][0].unregister(cssfile);
86             themes[module][cssfile].unshift(th);
87             if (loaded(module+".js"))
88                 th.register(cssfile);
89         }
90     }
92     function theme_unload (name) {
93         var th = theme_find(name);
94         for each (var cssfile in th.sheets) {
95             let module = theme_cssfile_module(cssfile);
96             themes[module][cssfile] =
97                 themes[module][cssfile].filter(function (x) { return x !== th; });
98             if (th.registered_p(cssfile)) {
99                 th.unregister(cssfile);
100                 if (themes[module][cssfile].length > 0)
101                     themes[module][cssfile][0].register(cssfile);
102             }
103         }
104         delete loaded_themes[name];
105     }
107     function theme_module (module) {
108         for (var cssfile in themes[module]) {
109             if (themes[module][cssfile].length > 0)
110                 themes[module][cssfile][0].register(cssfile);
111         }
112     }
115 theme_load("default");