forked from openmrs/openmrs-esm-patient-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspaces.ts
325 lines (296 loc) · 11.1 KB
/
workspaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { ExtensionRegistration, getGlobalStore, navigate, translateFrom } from '@openmrs/esm-framework';
// FIXME We should not rely on internals here
import { getExtensionRegistration } from '@openmrs/esm-framework/src/internal';
import _i18n from 'i18next';
import { WorkspaceWindowState } from '../types/workspace';
export interface Prompt {
title: string;
body: string;
/** Defaults to "Confirm" */
confirmText?: string;
onConfirm(): void;
/** Defaults to "Cancel" */
cancelText?: string;
}
export interface WorkspaceStoreState {
patientUuid: string | null;
openWorkspaces: Array<OpenWorkspace>;
prompt: Prompt | null;
workspaceWindowState: WorkspaceWindowState;
}
export interface OpenWorkspace extends WorkspaceRegistration {
additionalProps: object;
closeWorkspace(promptBeforeClosing?: boolean): void;
closeWorkspace(): void;
promptBeforeClosing(testFcn: () => boolean): void;
}
export interface WorkspaceRegistration {
name: string;
title: string;
/** Use `getLifecycle` or `getAsyncLifecycle` to get the value of `load` */
load(): Promise<any>;
/** Only one of each "type" of workspace is allowed to be open at a time. The default is "form" */
type?: string;
canHide?: boolean;
canMaximize?: boolean;
width?: 'narrow' | 'wider';
preferredWindowSize?: WorkspaceWindowState;
}
interface WorkspaceAdditionalProps {
workspaceTitle?: string;
onCloseWorkspace?: () => void;
[x: string]: any;
}
let registeredWorkspaces: Record<string, WorkspaceRegistration> = {};
/**
* Tells the workspace system about a workspace. Should be called at the top level
* of a module which will be loaded whenever the patient chart loads.
*/
export function registerWorkspace(workspace: WorkspaceRegistration) {
registeredWorkspaces[workspace.name] = {
...workspace,
preferredWindowSize: workspace.preferredWindowSize ?? 'normal',
type: workspace.type ?? 'form',
canHide: workspace.canHide ?? false,
canMaximize: workspace.canMaximize ?? false,
width: workspace.width ?? 'narrow',
};
}
/**
* This exists for compatibility with the old way of registering
* workspaces (as extensions).
*
* @param name of the workspace
*/
function getWorkspaceRegistration(name: string): WorkspaceRegistration {
if (registeredWorkspaces[name]) {
return registeredWorkspaces[name];
} else {
const workspaceExtension = getExtensionRegistration(name);
if (workspaceExtension) {
return {
name: workspaceExtension.name,
title: getTitleFromExtension(workspaceExtension),
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',
};
} else {
throw new Error(`No workspace named '${name}' has been registered.`);
}
}
}
function getTitleFromExtension(ext: ExtensionRegistration) {
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;
}
function promptBeforeLaunchingWorkspace(
workspace: OpenWorkspace,
newWorkspaceDetails: { name: string; additionalProps?: object },
) {
const store = getWorkspaceStore();
const { name, additionalProps } = newWorkspaceDetails;
const promptCheckFcn = getPromptBeforeClosingFcn(workspace.name);
const proceed = () => {
workspace.closeWorkspace();
// Calling the launchPatientWorkspace again, since one of the `if` case
// might resolve, but we need to check all the cases before launching the form.
launchPatientWorkspace(name, additionalProps);
};
if (!promptCheckFcn || promptCheckFcn()) {
const currentName = workspace.title ?? workspace.name;
const prompt: Prompt = {
title: translateFrom('@openmrs/esm-patient-chart-app', 'unsavedChanges', 'You have unsaved changes'),
body: translateFrom(
'@openmrs/esm-patient-chart-app',
'unsavedChangesInForm',
'There are unsaved changes in {{formName}}. Please save them before opening another form.',
{ formName: currentName },
),
onConfirm: () => {
store.setState({
prompt: null,
});
proceed();
},
confirmText: translateFrom('@openmrs/esm-patient-chart-app', 'openAnyway', 'Open anyway'),
};
store.setState((state) => ({ ...state, prompt }));
} else {
proceed();
}
}
/**
* Given a workspace specified by its name:
*
* - If no workspaces are open, or if no other workspaces with the same type are open,
* it will be opened and focused.
* - If a workspace with the same name is already open, it will be displayed/focused,
* if it was not already.
* - If a workspace is launched and a workspace which cannot be hidden is already open,
* a confirmation modal will pop up warning about closing the currently open workspace.
* - If another workspace with the same type is open, the workspace will be brought to
* the front and then a confirmation modal will pop up warning about closing the opened
* workspace
*
* Note that this function just manipulates the workspace store. The UI logic based on
* the workspace store is contained in esm-patient-chart-app.
*
* @param name The name of the workspace to launch
* @param additionalProps Props to pass to the workspace component being launched
*/
export function launchPatientWorkspace(name: string, additionalProps?: WorkspaceAdditionalProps) {
const store = getWorkspaceStore();
const workspace = getWorkspaceRegistration(name);
const newWorkspace = {
...workspace,
closeWorkspace: (ignoreChanges = true) => closeWorkspace(name, ignoreChanges, additionalProps?.onCloseWorkspace),
promptBeforeClosing: (testFcn) => promptBeforeClosing(name, testFcn),
additionalProps,
};
const updateStoreWithNewWorkspace = (workspaceToBeAdded: OpenWorkspace, restWorkspaces = null) => {
store.setState((state) => {
const openWorkspaces = [workspaceToBeAdded, ...(restWorkspaces ?? state.openWorkspaces)];
let workspaceWindowState = state.workspaceWindowState;
if (workspaceWindowState === 'hidden') {
workspaceWindowState = workspaceToBeAdded.preferredWindowSize === 'maximized' ? 'maximized' : 'normal';
}
return {
...state,
openWorkspaces,
workspaceWindowState,
};
});
};
const openWorkspaces = store.getState().openWorkspaces;
const workspaceIndexInOpenWorkspaces = openWorkspaces.findIndex((w) => w.name === name);
const isWorkspaceAlreadyOpen = workspaceIndexInOpenWorkspaces >= 0;
const openedWorkspaceWithSameType = openWorkspaces.find((w) => w.type == newWorkspace.type);
if (openWorkspaces.length === 0) {
updateStoreWithNewWorkspace(newWorkspace);
} else if (!openWorkspaces[0].canHide && workspaceIndexInOpenWorkspaces !== 0) {
promptBeforeLaunchingWorkspace(openWorkspaces[0], {
name,
additionalProps,
});
} else if (isWorkspaceAlreadyOpen) {
openWorkspaces[workspaceIndexInOpenWorkspaces].additionalProps = newWorkspace.additionalProps;
const restOfWorkspaces = openWorkspaces.filter((w) => w.name != name);
updateStoreWithNewWorkspace(openWorkspaces[workspaceIndexInOpenWorkspaces], restOfWorkspaces);
} else if (!!openedWorkspaceWithSameType) {
const restOfWorkspaces = store.getState().openWorkspaces.filter((w) => w.type != newWorkspace.type);
updateStoreWithNewWorkspace(openedWorkspaceWithSameType, restOfWorkspaces);
promptBeforeLaunchingWorkspace(openedWorkspaceWithSameType, {
name,
additionalProps,
});
} else {
updateStoreWithNewWorkspace(newWorkspace);
}
}
export function launchPatientChartWithWorkspaceOpen({
patientUuid,
workspaceName,
dashboardName,
additionalProps,
}: {
patientUuid: string;
workspaceName: string;
dashboardName?: string;
additionalProps?: object;
}) {
changeWorkspaceContext(patientUuid);
launchPatientWorkspace(workspaceName, additionalProps);
navigate({ to: '${openmrsSpaBase}/patient/' + `${patientUuid}/chart` + (dashboardName ? `/${dashboardName}` : '') });
}
const promptBeforeClosingFcns = {};
export function promptBeforeClosing(workspaceName: string, testFcn: () => boolean) {
promptBeforeClosingFcns[workspaceName] = testFcn;
}
export function getPromptBeforeClosingFcn(workspaceName: string) {
return promptBeforeClosingFcns[workspaceName];
}
export function cancelPrompt() {
const store = getWorkspaceStore();
const state = store.getState();
store.setState({ ...state, prompt: null });
}
export function closeWorkspace(name: string, ignoreChanges: boolean, onCloseWorkspace?: () => void) {
const store = getWorkspaceStore();
const promptCheckFcn = getPromptBeforeClosingFcn(name);
const updateStoreWithClosedWorkspace = () => {
const state = store.getState();
const newOpenWorkspaces = state.openWorkspaces.filter((w) => w.name !== name);
store.setState({
...state,
prompt: null,
openWorkspaces: newOpenWorkspaces,
});
if (onCloseWorkspace && typeof onCloseWorkspace === 'function') {
onCloseWorkspace?.();
}
};
if (!ignoreChanges && promptCheckFcn && promptCheckFcn()) {
const prompt: Prompt = {
title: translateFrom('@openmrs/esm-patient-chart-app', 'unsavedChangesTitleText', 'Unsaved Changes'),
body: translateFrom(
'@openmrs/esm-patient-chart-app',
'unsavedChangeText',
`You have unsaved changes in the side panel. Do you want to discard these changes?`,
),
onConfirm: () => {
updateStoreWithClosedWorkspace();
},
confirmText: translateFrom('@openmrs/esm-patient-chart-app', 'discard', 'Discard'),
};
store.setState({ ...store.getState(), prompt });
} else {
updateStoreWithClosedWorkspace();
}
}
export function closeAllWorkspaces() {
const store = getWorkspaceStore();
const state = store.getState();
store.setState({ ...state, openWorkspaces: [] });
}
/**
* The set of workspaces is specific to a particular patient. This function
* should be used when setting up workspaces for a new patient. If the current
* workspace data is for a different patient, the workspace state is cleared.
*/
export function changeWorkspaceContext(patientUuid) {
const store = getWorkspaceStore();
const state = store.getState();
if (state.patientUuid != patientUuid) {
store.setState({ patientUuid, openWorkspaces: [], prompt: null });
}
}
const initialState: WorkspaceStoreState = {
patientUuid: null,
openWorkspaces: [],
prompt: null,
workspaceWindowState: 'normal',
};
export function getWorkspaceStore() {
return getGlobalStore<WorkspaceStoreState>('workspace', initialState);
}
export function updateWorkspaceWindowState(value: WorkspaceWindowState) {
const store = getWorkspaceStore();
const state = store.getState();
store.setState({ ...state, workspaceWindowState: value });
}
/**
* @internal
* Just for testing.
*/
export function resetWorkspaceStore() {
getWorkspaceStore().setState(initialState);
}