Project

General

Profile

1
import { Component, OnInit } from '@angular/core';
2
import { ActivatedRoute } from '@angular/router';
3
import { BrokerService } from '../../services/broker.service';
4
import { loadingEvents, noEventsForTopic, noServiceMessage } from '../../domain/shared-messages';
5
import { EventsPage } from '../../domain/typeScriptClasses';
6

    
7
@Component ({
8
  selector: 'app-content-notifications-of-subscription',
9
  templateUrl: 'content-notifications-of-subscription.component.html'
10
})
11

    
12
export class ContentNotificationsOfSubscriptionComponent implements OnInit {
13
  noEvents: string;
14
  errorMessage: string;
15
  loadingMessage: string;
16

    
17
  subId: string;
18
  topic: string;
19
  lastTopicEntry = '';
20
  eventsPage: EventsPage;
21
  currentPage: number;  /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
22

    
23
  selectedItemIndex: number;
24

    
25
  constructor(private route: ActivatedRoute,
26
              private brokerService: BrokerService) {}
27

    
28
  ngOnInit () {
29
    this.subId = this.route.snapshot.paramMap.get('id');
30
    this.currentPage = 0; /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
31
    this.getEventsPage(0);
32
    let body = document.getElementsByTagName('body')[0];
33
    body.classList.remove("top_bar_active");   //remove the class
34
    body.classList.remove("page_heading_active");
35
    body.classList.remove("landing");
36
    body.classList.add("dashboard");
37
  }
38

    
39
  getEventsPage(page: number) {
40
    this.noEvents = '';
41
    this.errorMessage = '';
42
    this. loadingMessage = loadingEvents;
43
    this.brokerService.getNotificationsBySubscriptionId(this.subId, page, 10).subscribe(
44
      events => this.eventsPage = events,
45
      error => {
46
        this.loadingMessage = '';
47
        this.errorMessage = noServiceMessage;
48
        console.log(error);
49
      },
50
      () => {
51
        this.loadingMessage = '';
52
        console.log(this.eventsPage);
53
        if (!this.eventsPage.total) {
54
          this.noEvents = noEventsForTopic;
55
        }
56
        this.getCorrectTopic();
57
        // console.log('Topic: ' + this.topic);
58
        this.lastTopicEntry = this.topic.substring(this.topic.lastIndexOf('|') + 1).toLowerCase();
59
        this.lastTopicEntry = this.replaceAll(this.lastTopicEntry, '_', ' ');
60
        // console.log('Last topic entry: ' + this.lastTopicEntry);
61
      }
62
    );
63
  }
64

    
65
  replaceAll(str, find, replace) {
66
    return str.replace(new RegExp(find, 'g'), replace);
67
  }
68

    
69

    
70
  goToNextPage() {
71
    /* RESTORE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
72
    /*if(this.eventsPage.currPage < this.eventsPage.totalPages) {
73
      console.log(`Get me page ${this.eventsPage.currPage+1}!`);
74
      this.getEventsPage(this.eventsPage.currPage+1);
75
    }*/
76

    
77
    /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
78
    if ( (this.currentPage + 1) < this.eventsPage.totalPages) {
79
      this.currentPage = this.currentPage + 1;
80
      console.log(`Get me page ${this.currentPage}!`);
81
      this.getEventsPage(this.currentPage);
82
    }
83
  }
84

    
85
  goToPreviousPage() {
86
    /* RESTORE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
87
    /*if(this.eventsPage.currPage > 0) {
88
      console.log(`Get me page ${this.eventsPage.currPage-1}!`);
89
      this.getEventsPage(this.eventsPage.currPage-1);
90
    }*/
91

    
92
    /* DELETE WHEN getNotificationsBySubscriptionId IS FIXED AND SENDS CORRECT VALUE FOR CURRENT PAGE */
93
    if (this.currentPage > 0) {
94
      this.currentPage = this.currentPage - 1;
95
      console.log(`Get me page ${this.currentPage}!`);
96
      this.getEventsPage(this.currentPage);
97
    }
98
  }
99

    
100

    
101
  isHighlighted(item: any, itemList: any[]) {
102
    return itemList.some(x => x === item);
103
  }
104

    
105
  getCorrectTopic() {
106
    const temp = this.eventsPage.topic.split('/');
107
    this.topic = temp[0];
108
    for (let i = 1; i < temp.length; i++) {
109
      this.topic += ` | ${temp[i]}`;
110
    }
111
  }
112

    
113
  displayFullResultInfo(i: number) {
114
    if (this.selectedItemIndex === i) {
115
      this.selectedItemIndex = null;
116
    } else {
117
      this.selectedItemIndex = i;
118
    }
119
  }
120
}
(8-8/13)