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: Rebased, addressed comment and enabled to set global browser actions Created Jan. 15, 2014, 7: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
« chrome/ext/background.js ('K') | « chrome/ext/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 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,114 @@
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 toolbarItemProperites = {};
Wladimir Palant 2014/01/16 09:10:05 toolbarItemProperites => toolbarItemProperties?
Sebastian Noack 2014/01/16 12:45:02 Done.
+
+ var getToolbarItemProperty = function(name)
+ {
+ var property = toolbarItemProperites[name];
+ if (!property)
+ {
+ property = {global: safari.extension.toolbarItems[0][name], tabs: new TabMap()};
Wladimir Palant 2014/01/16 09:10:05 Will there always be at least one toolbar item? Wh
Sebastian Noack 2014/01/16 12:45:02 Done.
+ toolbarItemProperites[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)
+ {
+ getToolbarItemProperty(name).tabs.set(this._tab, value);
+
+ var currentWindow = this._tab._tab.browserWindow;
+ var toolbarItem = getToolbarItemForWindow(currentWindow);
+ if (toolbarItem && this._tab._tab == currentWindow.activeTab)
Wladimir Palant 2014/01/16 09:10:05 Nit: It seems that |this._tab._tab == currentWindo
+ toolbarItem[name] = value;
+ }
+ };
+
+ ext.tabs.onActivated.addListener(function(tab)
+ {
+ var toolbarItem = getToolbarItemForWindow(tab._tab.browserWindow);
+
+ if (!toolbarItem)
+ return;
+
+ for (var name in toolbarItemProperites)
+ {
+ var property = toolbarItemProperites[name];
+
+ if (property.tabs.has(tab))
+ toolbarItem[name] = property.tabs.get(tab);
+ else
+ toolbarItem[name] = property.global;
+ }
+ });
+
/* Windows */
@@ -538,14 +631,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()
{
« chrome/ext/background.js ('K') | « chrome/ext/background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld