buffer ordering
[conkeror.git] / modules / new-tabs.js
blob42006a4c065706997fb10fed0dfa499b859ced05
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Nicholas A. Zigarovich
4  * (C) Copyright 2008 John J. Foerch
5  * (C) Copyright 2011 Peter Lunicks
6  *
7  * Use, modification, and distribution are subject to the terms specified in the
8  * COPYING file.
9  *
10  * This is a tab bar which is based on tab-bar.js but makes customization of the
11  * tabs much easier. It provides sensible default styles but lets the user
12  * simply override these defaults by normal CSS.
13  **/
15 in_module(null);
17 require("mode.js");
19 define_variable("tab_bar_button_select", 0,
20                 "The mouse button that selects tabs." +
21                 "0 = left, 1 = middle, 2 = right, null = disabled.");
23 define_variable("tab_bar_button_close", 2,
24                 "The mouse button that closes tabs." +
25                 "0 = left, 1 = middle, 2 = right, null = disabled.");
27 define_variable("tab_bar_show_icon", false,
28                 "Whether or not to show buffer icons in tabs.");
30 define_variable("tab_bar_show_index", true,
31                 "Whether or not to show the tab index in each tab.");
33 /**
34  * Constructs a tab bar for the given window.
35  */
36 function tab_bar (window) {
37     window.tab_bar = this;
38     var scrollbox = create_XUL(window, "arrowscrollbox");
39     scrollbox.setAttribute("id", "tab2-bar");
40     scrollbox.setAttribute("orient", "horizontal");
41     var after = window.buffers.container;
42     this.window = window;
43     this.element = scrollbox;
44     after.parentNode.insertBefore(scrollbox, after);
46     add_hook.call(window, "select_buffer_hook", tab_bar_select_buffer);
47     add_hook.call(window, "create_buffer_early_hook", tab_bar_add_buffer);
48     add_hook.call(window, "kill_buffer_hook", tab_bar_kill_buffer);
49     add_hook.call(window, "move_buffer_hook", tab_bar_move_buffer);
50     add_hook.call(window, "create_buffer_hook", tab_bar_update_buffer_title);
51     add_hook.call(window, "buffer_title_change_hook", tab_bar_update_buffer_title);
52     add_hook.call(window, "buffer_description_change_hook", tab_bar_update_buffer_title);
53     add_hook.call(window, "buffer_icon_change_hook", tab_bar_update_buffer_icon);
55     window.buffers.for_each(function (b) tab_bar_add_buffer(b, true));
56     this.update_multiple_attribute();
57     if (window.buffers.current != null)
58         tab_bar_select_buffer(window.buffers.current);
62 /**
63  * Destroys the tab bar.
64  */
65 tab_bar.prototype.destroy = function () {
66     remove_hook.call(this.window, "select_buffer_hook", tab_bar_select_buffer);
67     remove_hook.call(this.window, "create_buffer_early_hook", tab_bar_add_buffer);
68     remove_hook.call(this.window, "kill_buffer_hook", tab_bar_kill_buffer);
69     remove_hook.call(this.window, "move_buffer_hook", tab_bar_move_buffer);
70     remove_hook.call(this.window, "create_buffer_hook", tab_bar_update_buffer_title);
71     remove_hook.call(this.window, "buffer_title_change_hook", tab_bar_update_buffer_title);
72     remove_hook.call(this.window, "buffer_description_change_hook", tab_bar_update_buffer_title);
73     remove_hook.call(this.window, "buffer_icon_change_hook", tab_bar_update_buffer_icon);
74     this.window.buffers.for_each(function (b) { delete b.tab; });
75     this.selected_buffer = null;
76     this.element.parentNode.removeChild(this.element);
77     delete this.window.tab_bar;
81 /**
82  * Updates the "index" node and "ordinal" attribute of all tabs.
83  */
84 tab_bar.prototype.update_ordinals = function () {
85     var buffers = this.window.buffers;
86     for (var i = 0, n = this.element.childNodes.length; i < n; i++) {
87         var ordinal = buffers.index_of(this.element.childNodes[i].buffer) + 1;
88         this.element.childNodes[i].setAttribute("ordinal", ordinal);
89         this.element.childNodes[i].index.setAttribute("value", ordinal);
90     }
94 /**
95  * Updates the "multiple" attribute of the tab bar.
96  */
97 tab_bar.prototype.update_multiple_attribute = function () {
98     if (this.window.buffers.count > 1)
99         this.element.setAttribute("multiple", "true");
100     else
101         this.element.setAttribute("multiple", "false");
106  * Adds a tab for the given buffer.  When second argument 'noupdate' is
107  * true, a new tab in the middle of the buffer list will not cause the
108  * ordinals of other tabs to be updated.  This is used during
109  * initialization of the tab bar.
110  */
111 function tab_bar_add_buffer (buffer, noupdate) {
113     // Get the tab bar
114     var tabbar = buffer.window.tab_bar;
115     tabbar.update_multiple_attribute();
117     var ordinal = buffer.window.buffers.index_of(buffer) + 1;
118     if (ordinal < buffer.window.buffers.buffer_list.length && ! noupdate)
119         tabbar.update_ordinals();
121     // Create a tab and add it to the tab bar
122     var tab = create_XUL(buffer.window, "hbox");
123     tab.buffer = buffer;
124     tab.setAttribute("class", "tab2");
125     tab.addEventListener("click", function (event) {
126             if (event.button == tab_bar_button_select) {
127                 if (!tab.buffer.dead)
128                     tab.buffer.window.buffers.current = tab.buffer;
129             }
130         }, false /* not capturing */);
131     tab.setAttribute("selected", "false");
132     tab.setAttribute("ordinal", ordinal);
134     // Create the label to hold the buffer icon
135     var image = create_XUL(buffer.window, "image");
136     image.setAttribute("class", "tab2-icon");
137     if (buffer.icon != null)
138         image.setAttribute("src", buffer.icon);
140     // Create the label to hold the tab number
141     var index = create_XUL(buffer.window, "label");
142     index.setAttribute("class", "tab2-index");
143     index.setAttribute("value", ordinal);
145     // Create the label to hold the tab title
146     var label = create_XUL(buffer.window, "label");
147     label.setAttribute("class", "tab2-label");
148     label.setAttribute("crop", "end");
150     // No close button, just use the designated mouse button.
151     tab.addEventListener("click", function (event) {
152             if (event.button == tab_bar_button_close) {
153                 kill_buffer(tab.buffer);
154                 event.stopPropagation();
155             }
156         }, false /* not capturing */);
158     // Add all the stuff to the new tab
159     tab.image = image;
160     tab.label = label;
161     tab.index = index;
162     if (tab_bar_show_index)
163         tab.appendChild(index);
164     if (tab_bar_show_icon)
165         tab.appendChild(image);
166     tab.appendChild(label);
167     tabbar.element.appendChild(tab);
168     buffer.tab = tab;
169     tab_bar_update_buffer_title(buffer);
174  * Removes the tab for the given buffer.
175  */
176 function tab_bar_kill_buffer (b) {
177     var t = b.window.tab_bar;
178     t.update_multiple_attribute();
179     if (t.selected_buffer == b)
180         t.selected_buffer = null;
181     b.tab.parentNode.removeChild(b.tab);
182     t = b.window.tab_bar;
183     delete b.tab;
185     // Renumber the tabs.
186     t.update_ordinals();
191  * Updates all tab indices and ensure that the current tab is still visible.
192  */
193 function tab_bar_move_buffer (b) {
194     var t = b.window.tab_bar;
195     t.update_ordinals();
196     t.element.ensureElementIsVisible(b.window.buffers.current.tab);
201  * Updates the tab of the given buffer to indicate it is the currently open one.
202  */
203 function tab_bar_select_buffer (b) {
204     var t = b.window.tab_bar;
205     if (t.selected_buffer != null)
206         t.selected_buffer.tab.setAttribute("selected", "false");
207     t.selected_buffer = b;
208     b.tab.setAttribute("selected", "true");
209     t.element.ensureElementIsVisible(b.tab);
214  * Updates the tab title for the given buffer.
215  */
216 function tab_bar_update_buffer_title (b) {
217     var title = b.title;
218     if (title == null || title.length == 0)
219         title = b.description;
220     b.tab.label.setAttribute("value", title);
225  * Updates the tab icon for the given buffer.
226  */
227 function tab_bar_update_buffer_icon (b) {
228     if (b.icon != null)
229         b.tab.image.setAttribute("src", b.icon);
230     else
231         b.tab.image.removeAttribute("src");
236  * Inserts the tab bar in the given window.
237  */
238 function tab_bar_install (window) {
239     if (window.tab_bar)
240         throw new Error("tab bar already initialized for window");
241     new tab_bar(window);
246  * Removes the tab bar from the given window.
247  * If the tab bar is not installed, throws an error.
248  */
249 function tab_bar_uninstall (window) {
250     if (!window.tab_bar)
251         throw new Error("tab bar not initialized for window");
252     window.tab_bar.destroy();
256 define_global_mode("tab_bar_mode",
257                    function () { // enable
258                        add_hook("window_initialize_hook", tab_bar_install);
259                        for_each_window(tab_bar_install);
260                    },
261                    function () { // disable
262                        remove_hook("window_initialize_hook", tab_bar_install);
263                        for_each_window(tab_bar_uninstall);
264                    });
266 tab_bar_mode(true);
268 provide("tab-bar");
269 provide("new-tabs");