Fix button visibility issue
[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 { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
21
22 import { AlertService } from '../../../@core-xds/services/alert.service';
23 import { SdkService, ISdk } from '../../../@core-xds/services/sdk.service';
24
25 import 'rxjs/add/operator/bufferTime';
26
27 @Component({
28   selector: 'xds-sdk-install-modal',
29   template: `
30   <div tabindex="-1">
31     <div class="modal-header">
32       SDK installation
33     </div>
34
35     <div class="modal-body row">
36       <div class="col-12 text-center">
37         Installation of <b> {{ sdk?.name }} '</b> <span [innerHTML]="instStatus"></span>
38       </div>
39       <br>
40       <br>
41       <div class="col-12 text-center">
42         <textarea rows="20" class="textarea-scroll" #scrollOutput [innerHtml]="installOutput"></textarea>
43       </div>
44       <div class="col-12 text-center">
45         <button type="button" class="btn btn-md" tabindex="1"
46         [ngClass]="(btnName=='Cancel')?'btn-secondary':'btn-primary'"
47         (click)="onBtnClick()">{{ btnName }}</button>
48       </div>
49     </div>
50
51     <!-- <div *ngIf="footer!=''" class="modal-footer">
52       <div class="col-12 text-center">
53       </div>
54     </div> -->
55   </div>
56   `,
57   styles: [`
58     .btn {
59       margin-top: 2em;
60       min-width: 10em;
61     }
62     .textarea-scroll {
63       font-family: monospace;
64       width: 100%;
65       overflow-y: scroll;
66   `],
67 })
68
69 export class SdkInstallComponent implements OnInit {
70   @Input() sdk;
71   @ViewChild('scrollOutput') private scrollContainer: ElementRef;
72
73   constructor(
74     private modalRef: NgbActiveModal,
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()
88       .bufferTime(500)  // prevent browser freeze
89       .subscribe(evts => {
90         let out = '';
91         evts.forEach(ev => {
92           if (ev.exited) {
93             this.btnName = 'OK';
94             this.instStatus = '<font color="green"> Done. </font>';
95
96             if (ev.code === 0) {
97               this.alert.info('SDK ' + ev.sdk.name + ' successfully installed.');
98
99             } else {
100               if (ev.sdk.lastError !== '') {
101                 this.alert.error(ev.sdk.lastError);
102               } else {
103                 this.alert.error('SDK ' + ev.sdk.name + ' installation failed. ' + ev.error);
104               }
105             }
106
107           } else {
108             if (ev.stdout !== '') {
109               out += ev.stdout;
110             }
111             if (ev.stderr !== '') {
112               out += ev.stderr;
113             }
114           }
115         });
116         if (out !== '') {
117           this.installOutput += out;
118           this._scrollToBottom();
119         }
120       });
121   }
122
123   onBtnClick(): void {
124     this.onInstallSub.unsubscribe();
125     if (this.btnName === 'Cancel') {
126       this.btnName = 'OK';
127       this.instStatus = '<b><font color="red"> ABORTED </font></b>';
128       this.sdkSvr.abortInstall(this.sdk).subscribe(r => { }, err => this.alert.error(err));
129     } else {
130       this.modalRef.close();
131     }
132   }
133
134   private _scrollToBottom(): void {
135     try {
136       this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
137     } catch (err) { }
138   }
139 }