aboutsummaryrefslogtreecommitdiffstats
path: root/webapp/src/app/pages/sdks/sdk-management/sdk-install.component.ts
blob: 68d87d1935e5dd1593f7e5839916ffbdd7f997e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @license
* Copyright (C) 2017-2018 "IoT.bzh"
* Author Sebastien Douheret <sebastien@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Component, OnInit, Input, ViewChild, AfterViewChecked, ElementRef } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

import { AlertService } from '../../../@core-xds/services/alert.service';
import { SdkService, ISdk } from '../../../@core-xds/services/sdk.service';

import 'rxjs/add/operator/bufferTime';

@Component({
  selector: 'xds-sdk-install-modal',
  template: `
  <div tabindex="-1">
    <div class="modal-header">
      SDK installation
    </div>

    <div class="modal-body row">
      <div class="col-12 text-center">
        Installation of <b> {{ sdk?.name }} '</b> <span [innerHTML]="instStatus"></span>
      </div>
      <br>
      <br>
      <div class="col-12 text-center">
        <textarea rows="20" class="textarea-scroll" #scrollOutput [innerHtml]="installOutput"></textarea>
      </div>
      <div class="col-12 text-center">
        <button type="button" class="btn btn-md" tabindex="1"
        [ngClass]="(btnName=='Cancel')?'btn-secondary':'btn-primary'"
        (click)="onBtnClick()">{{ btnName }}</button>
      </div>
    </div>

    <!-- <div *ngIf="footer!=''" class="modal-footer">
      <div class="col-12 text-center">
      </div>
    </div> -->
  </div>
  `,
  styles: [`
    .btn {
      margin-top: 2em;
      min-width: 10em;
    }
    .textarea-scroll {
      font-family: monospace;
      width: 100%;
      overflow-y: scroll;
  `],
})

export class SdkInstallComponent implements OnInit {
  @Input() sdk;
  @ViewChild('scrollOutput') private scrollContainer: ElementRef;

  constructor(
    private modalRef: NgbActiveModal,
    private alert: AlertService,
    private sdkSvr: SdkService,
  ) { }

  onInstallSub: any;
  installOutput = '';
  btnName = 'Cancel';
  instStatus = '';

  ngOnInit() {
    this.instStatus = 'in progress...';

    this.onInstallSub = this.sdkSvr.onInstall()
      .bufferTime(500)  // prevent browser freeze
      .subscribe(evts => {
        let out = '';
        evts.forEach(ev => {
          if (ev.exited) {
            this.btnName = 'OK';
            this.instStatus = '<font color="green"> Done. </font>';

            if (ev.code === 0) {
              this.alert.info('SDK ' + ev.sdk.name + ' successfully installed.');

            } else {
              if (ev.sdk.lastError !== '') {
                this.alert.error(ev.sdk.lastError);
              } else {
                this.alert.error('SDK ' + ev.sdk.name + ' installation failed. ' + ev.error);
              }
            }

          } else {
            if (ev.stdout !== '') {
              out += ev.stdout;
            }
            if (ev.stderr !== '') {
              out += ev.stderr;
            }
          }
        });
        if (out !== '') {
          this.installOutput += out;
          this._scrollToBottom();
        }
      });
  }

  onBtnClick(): void {
    this.onInstallSub.unsubscribe();
    if (this.btnName === 'Cancel') {
      this.btnName = 'OK';
      this.instStatus = '<b><font color="red"> ABORTED </font></b>';
      this.sdkSvr.abortInstall(this.sdk).subscribe(r => { }, err => this.alert.error(err));
    } else {
      this.modalRef.close();
    }
  }

  private _scrollToBottom(): void {
    try {
      this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
    } catch (err) { }
  }
}