Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from "@angular/router";
3
import {Subscriber, Subscription} from "rxjs";
4
import {OrcidService} from "./orcid.service";
5
import {ResultLandingInfo} from "../utils/entities/resultLandingInfo";
6
import {ResultLandingService} from "../landingPages/result/resultLanding.service";
7
import {properties} from "../../../environments/environment";
8
import {Session} from "../login/utils/helper.class";
9
import {LoginErrorCodes} from "../login/utils/guardHelper.class";
10
import {ExternalIDV3_0, WorkV3_0} from "./orcidWork";
11

    
12
declare var UIkit: any;
13

    
14
@Component({
15
  styles: [
16
    '.add-orcid-button { background-color: #a6ce39; color: #fff; border: 1px solid #a6ce39; } ' +
17
    '.add-orcid-button:hover { background-color: #b6d65c; border-color: #b6d65c; } ' +
18
    '.delete-orcid-button { background-color: #fff; color: #ff3d33; border: 1px solid #ff3d33 }' +
19
    '.delete-orcid-button:hover { color: #ff0d00; border-color: #ff0d00; } ' +
20
    ' a:not(:hover) .orcid-color { color: #a6ce39; }' +
21

    
22
    '.loading-action-button { width: 36px; height: 36px; }' +
23

    
24
    '.orcid-button { background-color: #fff; border: 1px solid #a6ce39; } ' +
25
    '.orcid-button:hover { background-color: #e0f0d5; } ' +
26

    
27
    'a.orcid_icon_opacity:hover > .uk-icon { opacity: 0.8; } '
28
  ],
29
  selector: 'orcid-work',
30
  template: `
31
    <ng-container *ngIf="pageType == 'search'">
32
      <span *ngIf="!putCodes || putCodes.length == 0"
33
            [attr.uk-tooltip]="(!isLoggedIn) 
34
                      ? 'Only logged in users can add or delete a work from their ORCID record '
35
                    : 'Add this work to your ORCID record'">
36
        <a (click)="saveWorkPreparation();"
37
           [class]="'uk-flex uk-flex-middle uk-flex-right uk-margin-right '+ ((showLoading || !isLoggedIn) ? 'uk-disabled ' : '') + (!isLoggedIn ? 'half-opacity' : '')">
38
  <!--         [class]="'uk-margin-right uk-flex uk-flex-middle '+ (showLoading ? 'uk-disabled' : '')">-->
39
          <svg *ngIf="!showLoading" width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"
40
               data-svg="plus">
41
            <rect x="7" y="1" width="1" height="13"></rect>
42
            <rect x="1" y="7" width="13" height="1"></rect>
43
          </svg>
44
          <span *ngIf="showLoading" class="uk-icon icon-button"><loading [top_margin]="false"></loading></span>
45
          <span class="uk-margin-small-left uk-flex uk-flex-middle">Add to 
46
            <span class="orcid-color space uk-flex uk-flex-middle">
47
              <span>ORCID</span>
48
              <img  class="space" src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}
49
            </span>
50
          </span>
51
<!--          <span class="uk-margin-small-left">Add to <span class="orcid-color">ORCID</span></span>-->
52
        </a>
53
      </span>
54

    
55
      <span *ngIf="putCodes && putCodes.length > 0"
56
            [attr.uk-tooltip]="(!isLoggedIn) 
57
                      ? 'Only logged in users can add or delete a work from their ORCID record '
58
                    : 'Delete this work from your ORCID record'">
59
        <a (click)="deleteWorks();"
60
           [class]="'uk-flex uk-flex-middle uk-flex-right uk-margin-right '+ (showLoading ? 'uk-disabled' : '')">
61
          <!--      <img src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}-->
62
<!--          <svg *ngIf="!showLoading" width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"-->
63
<!--               data-svg="minus">-->
64
<!--            <rect height="1" width="13" y="7" x="1" fill="#FF3D33"></rect>-->
65
<!--          </svg>-->
66
          <span *ngIf="!showLoading" class="uk-icon uk-preserve">
67
<!--            <svg width="14" height="14" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" data-svg="trash">-->
68
<!--              <polyline fill="none" stroke="#666" points="6.5 3 6.5 1.5 13.5 1.5 13.5 3"></polyline>-->
69
<!--              <polyline fill="none" stroke="#666" points="4.5 4 4.5 18.5 15.5 18.5 15.5 4"></polyline>-->
70
<!--              <rect x="8" y="7" width="1" height="9" color="#666"></rect>-->
71
<!--              <rect x="11" y="7" width="1" height="9" color="#666"></rect>-->
72
<!--              <rect x="2" y="3" width="16" height="1" color="#666"></rect>-->
73
<!--            </svg>-->
74
            <img src="assets/common-assets/common/bin.svg" width="13">
75
          </span>
76
          <span *ngIf="showLoading" class="uk-icon icon-button"><loading [top_margin]="false"></loading></span>
77
          <span class="uk-margin-small-left uk-flex uk-flex-middle">Delete from 
78
            <span class="orcid-color space uk-flex uk-flex-middle">
79
              <span>ORCID</span>
80
              <img class="space" src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}
81
            </span>
82
          </span>
83
        </a>
84
      </span>
85
    </ng-container>
86

    
87
    <ng-container *ngIf="pageType == 'landing'">
88
      <span *ngIf="!putCodes || putCodes.length == 0"
89
            [attr.uk-tooltip]="(!pids || !isLoggedIn) 
90
                      ? (!pids?'Only resources with a PID (persistent identifier) like DOI, handle, PMID can be added or deleted from your ORCID record'
91
                      : 'Only logged in users can add or delete a work from their ORCID record ')
92
                    : 'Add this work to your ORCID record'"
93
            [class]="(!pids || !isLoggedIn) ? 'half-opacity' : ''">
94
        <a (click)="saveWorkPreparation();"
95
           [class]="'orcid_icon_opacity uk-link-text uk-text-bold uk-text-uppercase '+ (showLoading ? 'uk-disabled' : '') + (!isLoggedIn || !pids ? 'uk-disabled uk-text-muted' : '')">
96
<!--          uk-icon-button add-orcid-button -->
97
          <span *ngIf="!showLoading" class="uk-icon">
98
<!--            <svg width="15" height="15" viewBox="0 0 15 15" xmlns="http://www.w3.org/2000/svg" data-svg="plus">-->
99
<!--              <rect x="7.5" y="1" width="1" height="15" fill="#A6CE39"></rect>-->
100
<!--              <rect x="1" y="7.5" width="15" height="1" fill="#A6CE39"></rect>-->
101
<!--            </svg>-->
102
            <img src="assets/common-assets/common/orcid_add.png" width="40" height="40">
103
          </span>
104
          <span *ngIf="showLoading" class="uk-icon icon-button loading-action-button"><loading
105
            [top_margin]="false"></loading></span>
106
          <span class="uk-margin-small-left">Add to 
107
            <span class="orcid-color">
108
<!--              <img src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}-->
109
              ORCID
110
            </span>
111
          </span>
112
<!--          <span class="uk-margin-small-left">Add to <span class="orcid-color">ORCID</span></span>-->
113
        </a>
114
      </span>
115

    
116
      <span *ngIf="putCodes && putCodes.length > 0"
117
            [attr.uk-tooltip]="(!pids || !isLoggedIn) 
118
                    ? (!pids?'Only resources with a PID (persistent identifier) like DOI, handle, PMID can be added or deleted from your ORCID record'
119
                    : 'Only logged in users can add or delete a work from their ORCID record ')
120
                  : 'Delete this work from your ORCID record'"
121
            [class]="(!pids || !isLoggedIn) ? 'half-opacity' : ''">
122
      <a (click)="deleteWorks();"
123
         [class]="'orcid_icon_opacity uk-link-text uk-text-bold uk-text-uppercase '+ (showLoading ? 'uk-disabled' : '') + (!isLoggedIn || !pids ? 'uk-disabled uk-text-muted' : '')">
124
<!--        <span *ngIf="!showLoading" class="uk-icon-button delete-orcid-button uk-icon">-->
125
<!--          <svg width="15" height="15" viewBox="0 0 15 15" xmlns="http://www.w3.org/2000/svg" data-svg="minus">-->
126
<!--            <rect height="1" width="15" y="7.5" x="1" fill="#FF3D33"></rect>-->
127
<!--          </svg>-->
128
<!--        </span>-->
129
<!--        uk-icon-button delete-orcid-button-->
130
        <span *ngIf="!showLoading" class="uk-icon uk-preserve">
131
<!--            <svg width="15" height="15" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" data-svg="trash">-->
132
<!--              <polyline fill="none" stroke="#FF3D33" points="6.5 3 6.5 1.5 13.5 1.5 13.5 3"></polyline>-->
133
<!--              <polyline fill="none" stroke="#FF3D33" points="4.5 4 4.5 18.5 15.5 18.5 15.5 4"></polyline>-->
134
<!--              <rect x="8" y="7" width="1" height="9" color="#FF3D33"></rect>-->
135
<!--              <rect x="11" y="7" width="1" height="9" color="#FF3D33"></rect>-->
136
<!--              <rect x="2" y="3" width="16" height="1" color="#FF3D33"></rect>-->
137
<!--            </svg>-->
138
          <img src="assets/common-assets/common/orcid_bin.png" width="40" height="40">
139
        </span>
140
        <span *ngIf="showLoading" class="uk-icon icon-button loading-action-button"><loading
141
          [top_margin]="false"></loading></span>
142
        <span class="uk-margin-small-left">Delete from
143
            <span class="orcid-color">
144
<!--              <img src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}-->
145
              ORCID
146
            </span>
147
          </span>
148
<!--        <span class="uk-margin-small-left">Delete from <span class="orcid-color">ORCID</span></span>-->
149
      </a>
150
    </span>
151
    </ng-container>
152

    
153
    <modal-alert #grantModal [classTitle]="'landing-modal-header uk-padding-small'"
154
                 [classBody]="'landing-modal uk-padding-removeremove'">
155
      <div class="uk-text-center uk-padding-small">
156
        <div>{{requestGrantMessage}}</div>
157

    
158
        <div class="uk-margin-medium-top uk-align-right">
159
          <button (click)="closeGrantModal()" type="submit"
160
                  class="uk-button uk-padding-small uk-padding-remove-vertical uk-button-default">
161
            <span>Cancel</span>
162
          </button>
163

    
164
          <button (click)="openGrantWindow()" type="submit"
165
                  class="uk-button uk-padding-small uk-padding-remove-vertical uk-margin-left uk-button-primary">
166
            <span>Grant OpenAIRE</span>
167
          </button>
168

    
169
<!--          <button (click)="openGrantWindow()" type="submit"-->
170
<!--                  class="uk-button uk-padding-small uk-padding-remove-vertical uk-margin-left orcid-button">-->
171
<!--            <img src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}-->
172
<!--            <span>Create or Connect your ORCID iD</span>-->
173
<!--          </button>-->
174
        </div>
175
      </div>
176
    </modal-alert>
177

    
178

    
179
    <ng-container *ngIf="pageType == 'my_search'">
180
      <span class="uk-margin-bottom uk-flex uk-flex-middle uk-flex-center">
181
        <span>ORCID</span>
182
        <img class="space" src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">
183
        <span class="space">ACTIONS</span>
184
      </span>
185
      
186
      <span [attr.uk-tooltip]="(!putCodes || putCodes.length == 0) 
187
                      ? 'This work is currently deleted.'
188
                    : 'View this work from your ORCID record'">
189
        <a (click)="currentAction='get'; getOrcidWorks()"
190
           [class]="'uk-button action uk-flex uk-flex-middle '+ ((showLoading || !putCodes || putCodes.length == 0) ? 'uk-disabled' : '')">
191
  <!--        <svg *ngIf="!showLoading" width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"-->
192
  <!--             data-svg="plus">-->
193
  <!--          <rect x="7" y="1" width="1" height="13" fill="#A6CE39"></rect>-->
194
  <!--          <rect x="1" y="7" width="13" height="1" fill="#A6CE39"></rect>-->
195
  <!--        </svg>-->
196
<!--          uk-icon="server"-->
197
          <span *ngIf="!showLoading || currentAction!='get'">
198
            <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
199
              <path d="M0 0h24v24H0z" fill="none"></path>
200
              <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z" fill="#666"></path>
201
            </svg>
202
          </span>
203
          
204
          <span *ngIf="showLoading && currentAction=='get'" class="uk-icon icon-button"><loading [top_margin]="false"></loading></span>
205
          <span class="uk-margin-small-left">View ORCID work</span>
206
<!--          <span class="uk-margin-small-left">Show <span class="orcid-color">ORCID</span> work</span>-->
207
        </a>
208
      </span>
209

    
210
      <span [attr.uk-tooltip]="(!putCodes || putCodes.length == 0) 
211
                      ? 'This work is currently deleted.'
212
                    : 'Update this work to your ORCID record'">
213
        <a (click)="currentAction='update'; updateWorkPreparation()"
214
           [class]="'uk-button action uk-margin-top uk-flex uk-flex-middle '+ ((showLoading || !putCodes || putCodes.length == 0) ? 'uk-disabled' : '')">
215
  <!--        <svg *ngIf="!showLoading" width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"-->
216
  <!--             data-svg="plus">-->
217
  <!--          <rect x="7" y="1" width="1" height="13" fill="#A6CE39"></rect>-->
218
  <!--          <rect x="1" y="7" width="13" height="1" fill="#A6CE39"></rect>-->
219
  <!--        </svg>-->
220
          <span *ngIf="!showLoading || currentAction!='update'" uk-icon="refresh"></span>
221
  
222
          <span *ngIf="showLoading && currentAction=='update'" class="uk-icon icon-button"><loading [top_margin]="false"></loading></span>
223
          <span class="uk-margin-small-left">Update ORCID work</span>
224
<!--          <span class="uk-margin-small-left">Update <span class="orcid-color">ORCID</span> work</span>-->
225
        </a>
226
      </span>
227

    
228
      <span *ngIf="!putCodes || putCodes.length == 0"
229
            uk-tooltip="Add this work to your ORCID record">
230
        <a (click)="currentAction='add'; saveWorkPreparation();"
231
           [class]="'uk-button action uk-margin-top uk-flex uk-flex-middle '+ (showLoading ? 'uk-disabled' : '')">
232
          <svg *ngIf="!showLoading || currentAction!='add'" width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"
233
               data-svg="plus">
234
            <rect x="7" y="1" width="1" height="13"></rect>
235
            <rect x="1" y="7" width="13" height="1"></rect>
236
          </svg>
237
          <span *ngIf="showLoading && currentAction=='add'" class="uk-icon icon-button"><loading [top_margin]="false"></loading></span>
238
          <span class="uk-margin-small-left">Add to ORCID</span>
239
          <!--          <span class="uk-margin-small-left">Add to <span class="orcid-color">ORCID</span></span>-->
240
        </a>
241
      </span>
242

    
243
      <span *ngIf="putCodes && putCodes.length > 0"
244
            uk-tooltip="Delete this work from your ORCID record">
245
        <a (click)="currentAction='delete'; deleteWorks();"
246
           [class]="'uk-button action uk-margin-top uk-flex uk-flex-middle '+ (showLoading ? 'uk-disabled' : '')">
247
          <!--      <img src="assets/common-assets/common/ORCIDiD_icon16x16.png" alt="">{{" "}}-->
248
          <!--          <svg *ngIf="!showLoading || currentAction!='delete'" width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg"-->
249
          <!--               data-svg="minus">-->
250
          <!--            <rect height="1" width="13" y="7" x="1" fill="#FF3D33"></rect>-->
251
          <!--          </svg>-->
252
          <span *ngIf="!showLoading || currentAction!='delete'" class="uk-icon uk-preserve">
253
<!--            <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" data-svg="trash">-->
254
            <!--              <polyline fill="none" stroke="#FF3D33" points="6.5 3 6.5 1.5 13.5 1.5 13.5 3"></polyline>-->
255
            <!--              <polyline fill="none" stroke="#FF3D33" points="4.5 4 4.5 18.5 15.5 18.5 15.5 4"></polyline>-->
256
            <!--              <rect x="8" y="7" width="1" height="9" color="#FF3D33"></rect>-->
257
            <!--              <rect x="11" y="7" width="1" height="9" color="#FF3D33"></rect>-->
258
            <!--              <rect x="2" y="3" width="16" height="1" color="#FF3D33"></rect>-->
259
            <!--            </svg>-->
260
            <img src="assets/common-assets/common/bin.svg" width="16">
261
          </span>
262

    
263
          
264
          <span *ngIf="showLoading && currentAction=='delete'" class="uk-icon icon-button"><loading [top_margin]="false"></loading></span>
265
          <span class="uk-margin-small-left">Delete from ORCID</span>
266
          <!--          <span class="uk-margin-small-left">Delete from <span class="orcid-color">ORCID</span></span>-->
267
        </a>
268
      </span>
269
    </ng-container>
270

    
271
    <modal-alert #workModal [classTitle]="'landing-modal-header uk-padding-small'"
272
                 [classBody]="'landing-modal uk-padding-remove'">
273
      <div *ngIf="orcidWorks" class="uk-padding-small">
274
        <div *ngFor="let work of orcidWorks['bulk']">
275
          <ng-container *ngTemplateOutlet="orcidWorkPreview; context:{work: work['work']}"></ng-container>
276
        </div>
277
      </div>
278
    </modal-alert>
279
    <ng-template #orcidWorkPreview1 let-work="work">
280
      {{work | json}}}
281
    </ng-template>
282

    
283
    <ng-template #orcidWorkPreview let-work="work">
284
      <div class="uk-card uk-card-default uk-padding uk-card-hover">
285
        <div>{{work.title?.title?.value}}</div>
286
        <div>{{work.title?.subtitle?.value}}</div>
287
  
288
        <div class="uk-text-uppercase">{{work['journal-title']?.value}}</div>
289
        
290
        <div *ngIf="work['publication-date'] || work['type']" class="uk-text-small uk-margin-small-bottom">
291
          <span *ngIf="work['publication-date']">
292
            <span *ngIf="work['publication-date']['year'] && work['publication-date']['year'].value">
293
              {{work['publication-date']?.year?.value}}
294
            </span>
295
            <span *ngIf="work['publication-date']['year'] && work['publication-date']['year'].value
296
                          && (
297
              (work['publication-date']['month'] && work['publication-date']['month'].value) || (work['publication-date']['day'] && work['publication-date']['day'].value))">
298
              -
299
            </span>
300
            <span *ngIf="work['publication-date']['month'] && work['publication-date']['month'].value">
301
              {{work['publication-date']?.month?.value}}
302
            </span>
303
            <span *ngIf="work['publication-date']['month'] && work['publication-date']['month'].value
304
                          && (work['publication-date']['day'] && work['publication-date']['day'].value) ">
305
              -
306
            </span>
307
            <span *ngIf="work['publication-date']['day'] && work['publication-date']['day'].value">
308
              {{work['publication-date']?.day?.value}}
309
            </span>
310
            
311
            <span *ngIf="work['type']"> | </span>
312
          </span>
313

    
314
          <span>{{work['type']}}</span>
315
        </div>
316

    
317
        <!-- Description -->
318
        <div *ngIf="work['short-description']" class="uk-margin-small-bottom multi-line-ellipsis lines-3">
319
          <p class="uk-text-muted uk-text-small">
320
            {{work['short-description']}}
321
          </p>
322
        </div>
323
        
324
        <div *ngIf="work['external-ids'] && work['external-ids']['external-id']" class="uk-margin-small-bottom">
325
          <showIdentifiers [identifiers]="parseIdentifiers(work['external-ids']['external-id'])"></showIdentifiers>
326
        </div>
327
        
328
        <div *ngIf="work['contributors'] && work['contributors']['contributor']">
329
          <div *ngFor="let contributor of work['contributors']['contributor']">
330
            <span *ngIf="contributor['credit-name'] && contributor['credit-name'].value">
331
              {{contributor['credit-name']?.value}}
332
            </span>
333
            <span *ngIf="(contributor['credit-name'] && contributor['credit-name'].value) && (
334
                  (contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-role'])
335
                  ||
336
                  (contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-sequence'])
337
                  || 
338
                  (contributor['contributor-orcid'] && contributor['contributor-orcid']['path'])
339
            )">
340
              (
341
            </span>
342
            <span *ngIf="contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-role']">
343
              {{contributor['contributor-attributes']['contributor-role']}}
344
            </span>
345
            <span *ngIf="(contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-role']) 
346
                        && (contributor['contributor-attributes']['contributor-sequence'] || (contributor['contributor-orcid'] && contributor['contributor-orcid']['path']))">
347
              ,
348
            </span>
349
            <span *ngIf="contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-sequence']">
350
              {{contributor['contributor-attributes']['contributor-sequence']}}
351
            </span>
352
            <span *ngIf="contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-sequence'] 
353
                        && contributor['contributor-orcid'] && contributor['contributor-orcid']['path']">
354
              , 
355
            </span>
356
            <span *ngIf="contributor['contributor-orcid'] && contributor['contributor-orcid']['path']">
357
              {{contributor['contributor-orcid']?.path}}
358
            </span>
359
            <span *ngIf="(contributor['credit-name'] && contributor['credit-name'].value) && (
360
                  (contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-role'])
361
            ||
362
            (contributor['contributor-attributes'] && contributor['contributor-attributes']['contributor-sequence'])
363
            ||
364
            (contributor['contributor-orcid'] && contributor['contributor-orcid']['path'])
365
            )">
366
            )
367
            </span>
368
          </div>
369
        </div>
370
      </div>
371
    </ng-template>
372

    
373
    <modal-alert #propagationModal [classTitle]="'landing-modal-header uk-padding-small'"
374
                 [classBody]="'landing-modal uk-padding-remove'">
375
      <div class="uk-text-center uk-padding-small">
376
        <div>
377
          This result in OpenAIRE is the deduplication of multiple harvested results. 
378
          You have already added {{this.putCodes?.length}} works in your ORCID record related to this result.
379
          <div *ngIf="currentAction == 'delete'">
380
            If you continue with delete action, all these works will be deleted.
381
          </div>
382
          <div *ngIf="currentAction == 'update'">
383
            If you continue with update action, all these works will be deleted and a new merged work will be added instead.
384
          </div>
385
        </div>
386

    
387
        <div class="uk-margin-medium-top uk-align-right">
388
          <button (click)="closePropagationModal()" type="submit"
389
                  class="uk-button uk-padding-small uk-padding-remove-vertical uk-button-default">
390
            <span>Cancel</span>
391
          </button>
392

    
393
          <button (click)="confirmedPropagation()" type="submit"
394
                  class="uk-button uk-padding-small uk-padding-remove-vertical uk-margin-left uk-button-primary">
395
            <span>Continue</span>
396
          </button>
397
        </div>
398
      </div>
399
    </modal-alert>
400
  `
401
})
402

    
403
export class OrcidWorkComponent {
404
  @Input() resultId: string = "";
405
  @Input() resultLandingInfo: ResultLandingInfo;
406
  @Input() pids: string = "";
407
  @Input() identifiers: Map<string, string[]>;
408
  @Input() type: string;
409
  @Input() putCodes: string[] = [];
410
  @Input() creationDates: string[] = [];
411
  @Input() updateDates: string[] = [];
412
  @Input() givenPutCode: boolean = false;
413
  @Input() pageType: string = "search";
414

    
415
  public subscriptions: Subscription[] = [];
416
  @ViewChild('workModal') workModal;
417
  @ViewChild('saveWorkModal') saveWorkModal;
418
  @ViewChild('grantModal') grantModal;
419
  @ViewChild('messageModal') messageModal;
420
  @ViewChild('propagationModal') propagationModal;
421

    
422
  private procedurePaused: string = "save";
423
  public requestGrant: boolean = false;
424
  public requestGrantMessage: string = "Please grant OpenAIRE to access and update your ORCID works.";
425
  private clientId: string = "APP-A5M3KTX6NCN67L91";
426
  private tokenUrl: string;
427
  public message: string = "";
428
  public showLoading: boolean = false;
429

    
430
  public works: any[] = [];
431
  public orcidWorks: any[] = [];
432
  public window: any;
433

    
434
  public isLoggedIn: boolean = Session.isLoggedIn();
435

    
436
  public currentAction: string = "";
437

    
438
  constructor(private route: ActivatedRoute,
439
              private _router: Router,
440
              private orcidService: OrcidService,
441
              private resultLandingService: ResultLandingService) {
442
    if(typeof document !== 'undefined') {
443
      this.tokenUrl = "https://sandbox.orcid.org/oauth/authorize?"
444
        + "client_id="+this.clientId
445
        // + "&response_type=code&scope=/activities/update"
446
        // + "&response_type=code&scope=/authenticate /activities/update /person/update /read-limited"
447
        + "&response_type=code&scope=/activities/update /read-limited"
448
        + "&redirect_uri="+location.origin+"/orcid?source=openaire";
449
    }
450
  }
451

    
452
  ngOnInit() {
453
    if(!this.givenPutCode) {
454
      this.getPutCode();
455
    }
456
  }
457

    
458
  ngOnDestroy() {
459
    this.subscriptions.forEach(subscription => {
460
      if (subscription instanceof Subscriber) {
461
        // console.debug("subscription closing...");
462
        subscription.unsubscribe();
463
      }
464
    });
465
  }
466

    
467
  public parseIdentifiers(identifiers: ExternalIDV3_0[]): Map<string, string[]> {
468
    let identifiersMap: Map<string, string[]> = new Map<string, string[]>();
469
    for(let identifier of identifiers) {
470
      if(!identifiersMap.has(identifier['external-id-type'])) {
471
        identifiersMap.set(identifier['external-id-type'], new Array<string>());
472
      }
473
      identifiersMap.get(identifier['external-id-type']).push(identifier['external-id-value']);
474
    }
475
    return identifiersMap;
476
  }
477

    
478
  openGrantWindow() {
479
    if(!Session.isLoggedIn()){
480
      //this.userValidMessage = "User session has expired. Please login again.";
481
      this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
482
    } else {
483
      // this.tokenUrl = location.origin+"/orcid";
484
      this.window = window.open(this.tokenUrl, '_blank',
485
        'location=yes,height=700,width=540,left=500,top=100,scrollbars=yes,status=yes');
486
      // this.requestGrant = false;
487
      this.closeGrantModal();
488

    
489
      // var timer = setInterval(function() {
490
      //   if(this.window.closed) {
491
      //     clearInterval(timer);
492
      //   }
493
      // }, 1000);
494

    
495
      let self = this;
496
      window.onmessage = function (ev) {
497
        if (ev.isTrusted && ev.origin !== location.origin && ev.data !== 'success')
498
          return;
499
        self.requestGrant = false;
500
        UIkit.notification({
501
          message: 'Thank you for <strong>connecting your ORCID iD</strong> with OpenAIRE!',
502
          status: 'success',
503
          timeout: 6000,
504
          pos: 'bottom-right'
505
        });
506
        if (self.procedurePaused == "save") {
507
          self.saveWorkPreparation();
508
        } else if (self.procedurePaused == "delete") {
509
          self.deleteWorks();
510
        } else if(self.procedurePaused == "update") {
511
          self.updateWorkPreparation();
512
        }
513
      };
514

    
515
      // window.addEventListener("message", (eventt) => {
516
      //   // if(event.origin !== location.origin+"/orcid")
517
      //   //   return;
518
      // }, false);
519

    
520
      // this.window.onbeforeunload = function () {
521
      //   UIkit.notification({
522
      //     message: '<strong>Thank you for connecting your ORCID iD with OpenAIRE!<strong>',
523
      //     status: 'success',
524
      //     timeout: 6000,
525
      //     pos: 'bottom-right'
526
      //   });
527
      // }
528
    }
529
  }
530

    
531
  private getPutCode() {
532
    this.subscriptions.push(this.orcidService.getPutCode(this.pids).subscribe(
533
      putCodes => {
534
        this.putCodes = putCodes;
535
      }, error => {
536

    
537
      }
538
    ))
539
  }
540

    
541
  private getResultLandingInfo(action: string) {
542
    this.subscriptions.push(this.resultLandingService.getResultLandingInfo(this.resultId, null, this.type, null, properties).subscribe(
543
      resultLandingInfo => {
544
        this.resultLandingInfo = resultLandingInfo;
545
        if (this.resultLandingInfo && this.resultLandingInfo.identifiers) {
546
          let pidsArray: string[] = [];
547
          for (let key of Array.from(this.resultLandingInfo.identifiers.keys())) {
548
            pidsArray = pidsArray.concat(this.resultLandingInfo.identifiers.get(key));
549
          }
550
          this.pids = pidsArray.join();
551
        }
552
        if(action == "save") {
553
          this.saveWork();
554
        } else if (action == "update") {
555
          this.updateWork();
556
        }
557
      },
558
      error => {
559
        this.handleError(error,
560
          "There was an error adding work with pids: "+this.pids+" to your ORCID record. Please try again later.",
561
          "save");
562
        console.error("Error getting landing info: ", error);
563
      }
564
    ))
565
  }
566

    
567
  private saveWorkPreparation() {
568
    if(!Session.isLoggedIn()){
569
      //this.userValidMessage = "User session has expired. Please login again.";
570
      this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
571
    } else {
572

    
573
      if (this.requestGrant) {
574
        this.openGrantModal("Add, delete or edit work in your ORCID record", "save");
575
      } else {
576
        this.showLoading = true;
577

    
578
        if (this.resultLandingInfo) {
579
          this.saveWork();
580
        } else {
581
          this.getResultLandingInfo("save");
582
        }
583
      }
584
    }
585
  }
586

    
587
  private saveWork() {
588
    this.subscriptions.push(this.orcidService.saveWork(this.resultLandingInfo, this.pids).subscribe(
589
      response => {
590

    
591
        // for testing only
592
        // this.openGrantModal("Add work in your ORCID record");
593
        // this.requestGrant = true;
594
        // this.requestGrantMessage = "Please grant OpenAIRE to access and update your ORCID record and works.";
595

    
596
        if(response == null) {
597
          this.handleError(null,
598
            "There was an error adding work with pids: "+this.pids+" to your ORCID record. Please try again later.",
599
            "save");
600
          console.error("Error posting landing info: null");
601
        } else {
602
          //this.work = this.orcidService.parse(this.resultLandingInfo);
603
          //this.work['put-code'] = response;
604
          // this.works.push(response);
605
          this.putCodes.push(""+response['put-code']);
606
          this.creationDates.push(response['created-date']['value']);
607
          this.updateDates.push(response['last-modified-date']['value']);
608

    
609
          // this.closeGrantModal();
610
          this.message = "You have successfully added work with pids: "+this.pids+" in your ORCID record!";
611
          // this.openMessageModal("Work added successfully");
612

    
613
          UIkit.notification({
614
            message: 'You have <strong>successfully added</strong> work with pids: <strong>'+this.pids+'</strong> in your ORCID record!',
615
            status: 'success',
616
            timeout: 6000,
617
            pos: 'bottom-right'
618
          });
619

    
620
          this.showLoading = false;
621
        }
622
      },
623
      error => {
624
        this.handleError(error,
625
          "There was an error adding work with pids: "+this.pids+" to your ORCID record. Please try again later.",
626
          "save");
627
        console.error("Error posting landing info", error);
628
      }
629
    ));
630
  }
631

    
632
  private updateWorkPreparation() {
633
    if(!Session.isLoggedIn()){
634
      //this.userValidMessage = "User session has expired. Please login again.";
635
      this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
636
    } else {
637

    
638
      if (this.requestGrant) {
639
        this.openGrantModal("Add, delete or edit work in your ORCID record", "save");
640
      } else if(this.putCodes.length > 1) {
641
        this.openPropagationModal("Update ORCID work");
642
      } else {
643
        this.showLoading = true;
644

    
645
        if (this.resultLandingInfo) {
646
          this.updateWork();
647
        } else {
648
          this.getResultLandingInfo("update");
649
        }
650
      }
651
    }
652
  }
653

    
654
  private updateWork() {
655
    this.subscriptions.push(this.orcidService.updateWork(this.resultLandingInfo, this.pids, this.putCodes[0]).subscribe(
656
      response => {
657
        //this.work = this.orcidService.parse(this.resultLandingInfo);
658
        //this.work['put-code'] = response;
659
        // this.work = response;
660

    
661
        this.updateDates[0] = response['last-modified-date'].value;
662

    
663
        UIkit.notification({
664
          message: 'You have <strong>successfully updated</strong> work with pids: <strong>'+this.pids+'</strong> in your ORCID record!',
665
          status: 'success',
666
          timeout: 6000,
667
          pos: 'bottom-right'
668
        });
669

    
670
        this.showLoading = false;
671
      },
672
      error => {
673
        this.handleError(error,
674
          "There was an error updating work with pids: "+this.pids+" to your ORCID record. Please try again later.",
675
          "update");
676
        console.error("Error updating landing info", error);
677
      }
678
    ));
679
  }
680

    
681
  public confirmedPropagation() {
682
    for(let putCode of this.putCodes)
683
    {
684
      this.deleteWorks();
685
    }
686
    if(this.currentAction == "update") {
687
      this.saveWork();
688
    }
689
  }
690

    
691
  private getOrcidWorks() {
692
    this.showLoading = true;
693
    this.subscriptions.push(this.orcidService.getOrcidWorks(this.putCodes).subscribe(
694
      (response:  WorkV3_0[]) => {
695
        this.orcidWorks = response;
696

    
697
        this.openWorkModal();
698
        this.showLoading = false;
699
      },
700
      error => {
701
        this.handleError(error,
702
          "There was an error getting work with pids: "+this.pids+" from your ORCID record. Please try again later.",
703
          "");
704
        console.error("Error getting work", error);
705
      }
706
    ));
707
  }
708

    
709

    
710
  private deleteWorks() {
711
    if(!Session.isLoggedIn()){
712
      //this.userValidMessage = "User session has expired. Please login again.";
713
      this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
714
    } else {
715

    
716
      // let put_code;// = this.testingPutCode;
717
      // if(this.work && this.work['put-code']) {
718
      //   put_code = this.work['put-code'];
719
      // }
720
      if (this.requestGrant) {
721
        this.openGrantModal("Add, delete or edit work in your ORCID record", "delete");
722
      } else if(this.putCodes.length > 1) {
723
        this.openPropagationModal("Delete ORCID work");
724
      } else {
725
        this.showLoading = true;
726

    
727
        if (!this.resultLandingInfo && this.identifiers) {
728
          let pidsArray: string[] = [];
729
          for (let key of Array.from(this.identifiers.keys())) {
730
            pidsArray = pidsArray.concat(this.identifiers.get(key));
731
          }
732
          this.pids = pidsArray.join();
733
        }
734

    
735
        this.subscriptions.push(this.orcidService.deleteWorks(this.putCodes).subscribe(
736
          deletedPutCodes => {
737
            let deletedAll: boolean = true;
738

    
739
            if (deletedPutCodes) {
740
              for (let i = 0; i < deletedPutCodes.length; i++) {
741
                let deletedPutCode = deletedPutCodes[i];
742
                if (deletedPutCode == null) {
743
                  deletedAll = false;
744
                } else {
745
                  this.putCodes.splice(i, 1);
746
                  this.creationDates.splice(i, 1);
747
                  this.updateDates.splice(i, 1);
748
                  // this.works.splice(i, 1);
749
                }
750
              }
751
            } else {
752
              deletedAll = false;
753
            }
754

    
755
            if (!deletedAll) {
756
              this.handleError(null,
757
                "There was an error deleting work with pids: "+this.pids+" from your ORCID record. Please try again later.",
758
                "delete");
759
              console.error("Error deleting landing info: null");
760
            } else {
761
              // this.closeGrantModal();
762
              this.message = "You have successfully deleted work with pids: "+this.pids+" from your ORCID record!";
763
              // this.openMessageModal("Work deleted successfully");
764
              UIkit.notification({
765
                message: 'You have <strong>successfully deleted</strong> work with pids: <strong>'+this.pids+'</strong> from your ORCID record!',
766
                status: 'success',
767
                timeout: 6000,
768
                pos: 'bottom-right'
769
              });
770
              this.showLoading = false;
771
            }
772
          },
773
          error => {
774
            this.handleError(error,
775
              "There was an error deleting work with pids: "+this.pids+" to your ORCID record. Please try again later.",
776
              "delete");
777
            console.error("Error deleting work", error);
778
          }
779
        ));
780
      }
781
    }
782
  }
783

    
784
  openGrantModal(title: string, procedurePaused: string) {
785
    this.grantModal.cancelButton = false;
786
    this.grantModal.okButton = false;
787
    this.grantModal.alertTitle = title;
788
    this.grantModal.open();
789
    this.procedurePaused = procedurePaused;
790
  }
791

    
792
  closeGrantModal() {
793
    this.grantModal.cancel();
794
  }
795

    
796
  openWorkModal() {
797
    this.workModal.cancelButton = false;
798
    this.workModal.okButton = false;
799
    this.workModal.alertTitle = "Work in your ORCID record";
800
    this.workModal.open();
801
  }
802

    
803
  closeWorkModal() {
804
    this.workModal.cancel();
805
  }
806

    
807
  openMessageModal(title: string) {
808
    this.messageModal.cancelButton = false;
809
    this.messageModal.okButton = false;
810
    this.messageModal.alertTitle = title;
811
    this.messageModal.open();
812
  }
813

    
814
  closeMessageModal() {
815
    this.messageModal.cancel();
816
  }
817

    
818
  openPropagationModal(title: string) {
819
    this.propagationModal.cancelButton = false;
820
    this.propagationModal.okButton = false;
821
    this.propagationModal.alertTitle = title;
822
    this.propagationModal.open();
823
  }
824

    
825
  closePropagationModal() {
826
    this.propagationModal.cancel();
827
  }
828

    
829
  handleError(error, errorMsg: string, procedurePaused: string) {
830
    if(error && error.status == "401") {
831
      this.openGrantModal("Add, delete or edit work in your ORCID record", procedurePaused);
832
      this.requestGrant = true;
833
      this.requestGrantMessage = "Please grant OpenAIRE to access and update your ORCID record and works. ";
834
        // + "If you have already granted OpenAIRE, you just need to login again to ORCID!";
835
    } else if(error && error.status == "403") {
836
      this.openGrantModal("Add, delete or edit work in your ORCID record", procedurePaused);
837
      this.requestGrant = true;
838
      this.requestGrantMessage = "Please login again to ORCID."
839
      // this.openGrantModal();
840
    } else {
841
      this.message = errorMsg;
842
      UIkit.notification({
843
        message: this.message,
844
        status: 'warning',
845
        timeout: 6000,
846
        pos: 'bottom-right'
847
      });
848
      // this.openMessageModal("An error occured");
849
    }
850
    this.showLoading = false;
851
  }
852
}
(1-1/5)