snappymail/dev/Settings/Admin/Contacts.js

152 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from 'ko';
import { SaveSettingsStep } from 'Common/Enums';
2021-03-10 18:44:48 +08:00
import { SettingsGet } from 'Common/Globals';
2021-03-16 18:38:40 +08:00
import {
settingsSaveHelperSimpleFunction,
defaultOptionsAfterRender,
addObservablesTo,
addSubscribablesTo
} from 'Common/Utils';
2014-08-21 23:08:34 +08:00
import Remote from 'Remote/Admin/Fetch';
import { decorateKoCommands } from 'Knoin/Knoin';
2015-03-28 06:06:56 +08:00
export class ContactsAdminSettings /*extends AbstractViewSettings*/ {
2016-07-16 05:29:42 +08:00
constructor() {
this.defaultOptionsAfterRender = defaultOptionsAfterRender;
2020-10-27 18:09:24 +08:00
addObservablesTo(this, {
2021-03-10 18:44:48 +08:00
enableContacts: !!SettingsGet('ContactsEnable'),
contactsSync: !!SettingsGet('ContactsSync'),
contactsType: SettingsGet('ContactsPdoType'),
2020-10-27 18:09:24 +08:00
2021-03-10 18:44:48 +08:00
pdoDsn: SettingsGet('ContactsPdoDsn'),
pdoUser: SettingsGet('ContactsPdoUser'),
pdoPassword: SettingsGet('ContactsPdoPassword'),
2020-10-27 18:09:24 +08:00
pdoDsnTrigger: SaveSettingsStep.Idle,
pdoUserTrigger: SaveSettingsStep.Idle,
pdoPasswordTrigger: SaveSettingsStep.Idle,
contactsTypeTrigger: SaveSettingsStep.Idle,
testing: false,
testContactsSuccess: false,
testContactsError: false,
testContactsErrorMessage: ''
});
2016-07-16 05:29:42 +08:00
2021-03-10 18:44:48 +08:00
const supportedTypes = SettingsGet('supportedPdoDrivers') || [],
types = [{
id:'sqlite',
name:'SQLite'
},{
id:'mysql',
name:'MySQL'
},{
id:'pgsql',
name:'PostgreSQL'
}].filter(type => supportedTypes.includes(type.id));
2014-08-21 23:08:34 +08:00
this.contactsSupported = 0 < types.length;
2014-08-21 23:08:34 +08:00
this.contactsTypesOptions = types;
2016-07-16 05:29:42 +08:00
2019-07-05 03:19:24 +08:00
this.mainContactsType = ko
.computed({
read: this.contactsType,
2021-03-16 18:38:40 +08:00
write: value => {
2019-07-05 03:19:24 +08:00
if (value !== this.contactsType()) {
if (supportedTypes.includes(value)) {
2019-07-05 03:19:24 +08:00
this.contactsType(value);
} else if (types.length) {
2019-07-05 03:19:24 +08:00
this.contactsType('');
}
} else {
this.contactsType.valueHasMutated();
2016-07-16 05:29:42 +08:00
}
2014-08-21 23:08:34 +08:00
}
2019-07-05 03:19:24 +08:00
})
.extend({ notify: 'always' });
2014-08-21 23:08:34 +08:00
2021-03-16 18:38:40 +08:00
addSubscribablesTo(this, {
enableContacts: value =>
Remote.saveConfig({
ContactsEnable: value ? 1 : 0
2021-03-16 18:38:40 +08:00
}),
contactsSync: value =>
Remote.saveConfig({
ContactsSync: value ? 1 : 0
2021-03-16 18:38:40 +08:00
}),
contactsType: value => {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
Remote.saveConfig({
ContactsPdoType: value.trim()
}, settingsSaveHelperSimpleFunction(this.contactsTypeTrigger, this))
2021-03-16 18:38:40 +08:00
},
pdoDsn: value =>
Remote.saveConfig({
ContactsPdoDsn: value.trim()
}, settingsSaveHelperSimpleFunction(this.pdoDsnTrigger, this)),
2021-03-16 18:38:40 +08:00
pdoUser: value =>
Remote.saveConfig({
ContactsPdoUser: value.trim()
}, settingsSaveHelperSimpleFunction(this.pdoUserTrigger, this)),
2021-03-16 18:38:40 +08:00
pdoPassword: value =>
Remote.saveConfig({
ContactsPdoPassword: value.trim()
}, settingsSaveHelperSimpleFunction(this.pdoPasswordTrigger, this))
2021-03-16 18:38:40 +08:00
})
2014-08-21 23:08:34 +08:00
decorateKoCommands(this, {
testContactsCommand: self => self.pdoDsn() && self.pdoUser()
});
2016-07-16 05:29:42 +08:00
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
testContactsCommand() {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
this.testing(true);
2021-12-03 07:11:19 +08:00
Remote.request('AdminContactsTest',
(iError, data) => {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
if (!iError && data.Result.Result) {
this.testContactsSuccess(true);
2021-04-23 03:56:01 +08:00
} else {
2021-12-03 07:11:19 +08:00
this.testContactsError(true);
if (data && data.Result) {
this.testContactsErrorMessage(data.Result.Message || '');
} else {
this.testContactsErrorMessage('');
}
2021-04-23 03:56:01 +08:00
}
2021-12-03 07:11:19 +08:00
this.testing(false);
}, {
ContactsPdoType: this.contactsType(),
ContactsPdoDsn: this.pdoDsn(),
ContactsPdoUser: this.pdoUser(),
ContactsPdoPassword: this.pdoPassword()
}
);
2016-09-10 06:38:16 +08:00
}
2016-07-16 05:29:42 +08:00
onShow() {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
}
}