From 25732e9057fc592d34f7dc4946e3b37758e68807 Mon Sep 17 00:00:00 2001 From: krisYanachkov Date: Fri, 6 Mar 2020 15:46:44 +0200 Subject: [PATCH 1/3] GPII-4386: fix for duplicate custom buttons (cherry picked from commit 24a232e3d2ede47dc550faaa02f809bc225f4da2) --- src/main/qss.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/qss.js b/src/main/qss.js index beeed00b6..278af9ba2 100644 --- a/src/main/qss.js +++ b/src/main/qss.js @@ -775,7 +775,7 @@ gpii.app.qssWrapper.alterSetting = function (that, updatedSetting, source) { }); } else { - if (setting.path === updatedSetting.path && !fluid.model.diff(setting, updatedSetting)) { + if (setting.id !== "MakeYourOwn" && setting.path === updatedSetting.path && !fluid.model.diff(setting, updatedSetting)) { // applying primary setting's change that.applier.change("settings." + index, updatedSetting, null, source); From 80bde2b2ac6c8d44a545ee918c1457959afb1d99 Mon Sep 17 00:00:00 2001 From: ste Date: Wed, 27 May 2020 14:14:28 +0100 Subject: [PATCH 2/3] GPII-4410: Opening web links in their existing tab. --- configs/app.base.json | 8 +++ siteconfig.json5 | 4 ++ src/main/app.js | 1 + src/main/linkRedirect.js | 74 ++++++++++++++++++++++++ src/main/siteConfigurationHandler.js | 44 +++++++++++++- src/renderer/common/js/utils.js | 20 +++++-- src/renderer/qss/js/qss.js | 9 +++ src/renderer/qss/js/qssServiceButtons.js | 2 +- 8 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/main/linkRedirect.js diff --git a/configs/app.base.json b/configs/app.base.json index 6a98f0689..834cd983f 100644 --- a/configs/app.base.json +++ b/configs/app.base.json @@ -26,6 +26,14 @@ "metrics": { "record": "gpii.app.metrics", "target": "{/ gpii.metrics}.options.gradeNames" + }, + "linkRedirect": { + "record": { + "linkRedirect": { + "type": "gpii.app.linkRedirect" + } + }, + "target": "{/ gpii.flowManager.local}.options.components" } } } diff --git a/siteconfig.json5 b/siteconfig.json5 index 0a650af84..d7e07bc83 100644 --- a/siteconfig.json5 +++ b/siteconfig.json5 @@ -31,6 +31,10 @@ alwaysUseChrome: false, + // The redirect url to open links by showing the tab if the link is already open in the browser. The encoded + // destination url is added to the end of this, or replaces `$1`. + currentTabRedirect: "http://opensametab.morphic.org/redirect/", + // The template that is used for every label of the language setting's options. // The "%" followed by a word specifies a variable and there are three possible variables: // - native - the name of the language in its native form diff --git a/src/main/app.js b/src/main/app.js index 7fc7b69a9..0bdcba232 100644 --- a/src/main/app.js +++ b/src/main/app.js @@ -38,6 +38,7 @@ require("./storage.js"); require("./tray.js"); require("./userErrorsHandler.js"); require("./metrics.js"); +require("./linkRedirect.js"); // enhance the normal require to work with .json5 files require("json5/lib/register"); diff --git a/src/main/linkRedirect.js b/src/main/linkRedirect.js new file mode 100644 index 000000000..98900fbd6 --- /dev/null +++ b/src/main/linkRedirect.js @@ -0,0 +1,74 @@ +/** + * Handles link redirection. + * + * Copyright 2020 Raising the Floor - International + * + * Licensed under the New BSD license. You may not use this file except in + * compliance with this License. + * + * The R&D leading to these results received funding from the + * Department of Education - Grant H421A150005 (GPII-APCP). However, + * these results do not necessarily represent the policy of the + * Department of Education, and you should not assume endorsement by the + * Federal Government. + * + * You may obtain a copy of the License at + * https://github.com/GPII/gpii-app/blob/master/LICENSE.txt + */ + +"use strict"; + +var fluid = require("infusion"), + URL = require("url").URL; + +var gpii = fluid.registerNamespace("gpii"); +fluid.registerNamespace("gpii.app.linkRedirect"); + +/** + * Handles /redirect, which redirects the browser to the provided url. + * This is a fall-back for the 'open link in existing tab' feature [GPII-4410], where the browser is opened with a + * special link which gets intercepted by a browser extension. The link really points to this handler, so it will + * still work with browsers that don't have the extension installed. + */ +fluid.defaults("gpii.app.linkRedirect", { + gradeNames: ["kettle.app"], + defaultRedirectUrl: "http://opensametab.morphic.org/redirect/$1", + requestHandlers: { + handle: { + type: "gpii.app.linkRedirect.request", + route: "/redirect/:destination(.*)" + } + } +}); + +fluid.defaults("gpii.app.linkRedirect.request", { + gradeNames: ["kettle.request.http"], + invokers: { + handleRequest: { + funcName: "gpii.app.linkRedirect.handleRequest", + args: [ + "{request}", + "{request}.req.params.destination" + ] + } + } +}); + +/** + * Handler for /redirect. Redirects the browser to the given url. + * + * @param {Component} request The request object. + * @param {String} destination The destination URL. + */ +gpii.app.linkRedirect.handleRequest = function (request, destination) { + try { + // Make sure the url is valid before sending it out. + var url = new URL(destination); + request.res.status(301).redirect(url.href); + } catch (e) { + request.handlerPromise.reject({ + statusCode: 400, + message: e.message + }); + } +}; diff --git a/src/main/siteConfigurationHandler.js b/src/main/siteConfigurationHandler.js index 3cc650aa9..c0bfdf7bb 100644 --- a/src/main/siteConfigurationHandler.js +++ b/src/main/siteConfigurationHandler.js @@ -14,7 +14,8 @@ */ "use strict"; -var fluid = require("infusion"); +var fluid = require("infusion"), + URL = require("url").URL; var gpii = fluid.registerNamespace("gpii"); @@ -111,6 +112,27 @@ fluid.defaults("gpii.app.siteConfigurationHandler", { distributeMetrics: { record: "{that}.options.siteConfig.metrics", target: "{/ gpii.app.metrics}.options.siteConfig" + }, + distributeQssConfigUrl: { + record: { + // Tweaks the redirect url so it's on the same port as the app. + expander: { + funcName: "gpii.app.siteConfigurationHandler.setUrlPort", + args: [ + { + expander: { + funcName: "fluid.firstDefined", + args: [ + "{that}.options.siteConfig.qss.currentTabRedirect", + "{gpii.app.linkRedirect}.options.defaultRedirectUrl" + ] + } + }, + "{kettle.server}.options.port" + ] + } + }, + target: "{that qssWrapper}.options.siteConfig.currentTabRedirect" } }, @@ -156,3 +178,23 @@ gpii.app.siteConfigurationHandler.requireFirst = function (files) { } return togo; }; + +/** + * Sets the port value of a url. + * @param {String} url The url to update. + * @param {Number} port The new port number. + * @return {String} The url, with the updated port. + */ +gpii.app.siteConfigurationHandler.setUrlPort = function (url, port) { + var togo; + if (url) { + try { + var u = new URL(url); + u.port = port; + togo = u.href; + } catch (e) { + fluid.log(fluid.logLevel.WARN, "Unable to parse url '" + url + "' ", e.message); + } + } + return togo; +}; diff --git a/src/renderer/common/js/utils.js b/src/renderer/common/js/utils.js index 8e148b638..05a769337 100644 --- a/src/renderer/common/js/utils.js +++ b/src/renderer/common/js/utils.js @@ -92,15 +92,27 @@ * @param {String} siteUrl - cloud folder's url * @param {Boolean} alwaysUseChrome - true to use chrome, rather than the default browser. * @param {Boolean} forceFullScreen - the function requires the browser to be open maximized + * @param {String} redirectUrl - [optional] A url which will wrap `siteUrl`. This can be used to make a redirection + * url. Current use case is to make an extension open an existing tab. `siteUrl` is encoded and added to the end of + * this, or a placeholder of `$1` can be used. */ - gpii.windows.openUrl = function (siteUrl, alwaysUseChrome, forceFullScreen) { + gpii.windows.openUrl = function (siteUrl, alwaysUseChrome, forceFullScreen, redirectUrl) { if (fluid.isValue(siteUrl)) { + var finalUrl = siteUrl; + + // Wrap the url in the opensametab + if (redirectUrl) { + finalUrl = redirectUrl.replace(/\$1|$/, encodeURIComponent(siteUrl)); + } + + fluid.log("Opening link", finalUrl); + if (alwaysUseChrome) { var command = // Check chrome is installed "reg query \"HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\" /ve" // If so, run chrome - + " && start chrome \"" + siteUrl.replace(/"/g, "%22") + "\""; + + " && start chrome \"" + finalUrl.replace(/"/g, "%22") + "\""; if (forceFullScreen) { // adding the full screen option for Chrome as well command += " --start-fullscreen"; @@ -108,12 +120,12 @@ child_process.exec(command, function (err) { if (err) { // It failed, so use the default browser. - shell.openExternal(siteUrl); + shell.openExternal(finalUrl); } }); } else { // we have the url, opening it in the default browser - shell.openExternal(siteUrl); + shell.openExternal(finalUrl); } } else { // there is no value in the config, sending the warning diff --git a/src/renderer/qss/js/qss.js b/src/renderer/qss/js/qss.js index d248d0218..763f06469 100644 --- a/src/renderer/qss/js/qss.js +++ b/src/renderer/qss/js/qss.js @@ -266,6 +266,15 @@ updateIsKeyedIn: { changePath: "isKeyedIn", value: "{arguments}.0" + }, + openUrl: { + funcName: "gpii.windows.openUrl", + args: [ + "{arguments}.0", // siteUrl + "{arguments}.1", // alwaysUseChrome + "{arguments}.2", // forceFullScreen + "{that}.options.siteConfig.currentTabRedirect" // redirectUrl, + ] } } }); diff --git a/src/renderer/qss/js/qssServiceButtons.js b/src/renderer/qss/js/qssServiceButtons.js index ad3d15176..a28a007fd 100644 --- a/src/renderer/qss/js/qssServiceButtons.js +++ b/src/renderer/qss/js/qssServiceButtons.js @@ -222,7 +222,7 @@ gradeNames: ["gpii.qss.buttonPresenter"], invokers: { activate: { - funcName: "gpii.windows.openUrl", + func: "{gpii.qss}.openUrl", args: [ "{that}.model.item.schema.url", // using the url from the custom button's schema true, // Override the OS default browser to always use Chrome instead From a57d80e5d9420619e17e2012f3c31616ffff3be8 Mon Sep 17 00:00:00 2001 From: ste Date: Thu, 28 May 2020 20:32:34 +0100 Subject: [PATCH 3/3] GPII-4410: Improved documentation for currentTabRedirect. --- siteconfig.json5 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/siteconfig.json5 b/siteconfig.json5 index d7e07bc83..43dab1098 100644 --- a/siteconfig.json5 +++ b/siteconfig.json5 @@ -32,7 +32,9 @@ alwaysUseChrome: false, // The redirect url to open links by showing the tab if the link is already open in the browser. The encoded - // destination url is added to the end of this, or replaces `$1`. + // destination url is added to the end of this, or replaces `$1`. Normally, this value is + // "http://opensametab.morphic.org/redirect/", but it can be "http://refreshsametab.morphic.org/redirect/" + // to make the tab reload itself. currentTabRedirect: "http://opensametab.morphic.org/redirect/", // The template that is used for every label of the language setting's options.