add constructor properties to prototypes
[conkeror.git] / contrib / modules / mode-line-buttons.js
blobc1a75268721280fd51671ba38d01c8b74b6bdebf
1 /**
2  * (C) Copyright 2009 David Kettler
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 in_module(null);
10 require("mode-line.js");
12 function button_widget (window) {
13     this.class_name = "button-widget";
14     text_widget.call(this, window);
16 button_widget.prototype = {
17     constructor: button_widget,
18     __proto__: text_widget.prototype,
20     make_element: function (window) {
21         var command = this.command;
22         var element = create_XUL(window, "image");
24         element.addEventListener("click", function (event) {
25             var I = new interactive_context(window.buffers.current);
26             co_call(call_interactively(I, command));
27         }, false);
29         element.addEventListener("mouseover", function (event) {
30             var msg = "Button: " + command;
31             var keymaps = get_current_keymaps(window);
32             var list = keymap_lookup_command(keymaps, command);
33             if (list.length)
34                 msg += " (which is on key " + list.join(", ") + ")";
35             window.minibuffer.show(msg);
36         }, false);
38         element.addEventListener("mouseout", function (event) {
39             window.minibuffer.show("");
40         }, false);
42         element.setAttribute("id", "button-widget-" + command);
43         element.setAttribute("class", this.class_name);
44         for (var a in this.attributes) {
45             element.setAttribute(a, this.attributes[a]);
46         }
48         return element;
49     }
52 function make_button_widget (command, attributes) {
53     if (typeof attributes == "string")
54         // Simple case
55         attributes = { src: "moz-icon://stock/gtk-" + attributes };
57     function new_widget (window) {
58         button_widget.call(this, window);
59     }
60     new_widget.prototype = {
61         constructor: new_widget,
62         __proto__: button_widget.prototype,
63         command: command,
64         attributes: attributes
65     }
66     new_widget.mode_line_adder = function (window) {
67         var widget = new new_widget(window);
68         window.mode_line.add_widget(widget, widget.make_element(window));
69     }
71     return new_widget;
74 function mode_line_add_buttons (buttons, prepend) {
75     for (var i = 0, n = buttons.length; i < n; i++) {
76         var j = prepend ? n - i - 1 : i;
77         var w = make_button_widget(buttons[j][0], buttons[j][1]);
78         add_hook("mode_line_hook", mode_line_adder(w), prepend);
79     }
82 standard_mode_line_buttons = [
83     ["find-url", "open"],
84     ["find-url-new-buffer", "new"],
85     ["back", "go-back"],
86     ["forward", "go-forward"],
87     ["reload", "refresh"],
88     ["kill-current-buffer", "close"],
89     ["buffer-previous", "go-up"],
90     ["buffer-next", "go-down"],
91     ["help-page", "help"],
94 provide("mode-line-buttons");