Project

General

Profile

1
/**
2
 * Created by stefania on 4/26/17.
3
 */
4
import { Component, ViewChild, OnInit } from '@angular/core';
5
import { Topic, CheckTopic } from "../../domain/topic";
6
import { FAQService } from "../../services/faq.service";
7
import { FormGroup } from "@angular/forms";
8
import { ModalFormComponent } from "../modal-form.component";
9
import { TopicsFormComponent } from "./topics-form.component";
10
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
11

    
12
@Component({
13
    selector: 'topics',
14
    templateUrl: './topics.component.html',
15
})
16

    
17
export class TopicsComponent implements OnInit {
18

    
19
    // @ViewChild(ModalFormComponent)
20
    @ViewChild('saveModal')
21
    public modal:ModalFormComponent;
22

    
23
    @ViewChild('updateModal')
24
    public updateModal:ModalFormComponent;
25

    
26
    @ViewChild('deleteConfirmationModal')
27
    public deleteConfirmationModal : DeleteConfirmationDialogComponent;
28

    
29
    @ViewChild(TopicsFormComponent)
30
    public formComponent : TopicsFormComponent;
31

    
32
    public topicsCheckboxes : CheckTopic[] = [];
33

    
34
    public topics : Topic[] = [];
35

    
36
    public errorMessage: string = null;
37

    
38
    public successMessage: string = null;
39

    
40
    public formGroup : FormGroup;
41

    
42
    private searchText : RegExp = new RegExp('');
43

    
44
    ngOnInit() {
45
        this.getTopics();
46
        this.formGroup = this.formComponent.form;
47
    }
48

    
49
    constructor(private _faqService: FAQService) {}
50

    
51
    getTopics() {
52
        let self = this;
53
        this._faqService.getTopics().subscribe(
54
            topics => {
55
                self.topics = topics;
56
                topics.forEach(_ => {
57
                    self.topicsCheckboxes.push(<CheckTopic>{topic : _, checked : false});
58
                });
59
            },
60
            error => this.handleError('System error retrieving topics topics', error));
61
    }
62

    
63
    public showModal():void {
64
        this.modal.showModal();
65
    }
66

    
67
    public toggleCheckBoxes(event) {
68
        this.topicsCheckboxes.forEach(_ => _.checked = event.target.checked);
69
    }
70

    
71
    public applyCheck(flag : boolean) {
72
        this.topicsCheckboxes.forEach(_ => _.checked = flag);
73
    }
74

    
75
    public getSelectedTopics() : string[] {
76
        return this.topicsCheckboxes.filter(topic => topic.checked == true).map(checkedTopic => checkedTopic.topic).map(res => res._id);
77
    }
78

    
79
    private deleteTopicsFromArray(ids : string[]) : void {
80
        this.successMessage = `Successfully deleted Topic(s)`;
81
        for(let id of ids) {
82
            let i = this.topicsCheckboxes.findIndex(_ => _.topic._id == id);
83
            this.topicsCheckboxes.splice(i, 1);
84
        }
85
    }
86

    
87
    public confirmDeleteTopic(id : string) {
88
        this.deleteConfirmationModal.ids = [id];
89
        this.deleteConfirmationModal.showModal();
90
    }
91

    
92
    public confirmDeleteSelectedTopics() {
93
        this.deleteConfirmationModal.ids = this.getSelectedTopics();
94
        this.deleteConfirmationModal.showModal();
95
    }
96

    
97
    public confirmedDeleteTopic(ids : string[]) {
98
        this._faqService.deleteTopics(ids).subscribe(
99
            _ => this.deleteTopicsFromArray(ids),
100
            error => this.handleError('System error deleting the selected topics', error)
101
        );
102
    }
103

    
104
    public sort(type : string) {
105
        if(type=='weight') {
106
            this.topicsCheckboxes.sort(function(a, b) {
107
                return a.topic.weight - b.topic.weight;
108
            });
109
        } else if (type == 'hits') {
110

    
111
        }
112
    }
113

    
114
    public editTopic(i : number) {
115
        let topic : Topic = this.topicsCheckboxes[i].topic;
116
        this.formGroup.patchValue(topic);
117
        this.updateModal.showModal();
118
    }
119

    
120
    public toggleTopic(order : string, ids : string[]) {
121
        this._faqService.orderTopic(ids,order).subscribe(
122
            ret => {
123
                for(let id of ret) {
124
                    let i = this.topicsCheckboxes.findIndex(_ => _.topic._id == id);
125
                    this.topicsCheckboxes[i].topic.questionOrder=order;
126
                }
127
            },
128
            error => this.handleError('System error ordering topics', <any>error)
129
        );
130
        this.applyCheck(false);
131
    }
132

    
133
    public topicSavedSuccessfully(topic: Topic) {
134
        this.topicsCheckboxes.push(<CheckTopic>{topic : topic, checked : false});
135
        this.successMessage = `Topic [${topic.name}] saved successfully`;
136
        this.applyCheck(false);
137
    }
138

    
139
    public topicUpdatedSuccessfully(topic : Topic) {
140
        this.topicsCheckboxes.find(checkItem => checkItem.topic._id==topic._id).topic = topic;
141
        this.successMessage = `Topic [${topic.name}] updated successfully`;
142
        this.applyCheck(false);
143
    }
144

    
145
    public filterBySearch(text : string) {
146
        this.searchText = new RegExp(text,'i');
147
        this.applyFilter();
148
    }
149

    
150
    public applyFilter() {
151
        this.topicsCheckboxes = [];
152
        this.topics.filter(item => this.filterQuestion(item)).forEach(
153
            _ => this.topicsCheckboxes.push(<CheckTopic>{topic: _, checked: false})
154
        );
155
    }
156

    
157
    public filterQuestion(topic : Topic) : boolean {
158
        let textFlag = this.searchText.toString() == '' || (topic.name + ' ' +topic.description).match(this.searchText) != null;
159
        return textFlag;
160
    }
161

    
162
    handleError(message: string, error) {
163
        if(error == null) {
164
            this.formComponent.reset();
165
        } else {
166
            this.errorMessage = message + ' (Server responded: ' + error + ')';
167
        }
168
    }
169
}
(8-8/8)