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

dev/View/Popup/Template.js   A

Complexity

Total Complexity 32
Complexity/F 2.29

Size

Lines of Code 198
Function Count 14

Duplication

Duplicated Lines 198
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 32
nc 20
mnd 3
bc 29
fnc 14
dl 198
loc 198
rs 9.6
bpm 2.0714
cpm 2.2857
noi 0
c 2
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
2 View Code Duplication
import ko from 'ko';
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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