From 5d22b9351e828834d9fdac235c93c71eebd6bfd3 Mon Sep 17 00:00:00 2001 From: spalger Date: Thu, 3 Sep 2015 15:00:25 -0700 Subject: [PATCH] [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. --- src/ui/public/chrome/Tab.js | 2 ++ src/ui/public/chrome/TabCollection.js | 21 ++++++++++++++------- src/ui/public/chrome/api/angular.js | 10 +++++++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/ui/public/chrome/Tab.js b/src/ui/public/chrome/Tab.js index 4abe392ea450..38f4aac4bcbf 100644 --- a/src/ui/public/chrome/Tab.js +++ b/src/ui/public/chrome/Tab.js @@ -1,4 +1,5 @@ var _ = require('lodash'); +var reEsc = require('lodash').escapeRegExp; var storage = window.sessionStorage; function Tab(spec) { @@ -8,6 +9,7 @@ function Tab(spec) { this.resetWhenActive = !!spec.resetWhenActive; this.lastUrlStoreKey = spec.trackLastPath ? 'lastUrl:' + this.id : null; this.rootUrl = '/' + this.id; + this.rootRegExp = new RegExp(`^${reEsc(this.rootUrl)}(/|$|\\?)`); this.lastUrl = this.lastUrlStoreKey && storage.getItem(this.lastUrlStoreKey); this.activeIndicatorColor = spec.activeIndicatorColor || null; diff --git a/src/ui/public/chrome/TabCollection.js b/src/ui/public/chrome/TabCollection.js index 5f9aa01384e3..650ff9aaa963 100644 --- a/src/ui/public/chrome/TabCollection.js +++ b/src/ui/public/chrome/TabCollection.js @@ -1,7 +1,10 @@ var _ = require('lodash'); +var { startsWith } = require('lodash'); var Tab = require('ui/chrome/Tab'); -var format = require('url').format; -var parse = _.wrap(require('url').parse, function (parse, path) { +var { format, parse } = require('url'); +var storage = window.sessionStorage; + +parse = _.wrap(parse, function (parse, path) { var parsed = parse(path, true); return { pathname: parsed.pathname, @@ -42,19 +45,23 @@ function TabCollection() { return activeTab; }; - this.consumeRouteUpdate = function ($location, persist) { - var url = parse($location.url(), true); - var id = $location.path().split('/')[1] || ''; + this.consumeRouteUpdate = function (appId, href, path, persist) { + var url = parse(href, true); tabs.forEach(function (tab) { - var active = tab.active = (tab.id === id); - var lastUrl = active ? url : parse(tab.lastUrl || tab.rootUrl); + tab.active = tab.rootRegExp.test(path); + + var lastUrl = tab.active ? url : parse(tab.lastUrl || tab.rootUrl); lastUrl.query._g = url.query._g; if (tab.active) activeTab = tab; if (persist) { tab.persistLastUrl(format(lastUrl)); } + + if (tab.active) { + storage.setItem(`appLastUrl:${appId}`, href); + } }); }; } diff --git a/src/ui/public/chrome/api/angular.js b/src/ui/public/chrome/api/angular.js index 31fc3f418df5..a1912501a998 100644 --- a/src/ui/public/chrome/api/angular.js +++ b/src/ui/public/chrome/api/angular.js @@ -44,7 +44,15 @@ module.exports = function (chrome, internals) { chrome.setVisible(!Boolean($location.search().embed)); // 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('$routeUpdate', onRouteChange); onRouteChange();