Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(delete): trigger delete button with keyboard #1104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addon/components/multi-document-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@color="danger"
class="uk-width-1"
{{on "click" showDialog}}
{{did-insert (fn this.registerDeleteDialog showDialog)}}
>
{{t "alexandria.delete"}}
</UkButton>
Expand Down
32 changes: 32 additions & 0 deletions addon/components/multi-document-details.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

export default class MultiDocumentDetailsComponent extends Component {
@service("alexandria-side-panel") sidePanel;
@service("alexandria-documents") documents;
@tracked deleteDialog = null;

constructor(parent, args) {
super(parent, args);

this.boundHandleKeyDown = this.handleKeyDown.bind(this);
window.addEventListener("keydown", this.boundHandleKeyDown);
}

@action
registerDeleteDialog(showDialog) {
this.deleteDialog = showDialog;
}

willDestroy() {
super.willDestroy();
window.removeEventListener("keydown", this.boundHandleKeyDown);
}

handleKeyDown(event) {
if (this.documents.shortcutsDisabled) {
return;
}

if (event.key === "Delete" && this.deleteDialog) {
this.deleteDialog();
}
}

get mergedTags() {
const tags = [];
Expand Down
1 change: 1 addition & 0 deletions addon/components/single-document-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
@color="danger"
class="uk-width-1"
{{on "click" showDialog}}
{{did-insert (fn this.registerDeleteDialog showDialog)}}
>
{{t "alexandria.delete"}}
</UkButton>
Expand Down
28 changes: 28 additions & 0 deletions addon/components/single-document-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ export default class SingleDocumentDetailsComponent extends Component {
@tracked editDescription = false;
@tracked editDate = false;
@tracked validTitle = true;
@tracked deleteDialog = null;

constructor(parent, args) {
super(parent, args);

this.boundHandleKeyDown = this.handleKeyDown.bind(this);
window.addEventListener("keydown", this.boundHandleKeyDown);
}

@action
registerDeleteDialog(showDialog) {
this.deleteDialog = showDialog;
}

willDestroy() {
super.willDestroy();
window.removeEventListener("keydown", this.boundHandleKeyDown);
}

handleKeyDown(event) {
if (this.documents.shortcutsDisabled) {
return;
}

if (event.key === "Delete" && this.deleteDialog) {
this.deleteDialog();
}
}

get locale() {
return this.intl.primaryLocale.split("-")[0];
Expand Down
70 changes: 70 additions & 0 deletions tests/acceptance/documents-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fillIn,
triggerEvent,
settled,
waitFor,
} from "@ember/test-helpers";
import { setupApplicationTest } from "dummy/tests/helpers";
import { setupMirage } from "ember-cli-mirage/test-support";
Expand Down Expand Up @@ -286,4 +287,73 @@ module("Acceptance | documents", function (hooks) {
.dom("[data-test-document-list-item].document-list-item--selected")
.doesNotExist();
});

test.each(
`trigger document delete with keyboard shortcut`,
["single", "multiple"],
async function (assert, type) {
const documents = this.server.createList("document", 3);

function isVisible(element) {
if (!element) {
return false;
}

const style = window.getComputedStyle(element);

return style.display !== "none";
}

await visit("/");
assert.dom(".uk-modal").doesNotExist();

if (type === "single") {
// select first document and check the single document details
await click("[data-test-document-list-item]:first-child");

assert
.dom("[data-test-document-list-item].document-list-item--selected")
.exists({ count: 1 });
assert.dom("[data-test-single-doc-details]").exists();
} else {
// select multiple documents and check the multi document details
await click("[data-test-document-list-item]:first-child", {
shiftKey: true,
});
await click("[data-test-document-list-item]:nth-child(3)", {
shiftKey: true,
});

assert
.dom("[data-test-document-list-item].document-list-item--selected")
.exists({ count: 3 });
assert.dom("[data-test-multi-doc-details]").exists();
}

// assert modal visibility on keyboard shortcut
await waitFor(".uk-modal");
assert.dom(".uk-modal").exists({ count: 1 });
const modal = document.querySelector(".uk-modal");

assert.false(isVisible(modal));
window.dispatchEvent(new KeyboardEvent("keydown", { key: "Delete" }));
await triggerEvent(window, "keydown", "Delete");
assert.ok(isVisible(modal));

// assert modal body text
if (type === "single") {
assert
.dom(modal)
.containsText(
`Do you really want to delete the document "${documents[0].title}" and its complete history?`,
);
} else {
assert
.dom(modal)
.containsText(
"Do you really want to delete 3 documents and their complete history?",
);
}
},
);
});
Loading