whitespace
[conkeror.git] / modules / theme.js
blob322887e37ccd7f7ff298a0e9c55faa4888915b5b
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 **/
8 in_module(null);
11     let themes = {};
12     let loaded_themes = {};
14     define_variable('theme_load_paths', ['chrome://conkeror-gui/skin/'],
15                     "List of directories and chrome urls within which themes "+
16                     "may be found.  The items can be represented as strings, "+
17                     "nsIURI's, or nsIFile's. Chrome urls must be given as "+
18                     "strings.");
20     function theme_cssfile_module (cssfile) {
21         cssfile = cssfile.substring(0, cssfile.indexOf('.'));
22         var sep = cssfile.indexOf('--');
23         if (sep == -1)
24             return cssfile;
25         else
26             return cssfile.substring(0, sep);
27     }
29     function theme (location, sheets) {
30         this.location = location;
31         this.sheets = sheets;
32     }
33     theme.prototype = {
34         constructor: theme,
35         register: function (cssfile) {
36             register_agent_stylesheet(this.location+cssfile);
37         },
38         unregister: function (cssfile) {
39             unregister_agent_stylesheet(this.location+cssfile);
40         },
41         registered_p: function (cssfile) {
42             return agent_stylesheet_registered_p(this.location+cssfile);
43         }
44     };
46     function theme_find (name) {
47         if (loaded_themes[name])
48             return loaded_themes[name];
49         for each (var path in theme_load_paths) {
50             var url;
51             if (path instanceof Ci.nsIURI) {
52                 url = path.spec;
53             } else if (path instanceof Ci.nsIFile) {
54                 url = make_uri(path).spec;
55             } else if (typeof(path) == "string") {
56                 if (path.substr(0, 7) == 'chrome:')
57                     url = path;
58                 else
59                     url = make_uri(make_file(path)).spec;
60             }
61             if (url.substr(-1) != '/')
62                 url += '/';
63             url += name + '/';
64             let def = get_contents_synchronously(url+'theme.json');
65             if (def === null)
66                 continue;
67             def = eval('('+def+')');
68             loaded_themes[name] = new theme(url, def.sheets);
69             return loaded_themes[name];
70         }
71         return null;
72     }
74     function theme_load (name) {
75         var th = theme_find(name);
76         if (! th) throw new Error("theme "+name+" not found.");
77         for each (var cssfile in th.sheets) {
78             let module = theme_cssfile_module(cssfile);
79             if (! themes[module]) {
80                 themes[module] = {};
81                 if (! featurep(module))
82                     call_after_load(module,
83                                     function () { theme_module(module); });
84             }
85             if (! themes[module][cssfile])
86                 themes[module][cssfile] = [];
87             if (themes[module][cssfile].length > 0)
88                 themes[module][cssfile][0].unregister(cssfile);
89             themes[module][cssfile].unshift(th);
90             if (featurep(module))
91                 th.register(cssfile);
92         }
93     }
95     function theme_unload (name) {
96         var th = theme_find(name);
97         for each (var cssfile in th.sheets) {
98             let module = theme_cssfile_module(cssfile);
99             themes[module][cssfile] =
100                 themes[module][cssfile].filter(function (x) { return x !== th; });
101             if (th.registered_p(cssfile)) {
102                 th.unregister(cssfile);
103                 if (themes[module][cssfile].length > 0)
104                     themes[module][cssfile][0].register(cssfile);
105             }
106         }
107         delete loaded_themes[name];
108     }
110     function theme_module (module) {
111         for (var cssfile in themes[module]) {
112             if (themes[module][cssfile].length > 0)
113                 themes[module][cssfile][0].register(cssfile);
114         }
115     }
118 theme_load("default");
120 provide("theme");