Project

General

Profile

« Previous | Next » 

Revision 56889

View differences:

adminPg-metrics.component.ts
1
import { Component, OnInit, ViewChild } from '@angular/core';
1
import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 2
import { PiwikService } from '../../services/piwik.service';
3 3
import { PiwikInfo } from '../../domain/typeScriptClasses';
4 4
import {
......
9 9
  validatePiwikSiteSuccess
10 10
} from '../../domain/shared-messages';
11 11
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
12
import {URLParameter} from '../../domain/url-parameter';
13
import {FormBuilder, FormGroup} from '@angular/forms';
14
import {RepositoryService} from '../../services/repository.service';
15
import {ActivatedRoute, Router} from '@angular/router';
16
import {PiwikInfoPage} from '../../domain/page-content';
17
import {environment} from '../../../environments/environment';
18
import {st} from '@angular/core/src/render3';
12 19

  
13 20
@Component ({
14 21
  selector: 'app-admin-metrics',
......
16 23
})
17 24

  
18 25
export class AdminPgMetricsComponent implements OnInit {
19
  piwiks: PiwikInfo[] = [];
26
  piwiks: PiwikInfoPage;
27
  urlParams: URLParameter[] = [];
20 28
  errorMessage: string;
21 29
  successMessage: string;
22 30
  loadingMessage: string;
......
25 33
  modalButton = 'Yes, validate';
26 34
  isModalShown: boolean;
27 35

  
36
  formPrepare = {
37
    searchField: '',
38
    orderField: 'REPOSITORY_NAME',
39
    order: 'ASC',
40
    page: '0',
41
    quantity: '25',
42
    from: '0'
43
  };
44

  
45
  dataForm: FormGroup;
46

  
28 47
  @ViewChild('confirmApprovalModal')
29 48
  public confirmApprovalModal: ConfirmationDialogComponent;
49
  private pageTotal: number;
50
  private piwiksTotal: number;
51
  private pages = [];
52
  private offset = 2;
30 53

  
31
  constructor(private piwikService: PiwikService) {}
54
  constructor(private piwikService: PiwikService,
55
              private fb: FormBuilder,
56
              private route: ActivatedRoute,
57
              private router: Router) {}
32 58

  
33 59
  ngOnInit() {
34
    this.getPiwiks();
60
    this.dataForm = this.fb.group(this.formPrepare);
61
    this.urlParams = [];
62
    this.route.queryParams
63
      .subscribe(params => {
64
          for (const i in params) {
65
            this.dataForm.get(i).setValue(params[i]);
66
          }
67
          for (let i in this.dataForm.controls) {
68
            if (this.dataForm.get(i).value) {
69
              this.urlParams.push({key: i, value: [this.dataForm.get(i).value]});
70
            }
71
          }
72
          this.handleChange();
73
        },
74
        error => this.errorMessage = <any>error
75
      );
76

  
35 77
    this.isModalShown = false;
36 78
  }
37 79

  
80
  downloadCSV() {
81
    const url = environment.API_ENDPOINT;
82
    let csvUrlParams = '/piwik/getPiwikSitesForRepos/csv?';
83
    for (let i in this.dataForm.controls) {
84
      if (this.dataForm.get(i).value !== '') {
85
        csvUrlParams = csvUrlParams.concat(i, '=', this.dataForm.get(i).value, '&');
86
      }
87
    }
88
    csvUrlParams = csvUrlParams.split('&page=')[0];
89
    window.open(url + csvUrlParams, '_blank');
90
  }
38 91

  
39 92
  getPiwiks() {
40 93
    this.loadingMessage = loadingReposMessage;
41
    this.piwikService.getPiwikSitesForRepos()
42
      .subscribe (
43
        piwiks => this.piwiks = piwiks.sort( function(a, b) {
44
          if (a.repositoryName < b.repositoryName) {
45
            return -1;
46
          } else if (a.repositoryName > b.repositoryName) {
47
            return 1;
48
          } else {
49
            return 0;
50
          }
51
        } ),
94
    this.piwikService.getPiwikSitesForRepos(this.urlParams)
95
      .subscribe(
96
        piwiks => {
97
          this.piwiks = piwiks;
98
          this.getPages();
99
        },
52 100
        error => {
53 101
          console.log(error);
54 102
          this.loadingMessage = '';
......
94 142
    );
95 143
  }
96 144

  
145
  handleChange() {
146
    this.urlParams = [];
147
    const map: { [name: string]: string; } = {};
148
    for (let i in this.dataForm.controls) {
149
      if (this.dataForm.get(i).value !== '') {
150
        this.urlParams.push({key: i, value: [this.dataForm.get(i).value]});
151
        map[i] = this.dataForm.get(i).value;
152
      }
153
    }
154

  
155
    this.router.navigate([`/admin/metrics`], {queryParams: map});
156
    this.getPiwiks();
157
  }
158

  
159
  handleChangeAndResetPage() {
160
    this.dataForm.get('page').setValue(0);
161
    this.dataForm.get('from').setValue(0);
162
    this.handleChange();
163
  }
164

  
165
  getPages() {
166
    let addToEndCounter = 0;
167
    let addToStartCounter = 0;
168
    this.pages = [];
169
    this.pageTotal = Math.ceil(this.piwiks.total / (this.dataForm.get('quantity').value));
170
    for ( let i = (+this.dataForm.get('page').value - this.offset); i < (+this.dataForm.get('page').value + 1 + this.offset); ++i ) {
171
      if ( i < 0 ) { addToEndCounter++; }
172
      if ( i >= this.pageTotal ) { addToStartCounter++; }
173
      if ((i >= 0) && (i < this.pageTotal)) {
174
        this.pages.push(i);
175
      }
176
    }
177
    for ( let i = 0; i < addToEndCounter; ++i ) {
178
      if (this.pages.length < this.pageTotal) {
179
        this.pages.push(this.pages.length);
180
      }
181
    }
182
    for ( let i = 0; i < addToStartCounter; ++i ) {
183
      if (this.pages[0] > 0) {
184
        this.pages.unshift(this.pages[0] - 1 );
185
      }
186
    }
187
  }
188

  
189
  selectPage(page) {
190
    this.dataForm.get('page').setValue(page);
191
    this.dataForm.get('from').setValue(((+this.dataForm.get('page').value) * (+this.dataForm.get('quantity').value)));
192
    this.handleChange();
193
  }
194

  
195
  previousPage() {
196
    if (this.dataForm.get('page').value > 0) {
197
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value - 1);
198
      this.dataForm.get('from').setValue(+this.dataForm.get('from').value - +this.dataForm.get('quantity').value);
199
      this.handleChange();
200
    }
201
  }
202

  
203
  nextPage() {
204
    // if ((this.dataForm.get('searchField').value) !== '') { this.piwiksTotal = this.piwiks.to; } else { this.piwiksTotal = this.piwiks.total; }
205
    this.pageTotal = Math.ceil(this.piwiks.total / (this.dataForm.get('quantity').value)) - 1;
206
    if (this.dataForm.get('page').value < this.pageTotal) {
207
      this.dataForm.get('page').setValue(+this.dataForm.get('page').value + 1);
208
      this.dataForm.get('from').setValue(+this.dataForm.get('from').value + +this.dataForm.get('quantity').value);
209
      this.handleChange();
210
    }
211
  }
212

  
97 213
}

Also available in: Unified diff