fix: #9474, load hooks on page load

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 397baf0254
commit 1af34b43e3

@ -13,6 +13,11 @@ ajaxify = window.ajaxify || {};
ajaxify.count = 0; ajaxify.count = 0;
ajaxify.currentPage = null; ajaxify.currentPage = null;
var hooks;
require(['hooks'], function (_hooks) {
hooks = _hooks;
});
ajaxify.go = function (url, callback, quiet) { ajaxify.go = function (url, callback, quiet) {
// Automatically reconnect to socket and re-ajaxify on success // Automatically reconnect to socket and re-ajaxify on success
if (!socket.connected) { if (!socket.connected) {
@ -54,9 +59,7 @@ ajaxify = window.ajaxify || {};
// If any listeners alter url and set it to an empty string, abort the ajaxification // If any listeners alter url and set it to an empty string, abort the ajaxification
if (url === null) { if (url === null) {
require(['hooks'], function (hooks) { hooks.fire('action:ajaxify.end', { url: url, tpl_url: ajaxify.data.template.name, title: ajaxify.data.title });
hooks.fire('action:ajaxify.end', { url: url, tpl_url: ajaxify.data.template.name, title: ajaxify.data.title });
});
return false; return false;
} }
@ -115,9 +118,7 @@ ajaxify = window.ajaxify || {};
url: url, url: url,
}; };
require(['hooks'], function (hooks) { hooks.fire('action:ajaxify.start', payload);
hooks.fire('action:ajaxify.start', payload);
});
ajaxify.count += 1; ajaxify.count += 1;
@ -291,9 +292,7 @@ ajaxify = window.ajaxify || {};
window.scrollTo(0, 0); window.scrollTo(0, 0);
} }
ajaxify.loadScript(tpl_url, function done() { ajaxify.loadScript(tpl_url, function done() {
require(['hooks'], function (hooks) { hooks.fire('action:ajaxify.end', { url: url, tpl_url: tpl_url, title: ajaxify.data.title });
hooks.fire('action:ajaxify.end', { url: url, tpl_url: tpl_url, title: ajaxify.data.title });
});
}); });
ajaxify.widgets.render(tpl_url); ajaxify.widgets.render(tpl_url);
@ -322,55 +321,53 @@ ajaxify = window.ajaxify || {};
}; };
ajaxify.loadScript = function (tpl_url, callback) { ajaxify.loadScript = function (tpl_url, callback) {
require(['hooks'], (hooks) => { var location = !app.inAdmin ? 'forum/' : '';
var location = !app.inAdmin ? 'forum/' : '';
if (tpl_url.startsWith('admin')) { if (tpl_url.startsWith('admin')) {
location = ''; location = '';
} }
const data = { const data = {
tpl_url: tpl_url, tpl_url: tpl_url,
scripts: [location + tpl_url], scripts: [location + tpl_url],
}; };
// Hint: useful if you want to load a module on a specific page (append module name to `scripts`)
hooks.fire('action:script.load', data);
hooks.fire('filter:script.load', data).then((data) => {
// Require and parse modules
var outstanding = data.scripts.length;
data.scripts.map(function (script) { // Hint: useful if you want to load a module on a specific page (append module name to `scripts`)
if (typeof script === 'function') { hooks.fire('action:script.load', data);
return function (next) { hooks.fire('filter:script.load', data).then((data) => {
script(); // Require and parse modules
next(); var outstanding = data.scripts.length;
};
} data.scripts.map(function (script) {
if (typeof script === 'string') { if (typeof script === 'function') {
return function (next) { return function (next) {
require([script], function (module) { script();
// Hint: useful if you want to override a loaded library (e.g. replace core client-side logic), next();
// or call a method other than .init() };
hooks.fire('static:script.init', { tpl_url, name: script, module }).then(() => { }
if (module && module.init) { if (typeof script === 'string') {
module.init(); return function (next) {
} require([script], function (module) {
next(); // Hint: useful if you want to override a loaded library (e.g. replace core client-side logic),
}); // or call a method other than .init()
}, function () { hooks.fire('static:script.init', { tpl_url, name: script, module }).then(() => {
// ignore 404 error if (module && module.init) {
module.init();
}
next(); next();
}); });
}; }, function () {
// ignore 404 error
next();
});
};
}
return null;
}).filter(Boolean).forEach(function (fn) {
fn(function () {
outstanding -= 1;
if (outstanding === 0) {
callback();
} }
return null;
}).filter(Boolean).forEach(function (fn) {
fn(function () {
outstanding -= 1;
if (outstanding === 0) {
callback();
}
});
}); });
}); });
}); });

Loading…
Cancel
Save