Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | var Appbar = { |
michael@0 | 7 | get starButton() { return document.getElementById('star-button'); }, |
michael@0 | 8 | get pinButton() { return document.getElementById('pin-button'); }, |
michael@0 | 9 | get menuButton() { return document.getElementById('menu-button'); }, |
michael@0 | 10 | |
michael@0 | 11 | // track selected/active richgrid/tilegroup - the context for contextual action buttons |
michael@0 | 12 | activeTileset: null, |
michael@0 | 13 | |
michael@0 | 14 | init: function Appbar_init() { |
michael@0 | 15 | // fired from appbar bindings |
michael@0 | 16 | Elements.contextappbar.addEventListener('MozAppbarShowing', this, false); |
michael@0 | 17 | Elements.contextappbar.addEventListener('MozAppbarDismissing', this, false); |
michael@0 | 18 | |
michael@0 | 19 | // fired when a context sensitive item (bookmarks) changes state |
michael@0 | 20 | window.addEventListener('MozContextActionsChange', this, false); |
michael@0 | 21 | |
michael@0 | 22 | // browser events we need to update button state on |
michael@0 | 23 | Elements.browsers.addEventListener('URLChanged', this, true); |
michael@0 | 24 | Elements.tabList.addEventListener('TabSelect', this, true); |
michael@0 | 25 | |
michael@0 | 26 | // tilegroup selection events for all modules get bubbled up |
michael@0 | 27 | window.addEventListener("selectionchange", this, false); |
michael@0 | 28 | Services.obs.addObserver(this, "metro_on_async_tile_created", false); |
michael@0 | 29 | |
michael@0 | 30 | // gather appbar telemetry data |
michael@0 | 31 | try { |
michael@0 | 32 | UITelemetry.addSimpleMeasureFunction("metro-appbar", |
michael@0 | 33 | this.getAppbarMeasures.bind(this)); |
michael@0 | 34 | } catch (ex) { |
michael@0 | 35 | // swallow exception that occurs if metro-appbar measure is already set up |
michael@0 | 36 | } |
michael@0 | 37 | }, |
michael@0 | 38 | |
michael@0 | 39 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 40 | switch (aTopic) { |
michael@0 | 41 | case "metro_on_async_tile_created": |
michael@0 | 42 | this._updatePinButton(); |
michael@0 | 43 | break; |
michael@0 | 44 | } |
michael@0 | 45 | }, |
michael@0 | 46 | |
michael@0 | 47 | handleEvent: function Appbar_handleEvent(aEvent) { |
michael@0 | 48 | switch (aEvent.type) { |
michael@0 | 49 | case 'URLChanged': |
michael@0 | 50 | case 'TabSelect': |
michael@0 | 51 | this.update(); |
michael@0 | 52 | this.flushActiveTileset(aEvent.lastTab); |
michael@0 | 53 | break; |
michael@0 | 54 | |
michael@0 | 55 | case 'MozAppbarShowing': |
michael@0 | 56 | this.update(); |
michael@0 | 57 | break; |
michael@0 | 58 | |
michael@0 | 59 | case 'MozAppbarDismissing': |
michael@0 | 60 | if (this.activeTileset && ('isBound' in this.activeTileset)) { |
michael@0 | 61 | this.activeTileset.clearSelection(); |
michael@0 | 62 | } |
michael@0 | 63 | this._clearContextualActions(); |
michael@0 | 64 | this.activeTileset = null; |
michael@0 | 65 | break; |
michael@0 | 66 | |
michael@0 | 67 | case 'MozContextActionsChange': |
michael@0 | 68 | let actions = aEvent.actions; |
michael@0 | 69 | let setName = aEvent.target.contextSetName; |
michael@0 | 70 | // could transition in old, new buttons? |
michael@0 | 71 | this.showContextualActions(actions, setName); |
michael@0 | 72 | break; |
michael@0 | 73 | |
michael@0 | 74 | case "selectionchange": |
michael@0 | 75 | let nodeName = aEvent.target.nodeName; |
michael@0 | 76 | if ('richgrid' === nodeName) { |
michael@0 | 77 | this._onTileSelectionChanged(aEvent); |
michael@0 | 78 | } |
michael@0 | 79 | break; |
michael@0 | 80 | } |
michael@0 | 81 | }, |
michael@0 | 82 | |
michael@0 | 83 | flushActiveTileset: function flushActiveTileset(aTab) { |
michael@0 | 84 | try { |
michael@0 | 85 | let tab = aTab || Browser.selectedTab; |
michael@0 | 86 | // Switching away from or loading a site into a startui tab that has actions |
michael@0 | 87 | // pending, we consider this confirmation that the user wants to flush changes. |
michael@0 | 88 | if (this.activeTileset && tab && tab.browser && tab.browser.currentURI.spec == kStartURI) { |
michael@0 | 89 | ContextUI.dismiss(); |
michael@0 | 90 | } |
michael@0 | 91 | } catch (ex) {} |
michael@0 | 92 | }, |
michael@0 | 93 | |
michael@0 | 94 | shutdown: function shutdown() { |
michael@0 | 95 | this.flushActiveTileset(); |
michael@0 | 96 | }, |
michael@0 | 97 | |
michael@0 | 98 | /* |
michael@0 | 99 | * Called from various places when the visible content |
michael@0 | 100 | * has changed such that button states may need to be |
michael@0 | 101 | * updated. |
michael@0 | 102 | */ |
michael@0 | 103 | update: function update() { |
michael@0 | 104 | this._updatePinButton(); |
michael@0 | 105 | this._updateStarButton(); |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | onPinButton: function() { |
michael@0 | 109 | if (this.pinButton.checked) { |
michael@0 | 110 | Browser.pinSite(); |
michael@0 | 111 | } else { |
michael@0 | 112 | Browser.unpinSite(); |
michael@0 | 113 | } |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | onStarButton: function(aValue) { |
michael@0 | 117 | if (aValue === undefined) { |
michael@0 | 118 | aValue = this.starButton.checked; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | if (aValue) { |
michael@0 | 122 | Browser.starSite(function () { |
michael@0 | 123 | Appbar._updateStarButton(); |
michael@0 | 124 | }); |
michael@0 | 125 | } else { |
michael@0 | 126 | Browser.unstarSite(function () { |
michael@0 | 127 | Appbar._updateStarButton(); |
michael@0 | 128 | }); |
michael@0 | 129 | } |
michael@0 | 130 | }, |
michael@0 | 131 | |
michael@0 | 132 | onMenuButton: function(aEvent) { |
michael@0 | 133 | let typesArray = []; |
michael@0 | 134 | |
michael@0 | 135 | if (BrowserUI.isPrivateBrowsingEnabled) { |
michael@0 | 136 | typesArray.push("private-browsing"); |
michael@0 | 137 | } |
michael@0 | 138 | if (!BrowserUI.isStartTabVisible) { |
michael@0 | 139 | typesArray.push("find-in-page"); |
michael@0 | 140 | if (ContextCommands.getPageSource()) |
michael@0 | 141 | typesArray.push("view-page-source"); |
michael@0 | 142 | } |
michael@0 | 143 | if (ContextCommands.getStoreLink()) |
michael@0 | 144 | typesArray.push("ms-meta-data"); |
michael@0 | 145 | if (ConsolePanelView.enabled) |
michael@0 | 146 | typesArray.push("open-error-console"); |
michael@0 | 147 | if (!Services.metro.immersive) |
michael@0 | 148 | typesArray.push("open-jsshell"); |
michael@0 | 149 | |
michael@0 | 150 | typesArray.push("view-on-desktop"); |
michael@0 | 151 | |
michael@0 | 152 | var x = this.menuButton.getBoundingClientRect().left; |
michael@0 | 153 | var y = Elements.toolbar.getBoundingClientRect().top; |
michael@0 | 154 | ContextMenuUI.showContextMenu({ |
michael@0 | 155 | json: { |
michael@0 | 156 | types: typesArray, |
michael@0 | 157 | string: '', |
michael@0 | 158 | xPos: x, |
michael@0 | 159 | yPos: y, |
michael@0 | 160 | leftAligned: true, |
michael@0 | 161 | bottomAligned: true |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | }); |
michael@0 | 165 | }, |
michael@0 | 166 | |
michael@0 | 167 | onViewOnDesktop: function() { |
michael@0 | 168 | let appStartup = Components.classes["@mozilla.org/toolkit/app-startup;1"]. |
michael@0 | 169 | getService(Components.interfaces.nsIAppStartup); |
michael@0 | 170 | |
michael@0 | 171 | Services.prefs.setBoolPref('browser.sessionstore.resume_session_once', true); |
michael@0 | 172 | this._incrementCountableEvent("switch-to-desktop-button"); |
michael@0 | 173 | appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit | |
michael@0 | 174 | Components.interfaces.nsIAppStartup.eRestart); |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | onAutocompleteCloseButton: function () { |
michael@0 | 178 | Elements.autocomplete.closePopup(); |
michael@0 | 179 | }, |
michael@0 | 180 | |
michael@0 | 181 | dispatchContextualAction: function(aActionName){ |
michael@0 | 182 | let activeTileset = this.activeTileset; |
michael@0 | 183 | if (activeTileset && ('isBound' in this.activeTileset)) { |
michael@0 | 184 | // fire event on the richgrid, others can listen |
michael@0 | 185 | // but we keep coupling loose so grid doesn't need to know about appbar |
michael@0 | 186 | let event = document.createEvent("Events"); |
michael@0 | 187 | event.action = aActionName; |
michael@0 | 188 | event.initEvent("context-action", true, true); // is cancelable |
michael@0 | 189 | activeTileset.dispatchEvent(event); |
michael@0 | 190 | if (!event.defaultPrevented) { |
michael@0 | 191 | activeTileset.clearSelection(); |
michael@0 | 192 | Elements.contextappbar.dismiss(); |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | }, |
michael@0 | 196 | |
michael@0 | 197 | showContextualActions: function(aVerbs, aSetName) { |
michael@0 | 198 | // When the appbar is not visible, we want the icons to refresh right away |
michael@0 | 199 | let immediate = !Elements.contextappbar.isShowing; |
michael@0 | 200 | |
michael@0 | 201 | if (aVerbs.length) { |
michael@0 | 202 | Elements.contextappbar.show(); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | // Look up all of the buttons for the verbs that should be visible. |
michael@0 | 206 | let idsToVisibleVerbs = new Map(); |
michael@0 | 207 | for (let verb of aVerbs) { |
michael@0 | 208 | let id = verb + "-selected-button"; |
michael@0 | 209 | if (!document.getElementById(id)) { |
michael@0 | 210 | throw new Error("Appbar.showContextualActions: no button for " + verb); |
michael@0 | 211 | } |
michael@0 | 212 | idsToVisibleVerbs.set(id, verb); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | // Sort buttons into 2 buckets - needing showing and needing hiding. |
michael@0 | 216 | let toHide = [], toShow = []; |
michael@0 | 217 | let buttons = Elements.contextappbar.getElementsByTagName("toolbarbutton"); |
michael@0 | 218 | for (let button of buttons) { |
michael@0 | 219 | let verb = idsToVisibleVerbs.get(button.id); |
michael@0 | 220 | if (verb != undefined) { |
michael@0 | 221 | // Button should be visible, and may or may not be showing. |
michael@0 | 222 | this._updateContextualActionLabel(button, verb, aSetName); |
michael@0 | 223 | if (button.hidden) { |
michael@0 | 224 | toShow.push(button); |
michael@0 | 225 | } |
michael@0 | 226 | } else if (!button.hidden) { |
michael@0 | 227 | // Button is visible, but shouldn't be. |
michael@0 | 228 | toHide.push(button); |
michael@0 | 229 | } |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | if (immediate) { |
michael@0 | 233 | toShow.forEach(function(element) { |
michael@0 | 234 | element.removeAttribute("fade"); |
michael@0 | 235 | element.hidden = false; |
michael@0 | 236 | }); |
michael@0 | 237 | toHide.forEach(function(element) { |
michael@0 | 238 | element.setAttribute("fade", true); |
michael@0 | 239 | element.hidden = true; |
michael@0 | 240 | }); |
michael@0 | 241 | return; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | return Task.spawn(function() { |
michael@0 | 245 | if (toHide.length) { |
michael@0 | 246 | yield Util.transitionElementVisibility(toHide, false); |
michael@0 | 247 | } |
michael@0 | 248 | if (toShow.length) { |
michael@0 | 249 | yield Util.transitionElementVisibility(toShow, true); |
michael@0 | 250 | } |
michael@0 | 251 | }); |
michael@0 | 252 | }, |
michael@0 | 253 | |
michael@0 | 254 | _clearContextualActions: function() { |
michael@0 | 255 | this.showContextualActions([]); |
michael@0 | 256 | }, |
michael@0 | 257 | |
michael@0 | 258 | _updateContextualActionLabel: function(aButton, aVerb, aSetName) { |
michael@0 | 259 | // True if the action's label string contains the set name and |
michael@0 | 260 | // thus has to be selected based on the list passed in. |
michael@0 | 261 | let usesSetName = aButton.hasAttribute("label-uses-set-name"); |
michael@0 | 262 | let name = "contextAppbar2." + aVerb + (usesSetName ? "." + aSetName : ""); |
michael@0 | 263 | aButton.label = Strings.browser.GetStringFromName(name); |
michael@0 | 264 | }, |
michael@0 | 265 | |
michael@0 | 266 | _onTileSelectionChanged: function _onTileSelectionChanged(aEvent){ |
michael@0 | 267 | let activeTileset = aEvent.target; |
michael@0 | 268 | |
michael@0 | 269 | // deselect tiles in other tile groups, |
michael@0 | 270 | // ensure previousyl-activeTileset is bound before calling methods on it |
michael@0 | 271 | if (this.activeTileset && |
michael@0 | 272 | ('isBound' in this.activeTileset) && |
michael@0 | 273 | this.activeTileset !== activeTileset) { |
michael@0 | 274 | this.activeTileset.clearSelection(); |
michael@0 | 275 | } |
michael@0 | 276 | // keep track of which view is the target/scope for the contextual actions |
michael@0 | 277 | this.activeTileset = activeTileset; |
michael@0 | 278 | |
michael@0 | 279 | // ask the view for the list verbs/action-names it thinks are |
michael@0 | 280 | // appropriate for the tiles selected |
michael@0 | 281 | let contextActions = activeTileset.contextActions; |
michael@0 | 282 | let verbs = [v for (v of contextActions)]; |
michael@0 | 283 | |
michael@0 | 284 | // fire event with these verbs as payload |
michael@0 | 285 | let event = document.createEvent("Events"); |
michael@0 | 286 | event.actions = verbs; |
michael@0 | 287 | event.initEvent("MozContextActionsChange", true, false); |
michael@0 | 288 | activeTileset.dispatchEvent(event); |
michael@0 | 289 | |
michael@0 | 290 | if (verbs.length) { |
michael@0 | 291 | Elements.contextappbar.show(); // should be no-op if we're already showing |
michael@0 | 292 | } else { |
michael@0 | 293 | Elements.contextappbar.dismiss(); |
michael@0 | 294 | } |
michael@0 | 295 | }, |
michael@0 | 296 | |
michael@0 | 297 | // track certain appbar events and interactions for the UITelemetry probe |
michael@0 | 298 | _countableEvents: {}, |
michael@0 | 299 | |
michael@0 | 300 | _incrementCountableEvent: function(aName) { |
michael@0 | 301 | if (!(aName in this._countableEvents)) { |
michael@0 | 302 | this._countableEvents[aName] = 0; |
michael@0 | 303 | } |
michael@0 | 304 | this._countableEvents[aName]++; |
michael@0 | 305 | }, |
michael@0 | 306 | |
michael@0 | 307 | getAppbarMeasures: function() { |
michael@0 | 308 | return { |
michael@0 | 309 | countableEvents: this._countableEvents |
michael@0 | 310 | }; |
michael@0 | 311 | }, |
michael@0 | 312 | |
michael@0 | 313 | _updatePinButton: function() { |
michael@0 | 314 | this.pinButton.checked = Browser.isSitePinned(); |
michael@0 | 315 | }, |
michael@0 | 316 | |
michael@0 | 317 | _updateStarButton: function() { |
michael@0 | 318 | Browser.isSiteStarredAsync(function (isStarred) { |
michael@0 | 319 | this.starButton.checked = isStarred; |
michael@0 | 320 | }.bind(this)); |
michael@0 | 321 | }, |
michael@0 | 322 | |
michael@0 | 323 | }; |