blob: af847524ae4942c4cf8e8a17b7fe815287950e02 [file] [log] [blame]
Radek Krejcid23f0df2017-08-31 16:34:49 +02001/*
2 * Schemas Inventory
3 */
4import { Component, Input, OnInit } from '@angular/core';
Radek Krejci6be087d2018-02-14 08:53:20 +01005import { Router } from '@angular/router';
Radek Krejcid23f0df2017-08-31 16:34:49 +02006import { Schema } from './schema';
Radek Krejciff245942018-08-31 15:36:32 +02007import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
8
9import {DialogueSchema} from './inventory.component';
10
Radek Krejci6be087d2018-02-14 08:53:20 +010011import { SchemasService } from '../yang/schemas.service'
Radek Krejciff245942018-08-31 15:36:32 +020012import {SocketService} from 'app/services/socket.service';
Radek Krejcid23f0df2017-08-31 16:34:49 +020013
Radek Krejci6be087d2018-02-14 08:53:20 +010014@Component( {
15 selector: 'inventorySchemas',
16 templateUrl: './schemas.component.html',
17 styleUrls: ['./inventory.component.scss']
18} )
Radek Krejcid23f0df2017-08-31 16:34:49 +020019
20export class InventorySchemasComponent implements OnInit {
Radek Krejci231cb292018-03-15 16:49:57 +010021 schemas;
Radek Krejci6be087d2018-02-14 08:53:20 +010022 addingSchema = false;
23 addingResult = -1;
Radek Krejciff245942018-08-31 15:36:32 +020024 constructor(private schemasService: SchemasService,
25 private socketService: SocketService,
26 private modalService: NgbModal,
27 private router: Router ) {
Radek Krejci2ca95a82018-07-31 09:59:49 +020028 this.schemas = [];
29 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020030
Radek Krejci6be087d2018-02-14 08:53:20 +010031 getSchemas(): void {
Jakub Man59c4ea12018-06-13 15:21:32 +020032 this.schemasService.getSchemas().subscribe( result => {this.schemas = result;});
Radek Krejci6be087d2018-02-14 08:53:20 +010033 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020034
Radek Krejci6be087d2018-02-14 08:53:20 +010035 showAddSchema() {
36 this.addingSchema = !this.addingSchema;
37 this.addingResult = -1;
38 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020039
Radek Krejciff245942018-08-31 15:36:32 +020040 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 Krejci231cb292018-03-15 16:49:57 +010047 upload(schema: File) {
48 if (!schema) {
Radek Krejci6be087d2018-02-14 08:53:20 +010049 /* do nothing */
50 return;
51 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020052
Radek Krejciff245942018-08-31 15:36:32 +020053 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 Krejci6be087d2018-02-14 08:53:20 +010063 /* upload the schema file to the server, if success the schema list is refreshed */
Radek Krejciff245942018-08-31 15:36:32 +020064 this.schemasService.addSchema(schema).subscribe(result => {
65 this.socketService.unsubscribe('getschema');
66 this.addingResult = result['success'] ? 1 : 0;
67 this.getSchemas();
68 });
Radek Krejci6be087d2018-02-14 08:53:20 +010069 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020070
Radek Krejci5a1571a2018-02-16 13:45:53 +010071 remove(key: string) {
72 this.schemasService.rmSchema(key).subscribe(
Radek Krejci6be087d2018-02-14 08:53:20 +010073 result => { if ( result['success'] ) { this.getSchemas() } } );
74 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020075
Radek Krejci6be087d2018-02-14 08:53:20 +010076 ngOnInit(): void {
77 this.getSchemas();
78 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020079
Radek Krejci5a1571a2018-02-16 13:45:53 +010080 onSelect(key: string): void {
Radek Krejci31a09d32018-08-14 12:43:35 +020081 this.schemasService.show(key)
82 .subscribe((result: object) => {
83 if (result['success']) {
84 this.router.navigateByUrl( '/netopeer/yang' );
85 }
86 });
Radek Krejci6be087d2018-02-14 08:53:20 +010087 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020088}