Skip to content

Commit

Permalink
organize plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Feb 19, 2025
1 parent c3b3d8c commit 5af836d
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 62 deletions.
26 changes: 14 additions & 12 deletions src/pages/NwbPage/NwbHierarchyView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const NwbHierarchyView: FunctionComponent<Props> = ({
new Set(),
);
const [
specialPluginsWithSecondaryPaths,
setSpecialPluginsWithSecondaryPaths,
launchablePluginsWithSecondaryPaths,
setLaunchablePluginsWithSecondaryPaths,
] = useState<{
[key: string]: { plugin: NwbObjectViewPlugin; secondaryPaths: string[] }[];
}>({});
Expand Down Expand Up @@ -77,8 +77,8 @@ const NwbHierarchyView: FunctionComponent<Props> = ({
}, [neurodataObjects, visiblyExpanded]);

useEffect(() => {
const loadSpecialPlugins = async () => {
const newSpecialPluginsWithSecondaryPaths: {
const loadLaunchablePlugins = async () => {
const newLaunchablePluginsWithSecondaryPaths: {
[key: string]: {
plugin: NwbObjectViewPlugin;
secondaryPaths: string[];
Expand All @@ -91,12 +91,12 @@ const NwbHierarchyView: FunctionComponent<Props> = ({
obj.path,
objectType,
{
special: true,
launchableFromTable: true,
defaultUnitsPath,
},
);
if (plugins.length > 0) {
newSpecialPluginsWithSecondaryPaths[obj.path] = plugins.map(
newLaunchablePluginsWithSecondaryPaths[obj.path] = plugins.map(
(plugin) => ({
plugin,
secondaryPaths: plugin.requiredDefaultUnits
Expand All @@ -106,9 +106,11 @@ const NwbHierarchyView: FunctionComponent<Props> = ({
);
}
}
setSpecialPluginsWithSecondaryPaths(newSpecialPluginsWithSecondaryPaths);
setLaunchablePluginsWithSecondaryPaths(
newLaunchablePluginsWithSecondaryPaths,
);
};
loadSpecialPlugins();
loadLaunchablePlugins();
}, [nwbUrl, neurodataObjects, defaultUnitsPath]);

const truncateDescription = useCallback(
Expand Down Expand Up @@ -307,7 +309,7 @@ const NwbHierarchyView: FunctionComponent<Props> = ({
</span>
)}
{specialPluginsWithSecondaryPaths[obj.path]?.map(
{launchablePluginsWithSecondaryPaths[obj.path]?.map(
({ plugin, secondaryPaths }) => {
const pluginString = `${plugin.name}|${[obj.path, ...secondaryPaths].join("^")}`;
return (
Expand Down Expand Up @@ -455,9 +457,9 @@ const NwbHierarchyView: FunctionComponent<Props> = ({
selection.split("|");
const [path, ...secondaryPaths] =
pathWithSecondary.split("^");
const plugin = specialPluginsWithSecondaryPaths[path]?.find(
(p) => p.plugin.name === pluginName,
)?.plugin;
const plugin = launchablePluginsWithSecondaryPaths[
path
]?.find((p) => p.plugin.name === pluginName)?.plugin;
if (plugin) {
onOpenObjectInNewTab?.(path, plugin, secondaryPaths);
}
Expand Down
41 changes: 19 additions & 22 deletions src/pages/NwbPage/NwbObjectView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const NwbObjectView: React.FC<NwbObjectViewProps> = ({
setPlugins([plugin]);
} else {
let suitable = await findSuitablePlugins(nwbUrl, path, objectType, {
special: false,
launchableFromTable: false,
});
if (inMultiView) {
// If we are in a multi-view, then we only use plugins that have showInMultiView set to true
Expand Down Expand Up @@ -90,27 +90,24 @@ const NwbObjectView: React.FC<NwbObjectViewProps> = ({

return (
<div style={{ position: "relative", width, height }}>
{plugins
.slice()
.reverse() // reverse it because the most specialized plugins come last
.map((plugin) => {
const PluginComponent = plugin.component;
return (
<div key={plugin.name}>
<PluginComponent
nwbUrl={nwbUrl}
path={path}
objectType={objectType}
onOpenObjectInNewTab={onOpenObjectInNewTab}
secondaryPaths={secondaryPaths}
width={componentWidth}
height={componentHeight}
condensed={inMultiView}
/>
{!inMultiView && <hr />}
</div>
);
})}
{plugins.slice().map((plugin) => {
const PluginComponent = plugin.component;
return (
<div key={plugin.name}>
<PluginComponent
nwbUrl={nwbUrl}
path={path}
objectType={objectType}
onOpenObjectInNewTab={onOpenObjectInNewTab}
secondaryPaths={secondaryPaths}
width={componentWidth}
height={componentHeight}
condensed={inMultiView}
/>
{!inMultiView && <hr />}
</div>
);
})}
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/pages/NwbPage/plugins/Image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const imagePlugin: NwbObjectViewPlugin = {
return false;
},
component: ImagePluginView,
special: false,
launchableFromTable: false,
requiresWindowDimensions: true,
showInMultiView: true,
};
Expand Down
3 changes: 2 additions & 1 deletion src/pages/NwbPage/plugins/PSTH/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const psthPlugin: NwbObjectViewPlugin = {
return true;
},
component: PSTHView,
special: true,
launchableFromTable: true,
requiresWindowDimensions: true,
requiredDefaultUnits: true,
};
2 changes: 1 addition & 1 deletion src/pages/NwbPage/plugins/Raster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const rasterPlugin: NwbObjectViewPlugin = {
return false;
},
component: RasterView,
special: true,
launchableFromTable: true,
requiresWindowDimensions: false,
showInMultiView: false,
};
Expand Down
2 changes: 1 addition & 1 deletion src/pages/NwbPage/plugins/SpatialSeries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export const spatialSeriesPlugin: NwbObjectViewPlugin = {
// We need window dimensions since we're displaying a spatial plot
requiresWindowDimensions: true,
showInMultiView: false,
special: true,
launchableFromTable: true,
};
2 changes: 1 addition & 1 deletion src/pages/NwbPage/plugins/SpikeDensity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const spikeDensityPlugin: NwbObjectViewPlugin = {
return false;
},
component: SpikeDensityView,
special: true,
launchableFromTable: true,
requiresWindowDimensions: false,
showInMultiView: false,
};
Expand Down
2 changes: 1 addition & 1 deletion src/pages/NwbPage/plugins/TrialAlignedSeries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export const trialAlignedSeriesPlugin: NwbObjectViewPlugin = {
return true;
},
component: TrialAlignedPluginView,
special: true,
launchableFromTable: true,
requiresWindowDimensions: true,
};
2 changes: 1 addition & 1 deletion src/pages/NwbPage/plugins/pluginInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface NwbObjectViewPlugin {
height?: number;
condensed?: boolean;
}>;
special?: boolean;
launchableFromTable?: boolean;
requiresWindowDimensions?: boolean;
showInMultiView?: boolean;
requiredDefaultUnits?: boolean;
Expand Down
45 changes: 24 additions & 21 deletions src/pages/NwbPage/plugins/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,52 @@ import { trialAlignedSeriesPlugin } from "./TrialAlignedSeries";
import { pythonScriptPlugin } from "./PythonScript";
import spikeDensityPlugin from "./SpikeDensity";

// List of plugins in priority order (last one is checked first)
// List of plugins in order they will appear in the UI when a single object is being viewed
export const nwbObjectViewPlugins: NwbObjectViewPlugin[] = [
pythonScriptPlugin,
defaultPlugin,
behavioralEventsPlugin,
dynamicTablePlugin,
twoPhotonSeriesPlugin,
spatialSeriesPlugin,
simpleTimeseriesPlugin,
psthPlugin,
rasterPlugin,
imagePlugin,
imageSegmentationPlugin,
timeIntervalsPlugin,
trialAlignedSeriesPlugin,

rasterPlugin,
spikeDensityPlugin,

defaultPlugin,
pythonScriptPlugin,
];

export const findSuitablePlugins = async (
nwbUrl: string,
path: string,
objectType: "group" | "dataset",
o: { special?: boolean; defaultUnitsPath?: string },
o: { launchableFromTable?: boolean; defaultUnitsPath?: string },
): Promise<NwbObjectViewPlugin[]> => {
const ret: NwbObjectViewPlugin[] = [];
for (let i = 0; i < nwbObjectViewPlugins.length; i++) {
const plugin = nwbObjectViewPlugins[i];
if (!!plugin.special === !!o.special) {
if (plugin.requiredDefaultUnits && !o.defaultUnitsPath) {
continue;
}
if (
await plugin.canHandle({
nwbUrl,
objectType,
path,
secondaryPaths: plugin.requiredDefaultUnits
? [o.defaultUnitsPath!]
: [],
})
) {
ret.push(plugin);
}
if (o.launchableFromTable && !plugin.launchableFromTable) {
continue;
}
if (plugin.requiredDefaultUnits && !o.defaultUnitsPath) {
continue;
}
if (
await plugin.canHandle({
nwbUrl,
objectType,
path,
secondaryPaths: plugin.requiredDefaultUnits
? [o.defaultUnitsPath!]
: [],
})
) {
ret.push(plugin);
}
}
return ret;
Expand Down

0 comments on commit 5af836d

Please sign in to comment.