Skip to content

Commit

Permalink
Re-added initOnAjaxLoad and added usage doc
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-farre committed Sep 14, 2023
1 parent df26f8b commit a4b1f87
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions static/js/humhub/humhub.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,37 @@ var humhub = humhub || (function ($) {
*
* A module can provide an `init` function, which by default is only called after the first initialization
* e.g. after a full page load when the document is ready or when loaded by means of ajax.
* In case a modules `init` function need to be called also after each `pjax` request, the modules `initOnPjaxLoad` has to be
*
* In case a module `init` function needs to be called also after each `pjax` request, the module `initOnPjaxLoad` has to be
* set to `true`:
*
* ```
* module.initOnPjaxLoad = true;
* ```
*
* It's also possible to call a module `init` function after each `ajax` request. The module `initOnAjaxLoad` has to be
* set to `true`:
*
* ```
* module.initOnAjaxLoad = true;
* ```
*
* You will also need to specify the list of URLs for which the ajax page must call the `init` function. In your Asset class, add something like this:
*
* ```php
* public static function register($view)
* {
* $view->registerJsConfig('myModule.moduleId', [
* 'initOnAjaxUrls' => [
* Url::to(['/path']), // Don't add any params to the URL
* ],
* ]);
*
* return parent::register($view);
* }
* ```
*
*
* Dependencies:
*
* The core modules are initialized in a specific order to provide the required dependencies for each module.
Expand Down Expand Up @@ -140,23 +164,25 @@ var humhub = humhub || (function ($) {
pjaxInitModules.push(instance);
}

initOnAjaxUrls = instance.config.initOnAjaxUrls;
// Allow single URL as string
if (typeof initOnAjaxUrls === "string") {
initOnAjaxUrls = [initOnAjaxUrls];
}
if (typeof initOnAjaxUrls === 'object') {
if (instance.initOnAjaxLoad) {
$(document).on('ajaxComplete', function (event, jqXHR, ajaxOptions) {
if (ajaxOptions && ajaxOptions.url) {
var ajaxUrl = new URL('https://domain.tld' + ajaxOptions.url);
// Remove all params except `r` param (in case pretty URLs are disabled)
ajaxUrl.searchParams.forEach(function (value, name) {
if (name !== 'r') {
ajaxUrl.searchParams.delete(name);
var initOnAjaxUrls = instance.config.initOnAjaxUrls;
// Allow single URL as string
if (typeof initOnAjaxUrls === "string") {
initOnAjaxUrls = [initOnAjaxUrls];
}
if (typeof initOnAjaxUrls === 'object') {
var ajaxUrl = new URL('https://domain.tld' + ajaxOptions.url);
// Remove all params except `r` param (in case pretty URLs are disabled)
ajaxUrl.searchParams.forEach(function (value, name) {
if (name !== 'r') {
ajaxUrl.searchParams.delete(name);
}
});
if (initOnAjaxUrls.includes(ajaxUrl.pathname + ajaxUrl.search)) {
initModule(instance);
}
});
if (initOnAjaxUrls.includes(ajaxUrl.pathname + ajaxUrl.search)) {
initModule(instance);
}
}
});
Expand All @@ -175,6 +201,7 @@ var humhub = humhub || (function ($) {
var createModule = function (id, instance) {
instance.require = require;
instance.initOnPjaxLoad = false;
instance.initOnAjaxLoad = false;
instance.isModule = true;
instance.id = 'humhub.modules.' + _cutModulePrefix(id);
instance.config = require('config').module(instance);
Expand Down

0 comments on commit a4b1f87

Please sign in to comment.