[FIX] web: don't add hotkey if readonly statusbar

Before this commit, the statusbar added a hotkey even if
it was readonly or non clickable.
Because of this, the user could change the status with a hotkey.

Now, if the statusbar is readonly or non clickable then
the hotkey won't be available.

closes odoo/odoo#110262

X-original-commit: 9569b4f463b6e82df3c8eafb8bd4329faac871b1
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Signed-off-by: Michaël Mattiello <mcm@odoo.com>
This commit is contained in:
Michael (mcm)
2023-01-18 17:38:23 +01:00
parent 67c3a3ad9c
commit 28df053a51
4 changed files with 77 additions and 2 deletions
@@ -176,6 +176,7 @@ export const commandService = {
registration.removeHotkey = hotkeyService.add(registration.hotkey, action, {
activeElement: registration.activeElement,
global: registration.global,
validate: registration.isAvailable,
});
}
@@ -36,7 +36,11 @@ export class StatusBarField extends Component {
],
};
},
{ category: "smart_action", hotkey: "alt+shift+x" }
{
category: "smart_action",
hotkey: "alt+shift+x",
isAvailable: () => !this.props.readonly && !this.props.isDisabled,
}
);
useCommand(
sprintf(this.env._t(`Move to next %s`), this.props.displayName),
@@ -60,8 +64,10 @@ export class StatusBarField extends Component {
isAvailable: () => {
const options = this.computeItems(false);
return (
!this.props.readonly &&
!this.props.isDisabled &&
options[options.length - 1].id !==
(this.type === "many2one" ? this.props.value[0] : this.props.value)
(this.type === "many2one" ? this.props.value[0] : this.props.value)
);
},
}
@@ -245,6 +245,27 @@ QUnit.test("global command with hotkey", async (assert) => {
assert.verifySteps([globalHotkey]);
});
QUnit.test("command with hotkey and isAvailable", async (assert) => {
assert.expect(3);
const hotkey = "a";
let isAvailable = false;
env.services.command.add("test", () => assert.step(hotkey), {
hotkey,
isAvailable: () => isAvailable,
});
await nextTick();
triggerHotkey("a");
await nextTick();
assert.verifySteps([]);
isAvailable = true;
triggerHotkey("a");
await nextTick();
assert.verifySteps([hotkey]);
});
QUnit.test("open command palette with command config", async (assert) => {
const hotkey = "alt+a";
const action = () => {};
@@ -550,4 +550,51 @@ QUnit.module("Fields", (hooks) => {
);
await click(target, "#o_command_2");
});
QUnit.test("smart actions are unavailable if readonly", async function (assert) {
await makeView({
serverData,
type: "form",
resModel: "partner",
arch: `
<form>
<header>
<field name="trululu" widget="statusbar" readonly="1"/>
</header>
</form>`,
resId: 1,
});
assert.containsOnce(target, ".o_field_widget");
triggerHotkey("control+k");
await nextTick();
const moveStages = [...target.querySelectorAll(".o_command")].map((el) => el.textContent);
assert.notOk(moveStages.includes("Move to Trululu...ALT + SHIFT + X"));
assert.notOk(moveStages.includes("Move to next...ALT + X"));
});
QUnit.test("hotkeys are unavailable if readonly", async function (assert) {
await makeView({
serverData,
type: "form",
resModel: "partner",
arch: `
<form>
<header>
<field name="trululu" widget="statusbar" readonly="1"/>
</header>
</form>`,
resId: 1,
});
assert.containsOnce(target, ".o_field_widget");
triggerHotkey("alt+shift+x"); // Move to stage...
await nextTick();
assert.containsNone(target, ".modal", "command palette should not open");
triggerHotkey("alt+x"); // Move to next
await nextTick();
assert.containsNone(target, ".modal", "command palette should not open");
});
});