parent
baf78d3d91
commit
5bd29b89dd
|
@ -87,6 +87,10 @@ export function addPostSmallActionClassesCallback(callback) {
|
|||
addPostSmallActionClassesCallbacks.push(callback);
|
||||
}
|
||||
|
||||
export function resetPostSmallActionClassesCallbacks() {
|
||||
addPostSmallActionClassesCallbacks.length = 0;
|
||||
}
|
||||
|
||||
export default createWidget("post-small-action", {
|
||||
buildKey: (attrs) => `post-small-act-${attrs.id}`,
|
||||
tagName: "div.small-action.onscreen-post",
|
||||
|
@ -97,13 +101,15 @@ export default createWidget("post-small-action", {
|
|||
|
||||
buildClasses(attrs) {
|
||||
let classNames = [];
|
||||
|
||||
if (attrs.deleted) {
|
||||
classNames.push("deleted");
|
||||
}
|
||||
|
||||
if (addPostSmallActionClassesCallbacks) {
|
||||
if (addPostSmallActionClassesCallbacks.length > 0) {
|
||||
addPostSmallActionClassesCallbacks.forEach((callback) => {
|
||||
const additionalClasses = callback.call(this, attrs);
|
||||
|
||||
if (additionalClasses) {
|
||||
classNames.push(...additionalClasses);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
|||
import { render } from "@ember/test-helpers";
|
||||
import { exists } from "discourse/tests/helpers/qunit-helpers";
|
||||
import { hbs } from "ember-cli-htmlbars";
|
||||
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||
import { resetPostSmallActionClassesCallbacks } from "discourse/widgets/post-small-action";
|
||||
|
||||
module(
|
||||
"Integration | Component | Widget | post-small-action",
|
||||
|
@ -89,5 +91,41 @@ module(
|
|||
"it adds the recover small action button"
|
||||
);
|
||||
});
|
||||
|
||||
test("`addPostSmallActionClassesCallback` plugin api", async function (assert) {
|
||||
try {
|
||||
withPluginApi("1.6.0", (api) => {
|
||||
api.addPostSmallActionClassesCallback((postAttrs) => {
|
||||
if (postAttrs.canRecover) {
|
||||
return ["abcde"];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this.set("args", { id: 123, canRecover: false });
|
||||
|
||||
await render(
|
||||
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
|
||||
);
|
||||
|
||||
assert.notOk(
|
||||
exists(".abcde"),
|
||||
"custom CSS class is not added when condition is not met"
|
||||
);
|
||||
|
||||
this.set("args", { id: 123, canRecover: true });
|
||||
|
||||
await render(
|
||||
hbs`<MountWidget @widget="post-small-action" @args={{this.args}} />`
|
||||
);
|
||||
|
||||
assert.ok(
|
||||
exists(".abcde"),
|
||||
"it adds custom CSS class as registered from the plugin API"
|
||||
);
|
||||
} finally {
|
||||
resetPostSmallActionClassesCallbacks();
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue