Skip to content

Commit 0285479

Browse files
committed
moved logic to json parser, removed full url check (now handled in cs-web-lib)
1 parent 491591e commit 0285479

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

src/utils/csWebLibActions.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export const executeOpenPageActionWithFileMetadata = (
4343
fileContext: any,
4444
overrideMacros?: MacroMap
4545
) => {
46-
// get macros
46+
let filePath = fileMetadata?.file ?? beamlineState.topLevelScreen;
47+
4748
const fileMetadataMacros: MacroMap =
4849
fileMetadata?.macros && fileMetadata?.macros.length > 0
4950
? fileMetadata?.macros[0]
@@ -53,26 +54,7 @@ export const executeOpenPageActionWithFileMetadata = (
5354
? overrideMacros
5455
: fileMetadataMacros;
5556

56-
// TEMP FIX - adding colon as PVs using M macros don't work because they are missing a colon
57-
if (selectedMacros.M && !selectedMacros.M.startsWith(":")) {
58-
selectedMacros.M = ":" + selectedMacros.M;
59-
}
60-
61-
let filePath = fileMetadata?.file ?? beamlineState.topLevelScreen;
62-
63-
// replace macro values first
64-
Object.entries(selectedMacros).forEach(([key, value]) => {
65-
filePath = filePath.replace(`$(${key})`, value);
66-
});
67-
68-
// check if the filepath is now already a url
69-
const isFullUrl =
70-
filePath.startsWith("http://") || filePath.startsWith("https://");
71-
72-
// build screen from url or relative path
73-
const newScreen = isFullUrl
74-
? filePath
75-
: buildUrl(beamlineState.host, filePath);
57+
const newScreen = buildUrl(beamlineState.host, filePath);
7658

7759
const beamlineUrlId = `/synoptic/${selectedBeamlineId}`;
7860

src/utils/parser.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,31 @@ export const RecursiveAppendDuplicateFileMacros = (
174174
if (!jsonSiblings) {
175175
return;
176176
}
177-
178177
for (const sibling of jsonSiblings) {
179-
if (sibling.duplicate && sibling.macros) {
178+
if (sibling.macros) {
179+
// TEMP?
180+
addMColon(sibling.macros);
181+
182+
// Substitute macros into the filename
183+
const filename =
184+
sibling.file && sibling.macros
185+
? substituteMacrosIntoFilename(sibling.file, sibling.macros)
186+
: sibling.file;
187+
188+
// Match against unresolved filename
180189
const matchingFileKey = Object.keys(fileMap).find(
181190
key => fileMap[key].file === sibling.file
182191
);
183192
if (matchingFileKey) {
184-
fileMap[matchingFileKey].macros = fileMap[matchingFileKey].macros
185-
? [...fileMap[matchingFileKey].macros, sibling.macros]
186-
: [sibling.macros];
193+
// Replace with resolved filename
194+
fileMap[matchingFileKey].file = filename;
195+
196+
if (sibling.duplicate) {
197+
// Attach macros
198+
fileMap[matchingFileKey].macros = fileMap[matchingFileKey].macros
199+
? [...fileMap[matchingFileKey].macros, sibling.macros]
200+
: [sibling.macros];
201+
}
187202
}
188203
}
189204

@@ -215,3 +230,22 @@ export const buildUrlId = (
215230
const urlId = `${idPrefix}${idPrefix === "" ? "" : "+"}${fileLabel}`;
216231
return { urlId, fileLabel };
217232
};
233+
234+
// TEMP? add colon to M macro
235+
const addMColon = (macros: any) => {
236+
if (macros.M && !macros.M.startsWith(":")) {
237+
macros.M = ":" + macros.M;
238+
}
239+
};
240+
241+
// Substitute macros into a filename
242+
const substituteMacrosIntoFilename = (
243+
filename: string,
244+
macros: any
245+
): string => {
246+
let resolvedFilename = filename;
247+
Object.entries(macros).forEach(([key, value]) => {
248+
resolvedFilename = resolvedFilename.replace(`$(${key})`, value as string);
249+
});
250+
return resolvedFilename;
251+
};

0 commit comments

Comments
 (0)