Project

General

Profile

1
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
2
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
3
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
4
import {RefineFieldResultsService} from '../../services/refineFieldResults.service';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {properties} from '../../../../environments/environment';
7
import {NumberSize, NumberUtils} from '../../utils/number-utils.class';
8
import {BehaviorSubject, Observable, Subscription, zip} from 'rxjs';
9
import {RouterHelper} from "../../utils/routerHelper.class";
10
import {HelperFunctions} from "../../utils/HelperFunctions.class";
11

    
12
export interface Numbers {
13
  publicationsSize?: NumberSize;
14
  datasetsSize?: NumberSize;
15
  datasetsLinkedSize?: NumberSize;
16
  softwareLinkedSize?: NumberSize;
17
  softwareSize?: NumberSize;
18
  otherSize?: NumberSize;
19
  fundersSize?: NumberSize;
20
  projectsSize?: NumberSize;
21
  datasourcesSize?: NumberSize;
22
}
23

    
24
type Entity = 'publication' | 'dataset' | 'software' | 'other' | 'project' | 'datasource';
25

    
26
interface Link {
27
  link: string,
28
  params?: any
29
}
30

    
31
@Component({
32
  selector: 'numbers',
33
  template: `
34
    <div *ngIf="!loading && numbers">
35
      <div class="uk-container uk-section uk-padding-remove-bottom uk-text-center">
36
        <div class="uk-grid uk-grid-large uk-child-width-1-3@m" uk-grid>
37
          <div *ngIf="numbers.fundersSize">
38
            <h3 *ngIf="links.get('project')">
39
              <a *ngIf="externalLink" [href]="links.get('project').link" target="_blank" class="uk-text-bold number">
40
                {{numbers.fundersSize.number|number}}<span class="number-size">{{numbers.fundersSize.size}}</span>
41
              </a>
42
              <a *ngIf="!externalLink" [routerLink]="links.get('project').link"
43
                 [queryParams]="links.get('project').params" class="uk-text-bold number">
44
                {{numbers.fundersSize.number|number}}<span class="number-size">{{numbers.fundersSize.size}}</span>
45
              </a>
46
            </h3>
47
            <span class="uk-text-uppercase uk-text-large">Funders</span>
48
          </div>
49
          <div *ngIf="numbers.datasourcesSize">
50
            <h3>
51
              <a *ngIf="externalLink" [href]="links.get('datasource').link" target="_blank" class="uk-text-bold number">
52
                {{numbers.datasourcesSize.number|number}}<span
53
                  class="number-size">{{numbers.datasourcesSize.size}}</span>
54
              </a>
55
              <a *ngIf="!externalLink" [routerLink]="links.get('datasource').link"
56
                 [queryParams]="links.get('datasource').params" class="uk-text-bold number">
57
                {{numbers.datasourcesSize.number|number}}<span
58
                  class="number-size">{{numbers.datasourcesSize.size}}</span>
59
              </a>
60
            </h3>
61
            <span class="uk-text-uppercase uk-text-large">Content providers</span>
62
          </div>
63
          <div *ngIf="numbers.projectsSize">
64
            <h3>
65
              <a *ngIf="externalLink" [href]="links.get('project').link" target="_blank" class="uk-text-bold number">
66
                {{numbers.projectsSize.number|number}}<span class="number-size">{{numbers.projectsSize.size}}</span>
67
              </a>
68
              <a *ngIf="!externalLink" [routerLink]="links.get('project').link"
69
                 [queryParams]="links.get('project').params" class="uk-text-bold number">
70
                {{numbers.projectsSize.number|number}}<span class="number-size">{{numbers.projectsSize.size}}</span>
71
              </a>
72
            </h3>
73
            <span class="uk-text-uppercase uk-text-large">Projects</span>
74
          </div>
75
        </div>
76
      </div>
77
      <div class="uk-section uk-margin-top" [ngClass]="backgroundClass">
78
        <div class="uk-container">
79
          <div class="uk-grid uk-flex-center uk-margin-auto-left@m uk-margin-auto-right@m uk-grid-large" uk-grid>
80
            <div *ngIf="numbers.publicationsSize" class="uk-width-1-2@m uk-flex uk-flex-center">
81
              <div class="number-width uk-flex uk-flex-column">
82
                <a *ngIf="externalLink" [href]="links.get('publication').link" target="_blank"
83
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.publicationsSize.count|number}}</a>
84
                <a *ngIf="!externalLink" [routerLink]="links.get('publication').link"
85
                   [queryParams]="links.get('publication').params"
86
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.publicationsSize.count|number}}</a>
87
                <div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
88
                  <icon name="book" ratio="1.5" [flex]="true" class="uk-margin-right" [customClass]="colorClass"></icon>
89
                  <span>publications</span>
90
                </div>
91
              </div>
92
            </div>
93
            <div *ngIf="numbers.datasetsSize" class="uk-width-1-2@m uk-flex uk-flex-center">
94
              <div class="number-width uk-flex uk-flex-column">
95
                <a *ngIf="externalLink" [href]="links.get('dataset').link" target="_blank"
96
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.datasetsSize.count|number}}</a>
97
                <a *ngIf="!externalLink" [routerLink]="links.get('dataset').link"
98
                   [queryParams]="links.get('dataset').params"
99
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.datasetsSize.count|number}}</a>
100
                <div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
101
                  <icon name="database" ratio="1.5" [flex]="true" class="uk-margin-right"
102
                        [customClass]="colorClass"></icon>
103
                  <span>research data</span>
104
                </div>
105
              </div>
106
            </div>
107
            <div *ngIf="numbers.softwareSize" class="uk-width-1-2@m uk-flex uk-flex-center">
108
              <div class="number-width uk-flex uk-flex-column">
109
                <a *ngIf="externalLink" [href]="links.get('software').link" target="_blank"
110
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.softwareSize.count|number}}</a>
111
                <a *ngIf="!externalLink" [routerLink]="links.get('software').link"
112
                   [queryParams]="links.get('software').params"
113
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.softwareSize.count|number}}</a>
114
                <div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
115
                  <icon name="cog" ratio="1.5" [flex]="true" class="uk-margin-right" [customClass]="colorClass"></icon>
116
                  <span>software</span>
117
                </div>
118
              </div>
119
            </div>
120
            <div *ngIf="numbers.otherSize" class="uk-width-1-2@m uk-flex uk-flex-center">
121
              <div class="number-width uk-flex uk-flex-column">
122
                <a *ngIf="externalLink" [href]="links.get('other').link" target="_blank"
123
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.otherSize.count|number}}</a>
124
                <a *ngIf="!externalLink" [routerLink]="links.get('other').link"
125
                   [queryParams]="links.get('other').params"
126
                   class="number uk-text-bold uk-margin-bottom uk-text-center uk-text-left@m">{{numbers.otherSize.count|number}}</a>
127
                <div class="uk-text-uppercase uk-flex uk-flex-middle uk-flex-center uk-flex-left@m">
128
                  <icon name="earth" ratio="1.5" [flex]="true" class="uk-margin-right"
129
                        [customClass]="colorClass"></icon>
130
                  <span>other research products</span>
131
                </div>
132
              </div>
133
            </div>
134
          </div>
135
        </div>
136
      </div>
137
    </div>
138
    <div *ngIf="loading" class="uk-container uk-section uk-text-center" style="min-height: 400px">
139
      <loading [color]="colorClass"></loading>
140
    </div>
141
  `,
142
})
143
export class NumbersComponent implements OnInit, OnDestroy {
144
  @Input() colorClass = 'portal-color';
145
  @Input() backgroundClass = null;
146
  /** Add a value if you want to apply refine query*/
147
  @Input() refineValue = null;
148
  /** True: Default initialization
149
   *  False: Call init method to initialize numbers */
150
  @Input() defaultInit = true;
151
  /** Add an external link for numbers link */
152
  @Input() externalLink;
153
  /** When numbers have been initialized this emitter will emitted */
154
  @Output() results: EventEmitter<Numbers> = new EventEmitter<Numbers>();
155
  public properties: EnvProperties = properties;
156
  public routerHelper: RouterHelper = new RouterHelper();
157
  public numbers: Numbers = {};
158
  public loading: boolean = true;
159
  public links: Map<Entity, Link> = new Map<Entity, Link>();
160
  private params: Map<Entity, {}> = new Map<Entity, {}>();
161
  private emptySubject: BehaviorSubject<number> = new BehaviorSubject<number>(0);
162
  private subs: any[] = [];
163
  
164
  constructor(private searchResearchResultsService: SearchResearchResultsService,
165
              private searchDataprovidersService: SearchDataprovidersService,
166
              private refineFieldResultsService: RefineFieldResultsService) {
167
    this.links = new Map<Entity, Link>();
168
    this.params = new Map<Entity, {}>();
169
    this.emptySubject = new BehaviorSubject<number>(0);
170
  }
171
  
172
  ngOnInit() {
173
    if (this.defaultInit) {
174
      this.init();
175
    }
176
    this.setLinks();
177
  }
178
  
179
  setParams() {
180
    this.params.set('publication', {type: 'publications'});
181
    this.params.set('dataset', {type: 'datasets'});
182
    this.params.set('software', {type: 'software'});
183
    this.params.set('other', {type: 'other'});
184
    this.params.set('project', {});
185
    this.params.set('datasource', {});
186
    if (this.refineValue) {
187
      this.params.forEach((value) => {
188
        value['fq'] = this.refineValue;
189
      });
190
    }
191
  }
192
  
193
  setLinks() {
194
    this.setParams();
195
    if (this.externalLink) {
196
      this.links.set('publication', {
197
        link: this.externalLink + properties.searchLinkToResults +
198
          this.routerHelper.createQueryParamsString(Object.keys(this.params.get('publication')), HelperFunctions.getValues(this.params.get('publication')))
199
      });
200
      this.links.set('dataset', {
201
        link: this.externalLink + properties.searchLinkToResults +
202
          this.routerHelper.createQueryParamsString(Object.keys(this.params.get('dataset')), HelperFunctions.getValues(this.params.get('dataset')))
203
      });
204
      this.links.set('software', {
205
        link: this.externalLink + properties.searchLinkToResults +
206
          this.routerHelper.createQueryParamsString(Object.keys(this.params.get('software')), HelperFunctions.getValues(this.params.get('software')))
207
      });
208
      this.links.set('other', {
209
        link: this.externalLink + properties.searchLinkToResults +
210
          this.routerHelper.createQueryParamsString(Object.keys(this.params.get('other')), HelperFunctions.getValues(this.params.get('other')))
211
      });
212
      this.links.set('project', {
213
        link: this.externalLink + properties.searchLinkToProjects +
214
          this.routerHelper.createQueryParamsString(Object.keys(this.params.get('project')), HelperFunctions.getValues(this.params.get('project')))
215
      });
216
      this.links.set('datasource', {
217
        link: this.externalLink + properties.searchLinkToDataProviders +
218
          this.routerHelper.createQueryParamsString(Object.keys(this.params.get('datasource')), HelperFunctions.getValues(this.params.get('datasource')))
219
      });
220
    } else {
221
      this.links.set('publication', {
222
        link: properties.searchLinkToResults,
223
        params: this.routerHelper.createQueryParams(Object.keys(this.params.get('publication')), HelperFunctions.getValues(this.params.get('publication')))
224
      });
225
      this.links.set('dataset', {
226
        link: properties.searchLinkToResults,
227
        params: this.routerHelper.createQueryParams(Object.keys(this.params.get('dataset')), HelperFunctions.getValues(this.params.get('dataset')))
228
      });
229
      this.links.set('software', {
230
        link: properties.searchLinkToResults,
231
        params: this.routerHelper.createQueryParams(Object.keys(this.params.get('software')), HelperFunctions.getValues(this.params.get('software')))
232
      });
233
      this.links.set('other', {
234
        link: properties.searchLinkToResults,
235
        params: this.routerHelper.createQueryParams(Object.keys(this.params.get('other')), HelperFunctions.getValues(this.params.get('other')))
236
      });
237
      this.links.set('project', {
238
        link: properties.searchLinkToProjects,
239
        params: this.routerHelper.createQueryParams(Object.keys(this.params.get('project')), HelperFunctions.getValues(this.params.get('project')))
240
      });
241
      this.links.set('datasource', {
242
        link: properties.searchLinkToDataProviders,
243
        params: this.routerHelper.createQueryParams(Object.keys(this.params.get('datasource')), HelperFunctions.getValues(this.params.get('datasource')))
244
      });
245
    }
246
  }
247
  
248
  init(getDatasetsLinked = false, getSoftwareLinked = false, getPublications = true, getDatasets = true,
249
       getSoftware = true, getOther = true, getProjects = true, getDataProviders = true, refineValue: string = null) {
250
    this.loading = true;
251
    if (refineValue) {
252
      this.refineValue = refineValue;
253
    }
254
    let refineParams = (this.refineValue) ? ('&fq=' + this.refineValue) : null;
255
    this.subs.push(zip(
256
      (getPublications) ? this.searchResearchResultsService.numOfSearchResults('publication', '', this.properties, refineParams) : this.empty,
257
      (getDatasets) ? this.searchResearchResultsService.numOfSearchResults('dataset', '', this.properties, refineParams) : this.empty,
258
      (getDatasetsLinked) ? this.searchResearchResultsService.numOfSearchResultsLinkedToPub("dataset", this.properties) : this.empty,
259
      (getSoftware) ? this.searchResearchResultsService.numOfSearchResults('software', '', this.properties, refineParams) : this.empty,
260
      (getSoftwareLinked) ? this.searchResearchResultsService.numOfSearchResultsLinkedToPub("software", this.properties) : this.empty,
261
      (getOther) ? this.searchResearchResultsService.numOfSearchResults('other', '', this.properties, refineParams) : this.empty,
262
      (getProjects) ? this.refineFieldResultsService.getRefineFieldsResultsByEntityName(['funder'], 'project', this.properties, refineParams) : this.empty,
263
      (getDataProviders) ? this.searchDataprovidersService.numOfSearchDataproviders('', this.properties, refineParams) : this.empty
264
    ).subscribe((data: any[]) => {
265
      if (data[0] && data[0] > 0) {
266
        this.numbers.publicationsSize = NumberUtils.roundNumber(data[0]);
267
      }
268
      if (data[1] && data[1] > 0) {
269
        this.numbers.datasetsSize = NumberUtils.roundNumber(data[1]);
270
      }
271
      if (data[2] && data[2] > 0) {
272
        this.numbers.datasetsLinkedSize = NumberUtils.roundNumber(data[2]);
273
      }
274
      if (data[3] && data[3] > 0) {
275
        this.numbers.softwareSize = NumberUtils.roundNumber(data[3]);
276
      }
277
      if (data[4] && data[4] > 0) {
278
        this.numbers.softwareLinkedSize = NumberUtils.roundNumber(data[4]);
279
      }
280
      if (data[5] && data[5] > 0) {
281
        this.numbers.otherSize = NumberUtils.roundNumber(data[5]);
282
      }
283
      if (data[6][0] && data[6][0] > 0) {
284
        this.numbers.projectsSize = NumberUtils.roundNumber(data[6][0]);
285
      }
286
      if (data[6][1] && data[6][1].length > 0 && data[6][1][0].filterId == 'funder' && data[6][1][0].values) {
287
        this.numbers.fundersSize = NumberUtils.roundNumber(data[6][1][0].values.length);
288
      }
289
      if (data[7] && data[7] > 0) {
290
        this.numbers.datasourcesSize = NumberUtils.roundNumber(data[7]);
291
      }
292
      this.results.emit(this.numbers);
293
      this.loading = false;
294
    }, err => {
295
      this.handleError('Error getting numbers', err);
296
      this.loading = false;
297
    }));
298
  }
299
  
300
  ngOnDestroy() {
301
    this.subs.forEach(sub => {
302
      if (sub instanceof Subscription) {
303
        sub.unsubscribe();
304
      }
305
    });
306
  }
307
  
308
  get empty(): Observable<number> {
309
    return this.emptySubject.asObservable();
310
  }
311
  
312
  private handleError(message: string, error) {
313
    console.error('Numbers: ' + message, error);
314
  }
315
}
(1-1/2)