Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
5
import { FormGroup } from "@angular/forms";
6
import { ActivatedRoute, Router } from "@angular/router";
7
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
8
import { HelpContentService } from "../../services/help-content.service";
9
import { PageHelpContent, CheckPageHelpContent, PageHelpContentFilterOptions } from "../../domain/page-help-content";
10
import { Page } from "../../domain/page";
11
import { Community } from "../../domain/community";
12
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
13
import {SafeHtmlPipe} from '../../openaireLibrary/utils/pipes/safeHTML.pipe';
14

    
15
import {Session} from '../../openaireLibrary/login/utils/helper.class';
16
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
17

    
18
@Component({
19
    selector: 'page-help-contents',
20
    templateUrl: './page-help-contents.component.html',
21
})
22

    
23
export class PageHelpContentsComponent implements OnInit {
24

    
25
    // @ViewChild(ModalFormComponent)
26
    // @ViewChild('saveModal')
27
    // public modal:ModalFormComponent;
28
    //
29
    // @ViewChild('updateModal')
30
    // public updateModal:ModalFormComponent;
31
    //
32
    // @ViewChild(PageHelpContentsFormComponent)
33
    // public formComponent : PageHelpContentsFormComponent;
34

    
35
    // @ViewChild('deleteConfirmationModal')
36
    // public deleteConfirmationModal : DeleteConfirmationDialogComponent;
37
    @ViewChild('AlertModalDeletePageHelpContents') alertModalDeletePageHelpContents;
38
    private selectedPageContents: string[] = [];
39

    
40
    public checkboxes : CheckPageHelpContent[] = [];
41

    
42
    public pageHelpContents : PageHelpContent[] = [];
43

    
44
    //public errorMessage: string;
45

    
46
    public formGroup : FormGroup;
47

    
48
    public pages: Page[];
49

    
50
    public checkboxAll : boolean = false;
51

    
52
    public filters : PageHelpContentFilterOptions = {id : '', active : null, text : new RegExp('')};
53
    public keyword: string = "";
54

    
55
    public counter = {all : 0, active : 0, inactive : 0};
56

    
57
    public communities: Community[] = [];
58

    
59
    public selectedCommunityPid: string;
60

    
61
    public selectedPageId: string;
62

    
63
    public community: Community;
64

    
65
    public page: Page;
66
    public properties:EnvProperties = null;
67

    
68
    public showLoading: boolean = true;
69
    public errorMessage: string = '';
70
    public updateErrorMessage: string = '';
71

    
72
    ngOnInit() {
73
      this.route.data
74
        .subscribe((data: { envSpecific: EnvProperties }) => {
75
           this.properties = data.envSpecific;
76
           this.route.queryParams.subscribe(params => {
77
             this.scroll();
78

    
79
             this.selectedCommunityPid = params['communityId'];
80
             this.selectedPageId = params['pageId'];
81

    
82
             if(this.selectedCommunityPid && this.selectedPageId) {
83
               this.getPage(this.selectedPageId);
84
             } else if(this.selectedCommunityPid) {
85
               this.selectedPageId = "";
86
               this.getPages(this.selectedCommunityPid);
87
             }
88
           });
89
      });
90
        // this.formGroup = this.formComponent.form;
91
    }
92

    
93
    constructor(private element: ElementRef, private route: ActivatedRoute, private router : Router, private _helpService: HelpContentService) {}
94

    
95
    getPage(pageId: string) {
96
      if(!Session.isLoggedIn()){
97
        console.info(this.router.url);
98
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
99
      } else {
100
        this.showLoading = true;
101
        this.updateErrorMessage = "";
102
        this.errorMessage = "";
103

    
104
        this._helpService.getPage(pageId, this.properties.adminToolsAPIURL).subscribe(
105
            page => {
106
                this.page = page;
107
                this.getPageHelpContents(this.selectedCommunityPid);
108
              },
109
              error => this.handleError('System error retrieving page', error));
110
      }
111
    }
112

    
113
    getPages(community_pid: string) {
114
      if(!Session.isLoggedIn()){
115
        console.info(this.router.url);
116
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
117
      } else {
118
        this.showLoading = true;
119
        this.updateErrorMessage = "";
120
        this.errorMessage = "";
121

    
122
        this._helpService.getCommunityPages(community_pid, "", this.properties.adminToolsAPIURL).subscribe(
123
            pages => {
124
              this.pages = pages;
125
              this.getPageHelpContents(this.selectedCommunityPid);
126
            },
127
            error => this.handleError('System error retrieving pages', error));
128
      }
129
    }
130

    
131
    public countPageHelpContents() {
132
        this.counter = {all : 0, active : 0, inactive : 0};
133
        let filter = Object.assign({},this.filters);
134
        filter.active = null;
135
        this.pageHelpContents.forEach(_ => {
136
            if(this.filterPageHelpContent(_,filter)){
137
                if (_.isActive==true) this.counter.active++;
138
                else this.counter.inactive++
139
            }
140
        });
141
        this.counter.all = this.counter.active + this.counter.inactive;
142
    }
143

    
144
    getPageHelpContents(community_pid: string) {
145
      if(!Session.isLoggedIn()){
146
        console.info(this.router.url);
147
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
148
      } else {
149
        this._helpService.getCommunityPageHelpContents(community_pid, this.properties.adminToolsAPIURL).subscribe(
150
            pageHelpContents => {
151
                this.pageHelpContents = pageHelpContents as Array<PageHelpContent>;
152
                this.counter.all = this.pageHelpContents.length;
153
                this.checkboxes = [];
154

    
155
                for (let i = this.pageHelpContents.length - 1; i >= 0; i -= 1) {
156
                  let page: Page =  this.pageHelpContents[i].page as Page;
157
                  if(!this.selectedPageId || (page._id == this.selectedPageId)) {
158
                    this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent : this.pageHelpContents[i], checked : false});
159
                  } else {
160
                    this.pageHelpContents.splice(i, 1);
161
                  }
162
                }
163

    
164
                this.countPageHelpContents();
165

    
166
                this.showLoading = false;
167
            },
168
            error => this.handleError('System error retrieving page contents', error));
169
        }
170
    }
171

    
172
    // public showModal():void {
173
    //     this.modal.showModal();
174
    // }
175

    
176
    public toggleCheckBoxes(event) {
177
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
178
        this.checkboxAll = event.target.checked;
179
    }
180

    
181
    public applyCheck(flag : boolean) {
182
        this.checkboxes.forEach(_ => _.checked = flag);
183
        this.checkboxAll = false;
184
    }
185

    
186
    public getSelectedPageHelpContents() : string[] {
187
        return this.checkboxes.filter(pageHelpContent => pageHelpContent.checked == true)
188
            .map(checkedPageHelpContent => checkedPageHelpContent.pageHelpContent).map(res => res._id);
189
    }
190

    
191
    public confirmDeletePageHelpContent(id : string) {
192
        //this.deleteConfirmationModal.ids = [id];
193
        //this.deleteConfirmationModal.showModal();
194
        this.selectedPageContents = [id];
195
        this.confirmModalOpen();
196
    }
197

    
198
    public confirmDeleteSelectedPageHelpContents() {
199
        //this.deleteConfirmationModal.ids = this.getSelectedPageHelpContents();
200
        //this.deleteConfirmationModal.showModal();
201
        this.selectedPageContents = this.getSelectedPageHelpContents();
202
        this.confirmModalOpen();
203
    }
204

    
205
    private confirmModalOpen() {
206
      if(!Session.isLoggedIn()){
207
        console.info(this.router.url);
208
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
209
      } else {
210
        this.alertModalDeletePageHelpContents.cancelButton = true;
211
        this.alertModalDeletePageHelpContents.okButton = true;
212
        this.alertModalDeletePageHelpContents.alertTitle = "Delete Confirmation";
213
        this.alertModalDeletePageHelpContents.message = "Are you sure you want to delete the selected page content(s)?";
214
        this.alertModalDeletePageHelpContents.okButtonText = "Yes";
215
        this.alertModalDeletePageHelpContents.open();
216
      }
217
    }
218

    
219
    public confirmedDeletePageHelpContents(data: any) {
220
      if(!Session.isLoggedIn()){
221
        console.info(this.router.url);
222
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
223
      } else {
224
        this.showLoading = true;
225
        this.updateErrorMessage = "";
226

    
227
        this._helpService.deletePageHelpContents(this.selectedPageContents, this.properties.adminToolsAPIURL).subscribe(
228
            _ => {
229
              this.deletePageHelpContentsFromArray(this.selectedPageContents);
230
              this.showLoading = false;
231
            },
232
            error => this.handleUpdateError('System error deleting the selected page content(s)', error)
233
        );
234
      }
235
    }
236

    
237
    private deletePageHelpContentsFromArray(ids : string[]) : void {
238
        for(let id of ids) {
239
            let iqc = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
240
            let iq = this.pageHelpContents.findIndex(_ => _._id == id);
241
            this.checkboxes.splice(iqc, 1);
242
            this.pageHelpContents.splice(iqc, 1);
243
        }
244
        this.countPageHelpContents();
245
    }
246

    
247
    public editPageHelpContent(id : string) {
248
        //this.router.navigate(['/pageContents/edit/', _id]);
249
        if(this.selectedPageId) {
250
          this.router.navigate( ['/pageContents/edit/'], { queryParams: { "pageContentId": id, "communityId": this.selectedCommunityPid, "pageId": this.selectedPageId  } } );
251
        } else {
252
          this.router.navigate( ['/pageContents/edit/'], { queryParams: { "pageContentId": id, "communityId": this.selectedCommunityPid } } );
253
        }
254
    }
255

    
256
    public togglePageHelpContents(status : boolean, ids : string[]) {
257
      if(!Session.isLoggedIn()){
258
        console.info(this.router.url);
259
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
260
      } else {
261
        this.showLoading = true;
262
        this.updateErrorMessage = "";
263

    
264
        this._helpService.togglePageHelpContents(ids,status, this.properties.adminToolsAPIURL).subscribe(
265
            () => {
266
                for(let id of ids) {
267
                    let i = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
268
                    console.info(i);
269
                    this.checkboxes[i].pageHelpContent.isActive=status;
270
                }
271
                this.countPageHelpContents();
272
                this.applyCheck(false);
273

    
274
                this.showLoading = false;
275
            },
276
            error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
277
        );
278
      }
279
    }
280

    
281
    public pageHelpContentSavedSuccessfully(pageHelpContent: PageHelpContent) {
282
        this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent : pageHelpContent, checked : false});
283
        this.pageHelpContents.push(pageHelpContent);
284
        this.applyCheck(false);
285
        this.countPageHelpContents();
286
    }
287

    
288
    public pageHelpContentUpdatedSuccessfully(pageHelpContent : PageHelpContent) {
289
        this.checkboxes.find(checkItem => checkItem.pageHelpContent._id==pageHelpContent._id).pageHelpContent = pageHelpContent;
290
        let index = this.pageHelpContents.findIndex(checkItem => checkItem._id==pageHelpContent._id);
291
        this.pageHelpContents[index] = pageHelpContent;
292
        this.applyCheck(false);
293
        this.countPageHelpContents();
294
    }
295

    
296

    
297
    public filterPageHelpContent(pageHelpContent : PageHelpContent, filters : PageHelpContentFilterOptions) : boolean {
298
        let idFlag = filters.id == '' || (<Page>pageHelpContent.page)._id == filters.id;
299
        let activeFlag = filters.active == null || pageHelpContent.isActive == filters.active;
300
        let textFlag = filters.text.toString() == '' || (pageHelpContent.content).match(filters.text) != null;
301
        return idFlag && activeFlag && textFlag;
302
    }
303

    
304
    public applyFilter() {
305
        this.checkboxes = [];
306
        this.pageHelpContents.filter(item => this.filterPageHelpContent(item,this.filters)).forEach(
307
            _ => this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent: _, checked: false})
308
        );
309
        this.countPageHelpContents();
310
    }
311

    
312
    public filterByPage(event: any) {
313
        this.filters.id = event.target.value;
314
        this.applyFilter();
315
    }
316

    
317
    public displayAllPageHelpContents() {
318
        this.filters.active = null;
319
        this.applyFilter();
320
    }
321

    
322
    public displayActivePageHelpContents() {
323
        this.filters.active = true;
324
        this.applyFilter();
325
    }
326

    
327
    public filterBySearch(text : string) {
328
        this.filters.text = new RegExp(text, "i");
329
        this.applyFilter();
330
    }
331

    
332
    public displayInactivePageHelpContents() {
333
        this.filters.active = false;
334
        this.applyFilter();
335
    }
336

    
337
    handleError(message: string, error) {
338
        this.errorMessage = message;
339
        console.log('Server responded: ' + error);
340

    
341
        this.showLoading = false;
342
    }
343

    
344
    handleUpdateError(message: string, error) {
345
        this.updateErrorMessage = message;
346
        console.log('Server responded: ' +error);
347

    
348
        this.showLoading = false;
349
    }
350

    
351
    public newPageContent() {
352
      if(this.selectedPageId) {
353
        this.router.navigate( ['/pageContents/new'], { queryParams: {communityId: this.selectedCommunityPid, pageId: this.selectedPageId} } );
354
      } else {
355
        this.router.navigate( ['/pageContents/new'], { queryParams: {communityId: this.selectedCommunityPid} } );
356
      }
357
    }
358

    
359
    public scroll() {
360
      console.info("scroll into view");
361
      if (typeof document !== 'undefined') {
362
         this.element.nativeElement.scrollIntoView();
363
      }
364
    }
365
}
(8-8/8)