@openmrs/esm-extensions
Version:
Coordinates extensions and extension points in the OpenMRS Frontend
123 lines (122 loc) • 5.04 kB
JavaScript
import { getExtensionRegistration } from "./index.js";
import { createGlobalStore } from "@openmrs/esm-state";
import { translateFrom } from "@openmrs/esm-translations";
const workspaceRegistrationStore = createGlobalStore('workspaceRegistrations', {
workspaces: {}
});
const workspaceGroupStore = createGlobalStore('workspaceGroups', {
workspaceGroups: {}
});
/**
* Tells the workspace system about a workspace. This is used by the app shell
* to register workspaces defined in the `routes.json` file.
* @internal
*/ export function registerWorkspace(workspace) {
workspaceRegistrationStore.setState((state)=>({
workspaces: {
...state.workspaces,
[workspace.name]: {
...workspace,
load: workspace.load,
preferredWindowSize: workspace.preferredWindowSize ?? 'normal',
type: workspace.type ?? 'form',
canHide: workspace.canHide ?? false,
canMaximize: workspace.canMaximize ?? false,
width: workspace.width ?? 'narrow',
groups: workspace.groups ?? []
}
}
}));
}
/**
* Tells the workspace system about a workspace group. This is used by the app shell
* to register workspace groups defined in the `routes.json` file.
* @internal
*/ export function registerWorkspaceGroup(workspaceGroup) {
workspaceGroupStore.setState((state)=>{
const group = state.workspaceGroups[workspaceGroup.name];
if (group) {
// This condition occurs when a workspace with a `groups` property is registered before
// the corresponding workspace group. In such cases, the workspace group is registered
// by the `attachWorkspaceToGroup` function.
return {
workspaceGroups: {
...state.workspaceGroups,
[workspaceGroup.name]: {
...group,
members: Array.from(new Set([
...group.members,
...workspaceGroup.members
]))
}
}
};
} else {
return {
workspaceGroups: {
...state.workspaceGroups,
[workspaceGroup.name]: {
name: workspaceGroup.name,
members: workspaceGroup.members
}
}
};
}
});
}
const workspaceExtensionWarningsIssued = new Set();
/**
* This exists for compatibility with the old way of registering
* workspaces (as extensions).
*
* @param name of the workspace
*/ export function getWorkspaceRegistration(name) {
const registeredWorkspaces = workspaceRegistrationStore.getState().workspaces;
if (registeredWorkspaces[name]) {
return registeredWorkspaces[name];
} else {
const workspaceExtension = getExtensionRegistration(name);
if (workspaceExtension) {
if (!workspaceExtensionWarningsIssued.has(name)) {
console.warn(`The workspace '${name}' is registered as an extension. This is deprecated. Please register it in the "workspaces" section of the routes.json file.`);
workspaceExtensionWarningsIssued.add(name);
}
return {
name: workspaceExtension.name,
title: getTitleFromExtension(workspaceExtension),
moduleName: workspaceExtension.moduleName,
preferredWindowSize: workspaceExtension.meta?.screenSize ?? 'normal',
load: workspaceExtension.load,
type: workspaceExtension.meta?.type ?? 'form',
canHide: workspaceExtension.meta?.canHide ?? false,
canMaximize: workspaceExtension.meta?.canMaximize ?? false,
width: workspaceExtension.meta?.width ?? 'narrow',
groups: workspaceExtension.meta?.groups ?? []
};
} else {
throw new Error(`No workspace named '${name}' has been registered.`);
}
}
}
/**
* This provides the workspace group registration and is also compatibile with the
* old way of registering workspace groups (as extensions), but isn't recommended.
*
* @param name of the workspace
*/ export function getWorkspaceGroupRegistration(name) {
const registeredWorkspaces = workspaceGroupStore.getState().workspaceGroups;
if (registeredWorkspaces[name]) {
return registeredWorkspaces[name];
} else {
throw new Error(`No workspace group named '${name}' has been registered.`);
}
}
function getTitleFromExtension(ext) {
const title = ext?.meta?.title;
if (typeof title === 'string') {
return title;
} else if (title && typeof title === 'object') {
return translateFrom(ext.moduleName, title.key, title.default);
}
return ext.name;
}