1
|
import {Component, OnInit} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
4
|
import {Page} from '../../utils/entities/adminTool/page';
|
5
|
import {HelpContentService} from '../../services/help-content.service';
|
6
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
7
|
import {properties} from '../../../../environments/environment';
|
8
|
import {Subscriber, Subscription, zip} from 'rxjs';
|
9
|
import {HelperFunctions} from '../../utils/HelperFunctions.class';
|
10
|
import {PageHelpContent} from '../../utils/entities/adminTool/page-help-content';
|
11
|
|
12
|
declare var UIkit;
|
13
|
|
14
|
@Component({
|
15
|
selector: 'page-content-form',
|
16
|
templateUrl: './page-help-content-form.component.html',
|
17
|
})
|
18
|
export class PageContentFormComponent implements OnInit {
|
19
|
|
20
|
myForm: FormGroup;
|
21
|
portal: string;
|
22
|
pageId: string;
|
23
|
pageContentId: string;
|
24
|
page: Page;
|
25
|
placementsOptions = [];
|
26
|
orderOptions = [];
|
27
|
public properties: EnvProperties = properties;
|
28
|
public showLoading: boolean = true;
|
29
|
private subs: Subscription[] = [];
|
30
|
public pageHelpContent: PageHelpContent;
|
31
|
|
32
|
constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService) {
|
33
|
}
|
34
|
|
35
|
ngOnInit() {
|
36
|
this.subs.push(this.route.params.subscribe(params => {
|
37
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
38
|
this.subs.push(this.route.queryParams.subscribe(params => {
|
39
|
HelperFunctions.scroll();
|
40
|
this.pageId = params['pageId'];
|
41
|
this.myForm = this.form;
|
42
|
this.pageContentId = params['pageContentId'];
|
43
|
if (!this.pageId) {
|
44
|
this._router.navigate(['../'], {relativeTo: this.route});
|
45
|
}
|
46
|
this.getInfo(this.pageId);
|
47
|
}));
|
48
|
}));
|
49
|
}
|
50
|
|
51
|
ngOnDestroy() {
|
52
|
this.subs.forEach(value => {
|
53
|
if (value instanceof Subscriber) {
|
54
|
value.unsubscribe();
|
55
|
}
|
56
|
});
|
57
|
}
|
58
|
|
59
|
getInfo(pageId: string) {
|
60
|
this.showLoading = true;
|
61
|
let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal), this._helpContentService.getCommunityPageHelpContents(this.portal, this.properties.adminToolsAPIURL, pageId));
|
62
|
this.subs.push(obs.subscribe(
|
63
|
results => {
|
64
|
this.page = results[0];
|
65
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
66
|
this._router.navigate(['../'], {relativeTo: this.route});
|
67
|
}
|
68
|
let countPageContents = results[1] ? results[1].length : 0;
|
69
|
console.log(results[1]);
|
70
|
for (let content of (results[1] as Array<PageHelpContent>)) {
|
71
|
// if(content.page['_id'] == pageId){
|
72
|
// countPageContents++;
|
73
|
if (this.pageContentId && this.pageContentId == content._id) {
|
74
|
this.pageHelpContent = content;
|
75
|
// this.pageHelpContent.page = pageId;
|
76
|
// this.pageHelpContent.portal = this.communityPid;
|
77
|
}
|
78
|
// }
|
79
|
}
|
80
|
this.setOptions(this.page, countPageContents + (this.pageHelpContent ? 0 : 1));
|
81
|
if (!this.pageContentId) {
|
82
|
this.myForm = this.form;
|
83
|
this.initFormWithSelectOptions();
|
84
|
} else {
|
85
|
this.updateForm(this.pageHelpContent);
|
86
|
}
|
87
|
this.showLoading = false;
|
88
|
},
|
89
|
error => this.handleError('System error retrieving page with id: ' + pageId, error)
|
90
|
));
|
91
|
|
92
|
}
|
93
|
|
94
|
/*private getPage(pageId: string) {
|
95
|
this.subs.push(this._helpContentService.getPageByPortal(pageId,this.properties.adminToolsAPIURL, this.communityPid).subscribe(
|
96
|
page => {
|
97
|
if(this.properties.adminToolsPortalType != page.portalType) {
|
98
|
this._router.navigate(['../'], {relativeTo: this.route});
|
99
|
} else {
|
100
|
this.page = page;
|
101
|
this.getPageContents(pageId);
|
102
|
}
|
103
|
},
|
104
|
error => this.handleError('System error retrieving page with id: '+pageId, error)
|
105
|
));
|
106
|
}
|
107
|
private getPageContents(pageId: string) {
|
108
|
this.subs.push(this._helpContentService.getCommunityPageHelpContents(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
|
109
|
pageHelpContents => {
|
110
|
let countPageContents = 1;
|
111
|
for (let content of (pageHelpContents as Array<PageHelpContent>)) {
|
112
|
if(content.page['_id'] == pageId){
|
113
|
countPageContents++;
|
114
|
}
|
115
|
}
|
116
|
this.setOptions(this.page, countPageContents);
|
117
|
if(!this.pageContentId) {
|
118
|
this.showLoading = false;
|
119
|
this.initFormWithSelectOptions();
|
120
|
}
|
121
|
},
|
122
|
error => this.handleError('System error retrieving page contents with id: ', error)
|
123
|
));
|
124
|
}
|
125
|
private getPageHelpContent(pageContentId: string) {
|
126
|
this.showLoading = true;
|
127
|
|
128
|
this.subs.push(this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
|
129
|
pageHelpContent => {
|
130
|
this.updateForm(pageHelpContent);
|
131
|
this.showLoading = false;
|
132
|
},
|
133
|
error => this.handleError('System error retrieving page help content', error)));
|
134
|
}*/
|
135
|
private updateForm(pageHelpContent: PageHelpContent) {
|
136
|
this.pageHelpContent = pageHelpContent;
|
137
|
this.myForm = this.form;
|
138
|
if (this.pageHelpContent) {
|
139
|
this.myForm.patchValue((pageHelpContent));
|
140
|
} else {
|
141
|
this.initFormWithSelectOptions();
|
142
|
}
|
143
|
if (this.orderOptions.length == 1) {
|
144
|
this.myForm.get('order').disable()
|
145
|
}
|
146
|
if (this.placementsOptions.length == 1) {
|
147
|
this.myForm.get('placement').disable()
|
148
|
}
|
149
|
this.myForm.markAsPristine();
|
150
|
|
151
|
}
|
152
|
|
153
|
initFormWithSelectOptions() {
|
154
|
if (this.placementsOptions.length == 1) {
|
155
|
this.myForm.get("placement").setValue(this.placementsOptions[0].value);
|
156
|
}
|
157
|
this.myForm.get("order").setValue(this.orderOptions[this.orderOptions.length - 1].value);
|
158
|
if (this.orderOptions.length == 1) {
|
159
|
this.myForm.get('order').disable()
|
160
|
}
|
161
|
if (this.placementsOptions.length == 1) {
|
162
|
this.myForm.get('placement').disable()
|
163
|
}
|
164
|
}
|
165
|
|
166
|
public setOptions(page: Page, countContents: number) {
|
167
|
this.placementsOptions = [];
|
168
|
if (page.top) {
|
169
|
this.placementsOptions.push({label: "top", value: "top"});
|
170
|
}
|
171
|
if (page.bottom) {
|
172
|
this.placementsOptions.push({label: "bottom", value: "bottom"});
|
173
|
}
|
174
|
if (page.left) {
|
175
|
this.placementsOptions.push({label: "left", value: "left"});
|
176
|
}
|
177
|
this.orderOptions = [];
|
178
|
for (let i = 1; i < countContents + 1; i++) {
|
179
|
this.orderOptions.push({label: "" + i, value: i});
|
180
|
}
|
181
|
}
|
182
|
|
183
|
public get form() {
|
184
|
return this._fb.group({
|
185
|
page: [this.pageId, Validators.required],
|
186
|
portal: this.portal,
|
187
|
placement: ['', Validators.required],
|
188
|
content: ['', Validators.required],
|
189
|
order: [1, Validators.required],
|
190
|
isActive: true,
|
191
|
//isPriorTo : false,
|
192
|
_id: '',
|
193
|
});
|
194
|
}
|
195
|
|
196
|
public reset() {
|
197
|
this.myForm.patchValue({
|
198
|
page: '',
|
199
|
portal: this.portal,
|
200
|
placement: '',
|
201
|
content: [''],
|
202
|
order: 1,
|
203
|
isActive: true,
|
204
|
//isPriorTo : false,
|
205
|
_id: ''
|
206
|
});
|
207
|
this.myForm.markAsPristine();
|
208
|
}
|
209
|
|
210
|
handleError(message: string, error) {
|
211
|
UIkit.notification(message, {
|
212
|
status: 'danger',
|
213
|
timeout: 6000,
|
214
|
pos: 'bottom-right'
|
215
|
});
|
216
|
console.error('Server responded: ' + error);
|
217
|
|
218
|
this.showLoading = false;
|
219
|
}
|
220
|
|
221
|
public saveCustom() {
|
222
|
if (this.myForm.valid) {
|
223
|
this.showLoading = true;
|
224
|
this.myForm.get('order').enable();
|
225
|
this.myForm.get('placement').enable();
|
226
|
let pageHelpContent: PageHelpContent = this.myForm.value;
|
227
|
this.subs.push(this._helpContentService.savePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL, this.portal).subscribe(
|
228
|
_ => {
|
229
|
UIkit.notification('Page content has been <b>successfully ' + (this.pageContentId ? 'updated' : 'created') + '</b>', {
|
230
|
status: 'success',
|
231
|
timeout: 6000,
|
232
|
pos: 'bottom-right'
|
233
|
});
|
234
|
this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
|
235
|
this.showLoading = false;
|
236
|
},
|
237
|
err => this.handleUpdateError('System error saving page content', err)
|
238
|
));
|
239
|
} else {
|
240
|
this.showLoading = false;
|
241
|
}
|
242
|
}
|
243
|
|
244
|
public cancelCustom() {
|
245
|
this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
|
246
|
}
|
247
|
|
248
|
public resetCustom() {
|
249
|
this.showLoading = true;
|
250
|
this.updateForm(this.pageHelpContent);
|
251
|
this.showLoading = false;
|
252
|
}
|
253
|
|
254
|
handleUpdateError(message: string, error) {
|
255
|
UIkit.notification(message, {
|
256
|
status: 'danger',
|
257
|
timeout: 6000,
|
258
|
pos: 'bottom-right'
|
259
|
});
|
260
|
console.error('Server responded: ' + error);
|
261
|
|
262
|
this.showLoading = false;
|
263
|
}
|
264
|
|
265
|
changeStatus() {
|
266
|
this.myForm.get('isActive').setValue(!this.myForm.get('isActive').value);
|
267
|
if (this.pageHelpContent && this.myForm.get('isActive').value != this.pageHelpContent.isActive || !this.pageHelpContent && !this.myForm.get('isActive').value) {
|
268
|
this.myForm.get('isActive').markAsDirty();
|
269
|
} else {
|
270
|
this.myForm.get('isActive').markAsPristine()
|
271
|
}
|
272
|
}
|
273
|
|
274
|
contentChanged() {
|
275
|
if (this.pageHelpContent && this.myForm.get('content').value != this.pageHelpContent.content || !this.pageHelpContent && this.myForm.get('content').value != '') {
|
276
|
this.myForm.get('content').markAsDirty();
|
277
|
} else {
|
278
|
this.myForm.get('content').markAsPristine()
|
279
|
}
|
280
|
}
|
281
|
}
|