OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 /* Tabs */ | 78 /* Tabs */ |
79 | 79 |
80 Tab = function(tab) | 80 Tab = function(tab) |
81 { | 81 { |
82 this._tab = tab; | 82 this._tab = tab; |
83 | 83 |
84 this.browserAction = new BrowserAction(this); | 84 this.browserAction = new BrowserAction(this); |
85 | 85 |
86 this.onLoading = new LoadingTabEventTarget(tab); | 86 this.onLoading = new LoadingTabEventTarget(tab); |
87 this.onBeforeNavigate = new TabEventTarget(tab, "beforeNavigate", false); | |
88 this.onCompleted = new TabEventTarget(tab, "navigate", false); | 87 this.onCompleted = new TabEventTarget(tab, "navigate", false); |
89 this.onActivated = new TabEventTarget(tab, "activate", false); | 88 this.onActivated = new TabEventTarget(tab, "activate", false); |
90 this.onRemoved = new TabEventTarget(tab, "close", false); | 89 this.onRemoved = new TabEventTarget(tab, "close", false); |
91 }; | 90 }; |
92 Tab.prototype = { | 91 Tab.prototype = { |
93 get url() | 92 get url() |
94 { | 93 { |
95 return this._tab.url; | 94 return this._tab.url; |
96 }, | 95 }, |
97 close: function() | 96 close: function() |
98 { | 97 { |
99 this._tab.close(); | 98 this._tab.close(); |
100 }, | 99 }, |
101 activate: function() | 100 activate: function() |
102 { | 101 { |
103 this._tab.activate(); | 102 this._tab.activate(); |
104 }, | 103 }, |
105 sendMessage: function(message, responseCallback) | 104 sendMessage: function(message, responseCallback) |
106 { | 105 { |
107 _sendMessage( | 106 _sendMessage( |
108 message, responseCallback, | 107 message, responseCallback, |
109 this._tab.page, this._tab | 108 this._tab.page, this._tab |
110 ); | 109 ); |
111 } | 110 } |
112 }; | 111 }; |
113 | 112 |
114 TabMap = function(deleteTabOnBeforeNavigate) | 113 TabMap = function(deleteOnPageUnload) |
115 { | 114 { |
116 this._tabs = []; | 115 this._data = []; |
117 this._values = []; | 116 this._deleteOnPageUnload = deleteOnPageUnload; |
118 | 117 |
119 this._deleteOnEvent = this._deleteOnEvent.bind(this); | 118 this.delete = this.delete.bind(this); |
120 this._deleteTabOnBeforeNavigate = deleteTabOnBeforeNavigate; | 119 this._delete = this._delete.bind(this); |
121 }; | 120 }; |
122 TabMap.prototype = | 121 TabMap.prototype = |
123 { | 122 { |
| 123 _indexOf: function(tab) |
| 124 { |
| 125 for (var i = 0; i < this._data.length; i++) |
| 126 if (this._data[i].tab._tab == tab._tab) |
| 127 return i; |
| 128 |
| 129 return -1; |
| 130 }, |
| 131 _delete: function(tab) |
| 132 { |
| 133 // delay so that other onClosed listeners can still look this tab up |
| 134 setTimeout(this.delete.bind(this, tab), 0); |
| 135 }, |
124 get: function(tab) { | 136 get: function(tab) { |
125 var idx; | 137 var idx; |
126 | 138 |
127 if (!tab || (idx = this._tabs.indexOf(tab._tab)) == -1) | 139 if (!tab || (idx = this._indexOf(tab)) == -1) |
128 return null; | 140 return null; |
129 | 141 |
130 return this._values[idx]; | 142 return this._data[idx].value; |
131 }, | 143 }, |
132 set: function(tab, value) | 144 set: function(tab, value) |
133 { | 145 { |
134 var idx = this._tabs.indexOf(tab._tab); | 146 var idx = this._indexOf(tab); |
135 | 147 |
136 if (idx != -1) | 148 if (idx != -1) |
137 this._values[idx] = value; | 149 this._data[idx].value = value; |
138 else | 150 else |
139 { | 151 { |
140 this._tabs.push(tab._tab); | 152 this._data.push({value: value, tab: tab}); |
141 this._values.push(value); | |
142 | 153 |
143 tab._tab.addEventListener("close", this._deleteOnEvent, false); | 154 tab.onRemoved.addListener(this._delete); |
144 if (this._deleteTabOnBeforeNavigate) | 155 if (this._deleteOnPageUnload) |
145 tab._tab.addEventListener("beforeNavigate", this._deleteOnEvent, false
); | 156 tab.onLoading.addListener(this.delete); |
146 } | 157 } |
147 }, | 158 }, |
148 has: function(tab) | 159 has: function(tab) |
149 { | 160 { |
150 return this._tabs.indexOf(tab._tab) != -1; | 161 return this._indexOf(tab) != -1; |
151 }, | 162 }, |
152 clear: function() | 163 clear: function() |
153 { | 164 { |
154 while (this._tabs.length > 0) | 165 while (this._data.length > 0) |
155 this._delete(this._tabs[0]); | 166 this.delete(this._data[0].tab); |
156 }, | |
157 _delete: function(tab) | |
158 { | |
159 var idx = this._tabs.indexOf(tab); | |
160 | |
161 if (idx != -1) | |
162 { | |
163 this._tabs.splice(idx, 1); | |
164 this._values.splice(idx, 1); | |
165 | |
166 tab.removeEventListener("close", this._deleteOnEvent, false); | |
167 tab.removeEventListener("beforeNavigate", this._deleteOnEvent, false); | |
168 } | |
169 }, | |
170 _deleteOnEvent: function(event) | |
171 { | |
172 // delay so that other event handlers can still look this tab up | |
173 setTimeout(this._delete.bind(this, event.target), 0); | |
174 } | 167 } |
175 }; | 168 }; |
176 TabMap.prototype["delete"] = function(tab) | 169 TabMap.prototype["delete"] = function(tab) |
177 { | 170 { |
178 this._delete(tab._tab); | 171 var idx = this._indexOf(tab); |
| 172 |
| 173 if (idx != -1) |
| 174 { |
| 175 tab = this._data[idx].tab; |
| 176 this._data.splice(idx, 1); |
| 177 |
| 178 tab.onRemoved.removeListener(this._delete); |
| 179 tab.onLoading.removeListener(this.delete); |
| 180 } |
179 }; | 181 }; |
180 | 182 |
181 ext.tabs = { | 183 ext.tabs = { |
182 onLoading: new LoadingTabEventTarget(safari.application), | 184 onLoading: new LoadingTabEventTarget(safari.application), |
183 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t
rue), | |
184 onCompleted: new TabEventTarget(safari.application, "navigate", true), | 185 onCompleted: new TabEventTarget(safari.application, "navigate", true), |
185 onActivated: new TabEventTarget(safari.application, "activate", true), | 186 onActivated: new TabEventTarget(safari.application, "activate", true), |
186 onRemoved: new TabEventTarget(safari.application, "close", true) | 187 onRemoved: new TabEventTarget(safari.application, "close", true) |
187 }; | 188 }; |
188 | 189 |
189 | 190 |
190 /* Browser actions */ | 191 /* Browser actions */ |
191 | 192 |
192 var toolbarItemProperties = {}; | 193 var toolbarItemProperties = {}; |
193 | 194 |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 for (var i = 0; i < contextMenuItems.length; i++) | 706 for (var i = 0; i < contextMenuItems.length; i++) |
706 { | 707 { |
707 if (contextMenuItems[i].id == event.command) | 708 if (contextMenuItems[i].id == event.command) |
708 { | 709 { |
709 contextMenuItems[i].onclick(event.userInfo.srcUrl, new Tab(safari.applic
ation.activeBrowserWindow.activeTab)); | 710 contextMenuItems[i].onclick(event.userInfo.srcUrl, new Tab(safari.applic
ation.activeBrowserWindow.activeTab)); |
710 break; | 711 break; |
711 } | 712 } |
712 } | 713 } |
713 }, false); | 714 }, false); |
714 })(); | 715 })(); |
OLD | NEW |