ability to load json config

This commit is contained in:
antelle 2016-06-16 20:00:24 +03:00
parent 303ea5ac24
commit f7da41db3e
4 changed files with 86 additions and 15 deletions

View File

@ -13,33 +13,78 @@ var AppModel = require('./models/app-model'),
Locale = require('./util/locale');
$(function() {
if ((window.parent !== window.top) || window.opener) {
if (isPopup()) {
return AuthReceiver.receive();
}
require('./mixins/view');
require('./helpers');
KeyHandler.init();
IdleTracker.init();
PopupNotifier.init();
window.kw = ExportApi;
loadMixins();
initModules();
var appModel = new AppModel();
ThemeChanger.setBySettings(appModel.settings);
var skipHttpsWarning = localStorage.skipHttpsWarning || appModel.settings.get('skipHttpsWarning');
if (['https:', 'file:', 'app:'].indexOf(location.protocol) < 0 && !skipHttpsWarning) {
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: showApp
var configParam = getConfigParam();
if (configParam) {
appModel.loadConfig(configParam, function(err) {
if (err) {
showSettingsLoadError();
} else {
ThemeChanger.setBySettings(appModel.settings);
showApp();
}
});
} else {
showApp();
}
function isPopup() {
return (window.parent !== window.top) || window.opener;
}
function loadMixins() {
require('./mixins/view');
require('./helpers');
}
function initModules() {
KeyHandler.init();
IdleTracker.init();
PopupNotifier.init();
window.kw = ExportApi;
}
function showSettingsLoadError() {
ThemeChanger.setBySettings(appModel.settings);
Alerts.error({
header: Locale.appSettingsError,
body: Locale.appSettingsErrorBody,
buttons: [],
esc: false, enter: false, click: false
});
}
function showApp() {
var skipHttpsWarning = localStorage.skipHttpsWarning || appModel.settings.get('skipHttpsWarning');
if (['https:', 'file:', 'app:'].indexOf(location.protocol) < 0 && !skipHttpsWarning) {
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
});
} else {
showView();
}
}
function showView() {
new AppView({ model: appModel }).render();
Updater.init();
}
function getConfigParam() {
var match = location.search.match(/[\?&]config=([^&]+)/i);
if (match && match[1]) {
return match[1];
}
}
});

View File

@ -41,6 +41,29 @@ var AppModel = Backbone.Model.extend({
this.appLogger = new Logger('app');
},
loadConfig: function(configLocation, callback) {
this.appLogger.debug('Loading config from', configLocation);
var ts = this.appLogger.ts();
var xhr = new XMLHttpRequest();
xhr.open('GET', configLocation);
xhr.responseType = 'json';
xhr.send();
var that = this;
xhr.addEventListener('load', function() {
if (!xhr.response) {
that.appLogger.error('Error loading app config', xhr.statusText);
return callback(true);
}
that.appLogger.info('Loaded app config from', configLocation, that.appLogger.ts(ts));
that.settings.set(xhr.response);
callback();
});
xhr.addEventListener('error', function() {
that.appLogger.error('Error loading app config', xhr.statusText, xhr.status);
callback(true);
});
},
addFile: function(file) {
if (this.files.get(file.id)) {
return false;

View File

@ -271,6 +271,8 @@ var Locale = {
appSaveError: 'Save Error',
appSaveErrorBody: 'Failed to auto-save file',
appSaveErrorBodyMul: 'Failed to auto-save files:',
appSettingsError: 'Error loading app',
appSettingsErrorBody: 'There was an error loading app settings. Please double check app url or contact your administrator.',
setGenTitle: 'General Settings',
setGenUpdate: 'Update',

View File

@ -16,6 +16,7 @@ Auto-type, ui improvements
`+` logout from remote storages on disable
`+` select file for new records
`+` customizable table view
`+` ability to load json config
`*` don't check updates at startup
`*` repos moved to github organization account
`*` allow opening same file twice