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