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 {
6
  PageHelpContent,
7
  CheckPageHelpContent,
8
  PageHelpContentFilterOptions
9
} from "../../utils/entities/adminTool/page-help-content";
10
import {Page} from "../../utils/entities/adminTool/page";
11
import {Portal} from "../../utils/entities/adminTool/portal";
12
import {EnvProperties} from '../../utils/properties/env-properties';
13
import {Session} from '../../login/utils/helper.class';
14
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
15
import {HelperFunctions} from "../../utils/HelperFunctions.class";
16
import {Subscriber} from "rxjs";
17
import {properties} from "../../../../environments/environment";
18
import {DomSanitizer} from '@angular/platform-browser';
19
import {SearchInputComponent} from '../../sharedComponents/search-input/search-input.component';
20
import {ConnectHelper} from '../../connect/connectHelper';
21

    
22
declare var UIkit;
23

    
24
@Component({
25
  selector: 'page-help-contents',
26
  templateUrl: './page-help-contents.component.html',
27
})
28
export class PageHelpContentsComponent implements OnInit {
29
  @ViewChild('AlertModalDeletePageHelpContents') alertModalDeletePageHelpContents;
30
  private selectedPageContents: string[] = [];
31
  public checkboxes: CheckPageHelpContent[] = [];
32
  public pageHelpContents: PageHelpContent[] = [];
33
  public formGroup: FormGroup;
34
  public pages: Page[];
35
  public checkboxAll: boolean = false;
36
  public filters: PageHelpContentFilterOptions = {id: '', active: null, text: new RegExp('')};
37
  public keyword: string = "";
38
  public counter = {all: 0, active: 0, inactive: 0};
39
  public communities: Portal[] = [];
40
  public portal: string;
41
  public selectedPageId: string;
42
  public community: Portal;
43
  public page: Page;
44
  public properties: EnvProperties = properties;
45
  public showLoading: boolean = true;
46
  public filterForm: FormControl;
47
  public selectForm: FormControl;
48
  private subscriptions: any[] = [];
49
  public selectedKeyword: string;
50
  @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
51
  
52
  ngOnInit() {
53
    this.filterForm = this._fb.control('');
54
    this.selectForm = this._fb.control('');
55
    this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
56
      this.filterBySearch(value);
57
    }));
58
    this.subscriptions.push(this.selectForm.valueChanges.subscribe(value => {
59
      this.filterByPage(value);
60
    }));
61
    this.subscriptions.push(this.route.params.subscribe(params => {
62
      this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
63
      ConnectHelper.setPortalTypeFromPid(this.portal);
64
      this.subscriptions.push(this.route.queryParams.subscribe(params => {
65
        HelperFunctions.scroll();
66
        this.selectedPageId = params['pageId'];
67
        if (this.portal && this.selectedPageId) {
68
          this.getPage(this.selectedPageId);
69
        }
70
        if (!this.selectedPageId) {
71
          this.router.navigate(['../pages'], {relativeTo: this.route});
72
        }
73
      }));
74
    }));
75
  }
76
  
77
  constructor(private element: ElementRef, private route: ActivatedRoute, private router: Router, private _helpService: HelpContentService, private _fb: FormBuilder, private sanitizer: DomSanitizer) {
78
  }
79
  
80
  ngOnDestroy(): void {
81
    this.subscriptions.forEach(value => {
82
      if (value instanceof Subscriber) {
83
        value.unsubscribe();
84
      } else if (value instanceof Function) {
85
        value();
86
      }
87
    });
88
  }
89
  
90
  getPage(pageId: string) {
91
    if (!Session.isLoggedIn()) {
92
      this.router.navigate(['/user-info'], {
93
        queryParams: {
94
          "errorCode": LoginErrorCodes.NOT_VALID,
95
          "redirectUrl": this.router.url
96
        }
97
      });
98
    } else {
99
      this.showLoading = true;
100
      this.subscriptions.push(this._helpService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal).subscribe(
101
        page => {
102
          if (this.properties.adminToolsPortalType != page.portalType) {
103
            this.router.navigate(['./pageContents']);
104
          } else {
105
            this.page = page;
106
            this.getPageHelpContents(this.portal);
107
          }
108
        },
109
        error => this.handleError('System error retrieving page', error)));
110
    }
111
  }
112

    
113
  public countPageHelpContents() {
114
    this.counter = {all: 0, active: 0, inactive: 0};
115
    let filter = Object.assign({}, this.filters);
116
    filter.active = null;
117
    this.pageHelpContents.forEach(_ => {
118
      if (this.filterPageHelpContent(_, filter)) {
119
        if (_.isActive == true) this.counter.active++;
120
        else this.counter.inactive++
121
      }
122
    });
123
    this.counter.all = this.counter.active + this.counter.inactive;
124
  }
125
  
126
  getPageHelpContents(community_pid: string) {
127
    if (!Session.isLoggedIn()) {
128
      this.router.navigate(['/user-info'], {
129
        queryParams: {
130
          "errorCode": LoginErrorCodes.NOT_VALID,
131
          "redirectUrl": this.router.url
132
        }
133
      });
134
    } else {
135
      this.subscriptions.push(this._helpService.getCommunityPageHelpContents(community_pid, this.properties.adminToolsAPIURL, this.selectedPageId).subscribe(
136
        pageHelpContents => {
137
          this.pageHelpContents = pageHelpContents as Array<PageHelpContent>;
138
          this.counter.all = this.pageHelpContents.length;
139
          this.checkboxes = [];
140
          
141
          for (let i = this.pageHelpContents.length - 1; i >= 0; i -= 1) {
142
            this.checkboxes.unshift(<CheckPageHelpContent>{
143
              pageHelpContent: this.pageHelpContents[i],
144
              checked: false
145
            });
146
          }
147
          
148
          this.countPageHelpContents();
149
          
150
          this.showLoading = false;
151
        },
152
        error => this.handleError('System error retrieving page contents', error)));
153
    }
154
  }
155
  
156
  public toggleCheckBoxes(event) {
157
    this.checkboxes.forEach(_ => _.checked = event.target.checked);
158
    this.checkboxAll = event.target.checked;
159
  }
160
  
161
  public applyCheck(flag: boolean) {
162
    this.checkboxes.forEach(_ => _.checked = flag);
163
    this.checkboxAll = false;
164
  }
165
  
166
  public getSelectedPageHelpContents(): string[] {
167
    return this.checkboxes.filter(pageHelpContent => pageHelpContent.checked == true)
168
      .map(checkedPageHelpContent => checkedPageHelpContent.pageHelpContent).map(res => res._id);
169
  }
170
  
171
  public confirmDeletePageHelpContent(id: string) {
172
    this.selectedPageContents = [id];
173
    this.confirmModalOpen();
174
  }
175
  
176
  public confirmDeleteSelectedPageHelpContents() {
177
    this.selectedPageContents = this.getSelectedPageHelpContents();
178
    this.confirmModalOpen();
179
  }
180
  
181
  private confirmModalOpen() {
182
    if (!Session.isLoggedIn()) {
183
      this.router.navigate(['/user-info'], {
184
        queryParams: {
185
          "errorCode": LoginErrorCodes.NOT_VALID,
186
          "redirectUrl": this.router.url
187
        }
188
      });
189
    } else {
190
      this.alertModalDeletePageHelpContents.cancelButton = true;
191
      this.alertModalDeletePageHelpContents.okButton = true;
192
      this.alertModalDeletePageHelpContents.alertTitle = "Delete Confirmation";
193
      this.alertModalDeletePageHelpContents.message = "Are you sure you want to delete the selected page content(s)?";
194
      this.alertModalDeletePageHelpContents.okButtonText = "Yes";
195
      this.alertModalDeletePageHelpContents.open();
196
    }
197
  }
198
  
199
  public confirmedDeletePageHelpContents(data: any) {
200
    if (!Session.isLoggedIn()) {
201
      this.router.navigate(['/user-info'], {
202
        queryParams: {
203
          "errorCode": LoginErrorCodes.NOT_VALID,
204
          "redirectUrl": this.router.url
205
        }
206
      });
207
    } else {
208
      this.showLoading = true;
209
      this.subscriptions.push(this._helpService.deletePageHelpContents(this.selectedPageContents, this.properties.adminToolsAPIURL, this.portal).subscribe(
210
        _ => {
211
          this.deletePageHelpContentsFromArray(this.selectedPageContents);
212
          UIkit.notification('Page content(s) has been <b>successfully deleted</b>', {
213
            status: 'success',
214
            timeout: 6000,
215
            pos: 'bottom-right'
216
          });
217
          this.showLoading = false;
218
        },
219
        error => this.handleUpdateError('System error deleting the selected page content(s)', error)
220
      ));
221
    }
222
  }
223
  
224
  private deletePageHelpContentsFromArray(ids: string[]): void {
225
    for (let id of ids) {
226
      let iqc = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
227
      let iq = this.pageHelpContents.findIndex(_ => _._id == id);
228
      this.checkboxes.splice(iqc, 1);
229
      this.pageHelpContents.splice(iqc, 1);
230
    }
231
    this.countPageHelpContents();
232
  }
233
  
234
  public editPageHelpContent(id: string) {
235
    //this.router.navigate(['/pageContents/edit/', _id]);
236
    if (this.selectedPageId) {
237
      this.router.navigate(['edit/'], {
238
        queryParams: {
239
          "pageContentId": id,
240
          "pageId": this.selectedPageId
241
        }, relativeTo: this.route
242
      });
243
    } else {
244
      this.router.navigate(['edit/'], {
245
        queryParams: {
246
          "pageContentId": id
247
        }, relativeTo: this.route
248
      });
249
    }
250
  }
251
  
252
  public togglePageHelpContents(status: boolean, ids: string[]) {
253
    if (!Session.isLoggedIn()) {
254
      this.router.navigate(['/user-info'], {
255
        queryParams: {
256
          "errorCode": LoginErrorCodes.NOT_VALID,
257
          "redirectUrl": this.router.url
258
        }
259
      });
260
    } else {
261
      this.subscriptions.push(this._helpService.togglePageHelpContents(ids, status, this.properties.adminToolsAPIURL, this.portal).subscribe(
262
        () => {
263
          for (let id of ids) {
264
            let i = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
265
            this.checkboxes[i].pageHelpContent.isActive = status;
266
          }
267
          UIkit.notification('Page content(s) has been <b>successfully updated</b>', {
268
            status: 'success',
269
            timeout: 6000,
270
            pos: 'bottom-right'
271
          });
272
          this.countPageHelpContents();
273
          this.applyCheck(false);
274
        },
275
        error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
276
      ));
277
    }
278
  }
279
  
280

    
281
  public filterPageHelpContent(pageHelpContent: PageHelpContent, filters: PageHelpContentFilterOptions): boolean {
282
    let idFlag = filters.id == '' || (<Page>pageHelpContent.page)._id == filters.id;
283
    let activeFlag = filters.active == null || pageHelpContent.isActive == filters.active;
284
    let textFlag = filters.text.toString() == '' || (pageHelpContent.content).match(filters.text) != null
285
      || ((<Page>pageHelpContent.page).name).match(filters.text) != null;
286
    return idFlag && activeFlag && textFlag;
287
  }
288
  
289
  
290
  public applyFilter() {
291
    this.checkboxes = [];
292
    this.pageHelpContents.filter(item => this.filterPageHelpContent(item, this.filters)).forEach(
293
      _ => {
294
        this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent: _, checked: false})
295
      }
296
    );
297
  }
298
  
299
  public filterByPage(event: any) {
300
    if (event.target && event.target.value) {
301
      this.filters.id = event.target.value;
302
      this.applyFilter();
303
    }
304
  }
305

    
306
  public filterBySearch(text: string) {
307
    this.filters.text = new RegExp(text, "i");
308
    this.applyFilter();
309
  }
310
  
311
  handleError(message: string, error) {
312
    UIkit.notification(message, {
313
      status: 'danger',
314
      timeout: 6000,
315
      pos: 'bottom-right'
316
    });
317
    console.log('Server responded: ' + error);
318
    this.showLoading = false;
319
  }
320
  
321
  handleUpdateError(message: string, error) {
322
    UIkit.notification(message, {
323
      status: 'danger',
324
      timeout: 6000,
325
      pos: 'bottom-right'
326
    });
327
    console.log('Server responded: ' + error);
328
    this.showLoading = false;
329
  }
330
  
331
  public newPageContent() {
332
    this.router.navigate(['edit'], {
333
      queryParams: {
334
        pageId: this.selectedPageId
335
      }, relativeTo: this.route
336
    });
337
  }
338
  
339
  public onSearchClose() {
340
    this.selectedKeyword = this.filterForm.value;
341
  }
342
  
343
  public reset() {
344
    this.selectedKeyword = null;
345
    this.searchInputComponent.reset()
346
  }
347
  
348
  selectAll() {
349
    let checked = (this.getSelectedPageHelpContents().length != this.checkboxes.length);
350
    for (let check of this.checkboxes) {
351
      check.checked = checked;
352
    }
353
  }
354
}
(7-7/8)