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