discourse/app/assets/javascripts/select-kit/mixins/plugin-api.js.es6

175 lines
5.1 KiB
Plaintext
Raw Normal View History

import Mixin from "@ember/object/mixin";
2019-11-09 03:00:19 +08:00
import { isNone } from "@ember/utils";
let _appendContentCallbacks = {};
function appendContent(pluginApiIdentifiers, contentFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_appendContentCallbacks[pluginApiIdentifiers])) {
_appendContentCallbacks[pluginApiIdentifiers] = [];
}
_appendContentCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _prependContentCallbacks = {};
function prependContent(pluginApiIdentifiers, contentFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_prependContentCallbacks[pluginApiIdentifiers])) {
_prependContentCallbacks[pluginApiIdentifiers] = [];
}
_prependContentCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _modifyContentCallbacks = {};
function modifyContent(pluginApiIdentifiers, contentFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_modifyContentCallbacks[pluginApiIdentifiers])) {
_modifyContentCallbacks[pluginApiIdentifiers] = [];
}
_modifyContentCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _modifyHeaderComputedContentCallbacks = {};
function modifyHeaderComputedContent(pluginApiIdentifiers, contentFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_modifyHeaderComputedContentCallbacks[pluginApiIdentifiers])) {
_modifyHeaderComputedContentCallbacks[pluginApiIdentifiers] = [];
}
2018-06-15 23:03:24 +08:00
_modifyHeaderComputedContentCallbacks[pluginApiIdentifiers].push(
contentFunction
);
}
let _modifyCollectionHeaderCallbacks = {};
function modifyCollectionHeader(pluginApiIdentifiers, contentFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_modifyCollectionHeaderCallbacks[pluginApiIdentifiers])) {
_modifyCollectionHeaderCallbacks[pluginApiIdentifiers] = [];
}
_modifyCollectionHeaderCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _onSelectNoneCallbacks = {};
function onSelectNone(pluginApiIdentifiers, mutationFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_onSelectNoneCallbacks[pluginApiIdentifiers])) {
_onSelectNoneCallbacks[pluginApiIdentifiers] = [];
}
_onSelectNoneCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
let _onSelectCallbacks = {};
function onSelect(pluginApiIdentifiers, mutationFunction) {
2019-11-09 03:00:19 +08:00
if (isNone(_onSelectCallbacks[pluginApiIdentifiers])) {
_onSelectCallbacks[pluginApiIdentifiers] = [];
}
_onSelectCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
export function applyContentPluginApiCallbacks(identifiers, content, context) {
2018-06-15 23:03:24 +08:00
identifiers.forEach(key => {
(_prependContentCallbacks[key] || []).forEach(c => {
content = c()
.concat(content)
.uniqBy("id");
});
2018-06-15 23:03:24 +08:00
(_appendContentCallbacks[key] || []).forEach(c => {
content = content.concat(c()).uniqBy("id");
});
2018-06-15 23:03:24 +08:00
(_modifyContentCallbacks[key] || []).forEach(c => {
content = c(context, content).uniqBy("id");
});
});
return content;
}
2018-06-15 23:03:24 +08:00
export function applyHeaderContentPluginApiCallbacks(
identifiers,
content,
context
) {
identifiers.forEach(key => {
(_modifyHeaderComputedContentCallbacks[key] || []).forEach(c => {
content = c(context, content);
});
});
return content;
}
export function applyCollectionHeaderCallbacks(identifiers, content, context) {
2018-06-15 23:03:24 +08:00
identifiers.forEach(key => {
(_modifyCollectionHeaderCallbacks[key] || []).forEach(c => {
content = c(context, content);
});
});
return content;
}
export function applyOnSelectPluginApiCallbacks(identifiers, val, context) {
2018-06-15 23:03:24 +08:00
identifiers.forEach(key => {
(_onSelectCallbacks[key] || []).forEach(c => c(context, val));
});
}
export function applyOnSelectNonePluginApiCallbacks(identifiers, context) {
identifiers.forEach(key => {
(_onSelectNoneCallbacks[key] || []).forEach(c => c(context));
});
}
export function modifySelectKit(pluginApiIdentifiers) {
return {
2018-06-15 23:03:24 +08:00
appendContent: content => {
appendContent(pluginApiIdentifiers, () => {
return content;
});
return modifySelectKit(pluginApiIdentifiers);
},
2018-06-15 23:03:24 +08:00
prependContent: content => {
prependContent(pluginApiIdentifiers, () => {
return content;
});
return modifySelectKit(pluginApiIdentifiers);
},
2018-06-15 23:03:24 +08:00
modifyContent: callback => {
modifyContent(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
2018-06-15 23:03:24 +08:00
modifyHeaderComputedContent: callback => {
modifyHeaderComputedContent(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
2018-06-15 23:03:24 +08:00
modifyCollectionHeader: callback => {
modifyCollectionHeader(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
2018-06-15 23:03:24 +08:00
onSelect: callback => {
onSelect(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
onSelectNone: callback => {
onSelectNone(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
}
};
}
export function clearCallbacks() {
_appendContentCallbacks = {};
_prependContentCallbacks = {};
_modifyContentCallbacks = {};
_modifyHeaderComputedContentCallbacks = {};
_modifyCollectionHeaderCallbacks = {};
_onSelectCallbacks = {};
_onSelectNoneCallbacks = {};
}
const EMPTY_ARRAY = Object.freeze([]);
export default Mixin.create({
concatenatedProperties: ["pluginApiIdentifiers"],
pluginApiIdentifiers: EMPTY_ARRAY
});