aboutsummaryrefslogtreecommitdiffstats
path: root/dox-sequence-diagram-ui/src/main/webapp/lib/ecomp/asdc/sequencer/components/editor/components/toolbar/Toolbar.jsx
blob: b6b0f574a2311c522d1c3affefeecd9efceb31fd (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
265
266
267
268
269
270
271
272
273
274
275
/*!
 * 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 Common from '../../../../common/Common';

import iconPlus from '../../../../../../../../res/ecomp/asdc/sequencer/sprites/icons/plus.svg';
import iconOpen from '../../../../../../../../res/ecomp/asdc/sequencer/sprites/icons/open.svg';

/**
 * Toolbar view. Buttons offered in the toolbar depend on the mode. Unless in demo mode,
 * all you get are the buttons for toggling between JSON/YAML/Designer.
 */
export default class Toolbar extends React.Component {

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

  /**
   * Construct view.
   */
  constructor(props, context) {
    super(props, context);
    this.application = Common.assertType(this.props.application, 'Object');
    this.editor = Common.assertType(this.props.editor, 'Object');
    this.mode = 'design';
  }

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

  /**
   * Set editor mode, one of {design, json, yaml}.
   * @param mode
   */
  setMode(mode = 'design') {
    this.mode = mode;
  }

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

  /**
   * Render into the DOM.
   */
  render() {

    const demo = this.application.getOptions().demo;
    const demoCss = demo ? '' : 'asdc-hide';

    return (
      <div className={`asdcs-editor-toolbar ${demoCss}`}>
        <div className="asdcs-editor-toolbar-demo">
          <button className="asdcs-button-new" data-title="New sequence">
            <svg>
              <use xlinkHref={iconPlus} className="asdcs-icon" />
            </svg>
          </button>
          <button className="asdcs-button-open" data-title="Open sequence">
            <svg>
              <use xlinkHref={iconOpen} className="asdcs-icon" />
            </svg>
          </button>
          <button className="asdcs-button-save" data-title="Save checkpoint">
            <svg>
              <use xlinkHref="#icon--save" className="asdcs-icon" />
            </svg>
          </button>
          <button className="asdcs-button-validate" data-title="Validate">
            <svg>
              <use xlinkHref="#icon--validate" className="asdcs-icon" />
            </svg>
          </button>
          <button className="asdcs-button-download" data-title="Download">
            <svg>
              <use xlinkHref="#icon--download" className="asdcs-icon" />
            </svg>
          </button>
          <button className="asdcs-button-upload" data-title="Upload">
            <svg>
              <use xlinkHref="#icon--upload" className="asdcs-icon" />
            </svg>
          </button>
        </div>
        <div className="asdcs-editor-toolbar-toggle">
          <button className="asdcs-button-design asdcs-button-mode asdcs-button-toggle-left">
            Design
          </button>
          <button className="asdcs-button-json asdcs-button-mode asdcs-button-toggle-center">
            JSON
          </button>
          <button className="asdcs-button-yaml asdcs-button-mode asdcs-button-toggle-right">
            YAML
          </button>
        </div>
      </div>
    );
  }

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

  /**
   * Initialize eventhandlers.
   * @private
   *
  _initEvents() {

    $('button.asdcs-button-open', this.$el).click(() => {
      this._doDemoOpen();
    });

    $('button.asdcs-button-new', this.$el).click(() => {
      this._doDemoNew();
    });

    $('button.asdcs-button-save', this.$el).click(() => {
      this._doDemoSave();
    });

    $('button.asdcs-button-upload', this.$el).click(() => {
      this._doDemoUpload();
    });

    $('button.asdcs-button-download', this.$el).click(() => {
      this._doDemoDownload();
    });

    $('button.asdcs-button-validate', this.$el).click(() => {
      this._doDemoValidate();
    });

    $('button.asdcs-button-json', this.$el).click((e) => {
      if ($(e.target).hasClass('asdcs-active')) {
        return;
      }
      this.editor.toggleToJSON();
    });

    $('button.asdcs-button-yaml', this.$el).click((e) => {
      if ($(e.target).hasClass('asdcs-active')) {
        return;
      }
      this.editor.toggleToYAML();
    });

    $('button.asdcs-button-design', this.$el).click((e) => {
      if ($(e.target).hasClass('asdcs-active')) {
        return;
      }
      this.editor.toggleToDesign();
    });
  }
  */

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

  /**
   * Demo action.
   *
  _doDemoOpen() {
    const complete = function complete() {
      const sequencer = this.application.getSequencer();
      const scenarios = sequencer.getDemoScenarios();
      sequencer.setModel(scenarios.getECOMP());
    };
    this.application.showConfirmDialog('[DEMO MODE] Open a canned DEMO sequence ' +
      'via the public #setModel() API?', complete);

  }
  */

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

  /**
   * Demo action.
   *
  _doDemoNew() {
    const complete = function complete() {
      const sequencer = this.application.getSequencer();
      sequencer.newModel();
    };
    this.application.showConfirmDialog('[DEMO MODE] Create an empty sequence via the ' +
      'public #newModel() API?', complete);
  }
  */

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

  /**
   * Demo action.
   *
  _doDemoSave() {
    const sequencer = this.application.getSequencer();
    Logger.info(`[DEMO MODE] model:\n${JSON.stringify(sequencer.getModel(), null, 4)}`);
    this.application.showInfoDialog('[DEMO MODE] Retrieved model via the public #getModel ' +
      'API and logged its JSON to the console.');
  }
  */

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

  /**
   * Demo action.
   *
  _doDemoUpload() {
    const sequencer = this.application.getSequencer();
    const svg = sequencer.getSVG();
    // console.log(`[DEMO MODE] SVG:\n${svg}`);
    const $control = this.$el.closest('.asdcs-control');
    Logger.info(`parent: ${$control.length}`);
    const $form = $('form.asdcs-export', $control);
    Logger.info(`form: ${$form.length}`);
    $('input[name=svg]', $form).val(svg);
    try {
      $form.submit();
    } catch (e) {
      Logger.error(e);
      this.application.showErrorDialog('[DEMO MODE] Export service not available. Retrieved ' +
        'SVG via the public #getSVG API and dumped it to the console.');
    }
  }
  */

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

  /**
   * Demo action.
   *
  _doDemoDownload() {
    const json = JSON.stringify(this.application.getSequencer().getModel());
    const $control = this.$el.closest('.asdcs-control');
    const $a = $('<a download="model.json" style="display:none">').appendTo($control);
    $a.attr('href', `data:application/json;charset=utf-8,${encodeURIComponent(json)}`);
    $a[0].click();
    $a.remove();
  }
  */

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

  /**
   * Demo action.
   *
  _doDemoValidate() {
    this.application.showInfoDialog('[DEMO MODE] Dumping validation result to the console.');
    const errors = this.application.getModel().validate();
    Logger.info(`[DEMO MODE] Validation: ${JSON.stringify(errors, null, 4)}`);
  }
  */
}

Toolbar.propTypes = {
  application: React.PropTypes.object.isRequired,
  editor: React.PropTypes.object.isRequired,
};