Project

General

Profile

1 53600 argiro.kok
/**
2
 * Created by stefania on 7/13/17.
3
 */
4 57073 k.triantaf
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
5
import {ActivatedRoute, Router} from '@angular/router';
6
import {HelpContentService} from '../../services/help-content.service';
7
import {FormGroup} from '@angular/forms';
8
import {PageFormComponent} from './page-form.component';
9
import {CheckPage, Page} from '../../domain/page';
10 59468 konstantin
import {Portal} from '../../domain/portal';
11 57073 k.triantaf
import {Entity} from '../../domain/entity';
12
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
13 53600 argiro.kok
import {Session} from '../../openaireLibrary/login/utils/helper.class';
14 53735 konstantin
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
15 57073 k.triantaf
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
16
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
17 58371 k.triantaf
import {Title} from '@angular/platform-browser';
18
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
19 59468 konstantin
import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service";
20 53600 argiro.kok
21
@Component({
22 57073 k.triantaf
  selector: 'pages',
23
  templateUrl: './pages.component.html',
24 53600 argiro.kok
})
25
26
export class PagesComponent implements OnInit {
27
28 57073 k.triantaf
  @ViewChild('AlertModalSavePage') alertModalSavePage;
29
  @ViewChild('AlertModalUpdatePage') alertModalUpdatePage;
30
  @ViewChild('AlertModalDeletePages') alertModalDeletePages;
31
  private selectedPages: string[] = [];
32 53600 argiro.kok
33 57073 k.triantaf
  @ViewChild(PageFormComponent)
34
  public formComponent: PageFormComponent;
35 53600 argiro.kok
36 57073 k.triantaf
  public checkboxes: CheckPage[] = [];
37 53600 argiro.kok
38 57073 k.triantaf
  public pages: Page[] = [];
39
  public pageWithDivIds: string[] = [];
40 53600 argiro.kok
41 57073 k.triantaf
  //public errorMessage: string;
42 53600 argiro.kok
43 57073 k.triantaf
  public formGroup: FormGroup;
44 53600 argiro.kok
45 57073 k.triantaf
  private searchText: RegExp = new RegExp('');
46
  public keyword: string = '';
47 53600 argiro.kok
48 59468 konstantin
  public communities: Portal[] = [];
49 53600 argiro.kok
50 57073 k.triantaf
  public selectedCommunityPid: string;
51 53600 argiro.kok
52 57073 k.triantaf
  public pagesType: string;
53
  public properties: EnvProperties = null;
54 53600 argiro.kok
55 57073 k.triantaf
  public showLoading: boolean = true;
56
  public errorMessage: string = '';
57
  public updateErrorMessage: string = '';
58
  public modalErrorMessage: string = '';
59
  public isPortalAdministrator = null;
60 53600 argiro.kok
61 57073 k.triantaf
  constructor(private element: ElementRef, private route: ActivatedRoute,
62 58371 k.triantaf
              private title: Title,
63 57073 k.triantaf
              private _router: Router, private _helpContentService: HelpContentService,
64 59468 konstantin
              private userManagementService: UserManagementService,
65
              private _clearCacheService: ClearCacheService) {
66 57073 k.triantaf
  }
67 53600 argiro.kok
68 57073 k.triantaf
  ngOnInit() {
69
    this.formGroup = this.formComponent.form;
70
    this.route.data
71
      .subscribe((data: { envSpecific: EnvProperties }) => {
72
        this.properties = data.envSpecific;
73 54074 konstantin
74 57073 k.triantaf
        this.route.queryParams.subscribe(params => {
75
          HelperFunctions.scroll();
76 58371 k.triantaf
          this.title.setTitle('Administration Dashboard | Pages');
77 57073 k.triantaf
          this.pagesType = '';
78
          if (params['type']) {
79
            this.pagesType = params['type'];
80 58371 k.triantaf
            this.title.setTitle('Administration Dashboard | ' + StringUtils.capitalize(this.pagesType) + ' Pages');
81 57073 k.triantaf
          }
82 53984 konstantin
83 57073 k.triantaf
          this.keyword = '';
84 57947 k.triantaf
          this.userManagementService.getUserInfo().subscribe( user => {
85 57073 k.triantaf
            this.selectedCommunityPid = params['communityId'];
86
            this.applyCommunityFilter(this.selectedCommunityPid);
87
            this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.selectedCommunityPid;
88
          });
89
          //this.getCommunities();
90 53600 argiro.kok
        });
91 57073 k.triantaf
      });
92
  }
93 53600 argiro.kok
94 57073 k.triantaf
  getPages(community_pid: string) {
95
    if (!Session.isLoggedIn()) {
96
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
97
    } else {
98
      this.showLoading = true;
99
      this.updateErrorMessage = '';
100
      this.errorMessage = '';
101 53600 argiro.kok
102 57073 k.triantaf
      this.pageWithDivIds = [];
103 53600 argiro.kok
104 57073 k.triantaf
      let parameters = '';
105
      if (this.pagesType) {
106
        parameters = '?page_type=' + this.pagesType;
107 53735 konstantin
      }
108 57073 k.triantaf
      if (community_pid) {
109 59468 konstantin
        this._helpContentService.getCommunityPagesByType(community_pid, this.pagesType, this.properties.adminToolsAPIURL).subscribe(
110 57073 k.triantaf
          pages => {
111
            this.pagesReturned(pages);
112
            //if(!this.pagesType || this.pagesType == "link") {
113
            this.getPagesWithDivIds(community_pid);
114
            //} else {
115
            //this.showLoading = false;
116
            //}
117
          },
118
          error => this.handleError('System error retrieving pages', error)
119
        );
120 53735 konstantin
      } else {
121 59468 konstantin
        this._helpContentService.getAllPagesFull(this.properties.adminToolsAPIURL).subscribe(
122 56294 konstantin
          pages => {
123 57073 k.triantaf
            this.pagesReturned(pages);
124
            this.showLoading = false;
125
          },
126
          error => this.handleError('System error retrieving pages', error)
127
        );
128 53735 konstantin
      }
129 53600 argiro.kok
    }
130 57073 k.triantaf
  }
131 53600 argiro.kok
132 57073 k.triantaf
  getPagesWithDivIds(community_pid: string) {
133
    if (!Session.isLoggedIn()) {
134
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
135
    } else {
136 59468 konstantin
      this._helpContentService.getPageIdsFromDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
137 57073 k.triantaf
        pages => {
138
          this.pageWithDivIds = pages;
139
          this.showLoading = false;
140 53600 argiro.kok
        },
141 57073 k.triantaf
        error => this.handleError('System error retrieving information about pages\' classes', error));
142 53600 argiro.kok
    }
143 57073 k.triantaf
  }
144 53600 argiro.kok
145 57073 k.triantaf
  pagesReturned(pages: Page[]) {
146
    this.pages = pages;
147
    this.checkboxes = [];
148 53600 argiro.kok
149 57073 k.triantaf
    if (pages) {
150
      pages.forEach(_ => {
151
        this.checkboxes.push(<CheckPage>{page: _, checked: false});
152
      });
153 53600 argiro.kok
    }
154 57073 k.triantaf
  }
155 53600 argiro.kok
156 57073 k.triantaf
  /*
157
      getCommunities() {
158
          this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
159
              communities => {
160
                  this.communities = communities;
161
                  this.selectedCommunityPid = this.communities[0].pid;
162
                  this.getPages(this.selectedCommunityPid);
163
                  this.getPagesWithDivIds(this.selectedCommunityPid);
164
          },
165
          error => this.handleError('System error retrieving communities', error));
166
      }
167
  */
168 53600 argiro.kok
169 57073 k.triantaf
  public toggleCheckBoxes(event) {
170
    this.checkboxes.forEach(_ => _.checked = event.target.checked);
171
  }
172 53600 argiro.kok
173 57073 k.triantaf
  public applyCheck(flag: boolean) {
174
    this.checkboxes.forEach(_ => _.checked = flag);
175
  }
176 53600 argiro.kok
177 57073 k.triantaf
  public getSelectedPages(): string[] {
178
    return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
179
  }
180 53600 argiro.kok
181 57073 k.triantaf
  private deletePagesFromArray(ids: string[]): void {
182
    for (let id of ids) {
183
      let i = this.checkboxes.findIndex(_ => _.page._id == id);
184
      this.checkboxes.splice(i, 1);
185 53600 argiro.kok
    }
186 57073 k.triantaf
  }
187 53600 argiro.kok
188 57073 k.triantaf
  public confirmDeletePage(id: string) {
189
    //this.deleteConfirmationModal.ids = [id];
190
    //this.deleteConfirmationModal.showModal();
191
    this.selectedPages = [id];
192
    this.confirmModalOpen();
193
  }
194 53600 argiro.kok
195 57073 k.triantaf
  public confirmDeleteSelectedPages() {
196
    //this.deleteConfirmationModal.ids = this.getSelectedPages();
197
    //this.deleteConfirmationModal.showModal();
198
    this.selectedPages = this.getSelectedPages();
199
    this.confirmModalOpen();
200
  }
201
202
  private confirmModalOpen() {
203
    if (!Session.isLoggedIn()) {
204
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
205
    } else {
206
      this.alertModalDeletePages.cancelButton = true;
207
      this.alertModalDeletePages.okButton = true;
208
      this.alertModalDeletePages.alertTitle = 'Delete Confirmation';
209
      this.alertModalDeletePages.message = 'Are you sure you want to delete the selected page(s)?';
210
      this.alertModalDeletePages.okButtonText = 'Yes';
211
      this.alertModalDeletePages.open();
212 53600 argiro.kok
    }
213 57073 k.triantaf
  }
214 53600 argiro.kok
215 57073 k.triantaf
  public confirmedDeletePages(data: any) {
216
    if (!Session.isLoggedIn()) {
217
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
218
    } else {
219
      this.showLoading = true;
220
      this.updateErrorMessage = '';
221 53600 argiro.kok
222 57073 k.triantaf
      this._helpContentService.deletePages(this.selectedPages, this.properties.adminToolsAPIURL).subscribe(
223
        _ => {
224
          this.deletePagesFromArray(this.selectedPages);
225
          this.showLoading = false;
226 59468 konstantin
          this._clearCacheService.clearCache("pages deleted");
227 57073 k.triantaf
        },
228
        error => this.handleUpdateError('System error deleting the selected pages', error)
229
      );
230 53600 argiro.kok
    }
231 57073 k.triantaf
  }
232 53600 argiro.kok
233 57073 k.triantaf
  public editPage(i: number) {
234
    let page: Page = this.checkboxes[i].page;
235
    this.formGroup.patchValue(page);
236
    this.formComponent.setEntities(page.entities as Entity[]);
237 59468 konstantin
    this.formGroup.controls['portalType'].disable();
238 53600 argiro.kok
239 57073 k.triantaf
    //console.info(this.formGroup.value);
240
    //this.updateModal.showModal();
241
    this.modalErrorMessage = '';
242
    this.pagesModalOpen(this.alertModalUpdatePage, 'Update', 'Update Page');
243
  }
244
245
  public newPage() {
246 59468 konstantin
    this.formGroup.controls['portalType'].enable();
247 57073 k.triantaf
    this.formComponent.reset();
248
    this.modalErrorMessage = '';
249
    this.pagesModalOpen(this.alertModalSavePage, 'Save', 'Add a new Page');
250
  }
251
252
  private pagesModalOpen(modal: any, title: string, yesBtn: string) {
253
    if (!Session.isLoggedIn()) {
254
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
255
    } else {
256
      modal.cancelButton = true;
257
      modal.okButton = true;
258
      modal.alertTitle = title;
259
      modal.okButtonText = yesBtn;
260
      modal.open();
261 53600 argiro.kok
    }
262 57073 k.triantaf
  }
263 53600 argiro.kok
264 57073 k.triantaf
  public pageSaveConfirmed(data: any) {
265
    if (!Session.isLoggedIn()) {
266
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
267
    } else {
268
      if (!this.formGroup.valid) {
269
        this.pagesModalOpen(this.alertModalSavePage, 'Save', 'Add a new Page');
270
        this.modalErrorMessage = 'Please fill in all required fields marked with *';
271 53600 argiro.kok
      } else {
272 57073 k.triantaf
        this.modalErrorMessage = '';
273
        this._helpContentService.savePage(<Page>this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
274
          page => {
275
            this.pageSavedSuccessfully(page);
276 59468 konstantin
            this._clearCacheService.clearCache("page saved");
277 57073 k.triantaf
          },
278
          error => this.handleUpdateError('System error creating page', error)
279
        );
280 53600 argiro.kok
      }
281
    }
282 57073 k.triantaf
  }
283 53600 argiro.kok
284 57073 k.triantaf
  public pageUpdateConfirmed(data: any) {
285
    if (!Session.isLoggedIn()) {
286
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
287
    } else {
288
      if (!this.formGroup.valid) {
289
        this.pagesModalOpen(this.alertModalUpdatePage, 'Update', 'Update Page');
290
        this.modalErrorMessage = 'Please fill in all required fields marked with *';
291 53600 argiro.kok
      } else {
292 59468 konstantin
        this.formGroup.controls['portalType'].enable();
293 57073 k.triantaf
        this._helpContentService.updatePage(<Page>this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
294
          page => {
295
            this.pageUpdatedSuccessfully(page);
296 59468 konstantin
            this._clearCacheService.clearCache("page updated");
297 57073 k.triantaf
          },
298
          error => this.handleUpdateError('System error updating page', error)
299
        );
300 53600 argiro.kok
      }
301
    }
302 57073 k.triantaf
  }
303 53600 argiro.kok
304 57073 k.triantaf
  public pageSavedSuccessfully(page: Page) {
305
    this.checkboxes.push(<CheckPage>{page: page, checked: false});
306
    this.applyCheck(false);
307
  }
308 53600 argiro.kok
309 57073 k.triantaf
  public pageUpdatedSuccessfully(page: Page) {
310
    this.checkboxes.find(checkItem => checkItem.page._id == page._id).page = page;
311
    this.applyCheck(false);
312
  }
313 53600 argiro.kok
314 57073 k.triantaf
  public filterBySearch(text: string) {
315
    this.searchText = new RegExp(text, 'i');
316
    this.applyFilter();
317
  }
318 53600 argiro.kok
319 57073 k.triantaf
  public applyFilter() {
320
    this.checkboxes = [];
321
    this.pages.filter(item => this.filterPages(item)).forEach(
322
      _ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
323
    );
324
  }
325 53600 argiro.kok
326 57073 k.triantaf
  public filterPages(page: Page): boolean {
327 59468 konstantin
    let textFlag = this.searchText.toString() == '' || (page.route + ' ' + page.name + ' ' + page.type + ' ' + page.portalType).match(this.searchText) != null;
328 57073 k.triantaf
    return textFlag;
329
  }
330 53600 argiro.kok
331 57073 k.triantaf
  handleError(message: string, error) {
332
    // if(error == null) {
333
    //     this.formComponent.reset();
334
    // } else {
335
    this.errorMessage = message;// + ' (Server responded: ' + error + ')';
336
    console.log('Server responded: ' + error);
337
    //}
338 53600 argiro.kok
339 57073 k.triantaf
    this.showLoading = false;
340
  }
341
342
  handleUpdateError(message: string, error) {
343
    if (error == null) {
344
      this.formComponent.reset();
345
    } else {
346
      this.updateErrorMessage = message;// + ' (Server responded: ' + error + ')';
347
      console.log('Server responded: ' + error);
348 53600 argiro.kok
    }
349
350 57073 k.triantaf
    this.showLoading = false;
351
  }
352 53600 argiro.kok
353 57073 k.triantaf
  // public filterByCommunity(event: any) {
354
  //     this.selectedCommunityPid = event.target.value;
355
  //     this.applyCommunityFilter(this.selectedCommunityPid);
356
  // }
357 53600 argiro.kok
358 57073 k.triantaf
  public applyCommunityFilter(community_pid: string) {
359
    this.getPages(community_pid);
360
  }
361 53600 argiro.kok
362 57073 k.triantaf
  public togglePages(status: boolean, ids: string[]) {
363
    if (!Session.isLoggedIn()) {
364
      this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
365
    } else {
366
      this.updateErrorMessage = '';
367 53600 argiro.kok
368 57073 k.triantaf
      this._helpContentService.togglePages(this.selectedCommunityPid, ids, status, this.properties.adminToolsAPIURL).subscribe(
369
        () => {
370
          for (let id of ids) {
371
            let i = this.checkboxes.findIndex(_ => _.page._id == id);
372
            this.checkboxes[i].page.isEnabled = status;
373
          }
374
          this.applyCheck(false);
375 59468 konstantin
          this._clearCacheService.clearCache("pages toggled (status: "+status+")");
376 57073 k.triantaf
        },
377
        error => this.handleUpdateError('System error changing the status of the selected page(s)', error)
378
      );
379 53600 argiro.kok
    }
380 57073 k.triantaf
  }
381 53600 argiro.kok
382 57073 k.triantaf
  public capitalizeFirstLetter(str: string) {
383
    return str.charAt(0).toUpperCase() + str.slice(1);
384
  }
385 53600 argiro.kok
}