2019-11-09 03:00:19 +08:00
|
|
|
import { isNone } from "@ember/utils";
|
2020-02-19 07:57:58 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2019-10-31 03:03:08 +08:00
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
let _appendContentCallbacks = {};
|
|
|
|
function appendContent(pluginApiIdentifiers, contentFunction) {
|
2019-11-09 03:00:19 +08:00
|
|
|
if (isNone(_appendContentCallbacks[pluginApiIdentifiers])) {
|
2017-11-21 18:53:09 +08:00
|
|
|
_appendContentCallbacks[pluginApiIdentifiers] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
_appendContentCallbacks[pluginApiIdentifiers].push(contentFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _prependContentCallbacks = {};
|
2020-05-06 23:16:20 +08:00
|
|
|
function prependContent(targetedIdentifier, contentFunction) {
|
|
|
|
if (isNone(_prependContentCallbacks[targetedIdentifier])) {
|
|
|
|
_prependContentCallbacks[targetedIdentifier] = [];
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
_prependContentCallbacks[targetedIdentifier].push(contentFunction);
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
let _onChangeCallbacks = {};
|
|
|
|
function onChange(pluginApiIdentifiers, mutationFunction) {
|
|
|
|
if (isNone(_onChangeCallbacks[pluginApiIdentifiers])) {
|
|
|
|
_onChangeCallbacks[pluginApiIdentifiers] = [];
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
_onChangeCallbacks[pluginApiIdentifiers].push(mutationFunction);
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
|
2022-10-13 02:38:42 +08:00
|
|
|
let _replaceContentCallbacks = {};
|
|
|
|
function replaceContent(pluginApiIdentifiers, contentFunction) {
|
|
|
|
if (isNone(_replaceContentCallbacks[pluginApiIdentifiers])) {
|
|
|
|
_replaceContentCallbacks[pluginApiIdentifiers] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
_replaceContentCallbacks[pluginApiIdentifiers].push(contentFunction);
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
export function applyContentPluginApiCallbacks(content, component) {
|
|
|
|
makeArray(component.pluginApiIdentifiers).forEach((key) => {
|
2017-11-21 18:53:09 +08:00
|
|
|
(_prependContentCallbacks[key] || []).forEach((c) => {
|
2020-05-14 20:38:03 +08:00
|
|
|
const prependedContent = c(component, content);
|
|
|
|
if (prependedContent) {
|
|
|
|
content = makeArray(prependedContent).concat(content);
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
});
|
|
|
|
(_appendContentCallbacks[key] || []).forEach((c) => {
|
2020-05-14 20:38:03 +08:00
|
|
|
const appendedContent = c(component, content);
|
|
|
|
if (appendedContent) {
|
|
|
|
content = content.concat(makeArray(appendedContent));
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
});
|
2022-10-13 02:38:42 +08:00
|
|
|
|
|
|
|
(_replaceContentCallbacks[key] || []).forEach((c) => {
|
|
|
|
const replacementContent = c(component, content);
|
|
|
|
if (replacementContent) {
|
|
|
|
content = makeArray(replacementContent);
|
|
|
|
}
|
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
export function applyOnChangePluginApiCallbacks(value, items, component) {
|
|
|
|
makeArray(component.pluginApiIdentifiers).forEach((key) => {
|
|
|
|
(_onChangeCallbacks[key] || []).forEach((c) => c(component, value, items));
|
2018-01-09 17:52:32 +08:00
|
|
|
});
|
2020-02-03 21:22:14 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:20 +08:00
|
|
|
export function modifySelectKit(targetedIdentifier) {
|
2017-11-21 18:53:09 +08:00
|
|
|
return {
|
2020-05-06 23:16:20 +08:00
|
|
|
appendContent: (callback) => {
|
|
|
|
appendContent(targetedIdentifier, callback);
|
|
|
|
return modifySelectKit(targetedIdentifier);
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
2020-05-06 23:16:20 +08:00
|
|
|
prependContent: (callback) => {
|
|
|
|
prependContent(targetedIdentifier, callback);
|
|
|
|
return modifySelectKit(targetedIdentifier);
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
2020-05-06 23:16:20 +08:00
|
|
|
onChange: (callback) => {
|
|
|
|
onChange(targetedIdentifier, callback);
|
|
|
|
return modifySelectKit(targetedIdentifier);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
2022-10-13 02:38:42 +08:00
|
|
|
replaceContent: (callback) => {
|
|
|
|
replaceContent(targetedIdentifier, callback);
|
|
|
|
return modifySelectKit(targetedIdentifier);
|
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearCallbacks() {
|
|
|
|
_appendContentCallbacks = {};
|
|
|
|
_prependContentCallbacks = {};
|
2020-05-06 23:16:20 +08:00
|
|
|
_onChangeCallbacks = {};
|
2022-10-13 02:38:42 +08:00
|
|
|
_replaceContentCallbacks = {};
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|