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