2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import React from 'react';
18 import PropTypes from 'prop-types';
19 import Common from '../../../../common/Common';
21 import iconPlus from '../../../../../../../../res/ecomp/asdc/sequencer/sprites/icons/plus.svg';
22 import iconOpen from '../../../../../../../../res/ecomp/asdc/sequencer/sprites/icons/open.svg';
25 * Toolbar view. Buttons offered in the toolbar depend on the mode. Unless in demo mode,
26 * all you get are the buttons for toggling between JSON/YAML/Designer.
28 export default class Toolbar extends React.Component {
29 // ///////////////////////////////////////////////////////////////////////////////////////////////
34 constructor(props, context) {
35 super(props, context);
36 this.application = Common.assertType(this.props.application, 'Object');
37 this.editor = Common.assertType(this.props.editor, 'Object');
41 // ///////////////////////////////////////////////////////////////////////////////////////////////
44 * Set editor mode, one of {design, json, yaml}.
47 setMode(mode = 'design') {
51 // ///////////////////////////////////////////////////////////////////////////////////////////////
54 * Render into the DOM.
57 const demo = this.application.getOptions().demo;
58 const demoCss = demo ? '' : 'asdc-hide';
61 <div className={`asdcs-editor-toolbar ${demoCss}`}>
62 <div className="asdcs-editor-toolbar-demo">
64 className="asdcs-button-new"
65 data-title="New sequence">
67 <use xlinkHref={iconPlus} className="asdcs-icon" />
71 className="asdcs-button-open"
72 data-title="Open sequence">
74 <use xlinkHref={iconOpen} className="asdcs-icon" />
78 className="asdcs-button-save"
79 data-title="Save checkpoint">
82 xlinkHref="#icon--save"
83 className="asdcs-icon"
88 className="asdcs-button-validate"
89 data-title="Validate">
92 xlinkHref="#icon--validate"
93 className="asdcs-icon"
98 className="asdcs-button-download"
99 data-title="Download">
102 xlinkHref="#icon--download"
103 className="asdcs-icon"
107 <button className="asdcs-button-upload" data-title="Upload">
110 xlinkHref="#icon--upload"
111 className="asdcs-icon"
116 <div className="asdcs-editor-toolbar-toggle">
117 <button className="asdcs-button-design asdcs-button-mode asdcs-button-toggle-left">
120 <button className="asdcs-button-json asdcs-button-mode asdcs-button-toggle-center">
123 <button className="asdcs-button-yaml asdcs-button-mode asdcs-button-toggle-right">
131 // ///////////////////////////////////////////////////////////////////////////////////////////////
132 // ///////////////////////////////////////////////////////////////////////////////////////////////
133 // ///////////////////////////////////////////////////////////////////////////////////////////////
134 // ///////////////////////////////////////////////////////////////////////////////////////////////
135 // ///////////////////////////////////////////////////////////////////////////////////////////////
136 // ///////////////////////////////////////////////////////////////////////////////////////////////
137 // ///////////////////////////////////////////////////////////////////////////////////////////////
138 // ///////////////////////////////////////////////////////////////////////////////////////////////
139 // ///////////////////////////////////////////////////////////////////////////////////////////////
140 // ///////////////////////////////////////////////////////////////////////////////////////////////
143 * Initialize eventhandlers.
148 $('button.asdcs-button-open', this.$el).click(() => {
152 $('button.asdcs-button-new', this.$el).click(() => {
156 $('button.asdcs-button-save', this.$el).click(() => {
160 $('button.asdcs-button-upload', this.$el).click(() => {
161 this._doDemoUpload();
164 $('button.asdcs-button-download', this.$el).click(() => {
165 this._doDemoDownload();
168 $('button.asdcs-button-validate', this.$el).click(() => {
169 this._doDemoValidate();
172 $('button.asdcs-button-json', this.$el).click((e) => {
173 if ($(e.target).hasClass('asdcs-active')) {
176 this.editor.toggleToJSON();
179 $('button.asdcs-button-yaml', this.$el).click((e) => {
180 if ($(e.target).hasClass('asdcs-active')) {
183 this.editor.toggleToYAML();
186 $('button.asdcs-button-design', this.$el).click((e) => {
187 if ($(e.target).hasClass('asdcs-active')) {
190 this.editor.toggleToDesign();
195 // ///////////////////////////////////////////////////////////////////////////////////////////////
201 const complete = function complete() {
202 const sequencer = this.application.getSequencer();
203 const scenarios = sequencer.getDemoScenarios();
204 sequencer.setModel(scenarios.getECOMP());
206 this.application.showConfirmDialog('[DEMO MODE] Open a canned DEMO sequence ' +
207 'via the public #setModel() API?', complete);
212 // ///////////////////////////////////////////////////////////////////////////////////////////////
218 const complete = function complete() {
219 const sequencer = this.application.getSequencer();
220 sequencer.newModel();
222 this.application.showConfirmDialog('[DEMO MODE] Create an empty sequence via the ' +
223 'public #newModel() API?', complete);
227 // ///////////////////////////////////////////////////////////////////////////////////////////////
233 const sequencer = this.application.getSequencer();
234 Logger.info(`[DEMO MODE] model:\n${JSON.stringify(sequencer.getModel(), null, 4)}`);
235 this.application.showInfoDialog('[DEMO MODE] Retrieved model via the public #getModel ' +
236 'API and logged its JSON to the console.');
240 // ///////////////////////////////////////////////////////////////////////////////////////////////
246 const sequencer = this.application.getSequencer();
247 const svg = sequencer.getSVG();
248 // console.log(`[DEMO MODE] SVG:\n${svg}`);
249 const $control = this.$el.closest('.asdcs-control');
250 Logger.info(`parent: ${$control.length}`);
251 const $form = $('form.asdcs-export', $control);
252 Logger.info(`form: ${$form.length}`);
253 $('input[name=svg]', $form).val(svg);
258 this.application.showErrorDialog('[DEMO MODE] Export service not available. Retrieved ' +
259 'SVG via the public #getSVG API and dumped it to the console.');
264 // ///////////////////////////////////////////////////////////////////////////////////////////////
270 const json = JSON.stringify(this.application.getSequencer().getModel());
271 const $control = this.$el.closest('.asdcs-control');
272 const $a = $('<a download="model.json" style="display:none">').appendTo($control);
273 $a.attr('href', `data:application/json;charset=utf-8,${encodeURIComponent(json)}`);
279 // ///////////////////////////////////////////////////////////////////////////////////////////////
285 this.application.showInfoDialog('[DEMO MODE] Dumping validation result to the console.');
286 const errors = this.application.getModel().validate();
287 Logger.info(`[DEMO MODE] Validation: ${JSON.stringify(errors, null, 4)}`);
292 Toolbar.propTypes = {
293 application: PropTypes.object.isRequired,
294 editor: PropTypes.object.isRequired