eye-guide: whitespace, style
[conkeror.git] / modules / tab-bar.js
blob592c2140cfb176620fd7bcda948159e0dd538f35
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 in_module(null);
10 require("mode.js");
12 function tab_bar (window) {
13     window.tab_bar = this;
14     var element = create_XUL(window, "arrowscrollbox");
15     element.setAttribute("id", "tab-bar");
16     element.setAttribute("orient", "horizontal");
17     var after = window.buffers.container;
18     this.window = window;
19     this.element = element;
20     after.parentNode.insertBefore(element, after);
22     add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
23     add_hook.call(window, "create_buffer_early_hook", tab_bar_add_buffer);
24     add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
25     add_hook.call(window, "create_buffer_hook", tab_bar_update_buffer_title);
26     add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
27     add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
28     add_hook.call(window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
29     window.buffers.for_each(tab_bar_add_buffer);
30     this.update_multiple_attribute();
31     if (window.buffers.current != null)
32         tab_bar_select_buffer(window.buffers.current);
34 tab_bar.prototype.destroy = function () {
35     remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
36     remove_hook.call(this.window, "create_buffer_early_hook", tab_bar_add_buffer);
37     remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
38     remove_hook.call(this.window, "create_buffer_hook", tab_bar_update_buffer_title);
39     remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
40     remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
41     remove_hook.call(this.window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
42     this.window.buffers.for_each(function (b) {
43             delete b.tab;
44         });
45     this.selected_buffer = null;
46     this.element.parentNode.removeChild(this.element);
48 tab_bar.prototype.update_multiple_attribute = function () {
49     if (this.window.buffers.count > 1)
50         this.element.setAttribute("multiple", "true");
51     else
52         this.element.setAttribute("multiple", "false");
55 function tab_bar_add_buffer (b) {
56     var t = b.window.tab_bar;
57     t.update_multiple_attribute();
58     var tab = create_XUL(b.window, "hbox");
59     tab.setAttribute("class", "tab");
60     tab.addEventListener("click", function () {
61             if (!b.dead)
62                 b.window.buffers.current = b;
63         }, false /* not capturing */);
64     tab.setAttribute("selected", "false");
65     var image = create_XUL(b.window, "image");
66     image.setAttribute("class", "tab-icon");
67     if (b.favicon != null)
68         image.setAttribute("src", b.favicon);
69     var label = create_XUL(b.window, "label");
70     label.setAttribute("class", "tab-label");
71     label.setAttribute("crop", "end");
72     var button = create_XUL(b.window, "toolbarbutton");
73     button.setAttribute("class", "tab-close-button");
74     button.addEventListener("click", function (event) {
75             kill_buffer(b);
76             event.stopPropagation();
77         }, false /* not capturing */);
78     tab.appendChild(image);
79     tab.appendChild(label);
80     tab.appendChild(button);
81     tab.tab_label = label;
82     tab.tab_image = image;
83     t.element.appendChild(tab);
84     b.tab = tab;
85     tab_bar_update_buffer_title(b);
88 function tab_bar_kill_buffer (b) {
89     var t = b.window.tab_bar;
90     t.update_multiple_attribute();
91     if (t.selected_buffer == b)
92         t.selected_buffer = null;
93     b.tab.parentNode.removeChild(b.tab);
94     delete b.tab;
97 function tab_bar_select_buffer (b) {
98     var t = b.window.tab_bar;
99     if (t.selected_buffer != null)
100         t.selected_buffer.tab.setAttribute("selected", "false");
101     t.selected_buffer = b;
102     b.tab.setAttribute("selected", "true");
103     t.element.ensureElementIsVisible(b.tab);
106 function tab_bar_update_buffer_title (b) {
107     var title = b.title;
108     if (title == null || title.length == 0)
109         title = b.description;
110     b.tab.tab_label.setAttribute("value", title);
113 function tab_bar_update_buffer_icon (b) {
114     if (b.favicon != null)
115         b.tab.tab_image.setAttribute("src", b.favicon);
116     else
117         b.tab.tab_image.removeAttribute("src");
120 function tab_bar_install (window) {
121     if (window.tab_bar)
122         throw new Error("tab bar already initialized for window");
123     new tab_bar(window);
126 function tab_bar_uninstall (window) {
127     if (!window.tab_bar)
128         throw new Error("tab bar not initialized for window");
129     window.tab_bar.destroy();
130     delete window.tab_bar;
133 define_global_mode("tab_bar_mode",
134                    function () { // enable
135                        add_hook("window_initialize_hook", tab_bar_install);
136                        for_each_window(tab_bar_install);
137                    },
138                    function () { // disable
139                        remove_hook("window_initialize_hook", tab_bar_install);
140                        for_each_window(tab_bar_uninstall);
141                    });
143 tab_bar_mode(true);
145 provide("new-tabs");
146 provide("tab-bar.js");