Project

General

Profile

1
import {Component, ViewChild, OnInit, ElementRef} from '@angular/core';
2
import {FormBuilder, FormControl, FormGroup} from "@angular/forms";
3
import {ActivatedRoute, Router} from "@angular/router";
4
import {HelpContentService} from "../../services/help-content.service";
5
import {PageHelpContentFilterOptions} from '../../utils/entities/adminTool/page-help-content';
6
import {Page} from "../../utils/entities/adminTool/page";
7
import {Portal} from "../../utils/entities/adminTool/portal";
8
import {EnvProperties} from '../../utils/properties/env-properties';
9
import {Session} from '../../login/utils/helper.class';
10
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
11
import {HelperFunctions} from "../../utils/HelperFunctions.class";
12
import {Subscriber} from "rxjs";
13
import {properties} from "../../../../environments/environment";
14
import {DomSanitizer} from '@angular/platform-browser';
15
import {SearchInputComponent} from '../../sharedComponents/search-input/search-input.component';
16
import {ConnectHelper} from '../../connect/connectHelper';
17
import {CheckDivHelpContent, DivHelpContent} from '../../utils/entities/adminTool/div-help-content';
18
declare var UIkit;
19

    
20

    
21
@Component({
22
  selector: 'class-help-contents',
23
  templateUrl: './class-help-contents.component.html',
24
})
25
export class ClassHelpContentsComponent implements OnInit {
26

    
27
  @ViewChild('AlertModalDeletePageHelpContents', { static: true }) alertModalDeletePageHelpContents;
28
  private selectedPageContents: string[] = [];
29

    
30
  public checkboxes: CheckDivHelpContent[] = [];
31

    
32
  public pageHelpContents: DivHelpContent[] = [];
33

    
34
  public formGroup: FormGroup;
35

    
36
  public pages: Page[];
37

    
38
  public checkboxAll: boolean = false;
39

    
40
  public filters: PageHelpContentFilterOptions = {id: '', active: null, text: new RegExp('')};
41
  public keyword: string = "";
42

    
43
  public counter = {all: 0, active: 0, inactive: 0};
44

    
45
  public communities: Portal[] = [];
46

    
47
  public portal: string;
48

    
49
  public selectedPageId: string;
50

    
51
  public community: Portal;
52

    
53
  public page: Page;
54
  public properties: EnvProperties = null;
55

    
56
  public showLoading: boolean = true;
57
  public errorMessage: string = '';
58
  public updateErrorMessage: string = '';
59
  public filterForm: FormControl;
60
  public selectForm: FormControl;
61
  private subscriptions: any[] = [];
62
  public selectedKeyword: string;
63
  @ViewChild('searchInputComponent', { static: true }) searchInputComponent: SearchInputComponent;
64

    
65
  ngOnInit() {
66
    this.filterForm = this._fb.control('');
67
    this.selectForm = this._fb.control('');
68
    this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
69
      this.filterBySearch(value);
70
    }));
71
    this.subscriptions.push(this.selectForm.valueChanges.subscribe(value => {
72
      this.filterByPage(value);
73
    }));
74

    
75
    this.properties = properties;
76
    this.subscriptions.push(this.route.params.subscribe(params => {
77
      this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
78
      ConnectHelper.setPortalTypeFromPid(this.portal);
79
      this.subscriptions.push(this.route.queryParams.subscribe(params => {
80
        HelperFunctions.scroll();
81
        this.selectedPageId = params['pageId'];
82
        if (this.portal && this.selectedPageId) {
83
          this.getPage(this.selectedPageId);
84
        }
85
        if(!this.selectedPageId) {
86
          this.router.navigate(['../pages'], {relativeTo: this.route });
87
        }
88
      }));
89
    }));
90
  }
91

    
92
  constructor(private element: ElementRef, private route: ActivatedRoute, private router: Router, private _helpService: HelpContentService, private _fb: FormBuilder, private sanitizer: DomSanitizer) {
93
  }
94
  ngOnDestroy(): void {
95
    this.subscriptions.forEach(value => {
96
      if (value instanceof Subscriber) {
97
        value.unsubscribe();
98
      } else if (value instanceof Function) {
99
        value();
100
      }
101
    });
102
  }
103
  init(){
104

    
105
  }
106
  getPage(pageId: string) {
107
    if (!Session.isLoggedIn()) {
108
      this.router.navigate(['/user-info'], {
109
        queryParams: {
110
          "errorCode": LoginErrorCodes.NOT_VALID,
111
          "redirectUrl": this.router.url
112
        }
113
      });
114
    } else {
115
      this.showLoading = true;
116
      this.updateErrorMessage = "";
117
      this.errorMessage = "";
118
      this.subscriptions.push(this._helpService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal).subscribe(
119
        page => {
120
          if (this.properties.adminToolsPortalType != page.portalType) {
121
            this.router.navigate(['./pageContents'], {queryParams: {"communityId": this.portal}});
122
          } else {
123
            this.page = page;
124
            this.getPageHelpContents(this.portal);
125
          }
126
        },
127
        error => this.handleError('System error retrieving page', error)));
128
    }
129
  }
130

    
131

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

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

    
160
          for (let i = this.pageHelpContents.length - 1; i >= 0; i -= 1) {
161
            this.checkboxes.unshift(<CheckDivHelpContent>{divHelpContent: this.pageHelpContents[i], checked: false});
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 toggleCheckBoxes(event) {
173
    this.checkboxes.forEach(_ => _.checked = event.target.checked);
174
    this.checkboxAll = event.target.checked;
175
  }
176

    
177
  public applyCheck(flag: boolean) {
178
    this.checkboxes.forEach(_ => _.checked = flag);
179
    this.checkboxAll = false;
180
  }
181

    
182
  public getSelectedPageHelpContents(): string[] {
183
    return this.checkboxes.filter(pageHelpContent => pageHelpContent.checked == true)
184
      .map(checkedPageHelpContent => checkedPageHelpContent.divHelpContent).map(res => res._id);
185
  }
186

    
187
  public confirmDeletePageHelpContent(id: string) {
188
    this.selectedPageContents = [id];
189
    this.confirmModalOpen();
190
  }
191

    
192
  public confirmDeleteSelectedPageHelpContents() {
193

    
194
    this.selectedPageContents = this.getSelectedPageHelpContents();
195
    this.confirmModalOpen();
196
  }
197

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

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

    
228
      this.subscriptions.push(this._helpService.deleteDivHelpContents(this.selectedPageContents, this.properties.adminToolsAPIURL, this.portal).subscribe(
229
        _ => {
230
          this.deletePageHelpContentsFromArray(this.selectedPageContents);
231
          UIkit.notification('Page content(s) has been <b>successfully deleted</b>', {
232
            status: 'success',
233
            timeout: 6000,
234
            pos: 'bottom-right'
235
          });
236
          this.showLoading = false;
237
        },
238
        error => this.handleUpdateError('System error deleting the selected page content(s)', error)
239
      ));
240
    }
241
  }
242

    
243
  private deletePageHelpContentsFromArray(ids: string[]): void {
244
    for (let id of ids) {
245
      let iqc = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
246
      let iq = this.pageHelpContents.findIndex(_ => _._id == id);
247
      this.checkboxes.splice(iqc, 1);
248
      this.pageHelpContents.splice(iqc, 1);
249
    }
250
    this.countPageHelpContents();
251
  }
252

    
253
  public editPageHelpContent(id: string) {
254
    if (this.selectedPageId) {
255
      this.router.navigate(['edit/'], {
256
        queryParams: {
257
          "pageContentId": id,
258
          "pageId": this.selectedPageId
259
        }, relativeTo: this.route
260
      });
261
    } else {
262
      this.router.navigate(['edit/'], {
263
        queryParams: {
264
          "pageContentId": id,
265
        }, relativeTo: this.route
266
      });
267
    }
268
  }
269

    
270
  public togglePageHelpContents(status: boolean, ids: string[]) {
271
    if (!Session.isLoggedIn()) {
272
      this.router.navigate(['/user-info'], {
273
        queryParams: {
274
          "errorCode": LoginErrorCodes.NOT_VALID,
275
          "redirectUrl": this.router.url
276
        }
277
      });
278
    } else {
279
      this.updateErrorMessage = "";
280

    
281
      this.subscriptions.push(this._helpService.toggleDivHelpContents(ids, status, this.properties.adminToolsAPIURL, this.portal).subscribe(
282
        () => {
283
          for (let id of ids) {
284
            let i = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
285
            this.checkboxes[i].divHelpContent.isActive = status;
286
          }
287
          this.countPageHelpContents();
288
          this.applyCheck(false);
289
          UIkit.notification('Page content(s) has been <b>successfully updated</b>', {
290
            status: 'success',
291
            timeout: 6000,
292
            pos: 'bottom-right'
293
          });
294
        },
295
        error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
296
      ));
297
    }
298
  }
299

    
300

    
301
  public filterPageHelpContent(pageHelpContent: DivHelpContent, filters: PageHelpContentFilterOptions): boolean {
302
    let activeFlag = filters.active == null || pageHelpContent.isActive == filters.active;
303
    let textFlag = filters.text.toString() == '' || (pageHelpContent.content).match(filters.text) != null;
304
    return   activeFlag && textFlag;
305
  }
306

    
307

    
308
  public applyFilter() {
309
    this.checkboxes = [];
310
    this.pageHelpContents.filter(item => this.filterPageHelpContent(item, this.filters)).forEach(
311
      _ => {
312
        this.checkboxes.push(<CheckDivHelpContent>{divHelpContent: _, checked: false})
313
      }
314
    );
315
  }
316

    
317
  public filterByPage(event: any) {
318
    if(event.target && event.target.value) {
319
      this.filters.id = event.target.value;
320
      this.applyFilter();
321
    }
322
  }
323
  public filterBySearch(text: string) {
324
    this.filters.text = new RegExp(text, "i");
325
    this.applyFilter();
326
  }
327

    
328
  handleError(message: string, error) {
329
    this.errorMessage = message;
330
    console.log('Server responded: ' + error);
331

    
332
    this.showLoading = false;
333
  }
334

    
335
  handleUpdateError(message: string, error) {
336
    this.updateErrorMessage = message;
337
    console.log('Server responded: ' + error);
338

    
339
    this.showLoading = false;
340
  }
341

    
342
  public newPageContent() {
343
      this.router.navigate(['edit'], {
344
        queryParams: {
345
          pageId: this.selectedPageId
346
        }, relativeTo: this.route
347
      });
348
  }
349

    
350
  public onSearchClose() {
351
    this.selectedKeyword = this.filterForm.value;
352
  }
353

    
354
  public reset() {
355
    this.selectedKeyword = null;
356
    this.searchInputComponent.reset()
357
  }
358
  selectAll(){
359
    let checked = !!(this.getSelectedPageHelpContents().length != this.checkboxes.length);
360
      for (let check of this.checkboxes) {
361
        check.checked = checked;
362
      }
363
  }
364
}
(7-7/8)