Add new code new version
[sdc.git] / dox-sequence-diagram-ui / src / main / webapp / lib / ecomp / asdc / sequencer / components / editor / components / designer / components / lifeline / Lifelines.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
19 import Common from '../../../../../../common/Common';
20
21 import Lifeline from './Lifeline';
22 import LifelineNew from './LifelineNew';
23
24 /**
25  * Lifeline container, facilitating DND.
26  * @param props lifeline element properties.
27  * @returns {*}
28  * @constructor
29  */
30 export default class Lifelines extends React.Component {
31
32   // ///////////////////////////////////////////////////////////////////////////////////////////////
33
34   /**
35    * Construct view.
36    * @param props element properties.
37    * @param context react context.
38    */
39   constructor(props, context) {
40     super(props, context);
41     this.setHoverIndex = this.setHoverIndex.bind(this);
42     this.getHoverIndex = this.getHoverIndex.bind(this);
43     this.onDrop = this.onDrop.bind(this);
44   }
45
46   // ///////////////////////////////////////////////////////////////////////////////////////////////
47
48   /**
49    * Record last hover index as non-state.
50    * @param index index.
51    */
52   setHoverIndex(index) {
53     this.hoverIndex = index;
54   }
55
56   // ///////////////////////////////////////////////////////////////////////////////////////////////
57
58   /**
59    * Get last recorded hover index.
60    * @returns {*}
61    */
62   getHoverIndex() {
63     return this.hoverIndex;
64   }
65
66   // ///////////////////////////////////////////////////////////////////////////////////////////////
67
68   /**
69    * Handle drop.
70    * @param dragIndex dragged item index; undefined if new.
71    * @param hoverIndex drop index.
72    */
73   onDrop(dragIndex, hoverIndex) {
74     if (hoverIndex >= 0) {
75       const application = this.props.application;
76       const model = application.getModel();
77       if (Common.isNumber(dragIndex)) {
78         if (dragIndex !== hoverIndex) {
79           model.reorderLifelines(dragIndex, hoverIndex);
80         }
81       } else {
82         model.addLifeline(hoverIndex);
83       }
84       this.forceUpdate();
85       application.renderDiagram();
86     }
87   }
88
89   // ///////////////////////////////////////////////////////////////////////////////////////////////
90
91   /**
92    * Render view.
93    * @returns {XML}
94    */
95   render() {
96     const model = this.props.application.getModel();
97     const metamodel = model.getMetamodel();
98     const diagram = model.unwrap().diagram;
99
100     const lifelines = [];
101     for (const lifeline of diagram.lifelines) {
102       lifelines.push(<Lifeline
103         key={`l${lifeline.id}`}
104         application={this.props.application}
105         designer={this.props.designer}
106         lifeline={lifeline}
107         active={this.props.activeLifelineId === lifeline.id}
108         id={lifeline.id}
109         metamodel={metamodel}
110         lifelines={this}
111         index={lifelines.length}
112       />);
113     }
114
115     lifelines.push(<LifelineNew
116       key="_l"
117       designer={this.props.designer}
118       lifelines={this}
119     />);
120
121     return (
122       <div className="asdcs-designer-lifelines">
123         {lifelines}
124       </div>
125     );
126   }
127 }
128
129 /**
130  * Declare properties.
131  */
132 Lifelines.propTypes = {
133   application: React.PropTypes.object.isRequired,
134   designer: React.PropTypes.object.isRequired,
135   activeLifelineId: React.PropTypes.string,
136 };