FIX: never open internal links in a new tab when user prefers opening external links in a new tab

This commit is contained in:
Régis Hanol 2018-02-21 17:51:53 +01:00
parent 97e19a7d02
commit 4e7244d8d9

View File

@ -26,7 +26,7 @@ export default {
} }
// don't track links in quotes or in elided part // don't track links in quotes or in elided part
let tracking = $link.parents('aside.quote,.elided').length === 0; let tracking = $link.parents('aside.quote, .elided').length === 0;
let href = $link.attr('href') || $link.data('href'); let href = $link.attr('href') || $link.data('href');
@ -113,8 +113,10 @@ export default {
return false; return false;
} }
const isInternal = DiscourseURL.isInternal(href);
// If we're on the same site, use the router and track via AJAX // If we're on the same site, use the router and track via AJAX
if (tracking && DiscourseURL.isInternal(href) && !$link.hasClass('attachment')) { if (tracking && isInternal && !$link.hasClass('attachment')) {
ajax("/clicks/track", { ajax("/clicks/track", {
data: { data: {
url: href, url: href,
@ -128,9 +130,11 @@ export default {
return false; return false;
} }
// Otherwise, use a custom URL with a redirect const modifierLeftClicked = (e.ctrlKey || e.metaKey) && e.which === 1;
// consider CTRL+mouse-left-click / CMD+mouse-left-click or mouse-middle-click as well const middleClicked = e.which === 2;
if (Discourse.User.currentProp('external_links_in_new_tab') || ((e.ctrlKey || e.metaKey) && (e.which === 1)) || (e.which === 2)) { const openExternalInNewTab = Discourse.User.currentProp('external_links_in_new_tab');
if (modifierLeftClicked || middleClicked || (!isInternal && openExternalInNewTab)) {
window.open(destUrl, '_blank').focus(); window.open(destUrl, '_blank').focus();
} else { } else {
DiscourseURL.redirectTo(destUrl); DiscourseURL.redirectTo(destUrl);