blob: b45df6502293f04fb3c928c66188c4d2aafca4b1 [file] [log] [blame]
xuegaof248df62019-07-15 15:16:18 +02001/*-
2 * ============LICENSE_START=======================================================
3 * ONAP CLAMP
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END============================================
19 * ===================================================================
20 *
21 */
22
23import React from 'react'
24import Select from 'react-select';
25import Button from 'react-bootstrap/Button';
26import Modal from 'react-bootstrap/Modal';
27import Form from 'react-bootstrap/Form';
28import Row from 'react-bootstrap/Row';
29import Col from 'react-bootstrap/Col';
30import FormCheck from 'react-bootstrap/FormCheck'
31import styled from 'styled-components';
sebdet493c3832019-07-15 17:26:18 +020032import LoopService from '../../../api/LoopService';
sebdetc0ec0fc2020-05-18 12:31:11 +020033import SvgGenerator from '../../loop_viewer/svg/SvgGenerator';
34import LoopCache from '../../../api/LoopCache';
xuegaof248df62019-07-15 15:16:18 +020035
36const ModalStyled = styled(Modal)`
37 background-color: transparent;
38`
39const CheckBoxStyled = styled(FormCheck.Input)`
40 margin-left:3rem;
41`
42
sebdet493c3832019-07-15 17:26:18 +020043export default class OpenLoopModal extends React.Component {
xuegaof248df62019-07-15 15:16:18 +020044 constructor(props, context) {
45 super(props, context);
46
47 this.getLoopNames = this.getLoopNames.bind(this);
sebdet493c3832019-07-15 17:26:18 +020048 this.handleOpen = this.handleOpen.bind(this);
xuegaof248df62019-07-15 15:16:18 +020049 this.handleClose = this.handleClose.bind(this);
sebdetc0ec0fc2020-05-18 12:31:11 +020050 this.handleDropDownListChange = this.handleDropDownListChange.bind(this);
Ted Humphrey083e5a22020-07-08 16:48:40 -040051 this.renderSvg = this.renderSvg.bind(this);
52 this.showReadOnly = props.showReadOnly !== undefined ? props.showReadOnly : true;
xuegaof248df62019-07-15 15:16:18 +020053 this.state = {
54 show: true,
55 chosenLoopName: '',
jingjincs3173d552020-03-04 09:11:10 +010056 loopNames: [],
sebdetc0ec0fc2020-05-18 12:31:11 +020057 loopCacheOpened: new LoopCache({})
xuegaof248df62019-07-15 15:16:18 +020058 };
59 }
sebdet493c3832019-07-15 17:26:18 +020060
sebdet2dacb9b2019-07-17 13:48:44 +020061 componentWillMount() {
sebdet493c3832019-07-15 17:26:18 +020062 this.getLoopNames();
63 }
64
xuegaof248df62019-07-15 15:16:18 +020065 handleClose() {
66 this.setState({ show: false });
sebdet493c3832019-07-15 17:26:18 +020067 this.props.history.push('/');
xuegaof248df62019-07-15 15:16:18 +020068 }
sebdet493c3832019-07-15 17:26:18 +020069
sebdetc0ec0fc2020-05-18 12:31:11 +020070 handleDropDownListChange(e) {
71 LoopService.getLoop(e.value).then(loop => {
72 this.setState({
73 chosenLoopName: e.value,
74 loopCacheOpened: new LoopCache(loop)
75 });
jingjincs3173d552020-03-04 09:11:10 +010076 });
xuegaof248df62019-07-15 15:16:18 +020077 }
sebdet493c3832019-07-15 17:26:18 +020078
xuegaof248df62019-07-15 15:16:18 +020079 getLoopNames() {
80 LoopService.getLoopNames().then(loopNames => {
sebdetaa486be2020-02-18 02:00:11 -080081 if (Object.entries(loopNames).length !== 0) {
82 const loopOptions = loopNames.filter(loopName => loopName!=='undefined').map((loopName) => { return { label: loopName, value: loopName } });
83 this.setState({ loopNames: loopOptions })
84 }
xuegaof248df62019-07-15 15:16:18 +020085 });
86 }
sebdet493c3832019-07-15 17:26:18 +020087
sebdet493c3832019-07-15 17:26:18 +020088 handleOpen() {
89 console.info("Loop " + this.state.chosenLoopName + " is chosen");
sebdetaa486be2020-02-18 02:00:11 -080090 this.handleClose();
sebdet83ce0762019-08-02 14:53:59 +020091 this.props.loadLoopFunction(this.state.chosenLoopName);
xuegaof248df62019-07-15 15:16:18 +020092 }
sebdet493c3832019-07-15 17:26:18 +020093
Ted Humphrey083e5a22020-07-08 16:48:40 -040094 renderSvg() {
95 return(
96 <SvgGenerator loopCache={this.state.loopCacheOpened} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_INSTANCE}/>
97 );
98 }
99
xuegaof248df62019-07-15 15:16:18 +0200100 render() {
101 return (
Ted Humphreydf68db22020-04-16 17:36:17 +0000102 <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
xuegaof248df62019-07-15 15:16:18 +0200103 <Modal.Header closeButton>
104 <Modal.Title>Open Model</Modal.Title>
105 </Modal.Header>
106 <Modal.Body>
107 <Form.Group as={Row} controlId="formPlaintextEmail">
Ted Humphreydf68db22020-04-16 17:36:17 +0000108 <Form.Label column sm="2">Model Name:</Form.Label>
xuegaof248df62019-07-15 15:16:18 +0200109 <Col sm="10">
sebdetc0ec0fc2020-05-18 12:31:11 +0200110 <Select onChange={this.handleDropDownListChange}
jingjincs3173d552020-03-04 09:11:10 +0100111 options={this.state.loopNames} />
xuegaof248df62019-07-15 15:16:18 +0200112 </Col>
113 </Form.Group>
Ted Humphreydf68db22020-04-16 17:36:17 +0000114 <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview">
115 <Form.Label column sm="2">Model Preview:</Form.Label>
116 <Col sm="10">
Ted Humphrey083e5a22020-07-08 16:48:40 -0400117 {this.renderSvg()}
Ted Humphreydf68db22020-04-16 17:36:17 +0000118 </Col>
sebdet493c3832019-07-15 17:26:18 +0200119 </Form.Group>
Ted Humphreydf68db22020-04-16 17:36:17 +0000120 {this.showReadOnly === true ?
sebdetc0ec0fc2020-05-18 12:31:11 +0200121 <Form.Group as={Row} controlId="formBasicCheckbox">
Ted Humphreydf68db22020-04-16 17:36:17 +0000122 <Form.Check>
123 <FormCheck.Label>Read Only Mode:</FormCheck.Label>
124 <CheckBoxStyled style={{marginLeft: '3.5em'}} type="checkbox" />
125 </Form.Check>
126 </Form.Group>
127 : null}
xuegaof248df62019-07-15 15:16:18 +0200128 </Modal.Body>
129 <Modal.Footer>
sebdet493c3832019-07-15 17:26:18 +0200130 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
sebdet5f6c1782019-07-18 15:23:13 +0200131 <Button variant="primary" type="submit" onClick={this.handleOpen}>Open</Button>
xuegaof248df62019-07-15 15:16:18 +0200132 </Modal.Footer>
133 </ModalStyled>
134
135 );
136 }
137}