2018-03-09 22:59:12 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
|
|
|
const SyncTargetRegistry = require('lib/SyncTargetRegistry');
|
2018-03-15 19:57:11 +02:00
|
|
|
const ObjectUtils = require('lib/ObjectUtils');
|
2018-03-09 22:59:12 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const shared = {};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
|
|
|
shared.init = function(comp) {
|
|
|
|
if (!comp.state) comp.state = {};
|
|
|
|
comp.state.checkSyncConfigResult = null;
|
2018-02-13 20:26:33 +02:00
|
|
|
comp.state.settings = {};
|
|
|
|
comp.state.changedSettingKeys = [];
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
|
|
|
shared.checkSyncConfig = async function(comp, settings) {
|
2018-03-09 22:59:12 +02:00
|
|
|
const syncTargetId = settings['sync.target'];
|
2018-02-06 20:59:36 +02:00
|
|
|
const SyncTargetClass = SyncTargetRegistry.classById(syncTargetId);
|
2018-03-09 22:59:12 +02:00
|
|
|
const options = Setting.subValues('sync.' + syncTargetId, settings);
|
|
|
|
comp.setState({ checkSyncConfigResult: 'checking' });
|
2018-03-15 19:57:11 +02:00
|
|
|
const result = await SyncTargetClass.checkConfig(ObjectUtils.convertValuesToFunctions(options));
|
2018-02-06 20:59:36 +02:00
|
|
|
comp.setState({ checkSyncConfigResult: result });
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
|
|
|
shared.checkSyncConfigMessages = function(comp) {
|
|
|
|
const result = comp.state.checkSyncConfigResult;
|
|
|
|
const output = [];
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (result === 'checking') {
|
|
|
|
output.push(_('Checking... Please wait.'));
|
2018-02-06 20:59:36 +02:00
|
|
|
} else if (result && result.ok) {
|
2018-03-09 22:59:12 +02:00
|
|
|
output.push(_('Success! Synchronisation configuration appears to be correct.'));
|
2018-02-06 20:59:36 +02:00
|
|
|
} else if (result && !result.ok) {
|
2018-03-09 22:59:12 +02:00
|
|
|
output.push(_('Error. Please check that URL, username, password, etc. are correct and that the sync target is accessible. The reported error was:'));
|
2018-02-06 20:59:36 +02:00
|
|
|
output.push(result.errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2018-02-13 20:26:33 +02:00
|
|
|
shared.updateSettingValue = function(comp, key, value) {
|
|
|
|
const settings = Object.assign({}, comp.state.settings);
|
|
|
|
const changedSettingKeys = comp.state.changedSettingKeys.slice();
|
|
|
|
settings[key] = Setting.formatValue(key, value);
|
|
|
|
if (changedSettingKeys.indexOf(key) < 0) changedSettingKeys.push(key);
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
settings: settings,
|
|
|
|
changedSettingKeys: changedSettingKeys,
|
|
|
|
});
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-13 20:26:33 +02:00
|
|
|
|
|
|
|
shared.saveSettings = function(comp) {
|
|
|
|
for (let key in comp.state.settings) {
|
|
|
|
if (!comp.state.settings.hasOwnProperty(key)) continue;
|
|
|
|
if (comp.state.changedSettingKeys.indexOf(key) < 0) continue;
|
2019-07-29 15:43:53 +02:00
|
|
|
console.info('Saving', key, comp.state.settings[key]);
|
2018-02-13 20:26:33 +02:00
|
|
|
Setting.setValue(key, comp.state.settings[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.setState({ changedSettingKeys: [] });
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-13 20:26:33 +02:00
|
|
|
|
|
|
|
shared.settingsToComponents = function(comp, device, settings) {
|
|
|
|
const keys = Setting.keys(true, device);
|
|
|
|
const settingComps = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (!Setting.isPublic(key)) continue;
|
|
|
|
|
|
|
|
const md = Setting.settingMetadata(key);
|
|
|
|
if (md.show && !md.show(settings)) continue;
|
|
|
|
|
|
|
|
const settingComp = comp.settingToComponent(key, settings[key]);
|
|
|
|
if (!settingComp) continue;
|
|
|
|
settingComps.push(settingComp);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return settingComps;
|
|
|
|
};
|
2018-02-13 20:26:33 +02:00
|
|
|
|
2019-01-28 01:15:56 +02:00
|
|
|
shared.settingsToComponents2 = function(comp, device, settings) {
|
|
|
|
const keys = Setting.keys(true, device);
|
|
|
|
const sectionComps = [];
|
|
|
|
const metadatas = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (!Setting.isPublic(key)) continue;
|
|
|
|
|
|
|
|
const md = Setting.settingMetadata(key);
|
|
|
|
if (md.show && !md.show(settings)) continue;
|
|
|
|
|
|
|
|
metadatas.push(md);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sections = Setting.groupMetadatasBySections(metadatas);
|
|
|
|
|
|
|
|
for (let i = 0; i < sections.length; i++) {
|
|
|
|
const section = sections[i];
|
|
|
|
const sectionComp = comp.sectionToComponent(section.name, section, settings);
|
|
|
|
if (!sectionComp) continue;
|
|
|
|
sectionComps.push(sectionComp);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return sectionComps;
|
|
|
|
};
|
2019-01-28 01:15:56 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = shared;
|