Project

General

Profile

1
import {Component, OnInit, ViewChild} from '@angular/core';
2
import {NavigationEnd, Router} from '@angular/router';
3
import {AuthenticationService} from './services/authentication.service';
4
import {environment} from '../environments/environment';
5
import {MatomoTracker} from 'ngx-matomo';
6
import {ConfirmationDialogComponent} from './shared/reusablecomponents/confirmation-dialog.component';
7
import {RepositoryService} from './services/repository.service';
8
import {RepositorySnippet} from './domain/typeScriptClasses';
9
import {FormBuilder, FormGroup, FormArray} from '@angular/forms';
10

    
11
@Component({
12
  selector: 'oa-repo-manager',
13
  templateUrl: './app.component.html',
14
  styleUrls: ['./app.component.css'],
15
})
16
export class AppComponent implements OnInit {
17
  reposOfUser: RepositorySnippet[] = [];
18
  modalTitle = 'Terms of Use';
19
  isModalShown: boolean;
20
  modalButton = 'OK';
21

    
22
  agreementForm = this.fb.group({
23
    terms: this.fb.array([])
24
  });
25

    
26
  consentTermsOfUseDate: Date;
27

    
28
  @ViewChild('subscribeToTermsModal')
29
  public subscribeToTermsModal: ConfirmationDialogComponent;
30

    
31
  open: boolean = true;
32

    
33
  constructor(private router: Router, private authService: AuthenticationService, private matomoTracker: MatomoTracker,
34
              private repositoryService: RepositoryService, private fb: FormBuilder) {
35

    
36
    // console.log('21-06-2019. Fixed matomo to log userIds?');
37

    
38
    /*disabling console.log in production*/
39
    if ( environment.production === true ) {
40
      console.log = function () {};
41
    }
42

    
43
    // URL of the SPA to redirect the user to after login
44
    // this.authService.redirectUrl = "/dashboard";
45

    
46
    if (window.location.pathname.includes('/compatibility/browseHistory/')) {
47
      this.authService.redirectUrl = window.location.pathname;
48
      console.log('redirectUrl', this.authService.redirectUrl);
49
    }
50

    
51
    this.authService.tryLogin();
52
  }
53

    
54
  ngOnInit() {
55
    this.router.events.subscribe((evt) => {
56
      if (!(evt instanceof NavigationEnd)) {
57
        return;
58
      }
59
      if (this.authService.isLoggedIn_) {
60
        this.matomoTracker.setUserId(this.authService.getUserEmail());
61
      }
62
      window.scrollTo(0, 0);
63
    });
64

    
65
    this.authService.isLoggedIn.subscribe(
66
      logged => {if (logged) {this.getReposOfUser(); }},
67
      error => {console.log(error); }
68
    );
69

    
70
  }
71

    
72
  getReposOfUser(): void {
73
      this.repositoryService.getRepositoriesSnippetsOfUser().subscribe(
74
        repos => {
75
          this.reposOfUser = repos;
76
        },
77
        error => {
78
          console.log(error);
79
        },
80
        () => {
81
          // console.log(this.reposOfUser);
82
          if (this.agreementForm.get('terms').value.length === 0) {
83
            this.reposOfUser.forEach(repo => {
84
              if (repo.consentTermsOfUse === null || repo.fullTextDownload === null) {
85
                this.addTerm(repo.officialname, repo.id, repo.consentTermsOfUse);
86
                this.isModalShown = true;
87
              }
88
            });
89
          }
90
        }
91
      );
92
  }
93

    
94
  updateTerms() {
95
    this.repositoryService.updateRepositoriesTerms(this.agreementForm.value.terms).subscribe(
96
      res => {},
97
      err => {console.log(err); }
98
    );
99
  }
100

    
101
  addTerm(name: string, id: string, consent: boolean) {
102
    this.terms.push(this.newTerm(name, id, consent));
103
  }
104

    
105
  newTerm(name: string, id: string, consent: boolean): FormGroup {
106
    return this.fb.group({
107
      id: [id],
108
      name: [name],
109
      // accept: [(consent ? consent : true)]
110
      consentTermsOfUse: false,
111
      fullTextDownload: false
112
    });
113
  }
114

    
115
  get terms() {
116
    return this.agreementForm.get('terms') as FormArray;
117
  }
118

    
119
  isLandingRoute() {
120
    // console.log('Is home route? Route is: ' + this.router.url);
121
    return (this.router.url === '/') || (this.router.url === '/home') || (this.router.url === '/about');
122
  }
123

    
124
  public toggleOpen(event: MouseEvent) {
125
    event.preventDefault();
126
    this.open = !this.open;
127
  }
128

    
129
  // ngAfterContentInit() {
130
  //
131
  //   // this.loadScript('assets/js/common.js');
132
  //   // this.loadScript('assets/js/uikit_custom.js');
133
  //   // this.loadScript('assets/js/altair_admin_common.js');
134
  //   this.loadScript('assets/js/altair_admin_common.min.js');
135
  //
136
  //   // setTimeout( () => {
137
  //   //   // this.loadScript('assets/js/common.js');
138
  //   //   // this.loadScript('assets/js/uikit_custom.js');
139
  //   //   this.loadScript('assets/js/altair_admin_common.min.js');
140
  //   // }, 2000);
141
  //
142
  //   // $.getScript('assets/js/altair_admin_common.min.js');
143
  //
144
  //
145
  //
146
  //   // // Load the script
147
  //   // // var self = this;
148
  //   //
149
  //   // var script = <HTMLScriptElement>document.createElement("SCRIPT");
150
  //   // script.src = 'assets/js/altair_admin_common.min.js';
151
  //   // script.type = 'text/javascript';
152
  //   // // self.script = <HTMLScriptElement>document.createElement("SCRIPT");
153
  //   // // self.script.src = '../Content/js/settings.js';
154
  //   // // self.script.type = 'text/javascript';
155
  //   // document.getElementsByTagName("head")[0].appendChild(script);
156
  // }
157
  //
158
  // public loadScript(url) {
159
  //   console.log('preparing to load...')
160
  //   let node = document.createElement('script');
161
  //   node.src = url;
162
  //   node.type = 'text/javascript';
163
  //   document.getElementsByTagName('head')[0].appendChild(node);
164
  // }
165

    
166
}
(5-5/6)