aboutsummaryrefslogtreecommitdiffstats
path: root/webapp/src/app/pages/sdks/sdk-management/sdk-management.component.ts
blob: 051b6efc29dd4e85c1067dbc7bb669f8f0ea370b (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @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, ViewEncapsulation, OnInit, isDevMode } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { LocalDataSource } from 'ng2-smart-table';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ConfirmModalComponent, EType } from '../../confirm/confirm-modal/confirm-modal.component';
import { SdkInstallComponent } from './sdk-install.component';

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

interface ISdkMgt extends ISdk {
  link: string;
  selected: boolean;
}

/*
 * FIXME / TODO:
 *  - support install of multi SDKs  (see settings.selectMode: 'multi')
 *  - add Uninstall button (use delete)
 *  - add (mouseover) to display description, date, size, ...
 */

@Component({
  selector: 'xds-sdk-management',
  templateUrl: 'sdk-management.component.html',
  styleUrls: ['sdk-management.component.scss'],
  encapsulation: ViewEncapsulation.None,
})

export class SdkManagementComponent implements OnInit {

  sdks: ISdkMgt[];
  source: LocalDataSource = new LocalDataSource();

  settings = {
    mode: 'external',
    actions: {
      add: false,
      edit: false,
      delete: false,  // TODO, add delete == uninstall
      custom: [
        { name: 'install', title: '<i class="nb-plus"></i>' },
      ],
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
    columns: {
      name: { title: 'Name', editable: false },
      profile: { title: 'Profile', editable: false, filter: { type: 'list', config: {} } },
      arch: { title: 'Architecture', editable: false, filter: { type: 'list', config: {} } },
      version: { title: 'Version', editable: false },
      // TODO: add status when delete supported:
      // status: { title: 'Status', editable: false },
      link: { title: 'Link', editable: false, type: 'html', filter: false, width: '2%' },
    },
  };

  constructor(
    private alert: AlertService,
    private sdkSvr: SdkService,
    private modalService: NgbModal,
  ) { }

  ngOnInit() {

    this.sdkSvr.Sdks$.subscribe(sdks => {
      const profMap = {};
      const archMap = {};
      this.sdks = [];

      if (sdks.length === 0) {
        return;
      }

      sdks.forEach(s => {
        // only display not installed SDK
        if (s.status !== StatusType.NOT_INSTALLED) {
          return;
        }
        profMap[s.profile] = s.profile;
        archMap[s.arch] = s.arch;

        const sm = <ISdkMgt>s;
        sm.selected = false;
        if (s.url !== '') {
          sm.link = '<a href="' + s.url.substr(0, s.url.lastIndexOf('/')) + '" target="_blank" class="fa fa-external-link"></a>';
        }
        this.sdks.push(sm);

      });

      // Create new reference of settings object to trig ngOnChanges event in ng2-smart-table
      // and consequently rebuild settings grid
      this.settings = Object.assign({}, this.settings);

      // Add text box filter for Profile and Arch columns
      const profList = [];
      Object.keys(profMap).forEach(a => profList.push({ value: a, title: a }));

      this.settings.columns.profile.filter = {
        type: 'list',
        config: { selectText: 'Select...', list: profList },
      };

      const archList = [];
      Object.keys(archMap).forEach(a => archList.push({ value: a, title: a }));

      this.settings.columns.arch.filter = {
        type: 'list',
        config: { selectText: 'Select...', list: archList },
      };

      // update sources
      this.source.load(this.sdks);

    });
  }

  onCustom(event): void {
    if (event.action === 'install') {
      const sdk = <ISdkMgt>event.data;
      const modal = this.modalService.open(ConfirmModalComponent, {
        size: 'lg',
        backdrop: 'static',
        container: 'nb-layout',
      });
      modal.componentInstance.title = 'Confirm SDK installation';
      modal.componentInstance.type = EType.YesNo;
      modal.componentInstance.question = `
      Please confirm installation of <b>` + sdk.name + `'</b> SDK ?<br>
      <br>
      <i><small>(size: ` + sdk.size + `, date: ` + sdk.date + `)</small></i>`;

      modal.result.then(res => {
        if (res === 'yes') {
          const modalInstall = this.modalService.open(SdkInstallComponent, {
            size: 'lg',
            backdrop: 'static',
            container: 'nb-layout',
          });
          modalInstall.componentInstance.sdk = sdk;

          // Request installation
          this.sdkSvr.install(sdk).subscribe(
            r => { },
            err => {
              modalInstall.dismiss('SDK install failure');
              this.alert.error(err);
            });
        }
      });


    } else if (event.action === 'uninstall') {
      // TODO

    } else {
      /* tslint:disable:no-console */
      if (isDevMode) {
        console.error('onCustom: unknown event action: ', event);
      }
    }
  }

}