Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possiblity to load custom javascript on startup (#6457) #6466

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ services:
volumes:
- "./ui-config:/usr/share/nginx/html/config"
- "./nginx.conf:/etc/nginx/conf.d/default.conf"
- "./externalJs:/usr/share/nginx/html/externalJs"
- "../../src/test/externalWebAppExample:/usr/share/nginx/html/external/appExample"
cards-external-diffusion:
container_name: cards-external-diffusion
Expand Down
9 changes: 9 additions & 0 deletions config/docker/externalJs/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Copyright (c) 2024, RTE (http://www.rte-france.com)
* See AUTHORS.txt
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
* This file is part of the OperatorFabric project.
*/
console.log('Custom js example executed');
3 changes: 2 additions & 1 deletion config/docker/ui-config/web-ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,6 @@
]
}
}
}
},
"customJsToLoad": ["http://localhost:2002/externalJs/example.js"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ a|authentication mode, available options:

|usercard.useDescriptionFieldForEntityList|false|no|If true, show entity `description` field instead of `name` in user card page

|customJsToLoad||no|List of URLs of javascript files to be loaded at startup

|===

Expand Down
5 changes: 5 additions & 0 deletions src/docs/asciidoc/reference_doc/template_description.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -777,3 +777,8 @@ To show the rich message in an HTML element you can call the `opfab.richTextEdit
The library https://www.chartjs.org/[charts.js] is integrated in OperatorFabric, it means it's possible to show charts in cards, you can find a bundle example in the operator fabric git (https://github.com/opfab/operatorfabric-core/tree/develop/src/test/resources/bundles/defaultProcess_V1[src/test/resources/bundle/defaultProcess_V1]).

The version of chartjs integrated in OperatorFabric is v3.7.1.

== Custom javascript files

It is possibile to configure Opfab to load custom javascript files at startup. This allows to share common functions and business logic between templates.
The list of URLs of javascript files to be loaded can be configured using `customJsToLoad` parameter in `web-ui.json` file.
12 changes: 12 additions & 0 deletions ui/main/src/app/business/application-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class ApplicationLoader {
this.initServices();
loadBuildInTemplates();
this.initOpfabAPI();
await this.loadCustomScripts();
await this.waitForStreamInitDone();
this.goToEntryPage();
this.loadingInProgress = false;
Expand Down Expand Up @@ -306,4 +307,15 @@ export class ApplicationLoader {
RouterService.navigateTo(defaultEntryPage);
}
}

private async loadCustomScripts() {
const customScripts = ConfigService.getConfigValue('customJsToLoad');
if (customScripts) {
for (const script of customScripts) {
await import(/* webpackIgnore: true */ script).catch((err) =>
logger.error('Error loading custom script ' + script + ' : ' + err)
);
}
}
}
}