utils.js: Add coroutine interface to XMLHttpRequest
[conkeror.git] / modules / mode-line.js
blob470131ca0173af58742213f4d182f016c2328d33
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     uninstall: 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([hook_name, 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 each (let i in this.window_hooks) {
115             remove_hook.call(this.window, i[0], i[1]);
116         }
117     },
119     remove : function ()
120     {
121         this.view.remove();
122     }
125 define_global_window_mode("mode_line", "window_initialize_early_hook");
127 function current_buffer_name_widget(window) {
128     this.name = "current-buffer-name-widget";
129     text_widget.call(this, window);
130     this.crop = "end";
131     this.add_hook("current_content_buffer_location_change_hook");
132     this.add_hook("select_buffer_hook");
134 current_buffer_name_widget.prototype.__proto__ = text_widget.prototype;
135 current_buffer_name_widget.prototype.update = function () {
136     this.view.text = this.window.buffers.current.description;
139 function current_buffer_scroll_position_widget(window) {
140     this.name = "current-buffer-scroll-position-widget";
141     text_widget.call(this, window);
142     this.add_hook("current_buffer_scroll_hook");
143     this.add_hook("select_buffer_hook");
144     this.add_hook("current_content_buffer_location_change_hook");
145     this.add_hook("current_content_buffer_focus_change_hook");
147 current_buffer_scroll_position_widget.prototype.__proto__ = text_widget.prototype;
148 current_buffer_scroll_position_widget.prototype.update = function () {
149     var b = this.window.buffers.current;
150     var scrollX, scrollY, scrollMaxX, scrollMaxY;
151     if (b instanceof content_buffer)
152     {
153         var w = b.focused_frame;
154         scrollX = w.scrollX;
155         scrollY = w.scrollY;
156         scrollMaxX = w.scrollMaxX;
157         scrollMaxY = w.scrollMaxY;
158     } else 
159     {
160         scrollX = b.scrollX;
161         scrollY = b.scrollY;
162         scrollMaxX = b.scrollMaxX;
163         scrollMaxY = b.scrollMaxY;
164     }
165     var x = scrollMaxX == 0 ? 100 : Math.round(scrollX / scrollMaxX * 100);
166     var y = scrollMaxY == 0 ? 100 : Math.round(scrollY / scrollMaxY * 100);
167     this.view.text = "(" + x + ", " + y + ")";
170 function clock_widget(window)
172     this.name = "clock-widget";
173     text_widget.call(this, window);
174     var obj = this;
175     this.timer_ID = window.setInterval(function () { obj.update(); }, 60000);
177 clock_widget.prototype.__proto__ = text_widget.prototype;
178 clock_widget.prototype.update = function () {
179     var time = new Date();
180     var hours = time.getHours();
181     var mins = time.getMinutes();
182     this.view.text = (hours<10 ? "0" + hours:hours) + ":" + (mins<10 ?"0" +mins:mins);
184 clock_widget.prototype.destroy = function () {
185     this.window.clearTimeout(this.timer_ID);
188 function mode_line_adder(widget_constructor) {
189     return function (window) { window.mode_line.add_text_widget(new widget_constructor(window)); }
192 add_hook("mode_line_hook", mode_line_adder(current_buffer_name_widget));
193 add_hook("mode_line_hook", mode_line_adder(clock_widget));
194 add_hook("mode_line_hook", mode_line_adder(current_buffer_scroll_position_widget));
196 mode_line_mode(true);