Skip to content

Commit

Permalink
Feat openemr fix openemr#7699 openemr#7698 setup header (openemr#7700)
Browse files Browse the repository at this point in the history
* Fixes openemr#7698 setupHeader changes for script tags

Made it so the config.yaml file can now have an object syntax to specify
additional attributes to add to the script tag such as the type syntax
that will be output in the HTML.

Fixes openemr#7698

* Fixes openemr#7699 questionnaire configuration

Added the questionnaire and questionnaire-lforms configuration option
that can be used in the setupHeader syntax to be able to embed
questionnaires into the form.

Fixes openemr#7699
  • Loading branch information
adunsulag authored Sep 10, 2024
1 parent bc37b3a commit 5d5c84d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
40 changes: 39 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@
# assets: Top-level key name (Required)
# asset-short-name: Short name
# basePath: %assets_static_relative%/full/path/to/deepest/common/file
# script: null filename or null
# script: null filename or null or array of filenames or array of objects
# link: jquery-ui.min.css filename or null
# autoload: true (defaults to false)
# loadInFile: relative path to file, load always in specific file (defaults to false) for custom assets.

# Example scripts config can be in the following format
# script: null
# script: filename.js
# script: - filename1.js
# - filename2.js
# script: - src: filename1.js
# type: module
# - src: filename2.js
# type: text/javascript
assets:
jquery:
basePath: '%assets_static_relative%/jquery/dist/'
Expand Down Expand Up @@ -274,3 +284,31 @@ assets:
dompurify:
basePath: '%assets_static_relative%/'
script: dompurify/dist/purify.js
questionnaires:
basePath: '%webroot%/interface/forms/questionnaire_assessments/'
script:
- src: lforms/webcomponent/assets/lib/zone.min.js
type: module
- src: lforms/webcomponent/runtime.js
type: module
- src: lforms/webcomponent/polyfills.js
type: module
- src: lforms/webcomponent/main.js
type: module
- src: lforms/fhir/R4/lformsFHIR.min.js
type: module
link: /assets/styles/styles_openemr_lforms.css
questionnaires-lform:
basePath: '%webroot%/interface/forms/questionnaire_assessments/'
script:
- src: lforms/webcomponent/assets/lib/zone.min.js
type: module
- src: lforms/webcomponent/runtime.js
type: module
- src: lforms/webcomponent/polyfills.js
type: module
- src: lforms/webcomponent/main.js
type: module
- src: lforms/fhir/R4/lformsFHIR.min.js
type: module
link: /assets/styles/styles_openemr_lforms.css
39 changes: 30 additions & 9 deletions src/Core/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ private static function buildAsset($opts = array(), $alreadyBuilt = false)
$script = (isset($opts['script'])) ? $opts['script'] : false;
$link = (isset($opts['link'])) ? $opts['link'] : false;
$path = (isset($opts['basePath'])) ? $opts['basePath'] : '';

$basePath = self::parsePlaceholders($path);

$scripts = [];
Expand All @@ -295,17 +296,24 @@ private static function buildAsset($opts = array(), $alreadyBuilt = false)
}

if (is_string($script)) {
$script = [$script];
// default is a non-module javascript file
$script = [['src' => $script, 'type' => 'text/javascript']];
}

foreach ($script as $k) {
$k = self::parsePlaceholders($k);
if (is_string($k)) {
$k = ['src' => $k, 'type' => 'text/javascript'];
} else if (empty($k['src'])) {
throw new \InvalidArgumentException("Script must be of type string or object with src property");
}
$k['src'] = self::parsePlaceholders($k['src']);
if ($alreadyBuilt) {
$path = $k;
$path = $k['src'];
} else {
$path = self::createFullPath($basePath, $k);
$path = self::createFullPath($basePath, $k['src']);
}
$scripts[] = self::createElement($path, 'script', $alreadyBuilt);
unset($k['src']);
$scripts[] = self::createElement($path, 'script', $alreadyBuilt, $k);
}
}

Expand Down Expand Up @@ -364,11 +372,24 @@ public static function parsePlaceholders($subject)
* @param string $type Must be `script` or `link`
* @return string mixed HTML element
*/
private static function createElement($path, $type, $alreadyBuilt)
private static function createElement($path, $type, $alreadyBuilt, $nodeAttributes = array())
{

$script = "<script src=\"%path%\"></script>\n";
$link = "<link rel=\"stylesheet\" href=\"%path%\" />\n";
$attrs = '';
// make sure we clear out any attributes we don't want overriden
if (isset($nodeAttributes['src'])) {
unset($nodeAttributes['src']);
}
if (isset($nodeAttributes['href'])) {
unset($nodeAttributes['href']);
}
if (isset($nodeAttributes['rel'])) {
unset($nodeAttributes['rel']);
}
foreach ($nodeAttributes as $k => $v) {
$attrs .= " " . $k . '="' . attr($v) . '"';
}
$script = "<script src=\"%path%\"" . $attrs . "></script>\n";
$link = "<link rel=\"stylesheet\" " . $attrs . " href=\"%path%\" />\n";

$template = ($type == 'script') ? $script : $link;
if (!$alreadyBuilt) {
Expand Down

0 comments on commit 5d5c84d

Please sign in to comment.