2017-01-31 07:50:28 +01:00
|
|
|
const Backbone = require('backbone');
|
|
|
|
const Alerts = require('./alerts');
|
|
|
|
const Launcher = require('./launcher');
|
|
|
|
const AuthReceiver = require('./auth-receiver');
|
|
|
|
const Links = require('../const/links');
|
|
|
|
const Timeouts = require('../const/timeouts');
|
|
|
|
const Locale = require('../util/locale');
|
2016-03-26 21:12:56 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const PopupNotifier = {
|
2016-03-26 21:12:56 +01:00
|
|
|
init: function() {
|
2016-03-27 16:47:29 +02:00
|
|
|
if (Launcher) {
|
|
|
|
window.open = this._openLauncherWindow;
|
|
|
|
} else {
|
2017-01-31 07:50:28 +01:00
|
|
|
const windowOpen = window.open;
|
2016-03-26 21:12:56 +01:00
|
|
|
window.open = function() {
|
2017-01-31 07:50:28 +01:00
|
|
|
const win = windowOpen.apply(window, arguments);
|
2016-03-26 21:12:56 +01:00
|
|
|
if (win) {
|
|
|
|
PopupNotifier.deferCheckClosed(win);
|
|
|
|
Backbone.trigger('popup-opened', win);
|
2016-03-27 15:36:07 +02:00
|
|
|
} else {
|
|
|
|
if (!Alerts.alertDisplayed) {
|
|
|
|
Alerts.error({
|
|
|
|
header: Locale.authPopupRequired,
|
|
|
|
body: Locale.authPopupRequiredBody
|
|
|
|
});
|
|
|
|
}
|
2016-03-26 21:12:56 +01:00
|
|
|
}
|
|
|
|
return win;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-27 16:47:29 +02:00
|
|
|
_openLauncherWindow: function(url, title, settings) {
|
2017-01-31 07:50:28 +01:00
|
|
|
const opts = {
|
2016-03-27 16:47:29 +02:00
|
|
|
show: false,
|
2016-07-12 07:05:07 +02:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false,
|
|
|
|
webSecurity: false,
|
|
|
|
allowDisplayingInsecureContent: true,
|
|
|
|
allowRunningInsecureContent: true
|
2016-03-27 16:47:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if (settings) {
|
2017-01-31 07:50:28 +01:00
|
|
|
const settingsObj = {};
|
2016-07-17 13:30:38 +02:00
|
|
|
settings.split(',').forEach(part => {
|
2017-01-31 07:50:28 +01:00
|
|
|
const parts = part.split('=');
|
2016-03-27 16:47:29 +02:00
|
|
|
settingsObj[parts[0].trim()] = parts[1].trim();
|
|
|
|
});
|
2017-12-25 18:57:09 +01:00
|
|
|
if (settingsObj.width) { opts.width = +settingsObj.width; }
|
|
|
|
if (settingsObj.height) { opts.height = +settingsObj.height; }
|
|
|
|
if (settingsObj.top) { opts.y = +settingsObj.top; }
|
|
|
|
if (settingsObj.left) { opts.x = +settingsObj.left; }
|
2016-03-27 16:47:29 +02:00
|
|
|
}
|
2017-01-31 07:50:28 +01:00
|
|
|
let win = Launcher.openWindow(opts);
|
2016-07-17 13:30:38 +02:00
|
|
|
win.webContents.on('did-get-redirect-request', (e, fromUrl, toUrl) => {
|
2016-08-14 22:51:01 +02:00
|
|
|
if (PopupNotifier.isOwnUrl(toUrl)) {
|
2016-03-27 16:47:29 +02:00
|
|
|
win.webContents.stop();
|
|
|
|
win.close();
|
|
|
|
PopupNotifier.processReturnToApp(toUrl);
|
|
|
|
}
|
|
|
|
});
|
2016-07-17 13:30:38 +02:00
|
|
|
win.webContents.on('will-navigate', (e, toUrl) => {
|
2016-08-14 22:51:01 +02:00
|
|
|
if (PopupNotifier.isOwnUrl(toUrl)) {
|
2016-03-27 16:47:29 +02:00
|
|
|
e.preventDefault();
|
|
|
|
win.close();
|
|
|
|
PopupNotifier.processReturnToApp(toUrl);
|
|
|
|
}
|
|
|
|
});
|
2016-07-12 06:40:01 +02:00
|
|
|
win.loadURL(url);
|
2016-08-14 23:22:25 +02:00
|
|
|
win.once('page-title-updated', () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (win) {
|
|
|
|
win.show();
|
|
|
|
win.focus();
|
|
|
|
}
|
|
|
|
}, Timeouts.PopupWaitTime);
|
|
|
|
});
|
2016-07-17 13:30:38 +02:00
|
|
|
win.on('closed', () => {
|
2016-03-27 16:47:29 +02:00
|
|
|
setTimeout(PopupNotifier.triggerClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
|
2016-08-14 23:22:25 +02:00
|
|
|
win = null;
|
2016-03-27 16:47:29 +02:00
|
|
|
});
|
|
|
|
Backbone.trigger('popup-opened', win);
|
|
|
|
return win;
|
|
|
|
},
|
|
|
|
|
2016-08-14 22:51:01 +02:00
|
|
|
isOwnUrl(url) {
|
|
|
|
return url.lastIndexOf(Links.WebApp, 0) === 0 ||
|
|
|
|
url.lastIndexOf(location.origin + location.pathname, 0) === 0;
|
|
|
|
},
|
|
|
|
|
2016-03-27 16:47:29 +02:00
|
|
|
processReturnToApp: function(url) {
|
2017-01-31 07:50:28 +01:00
|
|
|
const returnMessage = AuthReceiver.urlArgsToMessage(url);
|
2016-03-27 16:47:29 +02:00
|
|
|
if (Object.keys(returnMessage).length > 0) {
|
2017-01-31 07:50:28 +01:00
|
|
|
const evt = new Event('message');
|
2016-03-27 16:47:29 +02:00
|
|
|
evt.data = returnMessage;
|
|
|
|
window.dispatchEvent(evt);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-26 21:12:56 +01:00
|
|
|
deferCheckClosed: function(win) {
|
|
|
|
setTimeout(PopupNotifier.checkClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
|
|
|
|
},
|
|
|
|
|
|
|
|
checkClosed: function(win) {
|
|
|
|
if (win.closed) {
|
2016-03-27 13:54:35 +02:00
|
|
|
setTimeout(PopupNotifier.triggerClosed.bind(PopupNotifier, win), Timeouts.CheckWindowClosed);
|
2016-03-26 21:12:56 +01:00
|
|
|
} else {
|
|
|
|
PopupNotifier.deferCheckClosed(win);
|
|
|
|
}
|
2016-03-27 13:54:35 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
triggerClosed: function(win) {
|
|
|
|
Backbone.trigger('popup-closed', win);
|
2016-03-26 21:12:56 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = PopupNotifier;
|