6e9b81b8e20eb2645c4415d6fc81a15f74cfe8b1
[src/xds/xds-agent.git] / webapp / src / app / pages / sdks / sdk-management / sdk-management.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, ViewEncapsulation, OnInit, isDevMode } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { LocalDataSource } from 'ng2-smart-table';
22 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
23 import { ConfirmModalComponent, EType } from '../../confirm/confirm-modal/confirm-modal.component';
24 import { SdkInstallComponent } from './sdk-install.component';
25
26 import { AlertService } from '../../../@core-xds/services/alert.service';
27 import { SdkService, ISdk, StatusType } from '../../../@core-xds/services/sdk.service';
28 import { ISdkMessage } from '../../../@core-xds/services/xdsagent.service';
29
30 interface ISdkMgt extends ISdk {
31   link: string;
32   selected: boolean;
33 }
34
35 /*
36  * FIXME / TODO:
37  *  - support install of multi SDKs  (see settings.selectMode: 'multi')
38  *  - add Uninstall button (use delete)
39  *  - add (mouseover) to display description, date, size, ...
40  */
41
42 @Component({
43   selector: 'xds-sdk-management',
44   templateUrl: 'sdk-management.component.html',
45   styleUrls: ['sdk-management.component.scss'],
46   encapsulation: ViewEncapsulation.None,
47 })
48
49 export class SdkManagementComponent implements OnInit {
50
51   sdks$: Observable<ISdk[]>;
52   sdks: ISdkMgt[];
53   source: LocalDataSource = new LocalDataSource();
54
55   settings = {
56     mode: 'external',
57     actions: {
58       add: false,
59       edit: false,
60       delete: false,  // TODO, add delete == uninstall
61       custom: [
62         { name: 'install', title: '<i class="nb-plus"></i>' },
63       ],
64     },
65     delete: {
66       deleteButtonContent: '<i class="nb-trash"></i>',
67       confirmDelete: true,
68     },
69     columns: {
70       name: { title: 'Name', editable: false },
71       profile: { title: 'Profile', editable: false, filter: {} },
72       arch: { title: 'Architecture', editable: false, filter: {} },
73       version: { title: 'Version', editable: false },
74       // TODO: add status when delete supported:
75       // status: { title: 'Status', editable: false },
76       link: { title: 'Link', editable: false, type: 'html', filter: false, width: '2%' },
77     },
78   };
79
80   constructor(
81     private alert: AlertService,
82     private sdkSvr: SdkService,
83     private modalService: NgbModal,
84   ) { }
85
86   ngOnInit() {
87     this.sdkSvr.Sdks$.subscribe(sdks => {
88       const profMap = {};
89       const archMap = {};
90       this.sdks = [];
91       sdks.forEach(s => {
92         // only display not installed SDK
93         if (s.status !== StatusType.NOT_INSTALLED) {
94           return;
95         }
96         profMap[s.profile] = s.profile;
97         archMap[s.arch] = s.arch;
98
99         const sm = <ISdkMgt>s;
100         sm.selected = false;
101         if (s.url !== '') {
102           sm.link = '<a href="' + s.url.substr(0, s.url.lastIndexOf('/')) + '" target="_blank" class="fa fa-external-link"></a>';
103         }
104         this.sdks.push(sm);
105
106       });
107
108       // Add text box filter for Profile and Arch columns
109       const profList = []; Object.keys(profMap).forEach(a => profList.push({ value: a, title: a }));
110       this.settings.columns.profile.filter = {
111         type: 'list',
112         config: { selectText: 'Select...', list: profList },
113       };
114
115       const archList = []; Object.keys(archMap).forEach(a => archList.push({ value: a, title: a }));
116       this.settings.columns.arch.filter = {
117         type: 'list',
118         config: { selectText: 'Select...', list: archList },
119       };
120
121       // update sources
122       this.source.load(this.sdks);
123
124     });
125   }
126
127   onCustom(event): void {
128     if (event.action === 'install') {
129       const sdk = <ISdkMgt>event.data;
130       const modal = this.modalService.open(ConfirmModalComponent, {
131         size: 'lg',
132         backdrop: 'static',
133         container: 'nb-layout',
134       });
135       modal.componentInstance.title = 'Confirm SDK installation';
136       modal.componentInstance.type = EType.YesNo;
137       modal.componentInstance.question = `
138       Please confirm installation of <b>` + sdk.name + `'</b> SDK ?<br>
139       <br>
140       <i><small>(size: ` + sdk.size + `, date: ` + sdk.date + `)</small></i>`;
141
142       modal.result.then(res => {
143         if (res === 'yes') {
144           const modalInstall = this.modalService.open(SdkInstallComponent, {
145             size: 'lg',
146             backdrop: 'static',
147             container: 'nb-layout',
148           });
149           modalInstall.componentInstance.sdk = sdk;
150
151           // Request installation
152           this.sdkSvr.install(sdk).subscribe(
153             r => { },
154             err => {
155               modalInstall.dismiss('SDK install failure');
156               this.alert.error(err);
157             });
158         }
159       });
160
161
162     } else if (event.action === 'uninstall') {
163       // TODO
164
165     } else {
166       /* tslint:disable:no-console */
167       if (isDevMode) {
168         console.error('onCustom: unknown event action: ', event);
169       }
170     }
171   }
172
173 }