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 {
6
  CheckPageHelpContent,
7
  PageHelpContent,
8
  PageHelpContentFilterOptions
9
} from '../../utils/entities/adminTool/page-help-content';
10
import {CheckPage, 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

    
21
declare var UIkit;
22

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