Skip to content

Commit 9428faa

Browse files
committed
custom binding tool can now be an object with options
replaced layer constructor lookup with prexisting map changed protocol to provider is json structure
1 parent 29162ab commit 9428faa

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/neuroglancer/ui/default_viewer_setup.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import {setDefaultInputEventBindings} from 'neuroglancer/ui/default_input_event_
2020
import {makeDefaultViewer} from 'neuroglancer/ui/default_viewer';
2121
import {bindTitle} from 'neuroglancer/ui/title';
2222
import {UrlHashBinding} from 'neuroglancer/ui/url_hash_binding';
23-
import {UserLayer, UserLayerConstructor} from 'neuroglancer/layer';
24-
import {SegmentationUserLayer} from 'neuroglancer/segmentation_user_layer';
23+
import {UserLayer, UserLayerConstructor, layerTypes} from 'neuroglancer/layer';
2524
import {Tool, restoreTool} from 'neuroglancer/ui/tool';
25+
import {verifyObject, verifyObjectProperty, verifyString} from 'neuroglancer/util/json';
2626

2727
declare var NEUROGLANCER_DEFAULT_STATE_FRAGMENT: string|undefined;
2828

2929
type CustomBinding = {
30-
layer: string, tool: string, protocol?: string,
30+
layer: string, tool: unknown, provider?: string,
3131
}
3232

3333
type CustomBindings = {
@@ -44,16 +44,21 @@ export function setupDefaultViewer() {
4444
let viewer = (<any>window)['viewer'] = makeDefaultViewer();
4545
setDefaultInputEventBindings(viewer.inputEventBindings);
4646

47-
const bindActionToTool = (action: string, toolType: string, toolKey: string, desiredLayerType: UserLayerConstructor, desiredProtocol?: string) => {
47+
const bindNonLayerSpecificTool = (obj: unknown, toolKey: string, desiredLayerType: UserLayerConstructor, desiredProvider?: string) => {
4848
let previousTool: Tool<Object>|undefined;
4949
let previousLayer: UserLayer|undefined;
50-
viewer.bindAction(action, () => {
50+
if (typeof obj === 'string') {
51+
obj = {'type': obj};
52+
}
53+
verifyObject(obj);
54+
const type = verifyObjectProperty(obj, 'type', verifyString);
55+
viewer.bindAction(`tool-${type}`, () => {
5156
const acceptableLayers = viewer.layerManager.managedLayers.filter((managedLayer) => {
5257
const correctLayerType = managedLayer.layer instanceof desiredLayerType;
53-
if (desiredProtocol && correctLayerType) {
58+
if (desiredProvider && correctLayerType) {
5459
for (const dataSource of managedLayer.layer?.dataSources || []) {
5560
const protocol = viewer.dataSourceProvider.getProvider(dataSource.spec.url)[2];
56-
if (protocol === desiredProtocol) {
61+
if (protocol === desiredProvider) {
5762
return true;
5863
}
5964
}
@@ -66,7 +71,7 @@ export function setupDefaultViewer() {
6671
const firstLayer = acceptableLayers[0].layer;
6772
if (firstLayer) {
6873
if (firstLayer !== previousLayer) {
69-
previousTool = restoreTool(firstLayer, toolType);
74+
previousTool = restoreTool(firstLayer, obj);
7075
previousLayer = firstLayer;
7176
}
7277
if (previousTool) {
@@ -77,22 +82,16 @@ export function setupDefaultViewer() {
7782
});
7883
}
7984

80-
const nameToLayer: {[key: string]: UserLayerConstructor|undefined} = {};
81-
82-
for (let x of [SegmentationUserLayer]) {
83-
nameToLayer[x.type] = x;
84-
}
85-
8685
if (hasCustomBindings) {
8786
for (const [key, val] of Object.entries(CUSTOM_BINDINGS!)) {
8887
if (typeof val === 'string') {
8988
viewer.inputEventBindings.global.set(key, val);
9089
} else {
9190
viewer.inputEventBindings.global.set(key, `tool-${val.tool}`);
92-
const layerConstructor = nameToLayer[val.layer];
91+
const layerConstructor = layerTypes.get(val.layer);
9392
if (layerConstructor) {
9493
const toolKey = key.charAt(key.length - 1).toUpperCase();
95-
bindActionToTool(`tool-${val.tool}`, val.tool, toolKey, layerConstructor, val.protocol);
94+
bindNonLayerSpecificTool(val.tool, toolKey, layerConstructor, val.provider);
9695
}
9796
}
9897
}

0 commit comments

Comments
 (0)