snappymail/dev/Settings/Admin/Contacts.js

195 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import _ from '_';
import ko from 'ko';
import { settingsSaveHelperSimpleFunction, defautOptionsAfterRender, trim, boolToAjax } from 'Common/Utils';
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
import { SaveSettingsStep, StorageResultType, Magics } from 'Common/Enums';
import { i18n } from 'Common/Translator';
import { settingsGet } from 'Storage/Settings';
2016-09-10 06:38:16 +08:00
import Remote from 'Remote/Admin/Ajax';
2019-07-05 03:19:24 +08:00
import { command } from 'Knoin/Knoin';
2015-03-28 06:06:56 +08:00
2019-07-05 03:19:24 +08:00
class ContactsAdminSettings {
2016-07-16 05:29:42 +08:00
constructor() {
this.defautOptionsAfterRender = defautOptionsAfterRender;
this.enableContacts = ko.observable(!!settingsGet('ContactsEnable'));
this.contactsSync = ko.observable(!!settingsGet('ContactsSync'));
2019-07-05 03:19:24 +08:00
const supportedTypes = [],
2016-07-16 05:29:42 +08:00
types = ['sqlite', 'mysql', 'pgsql'],
getTypeName = (name) => {
2019-07-05 03:19:24 +08:00
switch (name) {
2016-07-16 05:29:42 +08:00
case 'sqlite':
name = 'SQLite';
break;
case 'mysql':
name = 'MySQL';
break;
case 'pgsql':
name = 'PostgreSQL';
break;
// no default
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
return name;
};
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (settingsGet('SQLiteIsSupported')) {
2016-07-16 05:29:42 +08:00
supportedTypes.push('sqlite');
}
2019-07-05 03:19:24 +08:00
if (settingsGet('MySqlIsSupported')) {
2016-07-16 05:29:42 +08:00
supportedTypes.push('mysql');
}
2019-07-05 03:19:24 +08:00
if (settingsGet('PostgreSqlIsSupported')) {
2016-07-16 05:29:42 +08:00
supportedTypes.push('pgsql');
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.contactsSupported = 0 < supportedTypes.length;
this.contactsTypes = ko.observableArray([]);
this.contactsTypesOptions = ko.computed(() =>
_.map(this.contactsTypes(), (value) => {
const disabled = supportedTypes.includes(value);
return {
'id': value,
'name': getTypeName(value) + (disabled ? ' (' + i18n('HINTS/NOT_SUPPORTED') + ')' : ''),
'disabled': disabled
};
})
);
2016-07-16 05:29:42 +08:00
this.contactsTypes(types);
this.contactsType = ko.observable('');
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 (0 < supportedTypes.length) {
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.pdoDsn = ko.observable(settingsGet('ContactsPdoDsn'));
this.pdoUser = ko.observable(settingsGet('ContactsPdoUser'));
this.pdoPassword = ko.observable(settingsGet('ContactsPdoPassword'));
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.pdoDsnTrigger = ko.observable(SaveSettingsStep.Idle);
this.pdoUserTrigger = ko.observable(SaveSettingsStep.Idle);
this.pdoPasswordTrigger = ko.observable(SaveSettingsStep.Idle);
this.contactsTypeTrigger = ko.observable(SaveSettingsStep.Idle);
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.testing = ko.observable(false);
this.testContactsSuccess = ko.observable(false);
this.testContactsError = ko.observable(false);
this.testContactsErrorMessage = ko.observable('');
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
2016-07-16 05:29:42 +08:00
this.onTestContactsResponse = _.bind(this.onTestContactsResponse, this);
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
@command((self) => '' !== self.pdoDsn() && '' !== self.pdoUser())
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() {
_.delay(() => {
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': boolToAjax(value)
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.contactsSync.subscribe((value) => {
Remote.saveAdminConfig(null, {
'ContactsSync': boolToAjax(value)
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.contactsType.subscribe((value) => {
Remote.saveAdminConfig(f5, {
'ContactsPdoType': trim(value)
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.pdoDsn.subscribe((value) => {
Remote.saveAdminConfig(f1, {
'ContactsPdoDsn': trim(value)
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.pdoUser.subscribe((value) => {
Remote.saveAdminConfig(f3, {
'ContactsPdoUser': trim(value)
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.pdoPassword.subscribe((value) => {
Remote.saveAdminConfig(f4, {
'ContactsPdoPassword': trim(value)
});
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.contactsType(settingsGet('ContactsPdoType'));
}, Magics.Time50ms);
}
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
export { ContactsAdminSettings, ContactsAdminSettings as default };