Add Image Path
[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       position: 'right',
61       custom: [
62         { name: 'install', title: '<button type="button">Install</button>' },
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: { type: 'list', config: {} } },
72       arch: { title: 'Architecture', editable: false, filter: { type: 'list', config: {} } },
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
88     this.sdkSvr.Sdks$.subscribe(sdks => {
89       const profMap = {};
90       const archMap = {};
91       this.sdks = [];
92
93       if (sdks.length === 0) {
94         return;
95       }
96
97       sdks.forEach(s => {
98         // only display not installed SDK
99         if (s.status !== StatusType.NOT_INSTALLED) {
100           return;
101         }
102         profMap[s.profile] = s.profile;
103         archMap[s.arch] = s.arch;
104
105         const sm = <ISdkMgt>s;
106         sm.selected = false;
107         if (s.url !== '') {
108           sm.link = '<a href="' + s.url.substr(0, s.url.lastIndexOf('/')) + '" target="_blank" class="fa fa-external-link"></a>';
109         }
110         this.sdks.push(sm);
111
112       });
113
114       // Create new reference of settings object to trig ngOnChanges event in ng2-smart-table
115       // and consequently rebuild settings grid
116       this.settings = Object.assign({}, this.settings);
117
118       // Add text box filter for Profile and Arch columns
119       const profList = [];
120       Object.keys(profMap).forEach(a => profList.push({ value: a, title: a }));
121
122       this.settings.columns.profile.filter = {
123         type: 'list',
124         config: { selectText: 'Select...', list: profList },
125       };
126
127       const archList = [];
128       Object.keys(archMap).forEach(a => archList.push({ value: a, title: a }));
129
130       this.settings.columns.arch.filter = {
131         type: 'list',
132         config: { selectText: 'Select...', list: archList },
133       };
134
135       // update sources
136       this.source.load(this.sdks);
137
138     });
139   }
140
141   onCustom(event): void {
142     if (event.action === 'install') {
143       const sdk = <ISdkMgt>event.data;
144       const modal = this.modalService.open(ConfirmModalComponent, {
145         size: 'lg',
146         backdrop: 'static',
147         container: 'nb-layout',
148       });
149       modal.componentInstance.title = 'Confirm SDK installation';
150       modal.componentInstance.type = EType.YesNo;
151       modal.componentInstance.question = `
152       Please confirm installation of <b>` + sdk.name + `'</b> SDK ?<br>
153       <br>
154       <i><small>(size: ` + sdk.size + `, date: ` + sdk.date + `)</small></i>`;
155
156       modal.result.then(res => {
157         if (res === 'yes') {
158           const modalInstall = this.modalService.open(SdkInstallComponent, {
159             size: 'lg',
160             backdrop: 'static',
161             container: 'nb-layout',
162           });
163           modalInstall.componentInstance.sdk = sdk;
164
165           // Request installation
166           this.sdkSvr.install(sdk).subscribe(
167             r => { },
168             err => {
169               modalInstall.dismiss('SDK install failure');
170               this.alert.error(err);
171             });
172         }
173       });
174
175
176     } else if (event.action === 'uninstall') {
177       // TODO
178
179     } else {
180       /* tslint:disable:no-console */
181       if (isDevMode) {
182         console.error('onCustom: unknown event action: ', event);
183       }
184     }
185   }
186
187 }