|
1 | 1 | const { render } = require('./src/render.js'); |
| 2 | +const { tokenize } = require('./src/tokenize.js'); |
2 | 3 |
|
3 | 4 | const { getMilestonesFromElementById } = require('@proceed/bpmn-helper/src/getters'); |
4 | 5 | /** |
@@ -446,9 +447,76 @@ function inlineUserTaskData(html, variables, milestones) { |
446 | 447 | return finalHtml; |
447 | 448 | } |
448 | 449 |
|
| 450 | +/** |
| 451 | + * Function that returns all occurences of variables of the form "@global..." in placeholders |
| 452 | + * |
| 453 | + * @param {string | Buffer} html the html that contains placeholders that might reference global |
| 454 | + * variables |
| 455 | + * @returns {string[]} an array with all unique occurences of variable placholders |
| 456 | + */ |
| 457 | +function getGlobalVariableReferences(html) { |
| 458 | + if (Buffer.isBuffer(html)) html = html.toString(); |
| 459 | + |
| 460 | + const globalDataVariables = tokenize(html); |
| 461 | + |
| 462 | + const uniqueGlobalDataVariables = globalDataVariables |
| 463 | + .filter( |
| 464 | + (token, index) => |
| 465 | + token.type === 'variable' && |
| 466 | + token.variableName.startsWith('@global') && |
| 467 | + !globalDataVariables |
| 468 | + .slice(0, index) |
| 469 | + .some((entry) => entry.variableName === token.variableName), |
| 470 | + ) |
| 471 | + .map(({ variableName }) => variableName.split('.').slice(1).join('.')); |
| 472 | + |
| 473 | + return uniqueGlobalDataVariables; |
| 474 | +} |
| 475 | + |
| 476 | +/** |
| 477 | + * Function that will create an object containing the values for global variables referenced in the |
| 478 | + * given html |
| 479 | + * |
| 480 | + * @param {string | Buffer} html the html that contains placeholders that might reference global |
| 481 | + * variables |
| 482 | + * @param {(varPath: string) => Promise<any>} variableGetterFn a function that given a path to a |
| 483 | + * specific global variable returns the value of that variable |
| 484 | + * @returns {Promise<{ [key: string]: any }>} an object that maps the global variable paths in the html to |
| 485 | + * values returned by the provided varaibleGetterFn |
| 486 | + */ |
| 487 | +async function getGlobalVariables(html, variableGetterFn) { |
| 488 | + const variableReferences = getGlobalVariableReferences(html); |
| 489 | + |
| 490 | + const globalVarPromises = variableReferences.map(async (varPath) => [ |
| 491 | + `@global.${varPath}`, |
| 492 | + await variableGetterFn(varPath), |
| 493 | + ]); |
| 494 | + |
| 495 | + const globalVariables = await Promise.all(globalVarPromises); |
| 496 | + |
| 497 | + const variables = {}; |
| 498 | + |
| 499 | + globalVariables.forEach(([varPath, value]) => { |
| 500 | + const path = varPath.split('.'); |
| 501 | + const [varName] = path.splice(path.length - 1); |
| 502 | + let current = variables; |
| 503 | + for (const entry of path) { |
| 504 | + if (!(entry in current)) { |
| 505 | + current[entry] = {}; |
| 506 | + } |
| 507 | + current = current[entry]; |
| 508 | + } |
| 509 | + current[varName] = value; |
| 510 | + }); |
| 511 | + |
| 512 | + return variables; |
| 513 | +} |
| 514 | + |
449 | 515 | module.exports = { |
450 | 516 | getCorrectVariableState, |
451 | 517 | getCorrectMilestoneState, |
| 518 | + getGlobalVariableReferences, |
| 519 | + getGlobalVariables, |
452 | 520 | inlineScript, |
453 | 521 | inlineUserTaskData, |
454 | 522 | }; |
0 commit comments