keeweb/app/scripts/comp/popup-notifier.js

120 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-03-26 21:12:56 +01:00
'use strict';
var Backbone = require('backbone'),
2016-03-27 15:36:07 +02:00
Alerts = require('./alerts'),
2016-03-27 16:47:29 +02:00
Launcher = require('./launcher'),
AuthReceiver = require('./auth-receiver'),
Links = require('../const/links'),
2016-03-27 15:36:07 +02:00
Timeouts = require('../const/timeouts'),
Locale = require('../util/locale');
2016-03-26 21:12:56 +01:00
var PopupNotifier = {
init: function() {
2016-03-27 16:47:29 +02:00
if (Launcher) {
window.open = this._openLauncherWindow;
} else {
2016-03-26 21:12:56 +01:00
var windowOpen = window.open;
window.open = function() {
var win = windowOpen.apply(window, arguments);
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) {
var opts = {
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) {
var settingsObj = {};
2016-07-17 13:30:38 +02:00
settings.split(',').forEach(part => {
2016-03-27 16:47:29 +02:00
var parts = part.split('=');
settingsObj[parts[0].trim()] = parts[1].trim();
});
if (settings.width) { opts.width = settings.width; }
if (settings.height) { opts.height = settings.height; }
if (settings.top) { opts.y = settings.top; }
if (settings.left) { opts.x = settings.left; }
}
var win = Launcher.openWindow(opts);
2016-07-17 13:30:38 +02:00
win.webContents.on('did-get-redirect-request', (e, fromUrl, toUrl) => {
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) => {
if (PopupNotifier.isOwnUrl(toUrl)) {
2016-03-27 16:47:29 +02:00
e.preventDefault();
win.close();
PopupNotifier.processReturnToApp(toUrl);
}
});
win.loadURL(url);
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);
win = null;
2016-03-27 16:47:29 +02:00
});
Backbone.trigger('popup-opened', win);
return win;
},
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) {
var returnMessage = AuthReceiver.urlArgsToMessage(url);
if (Object.keys(returnMessage).length > 0) {
var evt = new Event('message');
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;