Make it easier to remove mode_line_adder functions from hooks
[conkeror.git] / modules / tab-bar.js
blobdd34bbfbe181d4468915a175e6a52b1e8b5cb037
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 require("mode.js");
10 function tab_bar(window) {
11     window.tab_bar = this;
12     var element = create_XUL(window, "arrowscrollbox");
13     element.setAttribute("id", "tab-bar");
14     element.setAttribute("orient", "horizontal");
15     var after = window.buffers.container;
16     this.window = window;
17     this.element = element;
18     after.parentNode.insertBefore(element, after);
20     add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
21     add_hook.call(window, "create_buffer_hook", tab_bar_add_buffer);
22     add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
23     add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
24     add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
25     add_hook.call(window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
26     window.buffers.for_each(tab_bar_add_buffer);
27     this.update_multiple_attribute();
28     if (window.buffers.current != null)
29         tab_bar_select_buffer(window.buffers.current);
31 tab_bar.prototype.destroy = function () {
32     remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
33     remove_hook.call(this.window, "create_buffer_hook", tab_bar_add_buffer);
34     remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
35     remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
36     remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
37     remove_hook.call(this.window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
38     this.window.buffers.for_each(function (b) {
39             delete b.tab;
40         });
41     this.selected_buffer = null;
42     this.element.parentNode.removeChild(this.element);
44 tab_bar.prototype.update_multiple_attribute = function () {
45     if (this.window.buffers.count > 1)
46         this.element.setAttribute("multiple", "true");
47     else
48         this.element.setAttribute("multiple", "false");
51 function tab_bar_add_buffer(b) {
52     var t = b.window.tab_bar;
53     t.update_multiple_attribute();
54     var tab = create_XUL(b.window, "hbox");
55     tab.setAttribute("class", "tab");
56     tab.addEventListener("click", function () {
57             if (!b.dead)
58                 b.window.buffers.current = b;
59         }, false /* not capturing */);
60     tab.setAttribute("selected", "false");
61     var image = create_XUL(b.window, "image");
62     image.setAttribute("class", "tab-icon");
63     if (b.favicon != null)
64         image.setAttribute("src", b.favicon);
65     var label = create_XUL(b.window, "label");
66     label.setAttribute("class", "tab-label");
67     label.setAttribute("crop", "end");
68     var button = create_XUL(b.window, "toolbarbutton");
69     button.setAttribute("class", "tab-close-button");
70     button.addEventListener("click", function (event) {
71             kill_buffer(b);
72             event.stopPropagation();
73         }, false /* not capturing */);
74     tab.appendChild(image);
75     tab.appendChild(label);
76     tab.appendChild(button);
77     tab.tab_label = label;
78     tab.tab_image = image;
79     t.element.appendChild(tab);
80     b.tab = tab;
81     tab_bar_update_buffer_title(b);
84 function tab_bar_kill_buffer(b) {
85     var t = b.window.tab_bar;
86     t.update_multiple_attribute();
87     if (t.selected_buffer == b)
88         t.selected_buffer = null;
89     b.tab.parentNode.removeChild(b.tab);
90     delete b.tab;
93 function tab_bar_select_buffer(b) {
94     var t = b.window.tab_bar;
95     if (t.selected_buffer != null)
96         t.selected_buffer.tab.setAttribute("selected", "false");
97     t.selected_buffer = b;
98     b.tab.setAttribute("selected", "true");
99     t.element.ensureElementIsVisible(b.tab);
102 function tab_bar_update_buffer_title(b) {
103     var title = b.title;
104     if (title == null || title.length == 0)
105         title = b.description;
106     b.tab.tab_label.setAttribute("value", title);
109 function tab_bar_update_buffer_icon(b) {
110     if (b.favicon != null)
111         b.tab.tab_image.setAttribute("src", b.favicon);
112     else
113         b.tab.tab_image.removeAttribute("src");
116 function tab_bar_install(window) {
117     if (window.tab_bar)
118         throw new Error("tab bar already initialized for window");
119     new tab_bar(window);
122 function tab_bar_uninstall(window) {
123     if (!window.tab_bar)
124         throw new Error("tab bar not initialized for window");
125     window.tab_bar.destroy();
126     delete window.tab_bar;
129 define_global_mode("tab_bar_mode",
130                    function () { // enable
131                        add_hook("window_initialize_hook", tab_bar_install);
132                        for_each_window(tab_bar_install);
133                    },
134                    function () { // disable
135                        remove_hook("window_initialize_hook", tab_bar_install);
136                        for_each_window(tab_bar_uninstall);
137                    });
139 tab_bar_mode(true);