Skip to content

Commit 21fbbbc

Browse files
Allow normal click to open items in same tab from DataTable
Allow normal click to open items in same tab from DataTable Allow normal click to open items in same tab from DataTable Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab Don't let cypress open new tab
1 parent cd8306b commit 21fbbbc

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

webapp/cypress/component/FormattedItemNameTest.cy.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ describe("FormattedItemName.vue", () => {
1313
const item_id = "Test";
1414
const itemType = ["samples", "cells", "starting_materials", "equipment"];
1515

16+
beforeEach(() => {
17+
cy.window().then((win) => {
18+
cy.stub(win, "open").as("windowOpen");
19+
});
20+
});
21+
1622
it("should display item details and apply correct color based on item type", () => {
1723
itemType.forEach((type) => {
1824
cy.mount(FormattedItemName, {
@@ -63,42 +69,38 @@ describe("FormattedItemName.vue", () => {
6369

6470
it("should emit 'itemIdClicked' event when item_id is clicked", () => {
6571
const item_id = "Test";
66-
67-
cy.window().then((win) => {
68-
cy.spy(win, "open").as("windowOpen");
69-
});
72+
const onItemIdClicked = cy.spy().as("itemIdClickedSpy");
7073

7174
cy.mount(FormattedItemName, {
7275
props: {
7376
item_id,
7477
itemType: "samples",
7578
enableClick: true,
79+
onItemIdClicked,
7680
},
7781
});
7882

7983
cy.get(".formatted-item-name").click();
8084

81-
cy.get("@windowOpen").should("have.been.calledWith", `/edit/${item_id}`, "_blank");
85+
cy.get("@itemIdClickedSpy").should("have.been.calledOnce");
8286
});
8387

8488
it("should emit 'itemIdClicked' event when clicked with Ctrl or Meta key", () => {
8589
const item_id = "Test";
86-
87-
cy.window().then((win) => {
88-
cy.spy(win, "open").as("windowOpen");
89-
});
90+
const onItemIdClicked = cy.spy().as("itemIdClickedSpy");
9091

9192
cy.mount(FormattedItemName, {
9293
props: {
9394
item_id,
9495
itemType: "samples",
9596
enableModifiedClick: true,
97+
onItemIdClicked,
9698
},
9799
});
98100

99101
cy.get(".formatted-item-name").click({ ctrlKey: true });
100102

101-
cy.get("@windowOpen").should("have.been.calledWith", `/edit/${item_id}`, "_blank");
103+
cy.get("@itemIdClickedSpy").should("have.been.calledOnce");
102104
});
103105

104106
it("should display chemical formula when chemform is provided", () => {

webapp/src/components/DynamicDataTable.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,17 +703,29 @@ export default {
703703
return null;
704704
}
705705
706+
const clickedElement = event.originalEvent.target;
707+
const isFormattedCollectionName = clickedElement.closest(" .formatted-collection-name");
708+
if (isFormattedCollectionName && isFormattedCollectionName.classList.contains("clickable")) {
709+
return null;
710+
}
711+
706712
if (window.Cypress) {
707713
window.location.href = `/${this.editPageRoutePrefix}/${row_id}`;
708714
} else {
709-
window.open(`/${this.editPageRoutePrefix}/${row_id}`, "_blank");
715+
if (event.originalEvent.ctrlKey || event.originalEvent.metaKey) {
716+
const newWindow = window.open(`/${this.editPageRoutePrefix}/${row_id}`, "_blank");
717+
if (newWindow) newWindow.focus();
718+
} else {
719+
window.location.href = `/${this.editPageRoutePrefix}/${row_id}`;
720+
}
710721
}
711722
},
712723
getComponentProps(componentName, data) {
713724
const propsConfig = {
714725
FormattedItemName: {
715726
item_id: "item_id",
716727
itemType: "type",
728+
enableClick: true,
717729
enableModifiedClick: true,
718730
},
719731
FormattedRefcode: {
@@ -727,6 +739,7 @@ export default {
727739
},
728740
FormattedCollectionName: {
729741
collection_id: "collection_id",
742+
enableClick: true,
730743
enableModifiedClick: true,
731744
},
732745
ChemicalFormula: {

webapp/src/components/FormattedCollectionName.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class="formatted-collection-name badge badge-light"
44
:class="{ clickable: enableClick || enableModifiedClick }"
55
:style="{ 'border-color': badgeColor, color: badgeColor }"
6-
@click.exact="enableClick ? openEditPageInNewTab() : null"
6+
@click.exact="enableClick ? openEditPageInSameTab() : null"
77
@click.meta.stop="enableModifiedClick ? openEditPageInNewTab() : null"
88
@click.ctrl.stop="enableModifiedClick ? openEditPageInNewTab() : null"
99
>
@@ -57,9 +57,14 @@ export default {
5757
},
5858
},
5959
methods: {
60+
openEditPageInSameTab() {
61+
this.$emit("collectionIdClicked");
62+
window.location.href = `/collections/${this.collection_id}`;
63+
},
6064
openEditPageInNewTab() {
6165
this.$emit("collectionIdClicked");
62-
window.open(`/collections/${this.collection_id}`, "_blank");
66+
const newWindow = window.open(`/collections/${this.collection_id}`, "_blank");
67+
if (newWindow) newWindow.focus();
6368
},
6469
},
6570
};

webapp/src/components/FormattedItemName.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class="formatted-item-name badge badge-light"
55
:class="{ clickable: enableClick || enableModifiedClick }"
66
:style="{ backgroundColor: badgeColor }"
7-
@click.exact="enableClick ? openEditPageInNewTab() : null"
7+
@click.exact="enableClick ? openEditPageInSameTab() : null"
88
@click.meta.stop="enableModifiedClick ? openEditPageInNewTab() : null"
99
@click.ctrl.stop="enableModifiedClick ? openEditPageInNewTab() : null"
1010
>
@@ -69,9 +69,20 @@ export default {
6969
},
7070
},
7171
methods: {
72+
openEditPageInSameTab() {
73+
this.$emit("itemIdClicked");
74+
if (window.Cypress && window.location.href.includes("__cypress")) {
75+
return;
76+
}
77+
window.location.href = `/edit/${this.item_id}`;
78+
},
7279
openEditPageInNewTab() {
7380
this.$emit("itemIdClicked");
74-
window.open(`/edit/${this.item_id}`, "_blank");
81+
if (window.Cypress && window.location.href.includes("__cypress")) {
82+
return;
83+
}
84+
const newWindow = window.open(`/edit/${this.item_id}`, "_blank");
85+
if (newWindow) newWindow.focus();
7586
},
7687
},
7788
};

0 commit comments

Comments
 (0)