keeweb/app/scripts/app.js

174 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import Backbone from 'backbone';
import { FileInfoCollection } from 'collections/file-info-collection';
import { AppRightsChecker } from 'comp/app/app-rights-checker';
import { ExportApi } from 'comp/app/export-api';
import { SingleInstanceChecker } from 'comp/app/single-instance-checker';
import { Updater } from 'comp/app/updater';
import { AuthReceiver } from 'comp/browser/auth-receiver';
import { FeatureTester } from 'comp/browser/feature-tester';
import { FocusDetector } from 'comp/browser/focus-detector';
import { IdleTracker } from 'comp/browser/idle-tracker';
import { KeyHandler } from 'comp/browser/key-handler';
import { PopupNotifier } from 'comp/browser/popup-notifier';
import { Launcher } from 'comp/launcher';
import { SettingsManager } from 'comp/settings/settings-manager';
import { Alerts } from 'comp/ui/alerts';
import { Timeouts } from 'const/timeouts';
import { AppModel } from 'models/app-model';
import { AppSettingsModel } from 'models/app-settings-model';
import { RuntimeDataModel } from 'models/runtime-data-model';
import { UpdateModel } from 'models/update-model';
import { PluginManager } from 'plugins/plugin-manager';
import { Features } from 'util/features';
import { KdbxwebInit } from 'util/kdbxweb/kdbxweb-init';
import { Locale } from 'util/locale';
import { AppView } from 'views/app-view';
import 'view-engine/backbone-view-ext';
import 'hbs-helpers';
2015-10-17 23:49:24 +02:00
2019-08-16 23:05:39 +02:00
const ready = (Launcher && Launcher.ready) || $;
2017-02-05 11:08:26 +01:00
ready(() => {
2019-09-15 08:11:11 +02:00
if (AuthReceiver.receive() || Features.isFrame) {
return;
2015-10-25 17:27:34 +01:00
}
2015-10-17 23:49:24 +02:00
2017-02-05 11:08:26 +01:00
const appModel = new AppModel();
Promise.resolve()
.then(loadConfigs)
.then(initModules)
.then(loadRemoteConfig)
2017-05-16 22:56:52 +02:00
.then(ensureCanRun)
2017-05-16 21:26:25 +02:00
.then(showApp)
.then(postInit)
2017-05-16 21:26:25 +02:00
.catch(e => {
appModel.appLogger.error('Error starting app', e);
});
2017-02-05 11:08:26 +01:00
2017-05-16 22:56:52 +02:00
function ensureCanRun() {
2019-08-16 23:05:39 +02:00
return FeatureTester.test().catch(e => {
Alerts.error({
header: Locale.appSettingsError,
body: Locale.appNotSupportedError + '<br/><br/>' + e,
buttons: [],
esc: false,
enter: false,
click: false
2017-05-16 22:56:52 +02:00
});
2019-08-16 23:05:39 +02:00
throw 'Feature testing failed: ' + e;
});
2017-05-16 22:56:52 +02:00
}
function loadConfigs() {
return Promise.all([
AppSettingsModel.instance.load(),
UpdateModel.instance.load(),
RuntimeDataModel.instance.load(),
FileInfoCollection.instance.load()
]);
}
2016-06-16 19:00:24 +02:00
function initModules() {
KeyHandler.init();
IdleTracker.init();
PopupNotifier.init();
2017-01-30 21:26:31 +01:00
KdbxwebInit.init();
2019-01-21 20:52:40 +01:00
FocusDetector.init();
2016-06-16 19:00:24 +02:00
window.kw = ExportApi;
2019-09-15 13:29:20 +02:00
return PluginManager.instance.init();
2016-06-16 19:00:24 +02:00
}
function showSettingsLoadError() {
Alerts.error({
header: Locale.appSettingsError,
body: Locale.appSettingsErrorBody,
buttons: [],
2019-08-16 23:05:39 +02:00
esc: false,
enter: false,
click: false
2016-06-16 19:00:24 +02:00
});
}
function loadRemoteConfig() {
2017-05-16 21:26:25 +02:00
return Promise.resolve().then(() => {
SettingsManager.setBySettings(appModel.settings);
const configParam = getConfigParam();
if (configParam) {
2019-08-16 23:05:39 +02:00
return appModel
.loadConfig(configParam)
.then(() => {
SettingsManager.setBySettings(appModel.settings);
})
.catch(e => {
if (!appModel.settings.get('cacheConfigSettings')) {
showSettingsLoadError();
throw e;
}
});
}
});
}
2015-10-17 23:49:24 +02:00
function showApp() {
2019-08-16 23:05:39 +02:00
return Promise.resolve().then(() => {
2019-08-18 08:05:38 +02:00
const skipHttpsWarning =
localStorage.skipHttpsWarning || appModel.settings.get('skipHttpsWarning');
2019-08-16 23:05:39 +02:00
const protocolIsInsecure = ['https:', 'file:', 'app:'].indexOf(location.protocol) < 0;
const hostIsInsecure = location.hostname !== 'localhost';
if (protocolIsInsecure && hostIsInsecure && !skipHttpsWarning) {
return new Promise(resolve => {
Alerts.error({
header: Locale.appSecWarn,
icon: 'user-secret',
esc: false,
enter: false,
click: false,
body: Locale.appSecWarnBody1 + '<br/><br/>' + Locale.appSecWarnBody2,
buttons: [{ result: '', title: Locale.appSecWarnBtn, error: true }],
complete: () => {
showView();
resolve();
}
2018-08-30 22:16:31 +02:00
});
2019-08-16 23:05:39 +02:00
});
} else {
showView();
}
});
2016-06-16 19:00:24 +02:00
}
function postInit() {
Updater.init();
SingleInstanceChecker.init();
AppRightsChecker.init();
2019-09-15 14:16:32 +02:00
setTimeout(
() => PluginManager.instance.runAutoUpdate(),
Timeouts.AutoUpdatePluginsAfterStart
);
2017-05-20 12:46:21 +02:00
}
2016-06-16 19:00:24 +02:00
function showView() {
appModel.prepare();
2015-12-06 21:32:41 +01:00
new AppView({ model: appModel }).render();
Backbone.trigger('app-ready');
logStartupTime();
}
function logStartupTime() {
2017-02-19 20:19:03 +01:00
const time = Math.round(performance.now());
appModel.appLogger.info(`Started in ${time}ms ¯\\_(ツ)_/¯`);
2015-10-17 23:49:24 +02:00
}
2016-06-16 19:00:24 +02:00
function getConfigParam() {
2017-01-31 07:50:28 +01:00
const metaConfig = document.head.querySelector('meta[name=kw-config]');
if (metaConfig && metaConfig.content && metaConfig.content[0] !== '(') {
return metaConfig.content;
}
2017-01-31 07:50:28 +01:00
const match = location.search.match(/[?&]config=([^&]+)/i);
2016-06-16 19:00:24 +02:00
if (match && match[1]) {
return match[1];
}
}
2015-10-17 23:49:24 +02:00
});