Skip to content

Commit

Permalink
Factor out the scrollOrAction pattern (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Aug 20, 2020
1 parent 636d4eb commit ad0e492
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
23 changes: 8 additions & 15 deletions frontend/src/aspects/exploration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
13 changes: 13 additions & 0 deletions frontend/src/explorer/explorer-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad0e492

Please sign in to comment.