Debian package: Add support for xulrunner 18
[conkeror.git] / modules / theme.js
blob3a07611781241083b96882ba241961422dc64e36
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         constructor: theme,
33         register: function (cssfile) {
34             register_agent_stylesheet(this.location+cssfile);
35         },
36         unregister: function (cssfile) {
37             unregister_agent_stylesheet(this.location+cssfile);
38         },
39         registered_p: function (cssfile) {
40             return agent_stylesheet_registered_p(this.location+cssfile);
41         }
42     };
44     function theme_find (name) {
45         if (loaded_themes[name])
46             return loaded_themes[name];
47         for each (var path in theme_load_paths) {
48             var url;
49             if (path instanceof Ci.nsIURI) {
50                 url = path.spec;
51             } else if (path instanceof Ci.nsIFile) {
52                 url = make_uri(path).spec;
53             } else if (typeof(path) == "string") {
54                 if (path.substr(0, 7) == 'chrome:')
55                     url = path;
56                 else
57                     url = make_uri(make_file(path)).spec;
58             }
59             if (url.substr(-1) != '/')
60                 url += '/';
61             url += name + '/';
62             let def = get_contents_synchronously(url+'theme.json');
63             if (def === null)
64                 continue;
65             def = JSON.parse(def);
66             loaded_themes[name] = new theme(url, def.sheets);
67             return loaded_themes[name];
68         }
69         return null;
70     }
72     function theme_load (name) {
73         var th = theme_find(name);
74         if (! th) throw new Error("theme "+name+" not found.");
75         for each (var cssfile in th.sheets) {
76             let module = theme_cssfile_module(cssfile);
77             if (! themes[module]) {
78                 themes[module] = {};
79                 if (! featurep(module))
80                     call_after_load(module,
81                                     function () { theme_module(module); });
82             }
83             if (! themes[module][cssfile])
84                 themes[module][cssfile] = [];
85             if (themes[module][cssfile].length > 0)
86                 themes[module][cssfile][0].unregister(cssfile);
87             themes[module][cssfile].unshift(th);
88             if (featurep(module))
89                 th.register(cssfile);
90         }
91     }
93     function theme_unload (name) {
94         var th = theme_find(name);
95         for each (var cssfile in th.sheets) {
96             let module = theme_cssfile_module(cssfile);
97             themes[module][cssfile] =
98                 themes[module][cssfile].filter(function (x) { return x !== th; });
99             if (th.registered_p(cssfile)) {
100                 th.unregister(cssfile);
101                 if (themes[module][cssfile].length > 0)
102                     themes[module][cssfile][0].register(cssfile);
103             }
104         }
105         delete loaded_themes[name];
106     }
108     function theme_module (module) {
109         for (var cssfile in themes[module]) {
110             if (themes[module][cssfile].length > 0)
111                 themes[module][cssfile][0].register(cssfile);
112         }
113     }
116 theme_load("default");
118 provide("theme");