From ad0e4929ec01660ab5685763f38cdd84000a63b8 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Thu, 20 Aug 2020 14:04:34 +0200 Subject: [PATCH] Factor out the scrollOrAction pattern (#106) --- frontend/src/aspects/exploration.ts | 23 ++++++++--------------- frontend/src/explorer/explorer-view.ts | 13 +++++++++++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/frontend/src/aspects/exploration.ts b/frontend/src/aspects/exploration.ts index a5724355..c9fd75ba 100644 --- a/frontend/src/aspects/exploration.ts +++ b/frontend/src/aspects/exploration.ts @@ -42,20 +42,13 @@ channel.on('currentRoute', (route, panel) => { mainRouter.on('route:explore', () => { ensureSources(); - const cid = sourceListPanel.cid; - if (explorer.has(cid)) { - explorer.scroll(cid); - } else { - explorer.reset(sourceListPanel); - } + explorer.scrollOrAction( + sourceListPanel.cid, + () => explorer.reset(sourceListPanel) + ); }); -router.on('route', (route, [serial]) => { - const state = browserHistory.state; - // state can only be a string if we made it so. - if (isString(state) && explorer.has(state)) { - explorer.scroll(state); - } else { - executeRoute(route, controller, serial); - } -}); +router.on('route', (route, [serial]) => explorer.scrollOrAction( + browserHistory.state, + () => executeRoute(route, controller, serial) +)); diff --git a/frontend/src/explorer/explorer-view.ts b/frontend/src/explorer/explorer-view.ts index 69563843..d4b48979 100644 --- a/frontend/src/explorer/explorer-view.ts +++ b/frontend/src/explorer/explorer-view.ts @@ -214,6 +214,19 @@ export default class ExplorerView extends View { return this; } + /** + * Scroll to a panel if present, otherwise execute an action of choice. + * Useful in routing. + */ + scrollOrAction(cid, action: () => void): this { + if (isString(cid) && this.has(cid)) { + this.scroll(cid); + } else { + action(); + } + return this; + } + /** * Remove the topmost panel from the stack at position. Returns the deleted panel. * @param position The indes of the stack to remove the panel from