Skip to content

Commit 68e8000

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
1 parent cd8306b commit 68e8000

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

webapp/cypress/component/FormattedItemNameTest.cy.jsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ 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+
const locationStub = { href: win.location.href };
20+
cy.stub(win, "location").value(locationStub);
21+
});
22+
});
23+
1624
it("should display item details and apply correct color based on item type", () => {
1725
itemType.forEach((type) => {
1826
cy.mount(FormattedItemName, {
@@ -63,42 +71,38 @@ describe("FormattedItemName.vue", () => {
6371

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

7176
cy.mount(FormattedItemName, {
7277
props: {
7378
item_id,
7479
itemType: "samples",
7580
enableClick: true,
81+
onItemIdClicked,
7682
},
7783
});
7884

7985
cy.get(".formatted-item-name").click();
8086

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

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

9194
cy.mount(FormattedItemName, {
9295
props: {
9396
item_id,
9497
itemType: "samples",
9598
enableModifiedClick: true,
99+
onItemIdClicked,
96100
},
97101
});
98102

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

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

104108
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: 9 additions & 6 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
>
@@ -68,11 +68,14 @@ export default {
6868
return safeName;
6969
},
7070
},
71-
methods: {
72-
openEditPageInNewTab() {
73-
this.$emit("itemIdClicked");
74-
window.open(`/edit/${this.item_id}`, "_blank");
75-
},
71+
openEditPageInSameTab() {
72+
this.$emit("itemIdClicked");
73+
window.location.href = `/edit/${this.item_id}`;
74+
},
75+
openEditPageInNewTab() {
76+
this.$emit("itemIdClicked");
77+
const newWindow = window.open(`/edit/${this.item_id}`, "_blank");
78+
if (newWindow) newWindow.focus();
7679
},
7780
};
7881
</script>

0 commit comments

Comments
 (0)