2016-07-16 05:29:42 +08:00
|
|
|
import ko from 'ko';
|
2014-09-02 08:15:31 +08:00
|
|
|
|
2021-03-16 16:46:23 +08:00
|
|
|
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
|
|
|
|
2020-09-15 01:40:56 +08:00
|
|
|
import Remote from 'Remote/Admin/Fetch';
|
2021-02-19 19:09:20 +08:00
|
|
|
import { decorateKoCommands } from 'Knoin/Knoin';
|
2015-03-28 06:06:56 +08:00
|
|
|
|
2021-01-22 23:32:08 +08:00
|
|
|
export class ContactsAdminSettings {
|
2016-07-16 05:29:42 +08:00
|
|
|
constructor() {
|
2020-10-15 01:16:37 +08:00
|
|
|
this.defaultOptionsAfterRender = defaultOptionsAfterRender;
|
2020-10-27 18:09:24 +08:00
|
|
|
|
2021-03-16 17:59:47 +08:00
|
|
|
addObservablesTo(this, {
|
2021-03-10 18:44:48 +08:00
|
|
|
enableContacts: !!SettingsGet('ContactsEnable'),
|
|
|
|
contactsSync: !!SettingsGet('ContactsSync'),
|
2020-10-27 18:09:24 +08:00
|
|
|
contactsType: '',
|
|
|
|
|
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') || [],
|
2020-09-24 15:58:35 +08:00
|
|
|
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
|
|
|
|
2020-09-24 15:58:35 +08:00
|
|
|
this.contactsSupported = 0 < types.length;
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-09-24 15:58:35 +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()) {
|
2020-07-20 20:33:33 +08:00
|
|
|
if (supportedTypes.includes(value)) {
|
2019-07-05 03:19:24 +08:00
|
|
|
this.contactsType(value);
|
2020-09-24 15:58:35 +08:00
|
|
|
} 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-22 20:47:07 +08:00
|
|
|
this.contactsType(SettingsGet('ContactsPdoType'));
|
|
|
|
|
2021-03-16 18:38:40 +08:00
|
|
|
addSubscribablesTo(this, {
|
|
|
|
enableContacts: value =>
|
|
|
|
Remote.saveAdminConfig(null, {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsEnable: value ? 1 : 0
|
2021-03-16 18:38:40 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
contactsSync: value =>
|
|
|
|
Remote.saveAdminConfig(null, {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsSync: value ? 1 : 0
|
2021-03-16 18:38:40 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
contactsType: value => {
|
|
|
|
this.testContactsSuccess(false);
|
|
|
|
this.testContactsError(false);
|
|
|
|
this.testContactsErrorMessage('');
|
|
|
|
Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.contactsTypeTrigger, this), {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsPdoType: value.trim()
|
2021-03-16 18:38:40 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
pdoDsn: value =>
|
|
|
|
Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.pdoDsnTrigger, this), {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsPdoDsn: value.trim()
|
2021-03-16 18:38:40 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
pdoUser: value =>
|
|
|
|
Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.pdoUserTrigger, this), {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsPdoUser: value.trim()
|
2021-03-16 18:38:40 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
pdoPassword: value =>
|
|
|
|
Remote.saveAdminConfig(settingsSaveHelperSimpleFunction(this.pdoPasswordTrigger, this), {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsPdoPassword: value.trim()
|
2021-03-16 18:38:40 +08:00
|
|
|
})
|
|
|
|
})
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-07-20 21:47:33 +08:00
|
|
|
this.onTestContactsResponse = this.onTestContactsResponse.bind(this);
|
2021-02-19 19:09:20 +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);
|
|
|
|
|
|
|
|
Remote.testContacts(this.onTestContactsResponse, {
|
2021-03-25 04:26:40 +08:00
|
|
|
ContactsPdoType: this.contactsType(),
|
|
|
|
ContactsPdoDsn: this.pdoDsn(),
|
|
|
|
ContactsPdoUser: this.pdoUser(),
|
|
|
|
ContactsPdoPassword: this.pdoPassword()
|
2016-09-10 06:38:16 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:46:23 +08:00
|
|
|
onTestContactsResponse(iError, data) {
|
2016-07-16 05:29:42 +08:00
|
|
|
this.testContactsSuccess(false);
|
|
|
|
this.testContactsError(false);
|
|
|
|
this.testContactsErrorMessage('');
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-03-18 21:48:21 +08:00
|
|
|
if (!iError && 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('');
|
|
|
|
}
|
|
|
|
}
|