From 73c393df0af4a56713f93500ab6eba7b42b0e2ca Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Tue, 20 Aug 2024 16:29:11 +0300 Subject: [PATCH] feature: command+click opens url in a new tab (#3156) --- .../js/controllers/table_row_controller.js | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/app/javascript/js/controllers/table_row_controller.js b/app/javascript/js/controllers/table_row_controller.js index ee907eb855..4234cb659b 100644 --- a/app/javascript/js/controllers/table_row_controller.js +++ b/app/javascript/js/controllers/table_row_controller.js @@ -2,21 +2,36 @@ import { Controller } from '@hotwired/stimulus' export default class extends Controller { visitRecord(event) { - if (event.type === 'click') { - const isLinkOrButton = event.target.closest('a, button') - const isCheckbox = event.target.closest('input[type="checkbox"]') - if (isLinkOrButton || isCheckbox) { - return // Don't navigate if a link or button is clicked - } - - this.#executeTheVisit(event) + if (event.type !== 'click') { + return + } + + const isLinkOrButton = event.target.closest('a, button') + const isCheckbox = event.target.closest('input[type="checkbox"]') + + if (isLinkOrButton || isCheckbox) { + return // Don't navigate if a link or button is clicked } - } - #executeTheVisit(event) { const row = event.target.closest('tr') - if (row && row.dataset.visitPath) { - window.Turbo.visit(row.dataset.visitPath) + const url = row.dataset.visitPath + + if (!row || !url) { + return } + + if (event.metaKey || event.ctrlKey) { + this.#visitInNewTab(url) + } else { + this.#visitInSameTab(url) + } + } + + #visitInSameTab(url) { + window.Turbo.visit(url) + } + + #visitInNewTab(url) { + window.open(url, '_blank').focus() } }