Add facility to allow user functions to transform typed URLs.
[conkeror.git] / modules / new-tabs.js
blob3e8d424f21376b7ef974a47bc5c36aba52deb180
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Deniz Dogan
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7  *
8  * This is a tab bar which is based on tab-bar.js but makes customization of the
9  * tabs much easier. It provides sensible default styles but lets the user
10  * simply override these defaults by normal CSS.
11  **/
13 require("mode.js");
15 function tab_bar(window) {
16     window.tab_bar = this;
17     var scrollbox = create_XUL(window, "arrowscrollbox");
18     scrollbox.setAttribute("id", "tab2-bar");
19     scrollbox.setAttribute("orient", "horizontal");
20     var after = window.buffers.container;
21     this.window = window;
22     this.element = scrollbox;
23     after.parentNode.insertBefore(scrollbox, after);
25     add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
26     add_hook.call(window, "create_buffer_hook", tab_bar_add_buffer);
27     add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
28     add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
29     add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
30     //    add_hook.call(window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
31     window.buffers.for_each(tab_bar_add_buffer);
32     this.update_multiple_attribute();
33     if (window.buffers.current != null)
34         tab_bar_select_buffer(window.buffers.current);
37 tab_bar.prototype.destroy = function () {
38     remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
39     remove_hook.call(this.window, "create_buffer_hook", tab_bar_add_buffer);
40     remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
41     remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
42     remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
43     //    remove_hook.call(this.window, "buffer_favicon_change_hook", tab_bar_update_buffer_icon);
44     this.window.buffers.for_each(function (b) {
45             delete b.tab;
46         });
47     this.selected_buffer = null;
48     this.element.parentNode.removeChild(this.element);
51 tab_bar.prototype.update_multiple_attribute = function () {
52     if (this.window.buffers.count > 1)
53         this.element.setAttribute("multiple", "true");
54     else
55         this.element.setAttribute("multiple", "false");
58 function tab_bar_add_buffer(buffer) {
60     // Get the tab bar
61     var tabbar = buffer.window.tab_bar;
62     tabbar.update_multiple_attribute();
64     // Create a tab and add it to the tab bar
65     var tab = create_XUL(buffer.window, "hbox");
66     tab.setAttribute("class", "tab2");
67     tab.addEventListener("click", function () {
68             if (!buffer.dead)
69                 buffer.window.buffers.current = buffer;
70         }, false /* not capturing */);
71     tab.setAttribute("selected", "false");
73     // Create the label to hold the tab number
74     // TODO: Make the numbers optional and use the favicon if that's what the
75     // user wants.
76     var iconlabel = create_XUL(buffer.window, "label");
77     iconlabel.setAttribute("class", "tab2-icon");
79     // Create the label to hold the tab title
80     var label = create_XUL(buffer.window, "label");
81     label.setAttribute("class", "tab2-label");
82     label.setAttribute("crop", "end");
84     // No close button, just use right-click
85     tab.addEventListener("click", function (event) {
86             if (event.button == 2) { // Right mouse button
87                 kill_buffer(buffer);
88                 event.stopPropagation();
89             }
90         }, false /* not capturing */);
92     // Add all the stuff to the new tab
93     tab.appendChild(iconlabel);
94     tab.appendChild(label);
95     tab.tab_label = label;
96     tab.tab_icon = iconlabel;
97     tabbar.element.appendChild(tab);
98     buffer.tab = tab;
99     tab_bar_update_buffer_title(buffer);
101     // Set the tab number. Remember that at this point, the tab has already been
102     // added to the hbox.
103     var total = tabbar.element.getElementsByClassName("tab2").length;
104     iconlabel.value = total;
107 function tab_bar_kill_buffer(b) {
108     var t = b.window.tab_bar;
109     t.update_multiple_attribute();
110     if (t.selected_buffer == b)
111         t.selected_buffer = null;
112     b.tab.parentNode.removeChild(b.tab);
113     t = b.window.tab_bar;
114     delete b.tab;
116     // Renumber the tabs.
117     for (var i = 0; i < t.element.childNodes.length; i++) {
118         t.element.childNodes[i].childNodes[0].value = i + 1;
119     }
122 function tab_bar_select_buffer(b) {
124     var t = b.window.tab_bar;
125     if (t.selected_buffer != null)
126         t.selected_buffer.tab.setAttribute("selected", "false");
127     t.selected_buffer = b;
128     b.tab.setAttribute("selected", "true");
129     t.element.ensureElementIsVisible(b.tab);
132 function tab_bar_update_buffer_title(b) {
133     var title = b.title;
134     if (title == null || title.length == 0)
135         title = b.description;
136     b.tab.tab_label.setAttribute("value", title);
139 function tab_bar_install(window) {
140     if (window.tab_bar)
141         throw new Error("tab bar already initialized for window");
142     new tab_bar(window);
145 function tab_bar_uninstall(window) {
146     if (!window.tab_bar)
147         throw new Error("tab bar not initialized for window");
148     window.tab_bar.destroy();
149     delete window.tab_bar;
152 define_global_mode("tab_bar_mode",
153                    function () { // enable
154                        add_hook("window_initialize_hook", tab_bar_install);
155                        for_each_window(tab_bar_install);
156                    },
157                    function () { // disable
158                        remove_hook("window_initialize_hook", tab_bar_install);
159                        for_each_window(tab_bar_uninstall);
160                    });
162 tab_bar_mode(true);