Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Schemas Inventory |
| 3 | */ |
| 4 | import { Component, Input, OnInit } from '@angular/core'; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 5 | import { Router } from '@angular/router'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 6 | import { Schema } from './schema'; |
Radek Krejci | ff24594 | 2018-08-31 15:36:32 +0200 | [diff] [blame] | 7 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| 8 | |
| 9 | import {DialogueSchema} from './inventory.component'; |
| 10 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 11 | import { SchemasService } from '../yang/schemas.service' |
Radek Krejci | ff24594 | 2018-08-31 15:36:32 +0200 | [diff] [blame] | 12 | import {SocketService} from 'app/services/socket.service'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 13 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 14 | @Component( { |
| 15 | selector: 'inventorySchemas', |
| 16 | templateUrl: './schemas.component.html', |
| 17 | styleUrls: ['./inventory.component.scss'] |
| 18 | } ) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 19 | |
| 20 | export class InventorySchemasComponent implements OnInit { |
Radek Krejci | 231cb29 | 2018-03-15 16:49:57 +0100 | [diff] [blame] | 21 | schemas; |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 22 | addingSchema = false; |
| 23 | addingResult = -1; |
Radek Krejci | ff24594 | 2018-08-31 15:36:32 +0200 | [diff] [blame] | 24 | constructor(private schemasService: SchemasService, |
| 25 | private socketService: SocketService, |
| 26 | private modalService: NgbModal, |
| 27 | private router: Router ) { |
Radek Krejci | 2ca95a8 | 2018-07-31 09:59:49 +0200 | [diff] [blame] | 28 | this.schemas = []; |
| 29 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 30 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 31 | getSchemas(): void { |
Jakub Man | 59c4ea1 | 2018-06-13 15:21:32 +0200 | [diff] [blame] | 32 | this.schemasService.getSchemas().subscribe( result => {this.schemas = result;}); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 33 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 34 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 35 | showAddSchema() { |
| 36 | this.addingSchema = !this.addingSchema; |
| 37 | this.addingResult = -1; |
| 38 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 39 | |
Radek Krejci | ff24594 | 2018-08-31 15:36:32 +0200 | [diff] [blame] | 40 | socketAnswer(event: string, id:string, item: string, value: any, item2: string, value2: any) { |
| 41 | let data = {'id': id}; |
| 42 | data[item] = value; |
| 43 | data[item2] = value2 |
| 44 | this.socketService.send(event, data); |
| 45 | } |
| 46 | |
Radek Krejci | 231cb29 | 2018-03-15 16:49:57 +0100 | [diff] [blame] | 47 | upload(schema: File) { |
| 48 | if (!schema) { |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 49 | /* do nothing */ |
| 50 | return; |
| 51 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 52 | |
Radek Krejci | ff24594 | 2018-08-31 15:36:32 +0200 | [diff] [blame] | 53 | this.socketService.subscribe('getschema').subscribe((message: any) => { |
| 54 | let modalRef = this.modalService.open(DialogueSchema, {centered: true, backdrop: 'static', keyboard: false}); |
| 55 | modalRef.componentInstance.info = message; |
| 56 | modalRef.result.then((result) => { |
| 57 | this.socketAnswer('getschema_result', message['id'], 'filename', result['filename'], 'data', result['data']); |
| 58 | }, (reason) => { |
| 59 | this.socketAnswer('getschema_result', message['id'], 'filename', '', 'data', ''); |
| 60 | }); |
| 61 | }); |
| 62 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 63 | /* upload the schema file to the server, if success the schema list is refreshed */ |
Radek Krejci | ff24594 | 2018-08-31 15:36:32 +0200 | [diff] [blame] | 64 | this.schemasService.addSchema(schema).subscribe(result => { |
| 65 | this.socketService.unsubscribe('getschema'); |
| 66 | this.addingResult = result['success'] ? 1 : 0; |
| 67 | this.getSchemas(); |
| 68 | }); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 69 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 70 | |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame] | 71 | remove(key: string) { |
| 72 | this.schemasService.rmSchema(key).subscribe( |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 73 | result => { if ( result['success'] ) { this.getSchemas() } } ); |
| 74 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 75 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 76 | ngOnInit(): void { |
| 77 | this.getSchemas(); |
| 78 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 79 | |
Radek Krejci | 5a1571a | 2018-02-16 13:45:53 +0100 | [diff] [blame] | 80 | onSelect(key: string): void { |
Radek Krejci | 31a09d3 | 2018-08-14 12:43:35 +0200 | [diff] [blame] | 81 | this.schemasService.show(key) |
| 82 | .subscribe((result: object) => { |
| 83 | if (result['success']) { |
| 84 | this.router.navigateByUrl( '/netopeer/yang' ); |
| 85 | } |
| 86 | }); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 87 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 88 | } |