whitespace
[conkeror/arlinius.git] / modules / tab-bar.js
blob1ae53053bd8fc82963c792e0815ed817fa281340
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_early_hook", tab_bar_add_buffer);
22     add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
23     add_hook.call(window, "create_buffer_hook", tab_bar_update_buffer_title);
24     add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
25     add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
26     add_hook.call(window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
27     window.buffers.for_each(tab_bar_add_buffer);
28     this.update_multiple_attribute();
29     if (window.buffers.current != null)
30         tab_bar_select_buffer(window.buffers.current);
32 tab_bar.prototype.destroy = function () {
33     remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
34     remove_hook.call(this.window, "create_buffer_early_hook", tab_bar_add_buffer);
35     remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
36     remove_hook.call(this.window, "create_buffer_hook", tab_bar_update_buffer_title);
37     remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
38     remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
39     remove_hook.call(this.window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
40     this.window.buffers.for_each(function (b) {
41             delete b.tab;
42         });
43     this.selected_buffer = null;
44     this.element.parentNode.removeChild(this.element);
46 tab_bar.prototype.update_multiple_attribute = function () {
47     if (this.window.buffers.count > 1)
48         this.element.setAttribute("multiple", "true");
49     else
50         this.element.setAttribute("multiple", "false");
53 function tab_bar_add_buffer (b) {
54     var t = b.window.tab_bar;
55     t.update_multiple_attribute();
56     var tab = create_XUL(b.window, "hbox");
57     tab.setAttribute("class", "tab");
58     tab.addEventListener("click", function () {
59             if (!b.dead)
60                 b.window.buffers.current = b;
61         }, false /* not capturing */);
62     tab.setAttribute("selected", "false");
63     var image = create_XUL(b.window, "image");
64     image.setAttribute("class", "tab-icon");
65     if (b.favicon != null)
66         image.setAttribute("src", b.favicon);
67     var label = create_XUL(b.window, "label");
68     label.setAttribute("class", "tab-label");
69     label.setAttribute("crop", "end");
70     var button = create_XUL(b.window, "toolbarbutton");
71     button.setAttribute("class", "tab-close-button");
72     button.addEventListener("click", function (event) {
73             kill_buffer(b);
74             event.stopPropagation();
75         }, false /* not capturing */);
76     tab.appendChild(image);
77     tab.appendChild(label);
78     tab.appendChild(button);
79     tab.tab_label = label;
80     tab.tab_image = image;
81     t.element.appendChild(tab);
82     b.tab = tab;
83     tab_bar_update_buffer_title(b);
86 function tab_bar_kill_buffer (b) {
87     var t = b.window.tab_bar;
88     t.update_multiple_attribute();
89     if (t.selected_buffer == b)
90         t.selected_buffer = null;
91     b.tab.parentNode.removeChild(b.tab);
92     delete b.tab;
95 function tab_bar_select_buffer (b) {
96     var t = b.window.tab_bar;
97     if (t.selected_buffer != null)
98         t.selected_buffer.tab.setAttribute("selected", "false");
99     t.selected_buffer = b;
100     b.tab.setAttribute("selected", "true");
101     t.element.ensureElementIsVisible(b.tab);
104 function tab_bar_update_buffer_title (b) {
105     var title = b.title;
106     if (title == null || title.length == 0)
107         title = b.description;
108     b.tab.tab_label.setAttribute("value", title);
111 function tab_bar_update_buffer_icon (b) {
112     if (b.favicon != null)
113         b.tab.tab_image.setAttribute("src", b.favicon);
114     else
115         b.tab.tab_image.removeAttribute("src");
118 function tab_bar_install (window) {
119     if (window.tab_bar)
120         throw new Error("tab bar already initialized for window");
121     new tab_bar(window);
124 function tab_bar_uninstall (window) {
125     if (!window.tab_bar)
126         throw new Error("tab bar not initialized for window");
127     window.tab_bar.destroy();
128     delete window.tab_bar;
131 define_global_mode("tab_bar_mode",
132                    function () { // enable
133                        add_hook("window_initialize_hook", tab_bar_install);
134                        for_each_window(tab_bar_install);
135                    },
136                    function () { // disable
137                        remove_hook("window_initialize_hook", tab_bar_install);
138                        for_each_window(tab_bar_uninstall);
139                    });
141 tab_bar_mode(true);