Add new code new version
[sdc.git] / dox-sequence-diagram-ui / src / main / webapp / lib / ecomp / asdc / sequencer / Sequencer.jsx
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 Application from './components/application/Application';
19 import Common from './common/Common';
20 import Options from './common/Options';
21 import Model from './model/Model';
22 import Metamodel from './model/Metamodel';
23 import Metamodels from './model/Metamodels';
24 import Scenarios from './model/demo/scenarios/Scenarios';
25 import '../../../../res/sdc-sequencer.scss';
26 /**
27  * ASDC Sequencer entry point.
28  */
29 export default class Sequencer extends React.Component {
30
31   // //////////////////////////////////////////////////////////////////////////////////////////////
32
33   constructor(props, context) {
34     super(props, context);
35
36
37     this.setMetamodel.bind(this);
38     this.setModel.bind(this);
39     this.getModel.bind(this);
40     this.getMetamodel.bind(this);
41     this.getSVG.bind(this);
42     this.getDemoScenarios.bind(this);
43     this.newModel.bind(this);
44
45     // Parse options.
46
47     this.options = new Options(props.options);
48
49     // Default scenarios.
50
51     const scenarios = this.getDemoScenarios();
52     this.setMetamodel(scenarios.getMetamodels());
53
54     // this.setModel(scenarios.getBlank());
55     this.setModel(scenarios.getDimensions());
56     // this.setModel(scenarios.getECOMP());
57
58   }
59
60   // //////////////////////////////////////////////////////////////////////////////////////////////
61
62   /**
63    * Optionally save known metamodels so that subsequent loading and unloading
64    * of models needn't include the corresponding metamodel.
65    * @param metamodels array of conformant metamodel JSON definitions.
66    * @return this.
67    */
68   setMetamodel(metamodels) {
69     Common.assertType(metamodels, 'Array');
70     this.metamodels = new Metamodels(metamodels);
71     return this;
72   }
73
74   // //////////////////////////////////////////////////////////////////////////////////////////////
75
76   /**
77    * Set current diagram.
78    * @param modelJSON JSON diagram spec.
79    * @param metamodelIdOrDefinition optional metamodel definition or reference. Defaults to
80    * the model's metadata @ref, or the default (permissive) metamodel.
81    * @return this.
82    */
83   setModel(modelJSON, metamodelIdOrDefinition) {
84     Common.assertType(modelJSON, 'Object');
85     const ref = (modelJSON.metadata) ? modelJSON.metadata.ref : undefined;
86     const metamodel = this.getMetamodel(metamodelIdOrDefinition || ref);
87     Common.assertInstanceOf(metamodel, Metamodel);
88     this.model = new Model(modelJSON, metamodel);
89     if (this.application) {
90       this.application.setModel(this.model);
91     }
92     return this;
93   }
94
95   // //////////////////////////////////////////////////////////////////////////////////////////////
96
97   /**
98    * Get current diagram state. At any given instant the diagram might not make *sense*
99    * but it should always be syntactically valid.
100    * @return current Model.
101    */
102   getModel() {
103
104     if (this.application) {
105       const model = this.application.getModel();
106       if (model) {
107         return model.unwrap();
108       }
109     }
110
111     return this.model;
112   }
113
114   // //////////////////////////////////////////////////////////////////////////////////////////////
115
116   /**
117    * Extract SVG element.
118    * @return stringified SVG element.
119    */
120   getSVG() {
121     return this.application.getSVG();
122   }
123
124   // //////////////////////////////////////////////////////////////////////////////////////////////
125
126   /**
127    * Get demo scenarios, allowing initialization in demo mode from the outside.
128    * @returns {Scenarios}
129    */
130   getDemoScenarios() {
131     return new Scenarios();
132   }
133
134   // //////////////////////////////////////////////////////////////////////////////////////////////
135
136   /**
137    * Create new model.
138    * @param metamodelIdOrDefinition
139    * @return newly-created model.
140    */
141   newModel(metamodelIdOrDefinition) {
142     const metamodel = this.getMetamodel(metamodelIdOrDefinition);
143     Common.assertInstanceOf(metamodel, Metamodel);
144     const model = new Model({}, metamodel);
145     if (this.application) {
146       this.application.setModel(model);
147     }
148     return model;
149   }
150
151   // //////////////////////////////////////////////////////////////////////////////////////////////
152
153   /**
154    * Get Metamodel instance corresponding to an ID or JSON definition.
155    * @param metamodelIdOrDefinition String ID or JSON definition.
156    * @returns Metamodel instance.
157    * @private
158    */
159   getMetamodel(metamodelIdOrDefinition) {
160     const metamodelType = Common.getType(metamodelIdOrDefinition);
161     if (metamodelType === 'Object') {
162       return new Metamodel(metamodelIdOrDefinition);
163     }
164     return this.metamodels.getMetamodelOrDefault(metamodelIdOrDefinition);
165   }
166
167   // //////////////////////////////////////////////////////////////////////////////////////////////
168
169   /**
170    * Render current diagram state.
171    */
172   render() {
173
174     if (this.props.model) {
175
176       // If a model was specified as a property, apply it. Otherwise
177       // fall back to the demo model.
178
179       const scenarios = this.getDemoScenarios();
180       const metamodel = [scenarios.getBlankMetamodel(), scenarios.getECOMPMetamodel()];
181       if (this.props.metamodel) {
182         metamodel.push(this.props.metamodel);
183       }
184       this.setMetamodel(metamodel);
185       this.setModel(this.props.model);
186     }
187
188     return (
189       <Application options={this.options} sequencer={this} ref={(a) => { this.application = a; }} />
190     );
191   }
192
193 }
194
195 Sequencer.propTypes = {
196   options: React.PropTypes.object.isRequired,
197   model: React.PropTypes.object,
198   metamodel: React.PropTypes.object,
199 };