snappymail/dev/Settings/Admin/Contacts.js

235 lines
5.7 KiB
JavaScript
Raw Normal View History

2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2016-06-30 08:02:45 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
Translator = require('Common/Translator'),
2015-03-28 06:06:56 +08:00
2016-06-30 08:02:45 +08:00
Settings = require('Storage/Settings');
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
/**
* @constructor
*/
function ContactsAdminSettings()
{
var
Remote = require('Remote/Admin/Ajax');
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.defautOptionsAfterRender = Utils.defautOptionsAfterRender;
this.enableContacts = ko.observable(!!Settings.settingsGet('ContactsEnable'));
this.contactsSharing = ko.observable(!!Settings.settingsGet('ContactsSharing'));
this.contactsSync = ko.observable(!!Settings.settingsGet('ContactsSync'));
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
var
aTypes = ['sqlite', 'mysql', 'pgsql'],
aSupportedTypes = [],
getTypeName = function(sName) {
switch (sName)
{
case 'sqlite':
sName = 'SQLite';
break;
case 'mysql':
sName = 'MySQL';
break;
case 'pgsql':
sName = 'PostgreSQL';
break;
// no default
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
return sName;
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
if (Settings.settingsGet('SQLiteIsSupported'))
{
aSupportedTypes.push('sqlite');
}
if (Settings.settingsGet('MySqlIsSupported'))
{
aSupportedTypes.push('mysql');
}
if (Settings.settingsGet('PostgreSqlIsSupported'))
{
aSupportedTypes.push('pgsql');
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.contactsSupported = 0 < aSupportedTypes.length;
this.contactsTypes = ko.observableArray([]);
this.contactsTypesOptions = this.contactsTypes.map(function(sValue) {
var bDisabled = -1 === Utils.inArray(sValue, aSupportedTypes);
return {
'id': sValue,
'name': getTypeName(sValue) + (bDisabled ? ' (' + Translator.i18n('HINTS/NOT_SUPPORTED') + ')' : ''),
'disabled': bDisabled
};
});
this.contactsTypes(aTypes);
this.contactsType = ko.observable('');
this.mainContactsType = ko.computed({
'owner': this,
'read': this.contactsType,
'write': function(sValue) {
if (sValue !== this.contactsType())
{
if (-1 < Utils.inArray(sValue, aSupportedTypes))
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
this.contactsType(sValue);
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
else if (0 < aSupportedTypes.length)
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
this.contactsType('');
2014-08-21 23:08:34 +08:00
}
}
2016-06-30 08:02:45 +08:00
else
{
this.contactsType.valueHasMutated();
}
}
}).extend({'notify': 'always'});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.contactsType.subscribe(function() {
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
}, this);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.pdoDsn = ko.observable(Settings.settingsGet('ContactsPdoDsn'));
this.pdoUser = ko.observable(Settings.settingsGet('ContactsPdoUser'));
this.pdoPassword = ko.observable(Settings.settingsGet('ContactsPdoPassword'));
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.pdoDsnTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
this.pdoUserTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
this.pdoPasswordTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
this.contactsTypeTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +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-06-30 08:02:45 +08:00
this.testContactsCommand = Utils.createCommand(this, function() {
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
this.testing(true);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
Remote.testContacts(this.onTestContactsResponse, {
'ContactsPdoType': this.contactsType(),
'ContactsPdoDsn': this.pdoDsn(),
'ContactsPdoUser': this.pdoUser(),
'ContactsPdoPassword': this.pdoPassword()
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
}, function() {
return '' !== this.pdoDsn() && '' !== this.pdoUser();
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.contactsType(Settings.settingsGet('ContactsPdoType'));
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.onTestContactsResponse = _.bind(this.onTestContactsResponse, this);
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
ContactsAdminSettings.prototype.onTestContactsResponse = function(sResult, oData)
{
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
if (Enums.StorageResultType.Success === sResult && oData && oData.Result && oData.Result.Result)
{
this.testContactsSuccess(true);
}
else
{
this.testContactsError(true);
if (oData && oData.Result)
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
this.testContactsErrorMessage(oData.Result.Message || '');
2014-08-21 23:08:34 +08:00
}
else
{
2016-06-30 08:02:45 +08:00
this.testContactsErrorMessage('');
2014-08-21 23:08:34 +08:00
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.testing(false);
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
ContactsAdminSettings.prototype.onShow = function()
{
this.testContactsSuccess(false);
this.testContactsError(false);
this.testContactsErrorMessage('');
};
ContactsAdminSettings.prototype.onBuild = function()
{
var
self = this,
Remote = require('Remote/Admin/Ajax');
_.delay(function() {
2014-08-21 23:08:34 +08:00
2014-08-25 15:10:51 +08:00
var
2016-06-30 08:02:45 +08:00
f1 = Utils.settingsSaveHelperSimpleFunction(self.pdoDsnTrigger, self),
f3 = Utils.settingsSaveHelperSimpleFunction(self.pdoUserTrigger, self),
f4 = Utils.settingsSaveHelperSimpleFunction(self.pdoPasswordTrigger, self),
f5 = Utils.settingsSaveHelperSimpleFunction(self.contactsTypeTrigger, self);
self.enableContacts.subscribe(function(bValue) {
Remote.saveAdminConfig(null, {
'ContactsEnable': bValue ? '1' : '0'
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.contactsSharing.subscribe(function(bValue) {
Remote.saveAdminConfig(null, {
'ContactsSharing': bValue ? '1' : '0'
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.contactsSync.subscribe(function(bValue) {
Remote.saveAdminConfig(null, {
'ContactsSync': bValue ? '1' : '0'
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.contactsType.subscribe(function(sValue) {
Remote.saveAdminConfig(f5, {
'ContactsPdoType': sValue
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.pdoDsn.subscribe(function(sValue) {
Remote.saveAdminConfig(f1, {
'ContactsPdoDsn': Utils.trim(sValue)
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.pdoUser.subscribe(function(sValue) {
Remote.saveAdminConfig(f3, {
'ContactsPdoUser': Utils.trim(sValue)
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.pdoPassword.subscribe(function(sValue) {
Remote.saveAdminConfig(f4, {
'ContactsPdoPassword': Utils.trim(sValue)
2014-08-21 23:08:34 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
self.contactsType(Settings.settingsGet('ContactsPdoType'));
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
}, 50);
};
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
module.exports = ContactsAdminSettings;