From 59f721934e9da9fee229f9cc53a81e2935c3d80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 4 Jun 2024 21:51:42 +0200 Subject: [PATCH] Refactor export download script to use ES6 and webpack The export download script was refactored to use ES6 syntax and webpack's modular system. This included separating out the download script into its own file for better organization, removing globally-scoped JavaScript, and adding the new download script as a webpack entry point. Also, the import method for the 'mime' library was adjusted to use ES6 syntax. --- .../lib/download-report/download-report.js | 6 ++---- .../public/page/export/download-export.js | 16 ++++++++++++++++ .../Resources/views/Export/download.html.twig | 14 ++++++-------- .../ChillMainBundle/chill.webpack.config.js | 2 ++ 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Resources/public/page/export/download-export.js diff --git a/src/Bundle/ChillMainBundle/Resources/public/lib/download-report/download-report.js b/src/Bundle/ChillMainBundle/Resources/public/lib/download-report/download-report.js index 041e94c45..afbbe071a 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/lib/download-report/download-report.js +++ b/src/Bundle/ChillMainBundle/Resources/public/lib/download-report/download-report.js @@ -15,9 +15,9 @@ * along with this program. If not, see . */ -var mime = require('mime') +import mime from 'mime'; -var download_report = (url, container) => { +export const download_report = (url, container) => { var download_text = container.dataset.downloadText, alias = container.dataset.alias; @@ -63,5 +63,3 @@ var download_report = (url, container) => { .replaceChild(problem_text, container.firstChild); }); }; - -module.exports = download_report; diff --git a/src/Bundle/ChillMainBundle/Resources/public/page/export/download-export.js b/src/Bundle/ChillMainBundle/Resources/public/page/export/download-export.js new file mode 100644 index 000000000..a0559fdad --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/public/page/export/download-export.js @@ -0,0 +1,16 @@ +import {download_report} from "../../lib/download-report/download-report"; + +window.addEventListener("DOMContentLoaded", function(e) { + const export_generate_url = window.export_generate_url; + + if (typeof export_generate_url === 'undefined') { + console.error('Alias not found!'); + throw new Error('Alias not found!'); + } + + const query = window.location.search, + container = document.querySelector("#download_container") + ; + + download_report(export_generate_url + "?" + query.toString(), container); +}); diff --git a/src/Bundle/ChillMainBundle/Resources/views/Export/download.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Export/download.html.twig index bd7dbf7e9..13d064691 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Export/download.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Export/download.html.twig @@ -22,15 +22,13 @@ {% block js %} +{{ encore_entry_link_tags('page_download_exports') }} +{% endblock %} + +{% block css %} + {{ encore_entry_script_tags('page_download_exports') }} {% endblock %} {% block content %} diff --git a/src/Bundle/ChillMainBundle/chill.webpack.config.js b/src/Bundle/ChillMainBundle/chill.webpack.config.js index 287e6ae89..6576b2e0b 100644 --- a/src/Bundle/ChillMainBundle/chill.webpack.config.js +++ b/src/Bundle/ChillMainBundle/chill.webpack.config.js @@ -52,12 +52,14 @@ module.exports = function(encore, entries) Tabs: __dirname + '/Resources/public/lib/tabs' }); + // Page entrypoints encore.addEntry('page_login', __dirname + '/Resources/public/page/login/index.js'); encore.addEntry('page_location', __dirname + '/Resources/public/page/location/index.js'); encore.addEntry('page_workflow_show', __dirname + '/Resources/public/page/workflow-show/index.js'); encore.addEntry('page_homepage_widget', __dirname + '/Resources/public/page/homepage_widget/index.js'); encore.addEntry('page_export', __dirname + '/Resources/public/page/export/index.js'); + encore.addEntry('page_download_exports', __dirname + '/Resources/public/page/export/download-export.js'); buildCKEditor(encore);