Factor out mime.js matching logic to predicate_alist_match
[conkeror.git] / modules / mode-line.js
blob11c8e335f398953fe591cbfbf1d1b4235f4abc64
1 require("mode.js");
3 define_window_local_hook("mode_line_hook");
5 function generic_element_widget_container(window, container)
7     this.window = window;
8     this.container = container;
10 define_keywords("$flex", "$align", "$class", "$crop");
11 generic_element_widget_container.prototype = {
12     add_text_widget : function (widget) {
13         keywords(arguments);
14         var flex = arguments.$flex;
15         var class_name = arguments.$class;
16         var align = arguments.$align;
17         var crop = arguments.$crop;
18         var element = create_XUL(this.window, "label");
19         if (flex != null)
20             element.setAttribute("flex", flex);
21         if (align != null)
22             element.setAttribute("align", align);
23         if (class_name == null)
24             class_name = widget.name;
25         element.setAttribute("class", class_name);
26         if (crop == null)
27             crop = widget.crop;
28         element.setAttribute("crop", crop);
29         element.conkeror_widget = new generic_widget_element(element, widget);
30         this.container.appendChild(element);
31         return element.conkeror_widget;
32     },
33     destroy : function() {
34         var children = this.container.childNodes;
35         for (var i = 0; i < children.length; ++i)
36             children.item(i).conkeror_widget.destroy();
37     }
40 function mode_line(window)
42     var element = create_XUL(window, "hbox");
43     element.setAttribute("class", "mode-line");
44     /* FIXME: this will need to changed to be buffer-local */
45     var insert_before = window.document.getElementById("minibuffer");
46     insert_before.parentNode.insertBefore(element, insert_before);
47     window.mode_line = this;
48     generic_element_widget_container.call(this, window, element);
49     mode_line_hook.run(window, this);
51 mode_line.prototype = {
52     __proto__: generic_element_widget_container.prototype,
54     remove: function () {
55         this.container.parentNode.removeChild(this.window.mode_line.container);
56         this.__proto__.__proto__.destroy.call(this);
57     }
61 function generic_widget_element(element, widget)
63     this.element = element;
64     this.widget = widget;
65     widget.attach(this);
68 generic_widget_element.prototype = {
69     get text () {
70         return this.element.getAttribute("value");
71     },
73     set text (v) {
74         this.element.setAttribute("value", v);
75     },
77     destroy : function () {
78         this.widget.destroy();
79     },
81     remove : function () {
82         this.element.parentNode.removeChild(this.element);
83         this.destroy();
84     }
88 function text_widget(window)
90     this.window_hooks = [];
91     this.window = window;
93 text_widget.prototype = {
94     add_hook : function (hook_name, handler)
95     {
96         var obj = this;
97         if (handler == null) handler = function () { obj.update(); }
98         add_hook.call(this.window, hook_name, handler);
99         this.window_hooks.push(handler);
100     },
102     view : null,
104     attach : function (view) {
105         this.view = view;
106         this.update();
107     },
109     update : function () {
110     },
112     destroy : function ()
113     {
114         for (var i = 0; i < this.window_hooks.length; ++i)
115             remove_hook.call(this.window, this.window_hooks[i]);
116     },
118     remove : function ()
119     {
120         this.view.remove();
121     }
124 function mode_line_install(window)
126     if (window.mode_line)
127         throw new Error("mode line already initialized for window");
128     window.mode_line = new mode_line(window);
131 function mode_line_uninstall(window)
133     if (!window.mode_line)
134         throw new Error("mode line not initialized for window");
135     window.mode_line.remove();
136     delete window.mode_line;
139 define_global_mode("mode_line_mode",
140                    function () { // enable
141                        add_hook("window_initialize_early_hook", mode_line_install);
142                        for_each_window(mode_line_install);
143                    },
144                    function () { // disable
145                        remove_hook("window_initialize_early_hook", mode_line_install);
146                        for_each_window(mode_line_uninstall);
147                    });
149 function current_buffer_name_widget(window) {
150     this.name = "current-buffer-name-widget";
151     text_widget.call(this, window);
152     this.crop = "end";
153     this.add_hook("current_content_buffer_location_change_hook");
154     this.add_hook("select_buffer_hook");
156 current_buffer_name_widget.prototype.__proto__ = text_widget.prototype;
157 current_buffer_name_widget.prototype.update = function () {
158     this.view.text = this.window.buffers.current.description;
161 function current_buffer_scroll_position_widget(window) {
162     this.name = "current-buffer-scroll-position-widget";
163     text_widget.call(this, window);
164     this.add_hook("current_buffer_scroll_hook");
165     this.add_hook("select_buffer_hook");
166     this.add_hook("current_content_buffer_location_change_hook");
167     this.add_hook("current_content_buffer_focus_change_hook");
169 current_buffer_scroll_position_widget.prototype.__proto__ = text_widget.prototype;
170 current_buffer_scroll_position_widget.prototype.update = function () {
171     var b = this.window.buffers.current;
172     var scrollX, scrollY, scrollMaxX, scrollMaxY;
173     if (b instanceof content_buffer)
174     {
175         var w = b.focused_frame;
176         scrollX = w.scrollX;
177         scrollY = w.scrollY;
178         scrollMaxX = w.scrollMaxX;
179         scrollMaxY = w.scrollMaxY;
180     } else 
181     {
182         scrollX = b.scrollX;
183         scrollY = b.scrollY;
184         scrollMaxX = b.scrollMaxX;
185         scrollMaxY = b.scrollMaxY;
186     }
187     var x = scrollMaxX == 0 ? 100 : Math.round(scrollX / scrollMaxX * 100);
188     var y = scrollMaxY == 0 ? 100 : Math.round(scrollY / scrollMaxY * 100);
189     this.view.text = "(" + x + ", " + y + ")";
192 function clock_widget(window)
194     this.name = "clock-widget";
195     text_widget.call(this, window);
196     var obj = this;
197     this.timer_ID = window.setInterval(function () { obj.update(); }, 60000);
199 clock_widget.prototype.__proto__ = text_widget.prototype;
200 clock_widget.prototype.update = function () {
201     var time = new Date();
202     var hours = time.getHours();
203     var mins = time.getMinutes();
204     this.view.text = (hours<10 ? "0" + hours:hours) + ":" + (mins<10 ?"0" +mins:mins);
206 clock_widget.prototype.destroy = function () {
207     this.window.clearTimeout(this.timer_ID);
210 function mode_line_adder(widget_constructor) {
211     return function (window) { window.mode_line.add_text_widget(new widget_constructor(window)); }
214 add_hook("mode_line_hook", mode_line_adder(current_buffer_name_widget));
215 add_hook("mode_line_hook", mode_line_adder(clock_widget));
216 add_hook("mode_line_hook", mode_line_adder(current_buffer_scroll_position_widget));
218 mode_line_mode(true);