google-search-results.js: also add binding for return
[conkeror.git] / modules / tab-bar.js
blob834a49ec8c7cc084f558d594e638790761ae4494
1 require("mode.js");
3 function tab_bar(window) {
4     window.tab_bar = this;
5     var element = create_XUL(window, "arrowscrollbox");
6     element.setAttribute("id", "tab-bar");
7     element.setAttribute("orient", "horizontal");
8     var after = window.buffers.container;
9     this.window = window;
10     this.element = element;
11     after.parentNode.insertBefore(element, after);
13     add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
14     add_hook.call(window, "create_buffer_hook", tab_bar_add_buffer);
15     add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
16     add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
17     add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
18     add_hook.call(window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
19     window.buffers.for_each(tab_bar_add_buffer);
20     this.update_multiple_attribute();
21     if (window.buffers.current != null)
22         tab_bar_select_buffer(window.buffers.current);
24 tab_bar.prototype.destroy = function () {
25     remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
26     remove_hook.call(this.window, "create_buffer_hook", tab_bar_add_buffer);
27     remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
28     remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
29     remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
30     remove_hook.call(this.window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
31     this.window.buffers.for_each(function (b) {
32             delete b.tab;
33         });
34     this.selected_buffer = null;
35     this.element.parentNode.removeChild(this.element);
37 tab_bar.prototype.update_multiple_attribute = function () {
38     if (this.window.buffers.count > 1)
39         this.element.setAttribute("multiple", "true");
40     else
41         this.element.setAttribute("multiple", "false");
44 function tab_bar_add_buffer(b) {
45     var t = b.window.tab_bar;
46     t.update_multiple_attribute();
47     var tab = create_XUL(b.window, "hbox");
48     tab.setAttribute("class", "tab");
49     tab.addEventListener("click", function () {
50             if (!b.dead)
51                 b.window.buffers.current = b;
52         }, false /* not capturing */);
53     tab.setAttribute("selected", "false");
54     var image = create_XUL(b.window, "image");
55     image.setAttribute("class", "tab-icon");
56     if (b.favicon != null)
57         image.setAttribute("src", b.favicon);
58     var label = create_XUL(b.window, "label");
59     label.setAttribute("class", "tab-label");
60     label.setAttribute("crop", "end");
61     var button = create_XUL(b.window, "toolbarbutton");
62     button.setAttribute("class", "tab-close-button");
63     button.addEventListener("click", function (event) {
64             kill_buffer(b);
65             event.stopPropagation();
66         }, false /* not capturing */);
67     tab.appendChild(image);
68     tab.appendChild(label);
69     tab.appendChild(button);
70     tab.tab_label = label;
71     tab.tab_image = image;
72     t.element.appendChild(tab);
73     b.tab = tab;
74     tab_bar_update_buffer_title(b);
77 function tab_bar_kill_buffer(b) {
78     var t = b.window.tab_bar;
79     t.update_multiple_attribute();
80     if (t.selected_buffer == b)
81         t.selected_buffer = null;
82     b.tab.parentNode.removeChild(b.tab);
83     delete b.tab;
86 function tab_bar_select_buffer(b) {
87     var t = b.window.tab_bar;
88     if (t.selected_buffer != null)
89         t.selected_buffer.tab.setAttribute("selected", "false");
90     t.selected_buffer = b;
91     b.tab.setAttribute("selected", "true");
92     t.element.ensureElementIsVisible(b.tab);
95 function tab_bar_update_buffer_title(b) {
96     var title = b.title;
97     if (title == null || title.length == 0)
98         title = b.description;
99     b.tab.tab_label.setAttribute("value", title);
102 function tab_bar_update_buffer_icon(b) {
103     if (b.favicon != null)
104         b.tab.tab_image.setAttribute("src", b.favicon);
105     else
106         b.tab.tab_image.removeAttribute("src");
109 function tab_bar_install(window) {
110     if (window.tab_bar)
111         throw new Error("tab bar already initialized for window");
112     new tab_bar(window);
115 function tab_bar_uninstall(window) {
116     if (!window.tab_bar)
117         throw new Error("tab bar not initialized for window");
118     window.tab_bar.destroy();
119     delete window.tab_bar;
122 define_global_mode("tab_bar_mode",
123                    function () { // enable
124                        add_hook("window_initialize_hook", tab_bar_install);
125                        for_each_window(tab_bar_install);
126                    },
127                    function () { // disable
128                        remove_hook("window_initialize_hook", tab_bar_install);
129                        for_each_window(tab_bar_uninstall);
130                    });
132 tab_bar_mode(true);