Skip to content

Commit

Permalink
Allow scripts prioritization based on other apps
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Dec 1, 2021
1 parent c624c7e commit 4d970b7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
4 changes: 3 additions & 1 deletion apps/files_sharing/lib/Listener/LoadAdditionalListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function handle(Event $event): void {
return;
}

Util::addScript(Application::APP_ID, 'dist/files_sharing');
// After files for the files list shared content
Util::addScript(Application::APP_ID, 'dist/files_sharing', 'files');
// After files for the breadcrumb share indicator
Util::addScript(Application::APP_ID, 'dist/additionalScripts');
Util::addStyle(Application::APP_ID, 'icons');
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/Listener/LoadSidebarListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function handle(Event $event): void {
return;
}

Util::addScript(Application::APP_ID, 'dist/files_sharing_tab');
Util::addScript(Application::APP_ID, 'dist/files_sharing_tab', 'files');
}
}
3 changes: 1 addition & 2 deletions apps/systemtags/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function boot(IBootContext $context): void {
$dispatcher->addListener(
'OCA\Files::loadAdditionalScripts',
function () {
// FIXME: no public API for these ?
\OCP\Util::addScript('dist/systemtags');
\OCP\Util::addScript('core', 'dist/systemtags');
\OCP\Util::addScript(self::APP_ID, 'systemtags');
}
);
Expand Down
4 changes: 2 additions & 2 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct($renderAs, $appId = '') {

$this->initialState->provideInitialState('core', 'active-app', $this->navigationManager->getActiveEntry());
$this->initialState->provideInitialState('unified-search', 'limit-default', SearchQuery::LIMIT_DEFAULT);
Util::addScript('dist/unified-search', null, true);
Util::addScript('core', 'dist/unified-search', 'core');

// Add navigation entry
$this->assign('application', '');
Expand Down Expand Up @@ -209,7 +209,7 @@ public function __construct($renderAs, $appId = '') {
}

// Add the js files
$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
$jsFiles = self::findJavascriptFiles(array_merge(\OC_Util::$scripts, Util::getScripts()));
$this->assign('jsfiles', []);
if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
// this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call)
Expand Down
2 changes: 2 additions & 0 deletions lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ private static function generatePath($application, $directory, $file) {
/**
* add a javascript file
*
* @deprecated 24.0.0
*
* @param string $application application id
* @param string|null $file filename
* @param bool $prepend prepend the Script to the beginning of the list
Expand Down
73 changes: 70 additions & 3 deletions lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -173,10 +176,74 @@ public static function addStyle($application, $file = null) {
* 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) {
self::addTranslations($application);
}

// init app array if it doesn't exists
if (!array_key_exists($application, self::$scripts)) {
self::$scripts[$application] = ['first' => [], 'last' => []];
}

/**
* 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 {
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($appScripts['core'], ...array_values($appScripts));
// remove duplicates
return array_unique($scripts);
}

/**
Expand Down

0 comments on commit 4d970b7

Please sign in to comment.