Skip to content

Commit

Permalink
Replace route parser by a pattern that is easier on the brain (#62 #106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Aug 20, 2020
1 parent ad0e492 commit d30ed09
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 99 deletions.
30 changes: 24 additions & 6 deletions frontend/src/aspects/exploration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isString } from 'lodash';
import { partial, isString } from 'lodash';

import channel from '../explorer/radio';
import executeRoute from '../explorer/route-parser';
import * as act from '../explorer/route-actions';
import router from '../global/exploration-router';
import mainRouter from '../global/main-router';
import explorer from '../global/explorer-view';
Expand Down Expand Up @@ -48,7 +48,25 @@ mainRouter.on('route:explore', () => {
);
});

router.on('route', (route, [serial]) => explorer.scrollOrAction(
browserHistory.state,
() => executeRoute(route, controller, serial)
));
/**
* Common patterns for the explorer routes.
*/
function deepRoute(obtainAction, resetAction) {
return ([serial]) => explorer.scrollOrAction(
browserHistory.state,
() => resetAction(controller, obtainAction(serial))
);
}

const sourceRoute = partial(deepRoute, act.getSource);
const itemRoute = partial(deepRoute, act.getItem);

router.on('route:source:bare', sourceRoute(act.sourceWithoutAnnotations));
router.on('route:source:annotated', sourceRoute(act.sourceWithAnnotations));
router.on('route:item', itemRoute(act.item));
router.on('route:item:edit', itemRoute(act.itemInEditMode));
router.on('route:item:related', itemRoute(act.itemWithRelations));
router.on('route:item:related:edit', itemRoute(act.itemWithEditRelations));
router.on('route:item:external', itemRoute(act.itemWithExternal));
router.on('route:item:external:edit', itemRoute(act.itemWithEditExternal));
router.on('route:item:annotations', itemRoute(act.itemWithOccurrences));
53 changes: 53 additions & 0 deletions frontend/src/explorer/route-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import View from '../core/view';
import { source, item as itemNs } from '../jsonld/ns';
import { Namespace } from '../jsonld/vocabulary';
import ldChannel from '../jsonld/radio';
import Node from '../jsonld/node';
import Controller from './explorer-event-controller';

function obtainer<T extends readonly string[]>(namespace: Namespace<T>) {
return function(serial: string) {
return ldChannel.request('obtain', namespace(serial));
}
}

export const getSource = obtainer(source);
export const getItem = obtainer(itemNs);

export function sourceWithoutAnnotations(control: Controller, node: Node) {
return control.resetSource(node, false);
}

export function sourceWithAnnotations(control: Controller, node: Node) {
return control.resetSourcePair(node);
}

export function item(control: Controller, node: Node) {
return control.resetItem(node);
}

export function itemInEditMode(control: Controller, node: Node) {
return control.editAnnotation(item(control, node), node);
}

export function itemWithRelations(control: Controller, node: Node) {
return control.listRelated(item(control, node), node);
}

export function itemWithEditRelations(control: Controller, node: Node) {
return control.editRelated(itemWithRelations(control, node), node);
}

export function itemWithExternal(control: Controller, node: Node) {
return control.listExternal(item(control, node), node);
}

export function itemWithEditExternal(control: Controller, node: Node) {
return control.editExternal(itemWithExternal(control, node));
}

export function itemWithOccurrences(control: Controller, node: Node) {
// listItemAnnotations does not return the created panel
// see #342
control.listItemAnnotations(item(control, node), node);
}
93 changes: 0 additions & 93 deletions frontend/src/explorer/route-parser.ts

This file was deleted.

0 comments on commit d30ed09

Please sign in to comment.