Project

General

Profile

1
import { Component, OnInit, Input } from '@angular/core';
2
import { ActivatedRoute, Router } from "@angular/router";
3
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
4
import { Page } from "../../domain/page";
5
import { HelpContentService } from "../../services/help-content.service";
6
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
7

    
8
import {Session} from '../../openaireLibrary/login/utils/helper.class';
9
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
10

    
11
@Component({
12
    selector: 'page-content-form',
13
    templateUrl: './page-help-content-form.component.html',
14
})
15

    
16
export class PageContentFormComponent implements OnInit{
17

    
18
    @Input('group')
19
    myForm: FormGroup;
20
    @Input('communityPid')
21
    communityPid: string;
22
    @Input('pageId')
23
    pageId: string;
24
    @Input('page')
25
    page: Page;
26
    placements = {"top": false, "bottom": false, "left": false, "right": false}
27

    
28
    private availablePages : Page[] = [];
29
    //private errorMessage: string;
30

    
31
    private ckeditorContent : string;
32
    public properties:EnvProperties = null;
33

    
34
    public showLoading: boolean = true;
35
    public errorMessage: string = '';
36
    @Input() updateErrorMessage: string = '';
37

    
38
    constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService){}
39

    
40
    ngOnInit() {
41
        this.myForm = this.form;
42
        this.route.data
43
          .subscribe((data: { envSpecific: EnvProperties }) => {
44
             this.properties = data.envSpecific;
45

    
46
             if(!Session.isLoggedIn()){
47
               this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
48
             } else {
49
                 if(!this.pageId) {
50
                     this.myForm.valueChanges.subscribe(value => {
51
                         let pageId = value.page;
52
                         this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(page => {
53
                            this.setPlacements(page);
54
                         });
55
                     });
56
                 }
57
                 this._helpContentService.getCommunityPagesWithPositions(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
58
                   pages => {
59
                       this.availablePages = pages;
60
                       this.showLoading = false;
61
                   },
62
                   error => this.handleError('System error retrieving pages', error));
63
             }
64
        });
65
    }
66

    
67
    public setPlacements(page: Page) {
68
      this.placements.top = page.top;
69
      this.placements.bottom = page.bottom;
70
      this.placements.left = page.left;
71
      this.placements.right = page.right;
72
    }
73

    
74
    public get form() {
75
        return this._fb.group({
76
            page : [this.pageId, Validators.required],
77
            portal : this.communityPid,
78
            placement : ['', Validators.required],
79
            content : ['', Validators.required],
80
            order : [1, Validators.required],
81
            isActive : true,
82
            //isPriorTo : false,
83
            _id : '',
84
        });
85
    }
86

    
87
    public reset() {
88
        this.myForm.patchValue({
89
            page : '',
90
            portal : this.communityPid,
91
            placement : '',
92
            content : [''],
93
            order : 1,
94
            isActive : true,
95
            //isPriorTo : false,
96
            _id : ''
97
        });
98
        this.myForm.markAsPristine();
99
    }
100

    
101
    handleError(message: string, error) {
102
        this.errorMessage = message;
103
        console.log('Server responded: ' + error);
104

    
105
        this.showLoading = false;
106
    }
107
}
(10-10/15)