2013-05-07 18:38:08 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
|
2013-02-12 20:51:25 +00:00
|
|
|
var Appbar = {
|
|
|
|
get starButton() { return document.getElementById('star-button'); },
|
|
|
|
get pinButton() { return document.getElementById('pin-button'); },
|
2013-06-14 23:29:12 +00:00
|
|
|
get menuButton() { return document.getElementById('menu-button'); },
|
2013-02-12 20:51:25 +00:00
|
|
|
|
|
|
|
// track selected/active richgrid/tilegroup - the context for contextual action buttons
|
|
|
|
activeTileset: null,
|
|
|
|
|
|
|
|
init: function Appbar_init() {
|
2013-06-28 00:37:11 +00:00
|
|
|
// fired from appbar bindings
|
2013-05-16 00:21:06 +00:00
|
|
|
Elements.contextappbar.addEventListener('MozAppbarShowing', this, false);
|
|
|
|
Elements.contextappbar.addEventListener('MozAppbarDismissing', this, false);
|
2013-06-28 00:37:11 +00:00
|
|
|
|
|
|
|
// fired when a context sensitive item (bookmarks) changes state
|
2013-04-03 21:16:14 +00:00
|
|
|
window.addEventListener('MozContextActionsChange', this, false);
|
2013-06-28 00:37:11 +00:00
|
|
|
|
|
|
|
// browser events we need to update button state on
|
2013-04-04 16:18:10 +00:00
|
|
|
Elements.browsers.addEventListener('URLChanged', this, true);
|
|
|
|
Elements.tabList.addEventListener('TabSelect', this, true);
|
2013-02-12 20:51:25 +00:00
|
|
|
|
|
|
|
// tilegroup selection events for all modules get bubbled up
|
|
|
|
window.addEventListener("selectionchange", this, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function Appbar_handleEvent(aEvent) {
|
|
|
|
switch (aEvent.type) {
|
2013-05-31 15:55:53 +00:00
|
|
|
case 'URLChanged':
|
|
|
|
case 'TabSelect':
|
2013-08-09 09:50:59 +00:00
|
|
|
this.update();
|
2013-08-22 21:42:54 +00:00
|
|
|
this.flushActiveTileset(aEvent.lastTab);
|
2013-08-09 09:50:59 +00:00
|
|
|
break;
|
|
|
|
|
2013-04-04 16:18:06 +00:00
|
|
|
case 'MozAppbarShowing':
|
2013-06-28 00:37:11 +00:00
|
|
|
this.update();
|
2013-02-12 20:51:25 +00:00
|
|
|
break;
|
2013-06-28 00:37:11 +00:00
|
|
|
|
2013-05-07 18:38:08 +00:00
|
|
|
case 'MozAppbarDismissing':
|
2013-07-30 19:16:58 +00:00
|
|
|
if (this.activeTileset && ('isBound' in this.activeTileset)) {
|
2013-05-07 18:38:08 +00:00
|
|
|
this.activeTileset.clearSelection();
|
|
|
|
}
|
2013-08-02 19:47:22 +00:00
|
|
|
this._clearContextualActions();
|
2013-05-07 18:38:08 +00:00
|
|
|
this.activeTileset = null;
|
|
|
|
break;
|
2013-06-28 00:37:11 +00:00
|
|
|
|
2013-04-03 21:16:14 +00:00
|
|
|
case 'MozContextActionsChange':
|
|
|
|
let actions = aEvent.actions;
|
2013-07-19 01:28:24 +00:00
|
|
|
let setName = aEvent.target.contextSetName;
|
2013-04-03 21:16:14 +00:00
|
|
|
// could transition in old, new buttons?
|
2013-07-19 01:28:24 +00:00
|
|
|
this.showContextualActions(actions, setName);
|
2013-04-03 21:16:14 +00:00
|
|
|
break;
|
2013-06-28 00:37:11 +00:00
|
|
|
|
2013-02-12 20:51:25 +00:00
|
|
|
case "selectionchange":
|
|
|
|
let nodeName = aEvent.target.nodeName;
|
|
|
|
if ('richgrid' === nodeName) {
|
|
|
|
this._onTileSelectionChanged(aEvent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-22 21:42:54 +00:00
|
|
|
flushActiveTileset: function flushActiveTileset(aTab) {
|
|
|
|
try {
|
|
|
|
let tab = aTab || Browser.selectedTab;
|
|
|
|
// Switching away from or loading a site into a startui tab that has actions
|
|
|
|
// pending, we consider this confirmation that the user wants to flush changes.
|
|
|
|
if (this.activeTileset && tab && tab.browser && tab.browser.currentURI.spec == kStartURI) {
|
|
|
|
ContextUI.dismiss();
|
|
|
|
}
|
|
|
|
} catch (ex) {}
|
|
|
|
},
|
|
|
|
|
|
|
|
shutdown: function shutdown() {
|
|
|
|
this.flushActiveTileset();
|
|
|
|
},
|
|
|
|
|
2013-05-31 15:55:53 +00:00
|
|
|
/*
|
|
|
|
* Called from various places when the visible content
|
|
|
|
* has changed such that button states may need to be
|
|
|
|
* updated.
|
|
|
|
*/
|
|
|
|
update: function update() {
|
|
|
|
this._updatePinButton();
|
|
|
|
this._updateStarButton();
|
|
|
|
},
|
|
|
|
|
2013-02-12 20:51:25 +00:00
|
|
|
onPinButton: function() {
|
|
|
|
if (this.pinButton.checked) {
|
|
|
|
Browser.pinSite();
|
|
|
|
} else {
|
|
|
|
Browser.unpinSite();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onStarButton: function(aValue) {
|
|
|
|
if (aValue === undefined) {
|
|
|
|
aValue = this.starButton.checked;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aValue) {
|
|
|
|
Browser.starSite(function () {
|
|
|
|
Appbar._updateStarButton();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Browser.unstarSite(function () {
|
|
|
|
Appbar._updateStarButton();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-14 23:29:12 +00:00
|
|
|
onMenuButton: function(aEvent) {
|
2013-07-30 22:50:14 +00:00
|
|
|
let typesArray = [];
|
2013-06-25 19:20:17 +00:00
|
|
|
|
2013-08-09 09:50:58 +00:00
|
|
|
if (!BrowserUI.isStartTabVisible)
|
2013-07-30 22:50:14 +00:00
|
|
|
typesArray.push("find-in-page");
|
2013-11-04 22:18:21 +00:00
|
|
|
if (ContextCommands.getPageSource())
|
|
|
|
typesArray.push("view-page-source");
|
2013-10-10 14:59:48 +00:00
|
|
|
if (ContextCommands.getStoreLink())
|
|
|
|
typesArray.push("ms-meta-data");
|
2013-07-30 22:50:14 +00:00
|
|
|
if (ConsolePanelView.enabled)
|
|
|
|
typesArray.push("open-error-console");
|
2013-08-28 17:27:32 +00:00
|
|
|
if (!Services.metro.immersive)
|
2013-07-30 22:50:14 +00:00
|
|
|
typesArray.push("open-jsshell");
|
2013-06-25 19:20:17 +00:00
|
|
|
|
2013-02-12 20:51:25 +00:00
|
|
|
try {
|
|
|
|
// If we have a valid http or https URI then show the view on desktop
|
|
|
|
// menu item.
|
2013-07-30 22:50:14 +00:00
|
|
|
let uri = Services.io.newURI(Browser.selectedBrowser.currentURI.spec,
|
2013-02-12 20:51:25 +00:00
|
|
|
null, null);
|
|
|
|
if (uri.schemeIs('http') || uri.schemeIs('https')) {
|
|
|
|
typesArray.push("view-on-desktop");
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
|
2013-06-14 23:29:12 +00:00
|
|
|
var x = this.menuButton.getBoundingClientRect().left;
|
2013-07-10 19:14:00 +00:00
|
|
|
var y = Elements.toolbar.getBoundingClientRect().top;
|
2013-02-12 20:51:25 +00:00
|
|
|
ContextMenuUI.showContextMenu({
|
|
|
|
json: {
|
|
|
|
types: typesArray,
|
|
|
|
string: '',
|
|
|
|
xPos: x,
|
|
|
|
yPos: y,
|
|
|
|
leftAligned: true,
|
|
|
|
bottomAligned: true
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onViewOnDesktop: function() {
|
|
|
|
try {
|
|
|
|
// Make sure we have a valid URI so Windows doesn't prompt
|
|
|
|
// with an unrecognized command, select default program window
|
|
|
|
var uri = Services.io.newURI(Browser.selectedBrowser.currentURI.spec,
|
|
|
|
null, null);
|
|
|
|
if (uri.schemeIs('http') || uri.schemeIs('https')) {
|
2013-08-28 17:27:32 +00:00
|
|
|
Services.metro.launchInDesktop(Browser.selectedBrowser.currentURI.spec, "");
|
2013-02-12 20:51:25 +00:00
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-01 02:04:56 +00:00
|
|
|
onAutocompleteCloseButton: function () {
|
|
|
|
Elements.autocomplete.closePopup();
|
|
|
|
},
|
|
|
|
|
2013-02-12 20:51:25 +00:00
|
|
|
dispatchContextualAction: function(aActionName){
|
|
|
|
let activeTileset = this.activeTileset;
|
2013-07-30 19:16:58 +00:00
|
|
|
if (activeTileset && ('isBound' in this.activeTileset)) {
|
2013-02-12 20:51:25 +00:00
|
|
|
// fire event on the richgrid, others can listen
|
|
|
|
// but we keep coupling loose so grid doesn't need to know about appbar
|
|
|
|
let event = document.createEvent("Events");
|
|
|
|
event.action = aActionName;
|
2013-04-03 21:16:14 +00:00
|
|
|
event.initEvent("context-action", true, true); // is cancelable
|
2013-02-12 20:51:25 +00:00
|
|
|
activeTileset.dispatchEvent(event);
|
2013-04-03 21:16:14 +00:00
|
|
|
if (!event.defaultPrevented) {
|
|
|
|
activeTileset.clearSelection();
|
2013-05-16 00:21:06 +00:00
|
|
|
Elements.contextappbar.dismiss();
|
2013-04-03 21:16:14 +00:00
|
|
|
}
|
2013-02-12 20:51:25 +00:00
|
|
|
}
|
|
|
|
},
|
2013-05-07 18:38:08 +00:00
|
|
|
|
2013-07-19 01:28:24 +00:00
|
|
|
showContextualActions: function(aVerbs, aSetName) {
|
2013-07-18 14:02:50 +00:00
|
|
|
// When the appbar is not visible, we want the icons to refresh right away
|
|
|
|
let immediate = !Elements.contextappbar.isShowing;
|
|
|
|
|
2013-07-19 01:28:24 +00:00
|
|
|
if (aVerbs.length) {
|
2013-05-16 00:21:06 +00:00
|
|
|
Elements.contextappbar.show();
|
2013-07-19 01:28:24 +00:00
|
|
|
}
|
2013-05-14 07:27:04 +00:00
|
|
|
|
2013-07-16 20:38:28 +00:00
|
|
|
// Look up all of the buttons for the verbs that should be visible.
|
|
|
|
let idsToVisibleVerbs = new Map();
|
2013-02-12 20:51:25 +00:00
|
|
|
for (let verb of aVerbs) {
|
|
|
|
let id = verb + "-selected-button";
|
2013-07-16 20:38:28 +00:00
|
|
|
if (!document.getElementById(id)) {
|
2013-04-09 11:04:47 +00:00
|
|
|
throw new Error("Appbar.showContextualActions: no button for " + verb);
|
2013-02-12 20:51:25 +00:00
|
|
|
}
|
2013-07-16 20:38:28 +00:00
|
|
|
idsToVisibleVerbs.set(id, verb);
|
2013-02-12 20:51:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 20:38:28 +00:00
|
|
|
// Sort buttons into 2 buckets - needing showing and needing hiding.
|
|
|
|
let toHide = [], toShow = [];
|
|
|
|
let buttons = Elements.contextappbar.getElementsByTagName("toolbarbutton");
|
|
|
|
for (let button of buttons) {
|
|
|
|
let verb = idsToVisibleVerbs.get(button.id);
|
|
|
|
if (verb != undefined) {
|
|
|
|
// Button should be visible, and may or may not be showing.
|
2013-07-19 01:28:24 +00:00
|
|
|
this._updateContextualActionLabel(button, verb, aSetName);
|
2013-07-16 20:38:28 +00:00
|
|
|
if (button.hidden) {
|
|
|
|
toShow.push(button);
|
|
|
|
}
|
|
|
|
} else if (!button.hidden) {
|
|
|
|
// Button is visible, but shouldn't be.
|
|
|
|
toHide.push(button);
|
2013-02-12 20:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-18 14:02:50 +00:00
|
|
|
|
|
|
|
if (immediate) {
|
|
|
|
toShow.forEach(function(element) {
|
|
|
|
element.removeAttribute("fade");
|
|
|
|
element.hidden = false;
|
|
|
|
});
|
|
|
|
toHide.forEach(function(element) {
|
|
|
|
element.setAttribute("fade", true);
|
|
|
|
element.hidden = true;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-09 11:04:47 +00:00
|
|
|
return Task.spawn(function() {
|
|
|
|
if (toHide.length) {
|
|
|
|
yield Util.transitionElementVisibility(toHide, false);
|
|
|
|
}
|
|
|
|
if (toShow.length) {
|
|
|
|
yield Util.transitionElementVisibility(toShow, true);
|
|
|
|
}
|
|
|
|
});
|
2013-02-12 20:51:25 +00:00
|
|
|
},
|
2013-05-07 18:38:08 +00:00
|
|
|
|
2013-08-02 19:47:22 +00:00
|
|
|
_clearContextualActions: function() {
|
2013-05-07 18:38:08 +00:00
|
|
|
this.showContextualActions([]);
|
|
|
|
},
|
|
|
|
|
2013-07-19 01:28:24 +00:00
|
|
|
_updateContextualActionLabel: function(aButton, aVerb, aSetName) {
|
|
|
|
// True if the action's label string contains the set name and
|
|
|
|
// thus has to be selected based on the list passed in.
|
|
|
|
let usesSetName = aButton.hasAttribute("label-uses-set-name");
|
|
|
|
let name = "contextAppbar2." + aVerb + (usesSetName ? "." + aSetName : "");
|
|
|
|
aButton.label = Strings.browser.GetStringFromName(name);
|
2013-07-16 20:38:28 +00:00
|
|
|
},
|
|
|
|
|
2013-02-12 20:51:25 +00:00
|
|
|
_onTileSelectionChanged: function _onTileSelectionChanged(aEvent){
|
|
|
|
let activeTileset = aEvent.target;
|
|
|
|
|
2013-07-30 19:16:58 +00:00
|
|
|
// deselect tiles in other tile groups,
|
|
|
|
// ensure previousyl-activeTileset is bound before calling methods on it
|
|
|
|
if (this.activeTileset &&
|
|
|
|
('isBound' in this.activeTileset) &&
|
|
|
|
this.activeTileset !== activeTileset) {
|
2013-02-12 20:51:25 +00:00
|
|
|
this.activeTileset.clearSelection();
|
|
|
|
}
|
|
|
|
// keep track of which view is the target/scope for the contextual actions
|
|
|
|
this.activeTileset = activeTileset;
|
|
|
|
|
|
|
|
// ask the view for the list verbs/action-names it thinks are
|
|
|
|
// appropriate for the tiles selected
|
|
|
|
let contextActions = activeTileset.contextActions;
|
|
|
|
let verbs = [v for (v of contextActions)];
|
|
|
|
|
2013-04-03 21:16:14 +00:00
|
|
|
// fire event with these verbs as payload
|
|
|
|
let event = document.createEvent("Events");
|
|
|
|
event.actions = verbs;
|
|
|
|
event.initEvent("MozContextActionsChange", true, false);
|
2013-07-19 01:28:24 +00:00
|
|
|
activeTileset.dispatchEvent(event);
|
2013-02-12 20:51:25 +00:00
|
|
|
|
|
|
|
if (verbs.length) {
|
2013-05-16 00:21:06 +00:00
|
|
|
Elements.contextappbar.show(); // should be no-op if we're already showing
|
2013-02-12 20:51:25 +00:00
|
|
|
} else {
|
2013-05-16 00:21:06 +00:00
|
|
|
Elements.contextappbar.dismiss();
|
2013-02-12 20:51:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updatePinButton: function() {
|
|
|
|
this.pinButton.checked = Browser.isSitePinned();
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateStarButton: function() {
|
|
|
|
Browser.isSiteStarredAsync(function (isStarred) {
|
|
|
|
this.starButton.checked = isStarred;
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2013-06-25 19:20:17 +00:00
|
|
|
};
|