xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 1 | /*- |
| 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 | |
| 23 | import React from 'react' |
| 24 | import Select from 'react-select'; |
| 25 | import Button from 'react-bootstrap/Button'; |
| 26 | import Modal from 'react-bootstrap/Modal'; |
| 27 | import Form from 'react-bootstrap/Form'; |
| 28 | import Row from 'react-bootstrap/Row'; |
| 29 | import Col from 'react-bootstrap/Col'; |
| 30 | import FormCheck from 'react-bootstrap/FormCheck' |
| 31 | import styled from 'styled-components'; |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 32 | import LoopService from '../../../api/LoopService'; |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 33 | import SvgGenerator from '../../loop_viewer/svg/SvgGenerator'; |
| 34 | import LoopCache from '../../../api/LoopCache'; |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 35 | |
| 36 | const ModalStyled = styled(Modal)` |
| 37 | background-color: transparent; |
| 38 | ` |
| 39 | const CheckBoxStyled = styled(FormCheck.Input)` |
| 40 | margin-left:3rem; |
| 41 | ` |
| 42 | |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 43 | export default class OpenLoopModal extends React.Component { |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 44 | constructor(props, context) { |
| 45 | super(props, context); |
| 46 | |
| 47 | this.getLoopNames = this.getLoopNames.bind(this); |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 48 | this.handleOpen = this.handleOpen.bind(this); |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 49 | this.handleClose = this.handleClose.bind(this); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 50 | this.handleDropDownListChange = this.handleDropDownListChange.bind(this); |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 51 | this.renderSvg = this.renderSvg.bind(this); |
| 52 | this.showReadOnly = props.showReadOnly !== undefined ? props.showReadOnly : true; |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 53 | this.state = { |
| 54 | show: true, |
| 55 | chosenLoopName: '', |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 56 | loopNames: [], |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 57 | loopCacheOpened: new LoopCache({}) |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 58 | }; |
| 59 | } |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 60 | |
sebdet | 2dacb9b | 2019-07-17 13:48:44 +0200 | [diff] [blame] | 61 | componentWillMount() { |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 62 | this.getLoopNames(); |
| 63 | } |
| 64 | |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 65 | handleClose() { |
| 66 | this.setState({ show: false }); |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 67 | this.props.history.push('/'); |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 68 | } |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 69 | |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 70 | handleDropDownListChange(e) { |
| 71 | LoopService.getLoop(e.value).then(loop => { |
| 72 | this.setState({ |
| 73 | chosenLoopName: e.value, |
| 74 | loopCacheOpened: new LoopCache(loop) |
| 75 | }); |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 76 | }); |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 77 | } |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 78 | |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 79 | getLoopNames() { |
| 80 | LoopService.getLoopNames().then(loopNames => { |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 81 | 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 | } |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 85 | }); |
| 86 | } |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 87 | |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 88 | handleOpen() { |
| 89 | console.info("Loop " + this.state.chosenLoopName + " is chosen"); |
sebdet | aa486be | 2020-02-18 02:00:11 -0800 | [diff] [blame] | 90 | this.handleClose(); |
sebdet | 83ce076 | 2019-08-02 14:53:59 +0200 | [diff] [blame] | 91 | this.props.loadLoopFunction(this.state.chosenLoopName); |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 92 | } |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 93 | |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 94 | renderSvg() { |
| 95 | return( |
| 96 | <SvgGenerator loopCache={this.state.loopCacheOpened} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_INSTANCE}/> |
| 97 | ); |
| 98 | } |
| 99 | |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 100 | render() { |
| 101 | return ( |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 102 | <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 103 | <Modal.Header closeButton> |
| 104 | <Modal.Title>Open Model</Modal.Title> |
| 105 | </Modal.Header> |
| 106 | <Modal.Body> |
| 107 | <Form.Group as={Row} controlId="formPlaintextEmail"> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 108 | <Form.Label column sm="2">Model Name:</Form.Label> |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 109 | <Col sm="10"> |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 110 | <Select onChange={this.handleDropDownListChange} |
jingjincs | 3173d55 | 2020-03-04 09:11:10 +0100 | [diff] [blame] | 111 | options={this.state.loopNames} /> |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 112 | </Col> |
| 113 | </Form.Group> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 114 | <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview"> |
| 115 | <Form.Label column sm="2">Model Preview:</Form.Label> |
| 116 | <Col sm="10"> |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 117 | {this.renderSvg()} |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 118 | </Col> |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 119 | </Form.Group> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 120 | {this.showReadOnly === true ? |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 121 | <Form.Group as={Row} controlId="formBasicCheckbox"> |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 122 | <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} |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 128 | </Modal.Body> |
| 129 | <Modal.Footer> |
sebdet | 493c383 | 2019-07-15 17:26:18 +0200 | [diff] [blame] | 130 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
sebdet | 5f6c178 | 2019-07-18 15:23:13 +0200 | [diff] [blame] | 131 | <Button variant="primary" type="submit" onClick={this.handleOpen}>Open</Button> |
xuegao | f248df6 | 2019-07-15 15:16:18 +0200 | [diff] [blame] | 132 | </Modal.Footer> |
| 133 | </ModalStyled> |
| 134 | |
| 135 | ); |
| 136 | } |
| 137 | } |