Completed
Push — master ( e88c19...17669b )
by Rain
03:00
created

dev/View/Popup/Domain.js   F

Complexity

Total Complexity 83
Complexity/F 2.96

Size

Lines of Code 482
Function Count 28

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 256
dl 0
loc 482
rs 3.12
c 1
b 0
f 0
wmc 83
mnd 3
bc 59
fnc 28
bpm 2.1071
cpm 2.9642
noi 0

How to fix   Complexity   

Complexity

Complex classes like dev/View/Popup/Domain.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
2
import _ from '_';
3
import ko from 'ko';
4
5
import {StorageResultType, ServerSecure, Ports, Notification} from 'Common/Enums';
6
import {IMAP_DEFAULT_PORT, SIEVE_DEFAULT_PORT, SMTP_DEFAULT_PORT} from 'Common/Consts';
7
import {bMobileDevice} from 'Common/Globals';
8
import {createCommand, trim, pInt, pString} from 'Common/Utils';
9
import {i18n} from 'Common/Translator';
10
11
import CapaAdminStore from 'Stores/Admin/Capa';
12
13
import Remote from 'Remote/Admin/Ajax';
14
15
import {getApp} from 'Helper/Apps/Admin';
16
17
import {view, ViewType} from 'Knoin/Knoin';
18
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
19
20
@view({
21
	name: 'View/Popup/Domain',
22
	type: ViewType.Popup,
23
	templateID: 'PopupsDomain'
24
})
25
class DomainPopupView extends AbstractViewNext
26
{
27
	constructor() {
28
		super();
29
30
		this.edit = ko.observable(false);
31
		this.saving = ko.observable(false);
32
		this.savingError = ko.observable('');
33
		this.page = ko.observable('main');
34
		this.sieveSettings = ko.observable(false);
35
36
		this.testing = ko.observable(false);
37
		this.testingDone = ko.observable(false);
38
		this.testingImapError = ko.observable(false);
39
		this.testingSieveError = ko.observable(false);
40
		this.testingSmtpError = ko.observable(false);
41
		this.testingImapErrorDesc = ko.observable('');
42
		this.testingSieveErrorDesc = ko.observable('');
43
		this.testingSmtpErrorDesc = ko.observable('');
44
45
		this.testingImapError.subscribe((value) => {
46
			if (!value)
47
			{
48
				this.testingImapErrorDesc('');
49
			}
50
		});
51
52
		this.testingSieveError.subscribe((value) => {
53
			if (!value)
54
			{
55
				this.testingSieveErrorDesc('');
56
			}
57
		});
58
59
		this.testingSmtpError.subscribe((value) => {
60
			if (!value)
61
			{
62
				this.testingSmtpErrorDesc('');
63
			}
64
		});
65
66
		this.imapServerFocus = ko.observable(false);
67
		this.sieveServerFocus = ko.observable(false);
68
		this.smtpServerFocus = ko.observable(false);
69
70
		this.name = ko.observable('');
71
		this.name.focused = ko.observable(false);
72
73
		this.imapServer = ko.observable('');
74
		this.imapPort = ko.observable('' + IMAP_DEFAULT_PORT);
75
		this.imapSecure = ko.observable(ServerSecure.None);
76
		this.imapShortLogin = ko.observable(false);
77
		this.useSieve = ko.observable(false);
78
		this.sieveAllowRaw = ko.observable(false);
79
		this.sieveServer = ko.observable('');
80
		this.sievePort = ko.observable('' + SIEVE_DEFAULT_PORT);
81
		this.sieveSecure = ko.observable(ServerSecure.None);
82
		this.smtpServer = ko.observable('');
83
		this.smtpPort = ko.observable('' + SMTP_DEFAULT_PORT);
84
		this.smtpSecure = ko.observable(ServerSecure.None);
85
		this.smtpShortLogin = ko.observable(false);
86
		this.smtpAuth = ko.observable(true);
87
		this.smtpPhpMail = ko.observable(false);
88
		this.whiteList = ko.observable('');
89
		this.aliasName = ko.observable('');
90
91
		this.enableSmartPorts = ko.observable(false);
92
93
		this.allowSieve = ko.computed(() => CapaAdminStore.filters() && CapaAdminStore.sieve());
94
95
		this.headerText = ko.computed(() => {
96
97
			const
98
				name = this.name(),
99
				aliasName = this.aliasName();
100
101
			let result = '';
102
103
			if (this.edit())
104
			{
105
				result = i18n('POPUPS_DOMAIN/TITLE_EDIT_DOMAIN', {'NAME': name});
106
				if (aliasName)
107
				{
108
					result += ' ← ' + aliasName;
109
				}
110
			}
111
			else
112
			{
113
				result = ('' === name ? i18n('POPUPS_DOMAIN/TITLE_ADD_DOMAIN') :
114
					i18n('POPUPS_DOMAIN/TITLE_ADD_DOMAIN_WITH_NAME', {'NAME': name}));
115
			}
116
117
			return result;
118
119
		});
120
121
		this.domainDesc = ko.computed(() => {
122
			const name = this.name();
123
			return !this.edit() && name ? i18n('POPUPS_DOMAIN/NEW_DOMAIN_DESC', {'NAME': '*@' + name}) : '';
124
		});
125
126
		this.domainIsComputed = ko.computed(() => {
127
128
			const
129
				usePhpMail = this.smtpPhpMail(),
130
				allowSieve = this.allowSieve(),
131
				useSieve = this.useSieve();
132
133
			return '' !== this.name() &&
134
				'' !== this.imapServer() &&
135
				'' !== this.imapPort() &&
136
				(allowSieve && useSieve ? ('' !== this.sieveServer() && '' !== this.sievePort()) : true) &&
137
				(('' !== this.smtpServer() && '' !== this.smtpPort()) || usePhpMail);
138
139
		});
140
141
		this.canBeTested = ko.computed(() => !this.testing() && this.domainIsComputed());
142
		this.canBeSaved = ko.computed(() => !this.saving() && this.domainIsComputed());
143
144
		this.createOrAddCommand = createCommand(() => {
145
			this.saving(true);
146
			Remote.createOrUpdateDomain(
147
				_.bind(this.onDomainCreateOrSaveResponse, this),
148
				!this.edit(),
149
				this.name(),
150
151
				this.imapServer(),
152
				pInt(this.imapPort()),
153
				this.imapSecure(),
154
				this.imapShortLogin(),
155
156
				this.useSieve(),
157
				this.sieveAllowRaw(),
158
				this.sieveServer(),
159
				pInt(this.sievePort()),
160
				this.sieveSecure(),
161
162
				this.smtpServer(),
163
				pInt(this.smtpPort()),
164
				this.smtpSecure(),
165
				this.smtpShortLogin(),
166
				this.smtpAuth(),
167
				this.smtpPhpMail(),
168
169
				this.whiteList()
170
			);
171
		}, this.canBeSaved);
172
173
		this.testConnectionCommand = createCommand(() => {
174
175
			this.page('main');
176
177
			this.testingDone(false);
178
			this.testingImapError(false);
179
			this.testingSieveError(false);
180
			this.testingSmtpError(false);
181
			this.testing(true);
182
183
			Remote.testConnectionForDomain(
184
				_.bind(this.onTestConnectionResponse, this),
185
				this.name(),
186
187
				this.imapServer(),
188
				pInt(this.imapPort()),
189
				this.imapSecure(),
190
191
				this.useSieve(),
192
				this.sieveServer(),
193
				pInt(this.sievePort()),
194
				this.sieveSecure(),
195
196
				this.smtpServer(),
197
				pInt(this.smtpPort()),
198
				this.smtpSecure(),
199
				this.smtpAuth(),
200
				this.smtpPhpMail()
201
			);
202
		}, this.canBeTested);
203
204
		this.whiteListCommand = createCommand(() => {
205
			this.page('white-list');
206
		});
207
208
		this.backCommand = createCommand(() => {
209
			this.page('main');
210
		});
211
212
		this.sieveCommand = createCommand(() => {
213
			this.sieveSettings(!this.sieveSettings());
214
			this.clearTesting();
215
		});
216
217
		this.page.subscribe(() => {
218
			this.sieveSettings(false);
219
		});
220
221
		// smart form improvements
222
		this.imapServerFocus.subscribe((value) => {
223
			if (value && '' !== this.name() && '' === this.imapServer())
224
			{
225
				this.imapServer(this.name().replace(/[.]?[*][.]?/g, ''));
226
			}
227
		});
228
229
		this.sieveServerFocus.subscribe((value) => {
230
			if (value && '' !== this.imapServer() && '' === this.sieveServer())
231
			{
232
				this.sieveServer(this.imapServer());
233
			}
234
		});
235
236
		this.smtpServerFocus.subscribe((value) => {
237
			if (value && '' !== this.imapServer() && '' === this.smtpServer())
238
			{
239
				this.smtpServer(this.imapServer().replace(/imap/ig, 'smtp'));
240
			}
241
		});
242
243
		this.imapSecure.subscribe((value) => {
244
			if (this.enableSmartPorts())
245
			{
246
				const port = pInt(this.imapPort());
247
				switch (pString(value))
248
				{
249
					case '0':
250
						if (Ports.ImapSsl === port)
251
						{
252
							this.imapPort(pString(Ports.Imap));
253
						}
254
						break;
255
					case '1':
256
						if (Ports.Imap === port)
257
						{
258
							this.imapPort(pString(Ports.ImapSsl));
259
						}
260
						break;
261
					// no default
262
				}
263
			}
264
		});
265
266
		this.smtpSecure.subscribe((value) => {
267
			if (this.enableSmartPorts())
268
			{
269
				const port = pInt(this.smtpPort());
270
				switch (pString(value))
271
				{
272
					case '0':
273
						if (Ports.SmtpSsl === port || Ports.SmtpStartTls === port)
274
						{
275
							this.smtpPort(pString(Ports.Smtp));
276
						}
277
						break;
278
					case '1':
279
						if (Ports.Smtp === port || Ports.SmtpStartTls === port)
280
						{
281
							this.smtpPort(pString(Ports.SmtpSsl));
282
						}
283
						break;
284
					case '2':
285
						if (Ports.Smtp === port || Ports.SmtpSsl === port)
286
						{
287
							this.smtpPort(pString(Ports.SmtpStartTls));
288
						}
289
						break;
290
					// no default
291
				}
292
			}
293
		});
294
	}
295
296
	onTestConnectionResponse(sResult, oData) {
297
		this.testing(false);
298
		if (StorageResultType.Success === sResult && oData.Result)
299
		{
300
			let
301
				bImap = false,
302
				bSieve = false;
303
304
			this.testingDone(true);
305
			this.testingImapError(true !== oData.Result.Imap);
306
			this.testingSieveError(true !== oData.Result.Sieve);
307
			this.testingSmtpError(true !== oData.Result.Smtp);
308
309
			if (this.testingImapError() && oData.Result.Imap)
310
			{
311
				bImap = true;
312
				this.testingImapErrorDesc('');
313
				this.testingImapErrorDesc(oData.Result.Imap);
314
			}
315
316
			if (this.testingSieveError() && oData.Result.Sieve)
317
			{
318
				bSieve = true;
319
				this.testingSieveErrorDesc('');
320
				this.testingSieveErrorDesc(oData.Result.Sieve);
321
			}
322
323
			if (this.testingSmtpError() && oData.Result.Smtp)
324
			{
325
				this.testingSmtpErrorDesc('');
326
				this.testingSmtpErrorDesc(oData.Result.Smtp);
327
			}
328
329
			if (this.sieveSettings())
330
			{
331
				if (!bSieve && bImap)
332
				{
333
					this.sieveSettings(false);
334
				}
335
			}
336
			else if (bSieve && !bImap)
337
			{
338
				this.sieveSettings(true);
339
			}
340
		}
341
		else
342
		{
343
			this.testingImapError(true);
344
			this.testingSieveError(true);
345
			this.testingSmtpError(true);
346
			this.sieveSettings(false);
347
		}
348
	}
349
350
	onDomainCreateOrSaveResponse(sResult, oData) {
351
		this.saving(false);
352
		if (StorageResultType.Success === sResult && oData)
353
		{
354
			if (oData.Result)
355
			{
356
				getApp().reloadDomainList();
357
				this.closeCommand();
358
			}
359
			else if (Notification.DomainAlreadyExists === oData.ErrorCode)
360
			{
361
				this.savingError(i18n('ERRORS/DOMAIN_ALREADY_EXISTS'));
362
			}
363
		}
364
		else
365
		{
366
			this.savingError(i18n('ERRORS/UNKNOWN_ERROR'));
367
		}
368
	}
369
370
	clearTesting() {
371
		this.testing(false);
372
		this.testingDone(false);
373
		this.testingImapError(false);
374
		this.testingSieveError(false);
375
		this.testingSmtpError(false);
376
	}
377
378
	onHide() {
379
		this.page('main');
380
		this.sieveSettings(false);
381
	}
382
383
	onShow(oDomain) {
384
		this.saving(false);
385
386
		this.page('main');
387
		this.sieveSettings(false);
388
389
		this.clearTesting();
390
391
		this.clearForm();
392
		if (oDomain)
393
		{
394
			this.enableSmartPorts(false);
395
396
			this.edit(true);
397
398
			this.name(trim(oDomain.Name));
399
			this.imapServer(trim(oDomain.IncHost));
400
			this.imapPort('' + pInt(oDomain.IncPort));
401
			this.imapSecure(trim(oDomain.IncSecure));
402
			this.imapShortLogin(!!oDomain.IncShortLogin);
403
			this.useSieve(!!oDomain.UseSieve);
404
			this.sieveAllowRaw(!!oDomain.SieveAllowRaw);
405
			this.sieveServer(trim(oDomain.SieveHost));
406
			this.sievePort('' + pInt(oDomain.SievePort));
407
			this.sieveSecure(trim(oDomain.SieveSecure));
408
			this.smtpServer(trim(oDomain.OutHost));
409
			this.smtpPort('' + pInt(oDomain.OutPort));
410
			this.smtpSecure(trim(oDomain.OutSecure));
411
			this.smtpShortLogin(!!oDomain.OutShortLogin);
412
			this.smtpAuth(!!oDomain.OutAuth);
413
			this.smtpPhpMail(!!oDomain.OutUsePhpMail);
414
			this.whiteList(trim(oDomain.WhiteList));
415
			this.aliasName(trim(oDomain.AliasName));
416
417
			this.enableSmartPorts(true);
418
		}
419
	}
420
421
	onShowWithDelay() {
422
		if ('' === this.name() && !bMobileDevice)
423
		{
424
			this.name.focused(true);
425
		}
426
	}
427
428
	clearForm() {
429
		this.edit(false);
430
431
		this.page('main');
432
		this.sieveSettings(false);
433
434
		this.enableSmartPorts(false);
435
436
		this.savingError('');
437
438
		this.name('');
439
		this.name.focused(false);
440
441
		this.imapServer('');
442
		this.imapPort('' + IMAP_DEFAULT_PORT);
443
		this.imapSecure(ServerSecure.None);
444
		this.imapShortLogin(false);
445
446
		this.useSieve(false);
447
		this.sieveAllowRaw(false);
448
		this.sieveServer('');
449
		this.sievePort('' + SIEVE_DEFAULT_PORT);
450
		this.sieveSecure(ServerSecure.None);
451
452
		this.smtpServer('');
453
		this.smtpPort('' + SMTP_DEFAULT_PORT);
454
		this.smtpSecure(ServerSecure.None);
455
		this.smtpShortLogin(false);
456
		this.smtpAuth(true);
457
		this.smtpPhpMail(false);
458
459
		this.whiteList('');
460
		this.aliasName('');
461
		this.enableSmartPorts(true);
462
	}
463
}
464
465
module.exports = DomainPopupView;
466