snappymail/dev/Settings/Admin/Contacts.js

166 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from 'ko';
import { settingsSaveHelperSimpleFunction, defaultOptionsAfterRender } from 'Common/Utils';
2014-08-21 23:08:34 +08:00
2020-08-14 04:58:41 +08:00
import { SaveSettingsStep, StorageResultType } from 'Common/Enums';
import Remote from 'Remote/Admin/Fetch';
2019-07-05 03:19:24 +08:00
import { command } from 'Knoin/Knoin';
2015-03-28 06:06:56 +08:00
const settingsGet = rl.settings.get;
2021-01-22 23:32:08 +08:00
export class ContactsAdminSettings {
2016-07-16 05:29:42 +08:00
constructor() {
this.defaultOptionsAfterRender = defaultOptionsAfterRender;
2020-10-27 18:09:24 +08:00
ko.addObservablesTo(this, {
enableContacts: !!settingsGet('ContactsEnable'),
contactsSync: !!settingsGet('ContactsSync'),
contactsType: '',
pdoDsn: settingsGet('ContactsPdoDsn'),
pdoUser: settingsGet('ContactsPdoUser'),
pdoPassword: settingsGet('ContactsPdoPassword'),
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
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,
write: (value) => {
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
this.contactsType.subscribe(() => {
2016-07-16 05:29:42 +08:00
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
});
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.contactsType(settingsGet('ContactsPdoType'));
2014-08-21 23:08:34 +08:00
this.onTestContactsResponse = this.onTestContactsResponse.bind(this);
2016-07-16 05:29:42 +08:00
}
2014-08-21 23:08:34 +08:00
@command((self) => self.pdoDsn() && self.pdoUser())
2016-09-10 06:38:16 +08:00
testContactsCommand() {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
this.testing(true);
Remote.testContacts(this.onTestContactsResponse, {
'ContactsPdoType': this.contactsType(),
'ContactsPdoDsn': this.pdoDsn(),
'ContactsPdoUser': this.pdoUser(),
'ContactsPdoPassword': this.pdoPassword()
});
}
2016-07-16 05:29:42 +08:00
onTestContactsResponse(result, data) {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === result && data && data.Result && data.Result.Result) {
2016-07-16 05:29:42 +08:00
this.testContactsSuccess(true);
2019-07-05 03:19:24 +08:00
} else {
2016-07-16 05:29:42 +08:00
this.testContactsError(true);
2019-07-05 03:19:24 +08:00
if (data && data.Result) {
2016-07-16 05:29:42 +08:00
this.testContactsErrorMessage(data.Result.Message || '');
2019-07-05 03:19:24 +08:00
} else {
2016-07-16 05:29:42 +08:00
this.testContactsErrorMessage('');
}
2014-08-21 23:08:34 +08:00
}
2016-07-16 05:29:42 +08:00
this.testing(false);
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
onShow() {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
onBuild() {
setTimeout(() => {
2019-07-05 03:19:24 +08:00
const f1 = settingsSaveHelperSimpleFunction(this.pdoDsnTrigger, this),
2016-07-16 05:29:42 +08:00
f3 = settingsSaveHelperSimpleFunction(this.pdoUserTrigger, this),
f4 = settingsSaveHelperSimpleFunction(this.pdoPasswordTrigger, this),
f5 = settingsSaveHelperSimpleFunction(this.contactsTypeTrigger, this);
this.enableContacts.subscribe((value) => {
Remote.saveAdminConfig(null, {
'ContactsEnable': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.contactsSync.subscribe((value) => {
Remote.saveAdminConfig(null, {
'ContactsSync': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.contactsType.subscribe((value) => {
Remote.saveAdminConfig(f5, {
'ContactsPdoType': value.trim()
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.pdoDsn.subscribe((value) => {
Remote.saveAdminConfig(f1, {
'ContactsPdoDsn': value.trim()
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.pdoUser.subscribe((value) => {
Remote.saveAdminConfig(f3, {
'ContactsPdoUser': value.trim()
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.pdoPassword.subscribe((value) => {
Remote.saveAdminConfig(f4, {
'ContactsPdoPassword': value.trim()
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.contactsType(settingsGet('ContactsPdoType'));
2020-08-14 04:58:41 +08:00
}, 50);
2016-07-16 05:29:42 +08:00
}
}