Index: safari/ext/background.js |
=================================================================== |
--- a/safari/ext/background.js |
+++ b/safari/ext/background.js |
@@ -81,6 +81,8 @@ |
{ |
this._tab = tab; |
+ this.browserAction = new TabBrowserAction(this); |
+ |
this.onLoading = new LoadingTabEventTarget(tab); |
this.onBeforeNavigate = new TabEventTarget(tab, "beforeNavigate", false); |
this.onCompleted = new TabEventTarget(tab, "navigate", false); |
@@ -106,23 +108,6 @@ |
message, responseCallback, |
this._tab.page, this._tab |
); |
- }, |
- browserAction: { |
- setIcon: function(path) |
- { |
- safari.extension.toolbarItems[0].image = safari.extension.baseURI + path; |
- }, |
- setTitle: function(title) |
- { |
- safari.extension.toolbarItems[0].toolTip = title; |
- }, |
- setBadge: function(badge) |
- { |
- if (!badge) |
- safari.extension.toolbarItems[0].badge = 0; |
- else if ("number" in badge) |
- safari.extension.toolbarItems[0].badge = badge.number; |
- } |
} |
}; |
@@ -193,6 +178,122 @@ |
this._delete(tab._tab); |
}; |
+ ext.tabs = { |
+ onLoading: new LoadingTabEventTarget(safari.application), |
+ onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", true), |
+ onCompleted: new TabEventTarget(safari.application, "navigate", true), |
+ onActivated: new TabEventTarget(safari.application, "activate", true), |
+ onRemoved: new TabEventTarget(safari.application, "close", true) |
+ }; |
+ |
+ |
+ /* Browser actions */ |
+ |
+ var toolbarItemProperties = {}; |
+ |
+ var getToolbarItemProperty = function(name) |
+ { |
+ var property = toolbarItemProperties[name]; |
+ if (!property) |
+ { |
+ property = {tabs: new TabMap()}; |
+ toolbarItemProperties[name] = property; |
+ } |
+ return property; |
+ }; |
+ |
+ var getToolbarItemForWindow = function(win) |
+ { |
+ for (var i = 0; i < safari.extension.toolbarItems.length; i++) |
+ { |
+ var toolbarItem = safari.extension.toolbarItems[i]; |
+ |
+ if (toolbarItem.browserWindow == win) |
+ return toolbarItem; |
+ } |
+ |
+ return null; |
+ }; |
+ |
+ var BrowserAction = function() {}; |
+ BrowserAction.prototype = { |
+ setIcon: function(path) |
+ { |
+ this._set("image", safari.extension.baseURI + path); |
+ }, |
+ setTitle: function(title) |
+ { |
+ this._set("label", title); |
+ this._set("toolTip", title); |
+ }, |
+ setBadge: function(badge) |
+ { |
+ if (!badge) |
+ this._set("badge", 0); |
+ else if ("number" in badge) |
+ this._set("badge", badge.number); |
+ } |
+ }; |
+ |
+ var GlobalBrowserAction = function() {}; |
+ GlobalBrowserAction.prototype = { |
+ __proto__: BrowserAction.prototype, |
+ _set: function(name, value) |
+ { |
+ var property = getToolbarItemProperty(name); |
+ property.global = value; |
+ property.tabs.clear(); |
+ |
+ for (var i = 0; i < safari.extension.toolbarItems.length; i++) |
+ safari.extension.toolbarItems[i][name] = value; |
+ } |
+ }; |
+ |
+ ext.browserAction = new GlobalBrowserAction(); |
+ |
+ var TabBrowserAction = function(tab) |
+ { |
+ this._tab = tab; |
+ }; |
+ TabBrowserAction.prototype = { |
+ __proto__: BrowserAction.prototype, |
+ _set: function(name, value) |
+ { |
+ var currentWindow = this._tab._tab.browserWindow; |
+ var toolbarItem = getToolbarItemForWindow(currentWindow); |
+ |
+ if (toolbarItem) |
+ { |
+ var property = getToolbarItemProperty(name); |
+ property.tabs.set(this._tab, value); |
+ |
+ if (!("global" in property)) |
+ property.global = toolbarItem[name]; |
+ |
+ if (this._tab._tab == currentWindow.activeTab) |
+ toolbarItem[name] = value; |
+ } |
+ } |
+ }; |
+ |
+ ext.tabs.onActivated.addListener(function(tab) |
+ { |
+ var toolbarItem = getToolbarItemForWindow(tab._tab.browserWindow); |
+ |
+ if (!toolbarItem) |
+ return; |
+ |
+ for (var name in toolbarItemProperties) |
+ { |
+ var property = toolbarItemProperties[name]; |
+ |
+ if (property.tabs.has(tab)) |
+ toolbarItem[name] = property.tabs.get(tab); |
+ else |
+ toolbarItem[name] = property.global; |
+ } |
+ }); |
+ |
/* Windows */ |
@@ -538,14 +639,6 @@ |
} |
}; |
- ext.tabs = { |
- onLoading: new LoadingTabEventTarget(safari.application), |
- onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", true), |
- onCompleted: new TabEventTarget(safari.application, "navigate", true), |
- onActivated: new TabEventTarget(safari.application, "activate", true), |
- onRemoved: new TabEventTarget(safari.application, "close", true) |
- }; |
- |
ext.backgroundPage = { |
getWindow: function() |
{ |