2 * (C) Copyright 2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2008 Deniz Dogan
5 * Use, modification, and distribution are subject to the terms specified in the
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.
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;
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) {
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");
55 this.element.setAttribute("multiple", "false");
58 function tab_bar_add_buffer(buffer) {
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 () {
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
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
88 event.stopPropagation();
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);
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;
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;
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) {
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) {
141 throw new Error("tab bar already initialized for window");
145 function tab_bar_uninstall(window) {
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);
157 function () { // disable
158 remove_hook("window_initialize_hook", tab_bar_install);
159 for_each_window(tab_bar_uninstall);