Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: safari/ext/background.js

Issue 5099207639695360: Made browser actions per tab on Safari (Closed)
Patch Set: Forgot to set tab in BrowserAction constructor Created Jan. 17, 2014, 2:19 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 BrowserAction(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,101 @@
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(tab)
+ {
+ this._tab = tab;
+ };
+ 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;
+ }
+ },
+ 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);
+ }
+ };
+
+ 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 +618,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()
{
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld