summaryrefslogtreecommitdiffstats
path: root/lib/public/Util.php
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2021-11-30 15:46:09 +0100
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2021-12-02 20:00:46 +0100
commit71a3528510f0ef6e640fae79b9e30f0e639b716e (patch)
treebcdb3a8feec838ce734b0bc701c9e8671bc4707f /lib/public/Util.php
parent6b3c7037946bf63b5bd684c7f1ee3e72ea0e6148 (diff)
downloadnextcloud-server-71a3528510f0ef6e640fae79b9e30f0e639b716e.tar.gz
nextcloud-server-71a3528510f0ef6e640fae79b9e30f0e639b716e.zip
Allow scripts prioritization based on other apps
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'lib/public/Util.php')
-rw-r--r--lib/public/Util.php82
1 files changed, 78 insertions, 4 deletions
diff --git a/lib/public/Util.php b/lib/public/Util.php
index 103b65fe874..f77f8a58370 100644
--- a/lib/public/Util.php
+++ b/lib/public/Util.php
@@ -72,9 +72,12 @@ class Util {
*/
public const FATAL = 4;
- /** \OCP\Share\IManager */
+ /** @var \OCP\Share\IManager */
private static $shareManager;
+ /** @var array */
+ private static $scripts = [];
+
/**
* get the current installed version of Nextcloud
* @return array
@@ -173,10 +176,73 @@ class Util {
* add a javascript file
* @param string $application
* @param string $file
+ * @param string $afterAppId
* @since 4.0.0
*/
- public static function addScript($application, $file = null) {
- \OC_Util::addScript($application, $file);
+ public static function addScript($application, $file = null, $afterAppId = null) {
+ if (!empty($application)) {
+ $path = "$application/js/$file";
+ } else {
+ $path = "js/$file";
+ }
+
+ // Inject js translations if we load a script for
+ // a specific app that is not core, as those js files
+ // need separate handling
+ if ($application !== 'core'
+ && $file !== null
+ && strpos($file, 'l10n') === false) {
+ self::addTranslations($application);
+ }
+
+ // manage priorities if defined
+ // we store the data like this, then flatten everything
+ // [
+ // 'core' => [
+ // 'first' => [
+ // '/core/js/main.js',
+ // ],
+ // 'last' => [
+ // '/apps/viewer/js/viewer-main.js',
+ // ]
+ // ],
+ // 'viewer' => [
+ // 'first' => [
+ // '/apps/viewer/js/viewer-public.js',
+ // ],
+ // 'last' => [
+ // '/apps/files_pdfviewer/js/files_pdfviewer-main.js',
+ // ]
+ // ]
+ // ]
+ if (!empty($afterAppId)) {
+ // init afterAppId app array if it doesn't exists
+ if (!array_key_exists($afterAppId, self::$scripts)) {
+ self::$scripts[$afterAppId] = ['first' => [], 'last' => []];
+ }
+ self::$scripts[$afterAppId]['last'][] = $path;
+ } else {
+ // init app array if it doesn't exists
+ if (!array_key_exists($application, self::$scripts)) {
+ self::$scripts[$application] = ['first' => [], 'last' => []];
+ }
+ self::$scripts[$application]['first'][] = $path;
+ }
+ }
+
+ /**
+ * Return the list of scripts injected to the page
+ */
+ public static function getScripts(): array {
+ // merging first and last data set
+ $mapFunc = function (array $scriptsArray): array {
+ return array_merge(...array_values($scriptsArray));
+ };
+ $appScripts = array_map($mapFunc, self::$scripts);
+ // sort core first
+ $scripts = array_merge(isset($appScripts['core']) ? $appScripts['core'] : [], ...array_values($appScripts));
+ // remove duplicates
+ return array_unique($scripts);
}
/**
@@ -186,7 +252,15 @@ class Util {
* @since 8.0.0
*/
public static function addTranslations($application, $languageCode = null) {
- \OC_Util::addTranslations($application, $languageCode);
+ if (is_null($languageCode)) {
+ $languageCode = \OC::$server->getL10NFactory()->findLanguage($application);
+ }
+ if (!empty($application)) {
+ $path = "$application/l10n/$languageCode";
+ } else {
+ $path = "l10n/$languageCode";
+ }
+ self::$scripts[$application]['first'][] = $path;
}
/**