Completed
Push — master ( 2f8415...e6e0b0 )
by Rain
02:22
created

dev/Settings/Admin/Contacts.js   A

Size

Lines of Code 221

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
nc 1
dl 0
loc 221
rs 10
noi 0
1
2
import _ from '_';
3
import ko from 'ko';
4
5
import {
6
	settingsSaveHelperSimpleFunction,
7
	defautOptionsAfterRender,
8
	inArray, trim, boolToAjax
9
} from 'Common/Utils';
10
11
import {SaveSettingsStep, StorageResultType, Magics} from 'Common/Enums';
12
import {i18n} from 'Common/Translator';
13
import {settingsGet} from 'Storage/Settings';
14
import Remote from 'Remote/Admin/Ajax';
15
import {command} from 'Knoin/Knoin';
16
17
class ContactsAdminSettings
18
{
19
	constructor() {
20
		this.defautOptionsAfterRender = defautOptionsAfterRender;
21
		this.enableContacts = ko.observable(!!settingsGet('ContactsEnable'));
22
		this.contactsSharing = ko.observable(!!settingsGet('ContactsSharing'));
23
		this.contactsSync = ko.observable(!!settingsGet('ContactsSync'));
24
25
		const
26
			supportedTypes = [],
27
			types = ['sqlite', 'mysql', 'pgsql'],
28
			getTypeName = (name) => {
29
				switch (name)
30
				{
31
					case 'sqlite':
32
						name = 'SQLite';
33
						break;
34
					case 'mysql':
35
						name = 'MySQL';
36
						break;
37
					case 'pgsql':
38
						name = 'PostgreSQL';
39
						break;
40
					// no default
41
				}
42
43
				return name;
44
			};
45
46
		if (settingsGet('SQLiteIsSupported'))
47
		{
48
			supportedTypes.push('sqlite');
49
		}
50
		if (settingsGet('MySqlIsSupported'))
51
		{
52
			supportedTypes.push('mysql');
53
		}
54
		if (settingsGet('PostgreSqlIsSupported'))
55
		{
56
			supportedTypes.push('pgsql');
57
		}
58
59
		this.contactsSupported = 0 < supportedTypes.length;
60
61
		this.contactsTypes = ko.observableArray([]);
62
		this.contactsTypesOptions = this.contactsTypes.map((value) => {
63
			const disabled = -1 === inArray(value, supportedTypes);
64
			return {
65
				'id': value,
66
				'name': getTypeName(value) + (disabled ? ' (' + i18n('HINTS/NOT_SUPPORTED') + ')' : ''),
67
				'disabled': disabled
68
			};
69
		});
70
71
		this.contactsTypes(types);
72
		this.contactsType = ko.observable('');
73
74
		this.mainContactsType = ko.computed({
75
			read: this.contactsType,
76
			write: (value) => {
77
				if (value !== this.contactsType())
78
				{
79
					if (-1 < inArray(value, supportedTypes))
80
					{
81
						this.contactsType(value);
82
					}
83
					else if (0 < supportedTypes.length)
84
					{
85
						this.contactsType('');
86
					}
87
				}
88
				else
89
				{
90
					this.contactsType.valueHasMutated();
91
				}
92
			}
93
		}).extend({notify: 'always'});
94
95
		this.contactsType.subscribe(() => {
96
			this.testContactsSuccess(false);
97
			this.testContactsError(false);
98
			this.testContactsErrorMessage('');
99
		});
100
101
		this.pdoDsn = ko.observable(settingsGet('ContactsPdoDsn'));
102
		this.pdoUser = ko.observable(settingsGet('ContactsPdoUser'));
103
		this.pdoPassword = ko.observable(settingsGet('ContactsPdoPassword'));
104
105
		this.pdoDsnTrigger = ko.observable(SaveSettingsStep.Idle);
106
		this.pdoUserTrigger = ko.observable(SaveSettingsStep.Idle);
107
		this.pdoPasswordTrigger = ko.observable(SaveSettingsStep.Idle);
108
		this.contactsTypeTrigger = ko.observable(SaveSettingsStep.Idle);
109
110
		this.testing = ko.observable(false);
111
		this.testContactsSuccess = ko.observable(false);
112
		this.testContactsError = ko.observable(false);
113
		this.testContactsErrorMessage = ko.observable('');
114
115
		this.contactsType(settingsGet('ContactsPdoType'));
116
117
		this.onTestContactsResponse = _.bind(this.onTestContactsResponse, this);
118
	}
119
120
	@command((self) => '' !== self.pdoDsn() && '' !== self.pdoUser())
121
	testContactsCommand() {
122
		this.testContactsSuccess(false);
123
		this.testContactsError(false);
124
		this.testContactsErrorMessage('');
125
		this.testing(true);
126
127
		Remote.testContacts(this.onTestContactsResponse, {
128
			'ContactsPdoType': this.contactsType(),
129
			'ContactsPdoDsn': this.pdoDsn(),
130
			'ContactsPdoUser': this.pdoUser(),
131
			'ContactsPdoPassword': this.pdoPassword()
132
		});
133
	}
134
135
	onTestContactsResponse(result, data) {
136
		this.testContactsSuccess(false);
137
		this.testContactsError(false);
138
		this.testContactsErrorMessage('');
139
140
		if (StorageResultType.Success === result && data && data.Result && data.Result.Result)
141
		{
142
			this.testContactsSuccess(true);
143
		}
144
		else
145
		{
146
			this.testContactsError(true);
147
			if (data && data.Result)
148
			{
149
				this.testContactsErrorMessage(data.Result.Message || '');
150
			}
151
			else
152
			{
153
				this.testContactsErrorMessage('');
154
			}
155
		}
156
157
		this.testing(false);
158
	}
159
160
	onShow() {
161
		this.testContactsSuccess(false);
162
		this.testContactsError(false);
163
		this.testContactsErrorMessage('');
164
	}
165
166
	onBuild() {
167
		_.delay(() => {
168
			const
169
				f1 = settingsSaveHelperSimpleFunction(this.pdoDsnTrigger, this),
170
				f3 = settingsSaveHelperSimpleFunction(this.pdoUserTrigger, this),
171
				f4 = settingsSaveHelperSimpleFunction(this.pdoPasswordTrigger, this),
172
				f5 = settingsSaveHelperSimpleFunction(this.contactsTypeTrigger, this);
173
174
			this.enableContacts.subscribe((value) => {
175
				Remote.saveAdminConfig(null, {
176
					'ContactsEnable': boolToAjax(value)
177
				});
178
			});
179
180
			this.contactsSharing.subscribe((value) => {
181
				Remote.saveAdminConfig(null, {
182
					'ContactsSharing': boolToAjax(value)
183
				});
184
			});
185
186
			this.contactsSync.subscribe((value) => {
187
				Remote.saveAdminConfig(null, {
188
					'ContactsSync': boolToAjax(value)
189
				});
190
			});
191
192
			this.contactsType.subscribe((value) => {
193
				Remote.saveAdminConfig(f5, {
194
					'ContactsPdoType': trim(value)
195
				});
196
			});
197
198
			this.pdoDsn.subscribe((value) => {
199
				Remote.saveAdminConfig(f1, {
200
					'ContactsPdoDsn': trim(value)
201
				});
202
			});
203
204
			this.pdoUser.subscribe((value) => {
205
				Remote.saveAdminConfig(f3, {
206
					'ContactsPdoUser': trim(value)
207
				});
208
			});
209
210
			this.pdoPassword.subscribe((value) => {
211
				Remote.saveAdminConfig(f4, {
212
					'ContactsPdoPassword': trim(value)
213
				});
214
			});
215
216
			this.contactsType(settingsGet('ContactsPdoType'));
217
		}, Magics.Time50ms);
218
	}
219
}
220
221
export {ContactsAdminSettings, ContactsAdminSettings as default};
222