Project

General

Profile

1
import {Component, ElementRef, OnInit, ViewChild} 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 {CheckDivHelpContent, DivHelpContent} from '../../utils/entities/adminTool/div-help-content';
17

    
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
  @ViewChild('AlertModalDeletePageHelpContents') alertModalDeletePageHelpContents;
27
  private selectedPageContents: string[] = [];
28
  public checkboxes: CheckDivHelpContent[] = [];
29
  public divHelpContents: DivHelpContent[] = [];
30
  public formGroup: FormGroup;
31
  public pages: Page[];
32
  public checkboxAll: boolean = false;
33
  public filters: PageHelpContentFilterOptions = {id: '', active: null, text: new RegExp('')};
34
  public keyword: string = '';
35
  public counter = {all: 0, active: 0, inactive: 0};
36
  public communities: Portal[] = [];
37
  public portal: string;
38
  public selectedPageId: string;
39
  public community: Portal;
40
  public page: Page;
41
  public properties: EnvProperties = properties;
42
  public showLoading: boolean = true;
43
  public filterForm: FormControl;
44
  private subscriptions: any[] = [];
45
  public selectedKeyword: string;
46
  @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
47
  
48
  constructor(private element: ElementRef, private route: ActivatedRoute, private router: Router, private _helpService: HelpContentService, private _fb: FormBuilder, private sanitizer: DomSanitizer) {
49
  }
50
  
51
  ngOnInit() {
52
    this.filterForm = this._fb.control('');
53
    this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
54
      this.filterBySearch(value);
55
    }));
56
    this.subscriptions.push(this.route.params.subscribe(params => {
57
      this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
58
      this.subscriptions.push(this.route.queryParams.subscribe(params => {
59
        HelperFunctions.scroll();
60
        this.selectedPageId = params['pageId'];
61
        if (this.portal && this.selectedPageId) {
62
          this.getPage(this.selectedPageId);
63
        }
64
        if (!this.selectedPageId) {
65
          this.router.navigate(['../pages'], {relativeTo: this.route});
66
        }
67
      }));
68
    }));
69
  }
70
  
71
  ngOnDestroy(): void {
72
    this.subscriptions.forEach(value => {
73
      if (value instanceof Subscriber) {
74
        value.unsubscribe();
75
      } else if (value instanceof Function) {
76
        value();
77
      }
78
    });
79
  }
80
  
81
  getPage(pageId: string) {
82
    this.showLoading = true;
83
    this.subscriptions.push(this._helpService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal).subscribe(
84
      page => {
85
        if (this.properties.adminToolsPortalType != page.portalType) {
86
          this.router.navigate(['./pageContents'], {queryParams: {'communityId': this.portal}});
87
        } else {
88
          this.page = page;
89
          this.getPageHelpContents(this.portal);
90
        }
91
      },
92
      error => this.handleError('System error retrieving page', error)));
93
  }
94
  
95
  
96
  public countClassHelpContents() {
97
    this.counter = {all: 0, active: 0, inactive: 0};
98
    let filter = Object.assign({}, this.filters);
99
    filter.active = null;
100
    this.divHelpContents.forEach(_ => {
101
      if (this.filterPageHelpContent(_, filter)) {
102
        if (_.isActive == true) {
103
          this.counter.active++;
104
        } else {
105
          this.counter.inactive++;
106
        }
107
      }
108
    });
109
    this.counter.all = this.counter.active + this.counter.inactive;
110
  }
111
  
112
  getPageHelpContents(community_pid: string) {
113
    this.subscriptions.push(this._helpService.getCommunityDivHelpContents(community_pid, this.properties.adminToolsAPIURL, this.selectedPageId).subscribe(
114
      pageHelpContents => {
115
        this.divHelpContents = pageHelpContents as Array<DivHelpContent>;
116
        this.counter.all = this.divHelpContents.length;
117
        this.checkboxes = [];
118
        
119
        for (let i = this.divHelpContents.length - 1; i >= 0; i -= 1) {
120
          this.checkboxes.unshift(<CheckDivHelpContent>{divHelpContent: this.divHelpContents[i], checked: false});
121
        }
122
        
123
        this.countClassHelpContents();
124
        
125
        this.showLoading = false;
126
      },
127
      error => this.handleError('System error retrieving page contents', error)));
128
  }
129
  
130
  public applyCheck(flag: boolean) {
131
    this.checkboxes.forEach(_ => _.checked = flag);
132
    this.checkboxAll = false;
133
  }
134
  
135
  public getSelectedPageHelpContents(): string[] {
136
    return this.checkboxes.filter(pageHelpContent => pageHelpContent.checked == true)
137
      .map(checkedPageHelpContent => checkedPageHelpContent.divHelpContent).map(res => res._id);
138
  }
139
  
140
  public confirmDeletePageHelpContent(id: string) {
141
    this.selectedPageContents = [id];
142
    this.confirmModalOpen();
143
  }
144
  
145
  public confirmDeleteSelectedPageHelpContents() {
146
    
147
    this.selectedPageContents = this.getSelectedPageHelpContents();
148
    this.confirmModalOpen();
149
  }
150
  
151
  private confirmModalOpen() {
152
    this.alertModalDeletePageHelpContents.cancelButton = true;
153
    this.alertModalDeletePageHelpContents.okButton = true;
154
    this.alertModalDeletePageHelpContents.alertTitle = 'Delete Confirmation';
155
    this.alertModalDeletePageHelpContents.message = 'Are you sure you want to delete the selected page content(s)?';
156
    this.alertModalDeletePageHelpContents.okButtonText = 'Yes';
157
    this.alertModalDeletePageHelpContents.open();
158
  }
159
  
160
  public confirmedDeletePageHelpContents(data: any) {
161
    this.showLoading = true;
162
    this.subscriptions.push(this._helpService.deleteDivHelpContents(this.selectedPageContents, this.properties.adminToolsAPIURL, this.portal).subscribe(
163
      _ => {
164
        this.deletePageHelpContentsFromArray(this.selectedPageContents);
165
        UIkit.notification('Page content(s) has been <b>successfully deleted</b>', {
166
          status: 'success',
167
          timeout: 6000,
168
          pos: 'bottom-right'
169
        });
170
        this.showLoading = false;
171
      },
172
      error => this.handleUpdateError('System error deleting the selected page content(s)', error)
173
    ));
174
  }
175
  
176
  private deletePageHelpContentsFromArray(ids: string[]): void {
177
    for (let id of ids) {
178
      let i = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
179
      let j = this.divHelpContents.findIndex(_ => _._id == id);
180
      this.checkboxes.splice(i, 1);
181
      this.divHelpContents.splice(j, 1);
182
    }
183
    this.countClassHelpContents();
184
  }
185
  
186
  public editPageHelpContent(id: string) {
187
    if (this.selectedPageId) {
188
      this.router.navigate(['edit/'], {
189
        queryParams: {
190
          'pageContentId': id,
191
          'pageId': this.selectedPageId
192
        }, relativeTo: this.route
193
      });
194
    } else {
195
      this.router.navigate(['edit/'], {
196
        queryParams: {
197
          'pageContentId': id,
198
        }, relativeTo: this.route
199
      });
200
    }
201
  }
202
  
203
  public togglePageHelpContents(status: boolean, ids: string[]) {
204
    if (!Session.isLoggedIn()) {
205
      this.router.navigate(['/user-info'], {
206
        queryParams: {
207
          'errorCode': LoginErrorCodes.NOT_VALID,
208
          'redirectUrl': this.router.url
209
        }
210
      });
211
    } else {
212
      this.subscriptions.push(this._helpService.toggleDivHelpContents(ids, status, this.properties.adminToolsAPIURL, this.portal).subscribe(
213
        () => {
214
          for (let id of ids) {
215
            let i = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
216
            this.checkboxes[i].divHelpContent.isActive = status;
217
          }
218
          this.countClassHelpContents();
219
          this.applyCheck(false);
220
          UIkit.notification('Page content(s) has been <b>successfully updated</b>', {
221
            status: 'success',
222
            timeout: 6000,
223
            pos: 'bottom-right'
224
          });
225
        },
226
        error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
227
      ));
228
    }
229
  }
230
  
231
  
232
  public filterPageHelpContent(divHelpContent: DivHelpContent, filters: PageHelpContentFilterOptions): boolean {
233
    let activeFlag = filters.active == null || divHelpContent.isActive == filters.active;
234
    let textFlag = filters.text.toString() == '' || (divHelpContent.content && divHelpContent.content.match(filters.text) != null);
235
    return activeFlag && textFlag;
236
  }
237
  
238
  
239
  public applyFilter() {
240
    this.checkboxes = [];
241
    this.divHelpContents.filter(item => this.filterPageHelpContent(item, this.filters)).forEach(
242
      _ => {
243
        this.checkboxes.push(<CheckDivHelpContent>{divHelpContent: _, checked: false});
244
      }
245
    );
246
  }
247
  
248
  public filterBySearch(text: string) {
249
    this.filters.text = new RegExp(text, 'i');
250
    this.applyFilter();
251
  }
252
  
253
  handleError(message: string, error) {
254
    UIkit.notification(message, {
255
      status: 'danger',
256
      timeout: 6000,
257
      pos: 'bottom-right'
258
    });
259
    console.log('Server responded: ' + error);
260
    this.showLoading = false;
261
  }
262
  
263
  handleUpdateError(message: string, error) {
264
    UIkit.notification(message, {
265
      status: 'danger',
266
      timeout: 6000,
267
      pos: 'bottom-right'
268
    });
269
    console.log('Server responded: ' + error);
270
    this.showLoading = false;
271
  }
272
  
273
  public newPageContent() {
274
    this.router.navigate(['edit'], {
275
      queryParams: {
276
        pageId: this.selectedPageId
277
      }, relativeTo: this.route
278
    });
279
  }
280
  
281
  public onSearchClose() {
282
    this.selectedKeyword = this.filterForm.value;
283
  }
284
  
285
  public reset() {
286
    this.selectedKeyword = null;
287
    this.searchInputComponent.reset();
288
  }
289
  
290
  selectAll() {
291
    let checked = (this.getSelectedPageHelpContents().length != this.checkboxes.length);
292
    for (let check of this.checkboxes) {
293
      check.checked = checked;
294
    }
295
  }
296
}
(7-7/8)