2018-12-03 12:33:03 +01:00
|
|
|
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
|
2018-10-30 16:31:16 +01:00
|
|
|
import { FormGroup } from "@angular/forms";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
|
|
//import { Location } from "@angular/common";
|
|
|
|
import { HelpContentService } from "../../services/help-content.service";
|
|
|
|
import { HtmlPageContentService } from "./html-page-content.service";
|
|
|
|
import { HtmlPageContent, CheckHtmlPageContent } from "../../domain/html-page-content";
|
|
|
|
import { HtmlPageContentFormComponent } from "./html-page-content-form.component";
|
|
|
|
|
|
|
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
import { Subscription } from "rxjs/Subscription";
|
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'edit-htmlpage-content',
|
|
|
|
templateUrl: './edit-htmlpage-content.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class EditHtmlPageContentComponent implements OnInit {
|
|
|
|
@ViewChild(HtmlPageContentFormComponent)
|
|
|
|
public formComponent : HtmlPageContentFormComponent;
|
|
|
|
|
|
|
|
private communityPid: string;
|
|
|
|
private pageId: string;
|
|
|
|
private pageName: string = "";
|
|
|
|
public mode: string = "edit";
|
|
|
|
|
|
|
|
private sub: Subscription;
|
|
|
|
|
|
|
|
private htmlPageContent: HtmlPageContent;
|
|
|
|
|
|
|
|
public properties:EnvProperties = null;
|
|
|
|
|
|
|
|
public showLoading: boolean = true;
|
|
|
|
public errorMessage: string = '';
|
|
|
|
public updateErrorMessage: string = '';
|
|
|
|
|
|
|
|
constructor(
|
2018-12-03 12:33:03 +01:00
|
|
|
private element: ElementRef,
|
2018-10-30 16:31:16 +01:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
//private location: Location,
|
|
|
|
private _helpContentService: HelpContentService,
|
|
|
|
private _htmlContentService: HtmlPageContentService) {
|
|
|
|
this.router.routeReuseStrategy.shouldReuseRoute = function(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.sub = this.route.queryParams.subscribe(params => {
|
2018-12-03 12:33:03 +01:00
|
|
|
this.scroll();
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
this.communityPid = params['communityId'];
|
|
|
|
this.pageId = params['pageId'];
|
|
|
|
|
|
|
|
this.getPage(this.communityPid, this.pageId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
this.errorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
this.updateErrorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getPage(communityId: string, pageId: string) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
console.info(this.router.url);
|
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this.router.url} });
|
|
|
|
} else {
|
|
|
|
this._helpContentService.getPage(pageId, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
page => {
|
|
|
|
this.pageName = page.name;
|
|
|
|
this.getHtmlPageContent(communityId, page.route);
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page', error));
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private getHtmlPageContent(communityId: string, pageRoute: string) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
console.info(this.router.url);
|
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this.router.url} });
|
|
|
|
} else {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.errorMessage = "";
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
|
|
|
|
this._htmlContentService.getHtmlPageContent(communityId, pageRoute, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
htmlPageContent => {
|
|
|
|
if(htmlPageContent.length > 0) {
|
|
|
|
this.updateForm(htmlPageContent[0]);
|
|
|
|
}
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page help content', error));
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private updateForm(htmlPageContent : HtmlPageContent) {
|
|
|
|
this.htmlPageContent = htmlPageContent;
|
|
|
|
this.formComponent.myForm.patchValue((htmlPageContent));
|
|
|
|
}
|
|
|
|
|
|
|
|
private saveCustom() {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
console.info(this.router.url);
|
|
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this.router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
if(this.formComponent.myForm.valid) {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.mode = "";
|
|
|
|
|
|
|
|
let htmlPageContent : HtmlPageContent = this.formComponent.myForm.value;
|
|
|
|
console.info(htmlPageContent.community);
|
|
|
|
this._htmlContentService.updateHtmlPageContent(htmlPageContent, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
_ => {
|
|
|
|
this.mode = "preview";
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.mode = "edit";
|
|
|
|
this.handleUpdateError('System error updating page content', err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.mode = "edit";
|
|
|
|
this.errorMessage = "Please fill all required fields";
|
|
|
|
}
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private cancelCustom() {
|
|
|
|
this.errorMessage = "";
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.router.navigate(['/pages'], { queryParams: { "type": "html", "communityId": this.communityPid}});
|
|
|
|
//this.location.back();
|
|
|
|
//this.router.navigated = false;
|
|
|
|
//this.router.navigate( ['/htmlPageContent/edit'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId } } );
|
|
|
|
}
|
2018-12-03 12:33:03 +01:00
|
|
|
|
|
|
|
public scroll() {
|
|
|
|
console.info("scroll into view");
|
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
this.element.nativeElement.scrollIntoView();
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|