mirror of
https://github.com/discourse/discourse.git
synced 2024-12-23 06:16:07 +08:00
3e3c051164
This lays the groundwork for converting SelectKit subclasses to native class syntax. This commit is designed to be entirely backwards-compatible, so it should not affect any existing subclasses. Of interest: - Any properties which are designed to be overridden by subclasses are implemented using a local `@protoProp` decorator. That means they are applied to the prototype, so that they can be overridden in subclasses by both legacy `.extend()` prototype extensions, and by modern native-class fields. - New class decorators are introduced: `@selectKitOptions` and `@pluginApiIdentifiers`. These are native class versions of the legacy `concatenatedProperties` system. This follows the pattern Ember has introduced for `@className`, `@classNameBindings`, etc.
99 lines
3.0 KiB
JavaScript
99 lines
3.0 KiB
JavaScript
import { isNone } from "@ember/utils";
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
|
|
|
let _appendContentCallbacks = {};
|
|
function appendContent(pluginApiIdentifiers, contentFunction) {
|
|
if (isNone(_appendContentCallbacks[pluginApiIdentifiers])) {
|
|
_appendContentCallbacks[pluginApiIdentifiers] = [];
|
|
}
|
|
|
|
_appendContentCallbacks[pluginApiIdentifiers].push(contentFunction);
|
|
}
|
|
|
|
let _prependContentCallbacks = {};
|
|
function prependContent(targetedIdentifier, contentFunction) {
|
|
if (isNone(_prependContentCallbacks[targetedIdentifier])) {
|
|
_prependContentCallbacks[targetedIdentifier] = [];
|
|
}
|
|
|
|
_prependContentCallbacks[targetedIdentifier].push(contentFunction);
|
|
}
|
|
|
|
let _onChangeCallbacks = {};
|
|
function onChange(pluginApiIdentifiers, mutationFunction) {
|
|
if (isNone(_onChangeCallbacks[pluginApiIdentifiers])) {
|
|
_onChangeCallbacks[pluginApiIdentifiers] = [];
|
|
}
|
|
|
|
_onChangeCallbacks[pluginApiIdentifiers].push(mutationFunction);
|
|
}
|
|
|
|
let _replaceContentCallbacks = {};
|
|
function replaceContent(pluginApiIdentifiers, contentFunction) {
|
|
if (isNone(_replaceContentCallbacks[pluginApiIdentifiers])) {
|
|
_replaceContentCallbacks[pluginApiIdentifiers] = [];
|
|
}
|
|
|
|
_replaceContentCallbacks[pluginApiIdentifiers].push(contentFunction);
|
|
}
|
|
|
|
export function applyContentPluginApiCallbacks(content, component) {
|
|
makeArray(component.pluginApiIdentifiers).forEach((key) => {
|
|
(_prependContentCallbacks[key] || []).forEach((c) => {
|
|
const prependedContent = c(component, content);
|
|
if (prependedContent) {
|
|
content = makeArray(prependedContent).concat(content);
|
|
}
|
|
});
|
|
(_appendContentCallbacks[key] || []).forEach((c) => {
|
|
const appendedContent = c(component, content);
|
|
if (appendedContent) {
|
|
content = content.concat(makeArray(appendedContent));
|
|
}
|
|
});
|
|
|
|
(_replaceContentCallbacks[key] || []).forEach((c) => {
|
|
const replacementContent = c(component, content);
|
|
if (replacementContent) {
|
|
content = makeArray(replacementContent);
|
|
}
|
|
});
|
|
});
|
|
|
|
return content;
|
|
}
|
|
|
|
export function applyOnChangePluginApiCallbacks(value, items, component) {
|
|
makeArray(component.pluginApiIdentifiers).forEach((key) => {
|
|
(_onChangeCallbacks[key] || []).forEach((c) => c(component, value, items));
|
|
});
|
|
}
|
|
|
|
export function modifySelectKit(targetedIdentifier) {
|
|
return {
|
|
appendContent: (callback) => {
|
|
appendContent(targetedIdentifier, callback);
|
|
return modifySelectKit(targetedIdentifier);
|
|
},
|
|
prependContent: (callback) => {
|
|
prependContent(targetedIdentifier, callback);
|
|
return modifySelectKit(targetedIdentifier);
|
|
},
|
|
onChange: (callback) => {
|
|
onChange(targetedIdentifier, callback);
|
|
return modifySelectKit(targetedIdentifier);
|
|
},
|
|
replaceContent: (callback) => {
|
|
replaceContent(targetedIdentifier, callback);
|
|
return modifySelectKit(targetedIdentifier);
|
|
},
|
|
};
|
|
}
|
|
|
|
export function clearCallbacks() {
|
|
_appendContentCallbacks = {};
|
|
_prependContentCallbacks = {};
|
|
_onChangeCallbacks = {};
|
|
_replaceContentCallbacks = {};
|
|
}
|