Project

General

Profile

1
import {Component, OnInit, Input, ViewChild, ViewEncapsulation}                        from '@angular/core';
2
import {SimpleChanges, OnChanges}                        from '@angular/core';
3
import {FormGroup, FormArray, FormBuilder, Validators}   from '@angular/forms';
4
import {ActivatedRoute, Router}                          from '@angular/router';
5
import { Subject } from 'rxjs';
6

    
7
import { DataTableDirective } from 'angular-datatables';
8

    
9
import {HelpContentService}                              from '../../services/help-content.service';
10
import {EnvProperties}                                   from '../../openaireLibrary/utils/properties/env-properties';
11

    
12
import {ErrorCodes}                                      from '../../openaireLibrary/utils/properties/errorCodes';
13
import {SearchUtilsClass}                                from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
14
import {ZenodoCommunitiesService} from '../../openaireLibrary/connect/zenodoCommunities/zenodo-communities.service';
15
import {ZenodoCommunityInfo} from '../../openaireLibrary/connect/zenodoCommunities/zenodoCommunityInfo';
16

    
17
import {Session} from '../../openaireLibrary/login/utils/helper.class';
18
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
19
import{ManageZenodoCommunitiesService} from '../../services/manageZenodoCommunities.service';
20

    
21
@Component({
22
    selector: 'manage-zenodo-communities',
23
    templateUrl: './manage-zenodo-communities.component.html',
24
    styles: [`
25
      #dpTable_info, #dpTable_paginate,  #dpTable_length,  #dpTable_filter{
26
        display: none;
27
      }
28

    
29
      `],
30
    encapsulation: ViewEncapsulation.None // this used in order styles to work
31
})
32

    
33
export class ManageZenodoCommunitiesComponent implements OnInit {
34
    @Input() properties:EnvProperties = null;
35
    @Input() communityId = null;
36

    
37
    @Input() searchUtils:SearchUtilsClass = null;
38

    
39
    errorCodes: ErrorCodes;
40
    public rowsOnPage = 10;
41
    @Input() masterCommunity = null;
42
    @Input() selectedCommunities = [];
43

    
44
    @ViewChild(DataTableDirective) datatableElement: DataTableDirective;
45
    dtTrigger: Subject<any> = new Subject(); //necessary
46
    dtOptions: DataTables.Settings = {};
47
    private triggered: boolean = false;
48

    
49
    @ViewChild('AlertModalDeleteCommunity') alertModalDeleteCommunity;
50
    selectedToDelete = null;
51
    constructor (private route: ActivatedRoute,
52
                 private _router: Router,
53
                 public _fb: FormBuilder,
54
                 private _helpContentService: HelpContentService,
55
                 private _ΖenodoCommunitieService: ZenodoCommunitiesService,
56
               private _manageZenodoCommunitiesService: ManageZenodoCommunitiesService) {
57

    
58
       this.errorCodes = new ErrorCodes();
59

    
60
    }
61

    
62
    ngOnInit() {
63

    
64

    
65
        this.searchUtils.keyword = "";
66
        this.dtOptions = {
67
          // "paging": true,
68
          // "searching": true,
69
          // "lengthChange": false,
70
          "pageLength": this.rowsOnPage,
71
          "language": {
72
              "search": "",
73
              "searchPlaceholder": "Search projects..."
74
            }
75

    
76
        };
77
        if(!this.triggered) {
78
          this.triggerInitialLoad();
79
        } else {
80
          var table = $('#dpTable').DataTable();
81
          table.clear();
82

    
83
          this.rerender();
84
        }
85

    
86
        this.searchUtils.totalResults = this.selectedCommunities.length;
87
     }
88

    
89
     public ngOnDestroy() {
90

    
91
       $.fn['dataTable'].ext.search.pop();
92
     }
93
     rerender(): void {
94
       if(this.datatableElement.dtInstance){
95
          this.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
96
           // Destroy the table first
97
           dtInstance.destroy();
98

    
99
           // Call the dtTrigger to rerender again
100
           this.dtTrigger.next();
101
         });
102
       }
103
     }
104

    
105
     ngAfterViewInit(): void {
106
       $.fn['dataTable'].ext.search.push((settings, data, dataIndex) => {
107

    
108
          if (this.filterData(data, this.searchUtils.keyword )) {
109
            return true;
110
          }
111
          return false;
112
        });
113

    
114
        //console.info("ngAfterViewInit");
115
        this.searchUtils.totalResults = this.selectedCommunities.length;
116

    
117
     }
118

    
119
     filterData(row: any, query: string) {
120
       let returnValue: boolean = false;
121

    
122
       if(query) {
123
         for(var i=0; i <3; i++){
124
           var r= this.filterQuery(row[i], query);
125
           if(r) {
126
             returnValue = true;
127
             break;
128
           }
129
         }
130

    
131
         if(!returnValue) {
132
           return false;
133
         }
134
       }
135

    
136
       return true;
137
     }
138

    
139
     filterQuery(data, query){
140
       if(data.toLowerCase().indexOf(query.toLowerCase()) > -1){
141
         return true;
142
       }else{
143
         return false;
144
       }
145
     }
146

    
147

    
148
     /*
149
     Trigger a table draw in order to get the initial filtering
150
     */
151
     triggerInitialLoad(){
152
       this.triggered = true;
153
       //console.info("triggerInitialLoad");
154
       setTimeout(function(){
155
         var table = (<any>$('#dpTable')).DataTable();
156
         table.page( 0  ).draw( false );
157
       }, 500);
158
       this.dtTrigger.next();
159
     }
160

    
161
     goTo(page:number = 1){
162
         this.searchUtils.page=page;
163

    
164
         var table = $('#dpTable').DataTable();
165
         table.page( page - 1  ).draw( false );
166

    
167
         var info = table.page.info();
168
         this.searchUtils.totalResults = info.recordsDisplay;
169
     }
170

    
171
    public confirmedDeleteCommunity(data : any) {
172

    
173
      if(!Session.isLoggedIn()){
174
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
175
      } else {
176
         this._manageZenodoCommunitiesService.removeZCommunity(this.properties, this.communityId,this.selectedToDelete.openaireId ).subscribe(
177
          data => {
178
             var pos = -1;
179
            for(var i=0;  i< this.selectedCommunities.length;i++) {
180
               if( this.selectedCommunities[i].id == this.selectedToDelete.id) {
181
                pos= i;
182
                break;
183
              }
184
            }
185
             if(pos != -1){
186
              this.selectedCommunities.splice(pos, 1);
187
              this.searchUtils.totalResults = this.selectedCommunities.length;
188
            }
189
            this.searchUtils.totalResults = this.selectedCommunities.length;
190
            var table = $('#dpTable').DataTable();
191
            table.clear();
192
            this.rerender();
193

    
194
          },
195
          err => {
196
            console.log(err.status);
197
          }/*,
198
          () => {
199
            console.info("completed remove");
200
          }*/
201
        )
202
      }
203
    }
204

    
205
    public removeCommunity( comm) {
206
      if(!Session.isLoggedIn()){
207
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
208
      } else {
209
        this.selectedToDelete = comm;
210
        this.alertModalDeleteCommunity.cancelButton = true;
211
        this.alertModalDeleteCommunity.okButton = true;
212
        this.alertModalDeleteCommunity.alertTitle = "Remove zenodo community?";
213
        let title = "";
214
        if(comm.title) {
215
          title = comm.title;
216
        }
217

    
218

    
219
        this.alertModalDeleteCommunity.message = "Zenodo community";
220
        if(title) {
221
          this.alertModalDeleteCommunity.message += " '"+title+"' ";
222
        }
223
        this.alertModalDeleteCommunity.message += "will be removed from your community. Are you sure?";
224
        this.alertModalDeleteCommunity.okButtonText = "Yes";
225
        this.alertModalDeleteCommunity.open();
226
      }
227
    }
228

    
229

    
230
    totalPages(): number {
231
        let totalPages:any = this.searchUtils.totalResults/(this.rowsOnPage);
232
        if(!(Number.isInteger(totalPages))) {
233
            totalPages = (parseInt(totalPages, 10) + 1);
234
        }
235
        return totalPages;
236
    }
237

    
238

    
239
}
(4-4/7)