2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
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;
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) {
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");
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 () {
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) {
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);
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);
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) {
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);
117 b.tab.tab_image.removeAttribute("src");
120 function tab_bar_install (window) {
122 throw new Error("tab bar already initialized for window");
126 function tab_bar_uninstall (window) {
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);
138 function () { // disable
139 remove_hook("window_initialize_hook", tab_bar_install);
140 for_each_window(tab_bar_uninstall);
146 provide("tab-bar.js");