aboutsummaryrefslogtreecommitdiffstats
path: root/dox-sequence-diagram-ui/src/main/webapp/lib/ecomp/asdc/sequencer/components/editor/components/designer/components/lifeline/Lifeline.jsx
blob: 8a3d1847c237aaf83a2be8bb5ee26e40fadfe562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*!
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import React from 'react';
import { DragSource, DropTarget } from 'react-dnd';

import Common from '../../../../../../common/Common';

import Icon from '../../../../../icons/Icon';
import iconHandle from '../../../../../../../../../../res/ecomp/asdc/sequencer/sprites/icons/handle.svg';
import iconDelete from '../../../../../../../../../../res/ecomp/asdc/sequencer/sprites/icons/delete.svg';

/**
 * LHS lifeline row view.
 */
class Lifeline extends React.Component {

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Construct editor view.
   * @param props element properties.
   * @param context react context.
   */
  constructor(props, context) {
    super(props, context);

    this.state = {
      active: false,
      name: props.lifeline.name,
    };

    const metamodel = Common.assertNotNull(this.props.metamodel).unwrap();
    this.canReorder = metamodel.diagram.lifelines.constraints.reorder;
    this.canDelete = metamodel.diagram.lifelines.constraints.delete;

    // Bindings.

    this.onChangeName = this.onChangeName.bind(this);
    this.onBlurName = this.onBlurName.bind(this);
    this.onClickDelete = this.onClickDelete.bind(this);
    this.onMouseEnter = this.onMouseEnter.bind(this);
    this.onMouseLeave = this.onMouseLeave.bind(this);
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Handle name change.
   * @param event change event.
   */
  onChangeName(event) {
    this.setState({ name: event.target.value });
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Handle name change.
   * @param event change event.
   */
  onBlurName(event) {
    const options = this.props.application.getOptions();
    const sanitized = Common.sanitizeText(event.target.value, options, 'lifeline');
    const props = {
      id: this.props.lifeline.id,
      name: sanitized,
    };
    this.props.designer.updateLifeline(props);
    this.setState({ name: sanitized });
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Handle lifeline delete.
   */
  onClickDelete() {
    this.props.designer.deleteLifeline(this.props.lifeline.id);
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Handle mouseover event.
   */
  onMouseEnter() {
    this.setState({ active: true });
    this.props.designer.onMouseEnterLifeline(this.props.lifeline.id);
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Handle mouseleave event.
   */
  onMouseLeave() {
    this.setState({ active: false });
    this.props.designer.onMouseLeaveLifeline(this.props.lifeline.id);
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Get whether metadata permits reorder.
   * @returns true if reorderable.
   */
  isCanReorder() {
    return this.canReorder;
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * Get whether metadata permits delete.
   * @returns true if lifeline can be deleted.
   */
  isCanDelete() {
    return this.canDelete;
  }

  // ///////////////////////////////////////////////////////////////////////////////////////////////

  /**
   * React render.
   * @returns {*}
   */
  render() {

    const id = this.props.lifeline.id;
    const activeClass = (this.props.active === true) ? 'asdcs-active' : '';
    const { connectDragSource, connectDropTarget } = this.props;
    return connectDragSource(connectDropTarget(

      <div
        className={`asdcs-designer-lifeline ${activeClass}`}
        data-id={id}
        onMouseEnter={this.onMouseEnter}
        onMouseLeave={this.onMouseLeave}
      >
        <table className="asdcs-designer-layout asdcs-designer-lifeline-row1">
          <tbody>
            <tr>
              <td>
                <div className="asdcs-designer-sort asdcs-designer-icon">
                  <Icon glyph={iconHandle} />
                </div>
              </td>
              <td>
                <div className="asdcs-designer-lifeline-index">{this.props.lifeline.index}.</div>
              </td>
              <td>
                <div className="asdcs-designer-lifeline-name">
                  <input
                    type="text"
                    className="asdcs-editable"
                    placeholder="Unnamed"
                    value={this.state.name}
                    onChange={this.onChangeName}
                    onBlur={this.onBlurName}
                  />
                </div>
              </td>
              <td>
                <div className="asdcs-designer-delete asdcs-designer-icon" onClick={this.onClickDelete}>
                  <Icon glyph={iconDelete} />
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    ));
  }
}

/**
 * Declare properties.
 */
Lifeline.propTypes = {
  application: React.PropTypes.object.isRequired,
  designer: React.PropTypes.object.isRequired,
  lifeline: React.PropTypes.object.isRequired,
  active: React.PropTypes.bool.isRequired,
  metamodel: React.PropTypes.object.isRequired,
  id: React.PropTypes.any.isRequired,
  index: React.PropTypes.number.isRequired,
  lifelines: React.PropTypes.object.isRequired,
  isDragging: React.PropTypes.bool.isRequired,
  connectDragSource: React.PropTypes.func.isRequired,
  connectDropTarget: React.PropTypes.func.isRequired,
};

/** DND. */
const source = {
  beginDrag(props) {
    return {
      id: props.id,
      index: props.index,
    };
  },
};

/** DND. */
const sourceCollect = function collection(connect, monitor) {
  return {
    connectDragSource: connect.dragSource(),
    isDragging: monitor.isDragging(),
  };
};

/** DND. */
const target = {
  drop(props, monitor, component) {
    Common.assertNotNull(props);
    Common.assertNotNull(monitor);
    const decorated = component.getDecoratedComponentInstance();
    if (decorated) {
      const lifelines = decorated.props.lifelines;
      if (lifelines) {
        const dragIndex = monitor.getItem().index;
        const hoverIndex = lifelines.getHoverIndex();
        lifelines.onDrop(dragIndex, hoverIndex);
      }
    }
  },
  hover(props, monitor, component) {
    Common.assertNotNull(props);
    Common.assertNotNull(monitor);
    if (component) {
      const decorated = component.getDecoratedComponentInstance();
      if (decorated) {
        const lifelines = decorated.props.lifelines;
        if (lifelines) {
          lifelines.setHoverIndex(decorated.props.index);
        }
      }
    }
  },
};

/** DND. */
function targetCollect(connect, monitor) {
  return {
    connectDropTarget: connect.dropTarget(),
    isOver: monitor.isOver(),
  };
}

const wrapper1 = DragSource('lifeline', source, sourceCollect)(Lifeline);
export default DropTarget(['lifeline', 'lifeline-new'], target, targetCollect)(wrapper1);