Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / pages / sdks / sdk-management / sdk-install.component.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Component, OnInit, Input, ViewChild, AfterViewChecked, ElementRef } from '@angular/core';
20 import { DomSanitizer } from '@angular/platform-browser';
21 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
22
23 import { AlertService } from '../../../@core-xds/services/alert.service';
24 import { SdkService, ISdk } from '../../../@core-xds/services/sdk.service';
25
26 @Component({
27   selector: 'xds-sdk-install-modal',
28   template: `
29   <div tabindex="-1">
30     <div class="modal-header">
31       SDK installation
32     </div>
33
34     <div class="modal-body row">
35       <div class="col-12 text-center">
36         Installation of <b> {{ sdk?.name }} '</b> <span [innerHTML]="instStatus"></span>
37       </div>
38       <br>
39       <br>
40       <div class="col-12 text-center">
41         <textarea rows="20" class="textarea-scroll" #scrollOutput [innerHtml]="installOutput"></textarea>
42       </div>
43       <div class="col-12 text-center">
44         <button type="button" class="btn" tabindex="1"
45         [ngClass]="(btnName=='Cancel')?'btn-default':'btn-primary'"
46         (click)="onBtnClick()">{{ btnName }}</button>
47       </div>
48     </div>
49
50     <!-- <div *ngIf="footer!=''" class="modal-footer">
51       <div class="col-12 text-center">
52       </div>
53     </div> -->
54   </div>
55   `,
56   styles: [`
57     .btn {
58       margin-top: 2em;
59       min-width: 10em;
60     }
61     .textarea-scroll {
62       font-family: monospace;
63       width: 100%;
64       overflow-y: scroll;
65   `],
66 })
67
68 export class SdkInstallComponent implements OnInit {
69   @Input() sdk;
70   @ViewChild('scrollOutput') private scrollContainer: ElementRef;
71
72   constructor(
73     private modalRef: NgbActiveModal,
74     private sanitizer: DomSanitizer,
75     private alert: AlertService,
76     private sdkSvr: SdkService,
77   ) { }
78
79   onInstallSub: any;
80   installOutput = '';
81   btnName = 'Cancel';
82   instStatus = '';
83
84   ngOnInit() {
85     this.instStatus = 'in progress...';
86
87     this.onInstallSub = this.sdkSvr.onInstall().subscribe(ev => {
88       if (ev.exited) {
89         this.btnName = 'OK';
90         this.instStatus = '<font color="green"> Done. </font>';
91
92         if (ev.code === 0) {
93           this.alert.info('SDK ' + ev.sdk.name + ' successfully installed.');
94
95         } else {
96           if (ev.sdk.lastError !== '') {
97             this.alert.error(ev.sdk.lastError);
98           } else {
99             this.alert.error('SDK ' + ev.sdk.name + ' installation failed. ' + ev.error);
100           }
101         }
102
103       } else {
104         if (ev.stdout !== '') {
105           this.installOutput += ev.stdout;
106         }
107         if (ev.stderr !== '') {
108           this.installOutput += ev.stderr;
109         }
110         this._scrollToBottom();
111       }
112     });
113   }
114
115   onBtnClick(): void {
116     this.onInstallSub.unsubscribe();
117     if (this.btnName === 'Cancel') {
118       this.btnName = 'OK';
119       this.instStatus = '<b><font color="red"> ABORTED </font></b>';
120       this.sdkSvr.abortInstall(this.sdk).subscribe(r => { }, err => this.alert.error(err));
121     } else {
122       this.modalRef.close();
123     }
124   }
125
126   private _scrollToBottom(): void {
127     try {
128       this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
129     } catch (err) { }
130   }
131 }