Adding Prettier and fixing up eslint version
[sdc.git] / dox-sequence-diagram-ui / src / main / webapp / lib / ecomp / asdc / sequencer / Sequencer.jsx
1 /*!
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 import React from 'react';
18 import PropTypes from 'prop-types';
19 import Application from './components/application/Application';
20 import Common from './common/Common';
21 import Options from './common/Options';
22 import Model from './model/Model';
23 import Metamodel from './model/Metamodel';
24 import Metamodels from './model/Metamodels';
25 import Scenarios from './model/demo/scenarios/Scenarios';
26 import '../../../../res/sdc-sequencer.scss';
27 /**
28  * ASDC Sequencer entry point.
29  */
30 class Sequencer extends React.Component {
31     // //////////////////////////////////////////////////////////////////////////////////////////////
32
33     constructor(props, context) {
34         super(props, context);
35
36         this.setMetamodel.bind(this);
37         this.setModel.bind(this);
38         this.getModel.bind(this);
39         this.getMetamodel.bind(this);
40         this.getSVG.bind(this);
41         this.getDemoScenarios.bind(this);
42         this.newModel.bind(this);
43
44         // Parse options.
45
46         this.options = new Options(props.options);
47
48         // Default scenarios.
49
50         const scenarios = this.getDemoScenarios();
51         this.setMetamodel(scenarios.getMetamodels());
52
53         // this.setModel(scenarios.getBlank());
54         this.setModel(scenarios.getDimensions());
55         // this.setModel(scenarios.getECOMP());
56     }
57
58     // //////////////////////////////////////////////////////////////////////////////////////////////
59
60     /**
61      * Optionally save known metamodels so that subsequent loading and unloading
62      * of models needn't include the corresponding metamodel.
63      * @param metamodels array of conformant metamodel JSON definitions.
64      * @return this.
65      */
66     setMetamodel(metamodels) {
67         Common.assertType(metamodels, 'Array');
68         this.metamodels = new Metamodels(metamodels);
69         return this;
70     }
71
72     // //////////////////////////////////////////////////////////////////////////////////////////////
73
74     /**
75      * Set current diagram.
76      * @param modelJSON JSON diagram spec.
77      * @param metamodelIdOrDefinition optional metamodel definition or reference. Defaults to
78      * the model's metadata @ref, or the default (permissive) metamodel.
79      * @return this.
80      */
81     setModel(modelJSON, metamodelIdOrDefinition) {
82         Common.assertType(modelJSON, 'Object');
83         const ref = modelJSON.metadata ? modelJSON.metadata.ref : undefined;
84         const metamodel = this.getMetamodel(metamodelIdOrDefinition || ref);
85         Common.assertInstanceOf(metamodel, Metamodel);
86         this.model = new Model(modelJSON, metamodel);
87         if (this.application) {
88             this.application.setModel(this.model);
89         }
90         return this;
91     }
92
93     // //////////////////////////////////////////////////////////////////////////////////////////////
94
95     /**
96      * Get current diagram state. At any given instant the diagram might not make *sense*
97      * but it should always be syntactically valid.
98      * @return current Model.
99      */
100     getModel() {
101         if (this.application) {
102             const model = this.application.getModel();
103             if (model) {
104                 return model.unwrap();
105             }
106         }
107
108         return this.model;
109     }
110
111     // //////////////////////////////////////////////////////////////////////////////////////////////
112
113     /**
114      * Extract SVG element.
115      * @return stringified SVG element.
116      */
117     getSVG() {
118         return this.application.getSVG();
119     }
120
121     // //////////////////////////////////////////////////////////////////////////////////////////////
122
123     /**
124      * Get demo scenarios, allowing initialization in demo mode from the outside.
125      * @returns {Scenarios}
126      */
127     getDemoScenarios() {
128         return new Scenarios();
129     }
130
131     // //////////////////////////////////////////////////////////////////////////////////////////////
132
133     /**
134      * Create new model.
135      * @param metamodelIdOrDefinition
136      * @return newly-created model.
137      */
138     newModel(metamodelIdOrDefinition) {
139         const metamodel = this.getMetamodel(metamodelIdOrDefinition);
140         Common.assertInstanceOf(metamodel, Metamodel);
141         const model = new Model({}, metamodel);
142         if (this.application) {
143             this.application.setModel(model);
144         }
145         return model;
146     }
147
148     // //////////////////////////////////////////////////////////////////////////////////////////////
149
150     /**
151      * Get Metamodel instance corresponding to an ID or JSON definition.
152      * @param metamodelIdOrDefinition String ID or JSON definition.
153      * @returns Metamodel instance.
154      * @private
155      */
156     getMetamodel(metamodelIdOrDefinition) {
157         const metamodelType = Common.getType(metamodelIdOrDefinition);
158         if (metamodelType === 'Object') {
159             return new Metamodel(metamodelIdOrDefinition);
160         }
161         return this.metamodels.getMetamodelOrDefault(metamodelIdOrDefinition);
162     }
163
164     // //////////////////////////////////////////////////////////////////////////////////////////////
165
166     /**
167      * Render current diagram state.
168      */
169     render() {
170         if (this.props.model) {
171             // If a model was specified as a property, apply it. Otherwise
172             // fall back to the demo model.
173
174             const scenarios = this.getDemoScenarios();
175             const metamodel = [
176                 scenarios.getBlankMetamodel(),
177                 scenarios.getECOMPMetamodel()
178             ];
179             if (this.props.metamodel) {
180                 metamodel.push(this.props.metamodel);
181             }
182             this.setMetamodel(metamodel);
183             this.setModel(this.props.model);
184         }
185
186         return (
187             <Application
188                 options={this.options}
189                 sequencer={this}
190                 ref={a => {
191                     this.application = a;
192                 }}
193             />
194         );
195     }
196 }
197
198 Sequencer.propTypes = {
199     options: PropTypes.object.isRequired,
200     model: PropTypes.object,
201     metamodel: PropTypes.object
202 };
203
204 export default Sequencer;