[chrome] Implement app-level lastPath

We have always tracked the lastPath for tabs on the page, which means that clicking from one to the other restores your previous state. Now we are tracking the lastUrls for each app and using them in the appSwitcher so that the appSwitcher properly links to the last state of each app.
This commit is contained in:
spalger 2015-09-03 15:00:25 -07:00
parent 1bcd867f49
commit 5d22b9351e
3 changed files with 25 additions and 8 deletions

View file

@ -1,4 +1,5 @@
var _ = require('lodash'); var _ = require('lodash');
var reEsc = require('lodash').escapeRegExp;
var storage = window.sessionStorage; var storage = window.sessionStorage;
function Tab(spec) { function Tab(spec) {
@ -8,6 +9,7 @@ function Tab(spec) {
this.resetWhenActive = !!spec.resetWhenActive; this.resetWhenActive = !!spec.resetWhenActive;
this.lastUrlStoreKey = spec.trackLastPath ? 'lastUrl:' + this.id : null; this.lastUrlStoreKey = spec.trackLastPath ? 'lastUrl:' + this.id : null;
this.rootUrl = '/' + this.id; this.rootUrl = '/' + this.id;
this.rootRegExp = new RegExp(`^${reEsc(this.rootUrl)}(/|$|\\?)`);
this.lastUrl = this.lastUrlStoreKey && storage.getItem(this.lastUrlStoreKey); this.lastUrl = this.lastUrlStoreKey && storage.getItem(this.lastUrlStoreKey);
this.activeIndicatorColor = spec.activeIndicatorColor || null; this.activeIndicatorColor = spec.activeIndicatorColor || null;

View file

@ -1,7 +1,10 @@
var _ = require('lodash'); var _ = require('lodash');
var { startsWith } = require('lodash');
var Tab = require('ui/chrome/Tab'); var Tab = require('ui/chrome/Tab');
var format = require('url').format; var { format, parse } = require('url');
var parse = _.wrap(require('url').parse, function (parse, path) { var storage = window.sessionStorage;
parse = _.wrap(parse, function (parse, path) {
var parsed = parse(path, true); var parsed = parse(path, true);
return { return {
pathname: parsed.pathname, pathname: parsed.pathname,
@ -42,19 +45,23 @@ function TabCollection() {
return activeTab; return activeTab;
}; };
this.consumeRouteUpdate = function ($location, persist) { this.consumeRouteUpdate = function (appId, href, path, persist) {
var url = parse($location.url(), true); var url = parse(href, true);
var id = $location.path().split('/')[1] || '';
tabs.forEach(function (tab) { tabs.forEach(function (tab) {
var active = tab.active = (tab.id === id); tab.active = tab.rootRegExp.test(path);
var lastUrl = active ? url : parse(tab.lastUrl || tab.rootUrl);
var lastUrl = tab.active ? url : parse(tab.lastUrl || tab.rootUrl);
lastUrl.query._g = url.query._g; lastUrl.query._g = url.query._g;
if (tab.active) activeTab = tab; if (tab.active) activeTab = tab;
if (persist) { if (persist) {
tab.persistLastUrl(format(lastUrl)); tab.persistLastUrl(format(lastUrl));
} }
if (tab.active) {
storage.setItem(`appLastUrl:${appId}`, href);
}
}); });
}; };
} }

View file

@ -44,7 +44,15 @@ module.exports = function (chrome, internals) {
chrome.setVisible(!Boolean($location.search().embed)); chrome.setVisible(!Boolean($location.search().embed));
// listen for route changes, propogate to tabs // listen for route changes, propogate to tabs
var onRouteChange = _.bindKey(internals.tabs, 'consumeRouteUpdate', $location, chrome.getVisible()); var onRouteChange = function () {
internals.tabs.consumeRouteUpdate(
chrome.getAppId(),
window.location.href,
$location.url(),
chrome.getVisible()
);
};
$rootScope.$on('$routeChangeSuccess', onRouteChange); $rootScope.$on('$routeChangeSuccess', onRouteChange);
$rootScope.$on('$routeUpdate', onRouteChange); $rootScope.$on('$routeUpdate', onRouteChange);
onRouteChange(); onRouteChange();