Code Duplication    Length = 196-198 lines in 2 locations

dev/View/Popup/Template.js 1 location

@@ 2-199 (lines=198) @@
1
2
import ko from 'ko';
3
4
import {StorageResultType, Notification} from 'Common/Enums';
5
import {trim, isNormal, createCommand} from 'Common/Utils';
6
import {getNotification} from 'Common/Translator';
7
import {HtmlEditor} from 'Common/HtmlEditor';
8
9
import Remote from 'Remote/User/Ajax';
10
11
import {getApp} from 'Helper/Apps/User';
12
13
import {view, ViewType} from 'Knoin/Knoin';
14
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
15
16
@view({
17
	name: 'View/Popup/Template',
18
	type: ViewType.Popup,
19
	templateID: 'PopupsTemplate'
20
})
21
class TemplatePopupView extends AbstractViewNext
22
{
23
	constructor() {
24
		super();
25
26
		this.editor = null;
27
		this.signatureDom = ko.observable(null);
28
29
		this.id = ko.observable('');
30
31
		this.name = ko.observable('');
32
		this.name.error = ko.observable(false);
33
		this.name.focus = ko.observable(false);
34
35
		this.body = ko.observable('');
36
		this.body.loading = ko.observable(false);
37
		this.body.error = ko.observable(false);
38
39
		this.name.subscribe(() => {
40
			this.name.error(false);
41
		});
42
43
		this.body.subscribe(() => {
44
			this.body.error(false);
45
		});
46
47
		this.submitRequest = ko.observable(false);
48
		this.submitError = ko.observable('');
49
50
		this.addTemplateCommand = createCommand(() => {
51
52
			this.populateBodyFromEditor();
53
54
			this.name.error('' === trim(this.name()));
55
			this.body.error('' === trim(this.body()) || ':HTML:' === trim(this.body()));
56
57
			if (this.name.error() || this.body.error())
58
			{
59
				return false;
60
			}
61
62
			this.submitRequest(true);
63
64
			Remote.templateSetup((result, data) => {
65
66
				this.submitRequest(false);
67
				if (StorageResultType.Success === result && data)
68
				{
69
					if (data.Result)
70
					{
71
						getApp().templates();
72
						this.cancelCommand();
73
					}
74
					else if (data.ErrorCode)
75
					{
76
						this.submitError(getNotification(data.ErrorCode));
77
					}
78
				}
79
				else
80
				{
81
					this.submitError(getNotification(Notification.UnknownError));
82
				}
83
84
			}, this.id(), this.name(), this.body());
85
86
			return true;
87
88
		}, () => !this.submitRequest());
89
	}
90
91
	clearPopup() {
92
		this.id('');
93
94
		this.name('');
95
		this.name.error(false);
96
97
		this.body('');
98
		this.body.loading(false);
99
		this.body.error(false);
100
101
		this.submitRequest(false);
102
		this.submitError('');
103
104
		if (this.editor)
105
		{
106
			this.editor.setPlain('', false);
107
		}
108
	}
109
110
	populateBodyFromEditor() {
111
		if (this.editor)
112
		{
113
			this.body(this.editor.getDataWithHtmlMark());
114
		}
115
	}
116
117
	editorSetBody(sBody) {
118
		if (!this.editor && this.signatureDom())
119
		{
120
			this.editor = new HtmlEditor(this.signatureDom(), () => {
121
				this.populateBodyFromEditor();
122
			}, () => {
123
				this.editor.setHtmlOrPlain(sBody);
124
			});
125
		}
126
		else
127
		{
128
			this.editor.setHtmlOrPlain(sBody);
129
		}
130
	}
131
132
	onShow(template) {
133
134
		this.clearPopup();
135
136
		if (template && template.id)
137
		{
138
			this.id(template.id);
139
			this.name(template.name);
140
			this.body(template.body);
141
142
			if (template.populated)
143
			{
144
				this.editorSetBody(this.body());
145
			}
146
			else
147
			{
148
				this.body.loading(true);
149
				this.body.error(false);
150
151
				Remote.templateGetById((result, data) => {
152
153
					this.body.loading(false);
154
155
					if (StorageResultType.Success === result && data && data.Result &&
156
						'Object/Template' === data.Result['@Object'] && isNormal(data.Result.Body))
157
					{
158
						template.body = data.Result.Body;
159
						template.populated = true;
160
161
						this.body(template.body);
162
						this.body.error(false);
163
					}
164
					else
165
					{
166
						this.body('');
167
						this.body.error(true);
168
					}
169
170
					this.editorSetBody(this.body());
171
172
				}, this.id());
173
			}
174
		}
175
		else
176
		{
177
			this.editorSetBody('');
178
		}
179
	}
180
181
	onShowWithDelay() {
182
		this.name.focus(true);
183
	}
184
}
185
186
module.exports = TemplatePopupView;
187

dev/View/Popup/Identity.js 1 location

@@ 2-197 (lines=196) @@
1
2
import ko from 'ko';
3
4
import {StorageResultType, Notification} from 'Common/Enums';
5
import {bMobileDevice} from 'Common/Globals';
6
import {createCommand, trim, fakeMd5} from 'Common/Utils';
7
import {getNotification} from 'Common/Translator';
8
9
import Remote from 'Remote/User/Ajax';
10
11
import {getApp} from 'Helper/Apps/User';
12
13
import {view, ViewType} from 'Knoin/Knoin';
14
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
15
16
@view({
17
	name: 'View/Popup/Identity',
18
	type: ViewType.Popup,
19
	templateID: 'PopupsIdentity'
20
})
21
class IdentityPopupView extends AbstractViewNext
22
{
23
	constructor() {
24
		super();
25
26
		this.id = '';
27
		this.edit = ko.observable(false);
28
		this.owner = ko.observable(false);
29
30
		this.email = ko.observable('').validateEmail();
31
		this.email.focused = ko.observable(false);
32
		this.name = ko.observable('');
33
		this.name.focused = ko.observable(false);
34
		this.replyTo = ko.observable('').validateSimpleEmail();
35
		this.replyTo.focused = ko.observable(false);
36
		this.bcc = ko.observable('').validateSimpleEmail();
37
		this.bcc.focused = ko.observable(false);
38
39
		this.signature = ko.observable('');
40
		this.signatureInsertBefore = ko.observable(false);
41
42
		this.showBcc = ko.observable(false);
43
		this.showReplyTo = ko.observable(false);
44
45
		this.submitRequest = ko.observable(false);
46
		this.submitError = ko.observable('');
47
48
		this.bcc.subscribe((value) => {
49
			if (false === this.showBcc() && 0 < value.length)
50
			{
51
				this.showBcc(true);
52
			}
53
		});
54
55
		this.replyTo.subscribe((value) => {
56
			if (false === this.showReplyTo() && 0 < value.length)
57
			{
58
				this.showReplyTo(true);
59
			}
60
		});
61
62
		this.addOrEditIdentityCommand = createCommand(() => {
63
64
			if (this.signature && this.signature.__fetchEditorValue)
65
			{
66
				this.signature.__fetchEditorValue();
67
			}
68
69
			if (!this.email.hasError())
70
			{
71
				this.email.hasError('' === trim(this.email()));
72
			}
73
74
			if (this.email.hasError())
75
			{
76
				if (!this.owner())
77
				{
78
					this.email.focused(true);
79
				}
80
81
				return false;
82
			}
83
84
			if (this.replyTo.hasError())
85
			{
86
				this.replyTo.focused(true);
87
				return false;
88
			}
89
90
			if (this.bcc.hasError())
91
			{
92
				this.bcc.focused(true);
93
				return false;
94
			}
95
96
			this.submitRequest(true);
97
98
			Remote.identityUpdate((result, data) => {
99
100
				this.submitRequest(false);
101
				if (StorageResultType.Success === result && data)
102
				{
103
					if (data.Result)
104
					{
105
						getApp().accountsAndIdentities();
106
						this.cancelCommand();
107
					}
108
					else if (data.ErrorCode)
109
					{
110
						this.submitError(getNotification(data.ErrorCode));
111
					}
112
				}
113
				else
114
				{
115
					this.submitError(getNotification(Notification.UnknownError));
116
				}
117
118
			}, this.id, this.email(), this.name(), this.replyTo(), this.bcc(), this.signature(), this.signatureInsertBefore());
119
120
			return true;
121
122
		}, () => !this.submitRequest());
123
	}
124
125
	clearPopup() {
126
		this.id = '';
127
		this.edit(false);
128
		this.owner(false);
129
130
		this.name('');
131
		this.email('');
132
		this.replyTo('');
133
		this.bcc('');
134
		this.signature('');
135
		this.signatureInsertBefore(false);
136
137
		this.email.hasError(false);
138
		this.replyTo.hasError(false);
139
		this.bcc.hasError(false);
140
141
		this.showBcc(false);
142
		this.showReplyTo(false);
143
144
		this.submitRequest(false);
145
		this.submitError('');
146
	}
147
148
	/**
149
	 * @param {?IdentityModel} oIdentity
150
	 */
151
	onShow(identity) {
152
153
		this.clearPopup();
154
155
		if (identity)
156
		{
157
			this.edit(true);
158
159
			this.id = identity.id() || '';
160
			this.name(identity.name());
161
			this.email(identity.email());
162
			this.replyTo(identity.replyTo());
163
			this.bcc(identity.bcc());
164
			this.signature(identity.signature());
165
			this.signatureInsertBefore(identity.signatureInsertBefore());
166
167
			this.owner('' === this.id);
168
		}
169
		else
170
		{
171
			this.id = fakeMd5();
172
		}
173
	}
174
175
	onShowWithDelay() {
176
		if (!this.owner() && !bMobileDevice)
177
		{
178
			this.email.focused(true);
179
		}
180
	}
181
182
	onHideWithDelay() {
183
		this.clearPopup();
184
	}
185
}
186
187
module.exports = IdentityPopupView;
188