mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 16:30:34 +01:00
refactor: deprecate action:script.load, use filter:script.load instead
This commit is contained in:
@@ -320,53 +320,56 @@ ajaxify = window.ajaxify || {};
|
|||||||
};
|
};
|
||||||
|
|
||||||
ajaxify.loadScript = function (tpl_url, callback) {
|
ajaxify.loadScript = function (tpl_url, callback) {
|
||||||
var location = !app.inAdmin ? 'forum/' : '';
|
require(['hooks'], (hooks) => {
|
||||||
|
var location = !app.inAdmin ? 'forum/' : '';
|
||||||
|
|
||||||
if (tpl_url.startsWith('admin')) {
|
if (tpl_url.startsWith('admin')) {
|
||||||
location = '';
|
location = '';
|
||||||
}
|
|
||||||
var data = {
|
|
||||||
tpl_url: tpl_url,
|
|
||||||
scripts: [location + tpl_url],
|
|
||||||
};
|
|
||||||
|
|
||||||
// Hint: useful if you want to load a module on a specific page (append module name to `scripts`)
|
|
||||||
$(window).trigger('action:script.load', data);
|
|
||||||
|
|
||||||
// Require and parse modules
|
|
||||||
var outstanding = data.scripts.length;
|
|
||||||
|
|
||||||
data.scripts.map(function (script) {
|
|
||||||
if (typeof script === 'function') {
|
|
||||||
return function (next) {
|
|
||||||
script();
|
|
||||||
next();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
if (typeof script === 'string') {
|
const data = {
|
||||||
return function (next) {
|
tpl_url: tpl_url,
|
||||||
require(['hooks', script], function (hooks, module) {
|
scripts: [location + tpl_url],
|
||||||
// Hint: useful if you want to override a loaded library (e.g. replace core client-side logic),
|
};
|
||||||
// or call a method other than .init()
|
|
||||||
hooks.fire('static:script.init', { tpl_url, name: script, module }).then(() => {
|
// Hint: useful if you want to load a module on a specific page (append module name to `scripts`)
|
||||||
if (module && module.init) {
|
hooks.fire('action:script.load', data);
|
||||||
module.init();
|
hooks.fire('filter:script.load', data).then((data) => {
|
||||||
}
|
// Require and parse modules
|
||||||
|
var outstanding = data.scripts.length;
|
||||||
|
|
||||||
|
data.scripts.map(function (script) {
|
||||||
|
if (typeof script === 'function') {
|
||||||
|
return function (next) {
|
||||||
|
script();
|
||||||
next();
|
next();
|
||||||
});
|
};
|
||||||
}, function () {
|
}
|
||||||
// ignore 404 error
|
if (typeof script === 'string') {
|
||||||
next();
|
return function (next) {
|
||||||
|
require([script], function (module) {
|
||||||
|
// Hint: useful if you want to override a loaded library (e.g. replace core client-side logic),
|
||||||
|
// or call a method other than .init()
|
||||||
|
hooks.fire('static:script.init', { tpl_url, name: script, module }).then(() => {
|
||||||
|
if (module && module.init) {
|
||||||
|
module.init();
|
||||||
|
}
|
||||||
|
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();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,11 +4,28 @@ define('hooks', [], () => {
|
|||||||
const Hooks = {
|
const Hooks = {
|
||||||
loaded: {},
|
loaded: {},
|
||||||
temporary: new Set(),
|
temporary: new Set(),
|
||||||
|
deprecated: {
|
||||||
|
'action:script.load': 'filter:script.load', // 👋 @ 1.18.0
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
Hooks.register = (hookName, method) => {
|
Hooks.register = (hookName, method) => {
|
||||||
Hooks.loaded[hookName] = Hooks.loaded[hookName] || new Set();
|
Hooks.loaded[hookName] = Hooks.loaded[hookName] || new Set();
|
||||||
Hooks.loaded[hookName].add(method);
|
Hooks.loaded[hookName].add(method);
|
||||||
|
|
||||||
|
if (Hooks.deprecated.hasOwnProperty(hookName)) {
|
||||||
|
const deprecated = Hooks.deprecated[hookName];
|
||||||
|
|
||||||
|
if (deprecated) {
|
||||||
|
console.groupCollapsed(`[hooks] Hook "${hookName}" is deprecated, please use "${deprecated}" instead.`);
|
||||||
|
} else {
|
||||||
|
console.groupCollapsed(`[hooks] Hook "${hookName}" is deprecated, there is no alternative.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.info(method);
|
||||||
|
console.groupEnd();
|
||||||
|
}
|
||||||
|
|
||||||
console.debug(`[hooks] Registered ${hookName}`, method);
|
console.debug(`[hooks] Registered ${hookName}`, method);
|
||||||
};
|
};
|
||||||
Hooks.on = Hooks.register;
|
Hooks.on = Hooks.register;
|
||||||
|
|||||||
Reference in New Issue
Block a user