From 7129637279ea1de43712801294df13853d688bb7 Mon Sep 17 00:00:00 2001 From: hawm Date: Thu, 12 Sep 2019 23:19:43 +0800 Subject: [PATCH] FEATURE: Make share button support custom javascript (#8090) * FEATURE: Make share button support custom javascript * clean code * formatting * formatting --- .../javascripts/discourse/lib/sharing.js.es6 | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/sharing.js.es6 b/app/assets/javascripts/discourse/lib/sharing.js.es6 index 4e57b9dfa5e..2a3b4d191b5 100644 --- a/app/assets/javascripts/discourse/lib/sharing.js.es6 +++ b/app/assets/javascripts/discourse/lib/sharing.js.es6 @@ -19,6 +19,11 @@ return "http://twitter.com/intent/tweet?url=" + encodeURIComponent(link) + "&text=" + encodeURIComponent(title); }, + // If provided, handle by custom javascript rather than default url open + clickHandler: function(link, title){ + alert("Hello!") + } + // If true, opens in a popup of `popupHeight` size. If false it's opened in a new tab shouldOpenInPopup: true, popupHeight: 265 @@ -48,23 +53,27 @@ export default { }, shareSource(source, data) { - const url = source.generateUrl(data.url, data.title); - const options = { - menubar: "no", - toolbar: "no", - resizable: "yes", - scrollbars: "yes", - width: 600, - height: source.popupHeight || 315 - }; - const stringOptions = Object.keys(options) - .map(k => `${k}=${options[k]}`) - .join(","); - - if (source.shouldOpenInPopup) { - window.open(url, "", stringOptions); + if (source.clickHandler) { + source.clickHandler(data.url, data.title); } else { - window.open(url, "_blank"); + const url = source.generateUrl(data.url, data.title); + const options = { + menubar: "no", + toolbar: "no", + resizable: "yes", + scrollbars: "yes", + width: 600, + height: source.popupHeight || 315 + }; + const stringOptions = Object.keys(options) + .map(k => `${k}=${options[k]}`) + .join(","); + + if (source.shouldOpenInPopup) { + window.open(url, "", stringOptions); + } else { + window.open(url, "_blank"); + } } },