051b6efc29dd4e85c1067dbc7bb669f8f0ea370b
[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: ISdkMgt[];
52   source: LocalDataSource = new LocalDataSource();
53
54   settings = {
55     mode: 'external',
56     actions: {
57       add: false,
58       edit: false,
59       delete: false,  // TODO, add delete == uninstall
60       custom: [
61         { name: 'install', title: '<i class="nb-plus"></i>' },
62       ],
63     },
64     delete: {
65       deleteButtonContent: '<i class="nb-trash"></i>',
66       confirmDelete: true,
67     },
68     columns: {
69       name: { title: 'Name', editable: false },
70       profile: { title: 'Profile', editable: false, filter: { type: 'list', config: {} } },
71       arch: { title: 'Architecture', editable: false, filter: { type: 'list', config: {} } },
72       version: { title: 'Version', editable: false },
73       // TODO: add status when delete supported:
74       // status: { title: 'Status', editable: false },
75       link: { title: 'Link', editable: false, type: 'html', filter: false, width: '2%' },
76     },
77   };
78
79   constructor(
80     private alert: AlertService,
81     private sdkSvr: SdkService,
82     private modalService: NgbModal,
83   ) { }
84
85   ngOnInit() {
86
87     this.sdkSvr.Sdks$.subscribe(sdks => {
88       const profMap = {};
89       const archMap = {};
90       this.sdks = [];
91
92       if (sdks.length === 0) {
93         return;
94       }
95
96       sdks.forEach(s => {
97         // only display not installed SDK
98         if (s.status !== StatusType.NOT_INSTALLED) {
99           return;
100         }
101         profMap[s.profile] = s.profile;
102         archMap[s.arch] = s.arch;
103
104         const sm = <ISdkMgt>s;
105         sm.selected = false;
106         if (s.url !== '') {
107           sm.link = '<a href="' + s.url.substr(0, s.url.lastIndexOf('/')) + '" target="_blank" class="fa fa-external-link"></a>';
108         }
109         this.sdks.push(sm);
110
111       });
112
113       // Create new reference of settings object to trig ngOnChanges event in ng2-smart-table
114       // and consequently rebuild settings grid
115       this.settings = Object.assign({}, this.settings);
116
117       // Add text box filter for Profile and Arch columns
118       const profList = [];
119       Object.keys(profMap).forEach(a => profList.push({ value: a, title: a }));
120
121       this.settings.columns.profile.filter = {
122         type: 'list',
123         config: { selectText: 'Select...', list: profList },
124       };
125
126       const archList = [];
127       Object.keys(archMap).forEach(a => archList.push({ value: a, title: a }));
128
129       this.settings.columns.arch.filter = {
130         type: 'list',
131         config: { selectText: 'Select...', list: archList },
132       };
133
134       // update sources
135       this.source.load(this.sdks);
136
137     });
138   }
139
140   onCustom(event): void {
141     if (event.action === 'install') {
142       const sdk = <ISdkMgt>event.data;
143       const modal = this.modalService.open(ConfirmModalComponent, {
144         size: 'lg',
145         backdrop: 'static',
146         container: 'nb-layout',
147       });
148       modal.componentInstance.title = 'Confirm SDK installation';
149       modal.componentInstance.type = EType.YesNo;
150       modal.componentInstance.question = `
151       Please confirm installation of <b>` + sdk.name + `'</b> SDK ?<br>
152       <br>
153       <i><small>(size: ` + sdk.size + `, date: ` + sdk.date + `)</small></i>`;
154
155       modal.result.then(res => {
156         if (res === 'yes') {
157           const modalInstall = this.modalService.open(SdkInstallComponent, {
158             size: 'lg',
159             backdrop: 'static',
160             container: 'nb-layout',
161           });
162           modalInstall.componentInstance.sdk = sdk;
163
164           // Request installation
165           this.sdkSvr.install(sdk).subscribe(
166             r => { },
167             err => {
168               modalInstall.dismiss('SDK install failure');
169               this.alert.error(err);
170             });
171         }
172       });
173
174
175     } else if (event.action === 'uninstall') {
176       // TODO
177
178     } else {
179       /* tslint:disable:no-console */
180       if (isDevMode) {
181         console.error('onCustom: unknown event action: ', event);
182       }
183     }
184   }
185
186 }