2021-07-08 15:17:27 -04:00
|
|
|
import {attr as domAttr, classes as domClasses, event as domEvent,} from 'min-dom';
|
|
|
|
|
|
|
|
import {assign} from 'min-dash';
|
|
|
|
|
2021-06-03 04:46:06 -04:00
|
|
|
import * as ace from 'ace-builds/src-noconflict/ace';
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-06-03 04:46:06 -04:00
|
|
|
// Enable dynamic loading of formatting modes
|
|
|
|
ace.config.set('basePath', 'https://unpkg.com/ace-builds@1.4.12/src-noconflict');
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
/**
|
2021-07-08 15:17:27 -04:00
|
|
|
* A code editor that allows you to add syntax highlighting and testing to bpmn properties panel
|
2021-06-03 01:45:51 -04:00
|
|
|
*/
|
|
|
|
export default function Editor(
|
2021-07-08 15:17:27 -04:00
|
|
|
config, injector, eventBus,
|
|
|
|
canvas, elementRegistry) {
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
var self = this;
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._canvas = canvas;
|
|
|
|
// this._elementRegistry = elementRegistry;
|
|
|
|
this._eventBus = eventBus;
|
|
|
|
this._injector = injector;
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._state = {
|
|
|
|
isOpen: undefined,
|
|
|
|
};
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this.init();
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this.toggle((config && config.open) || false);
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domEvent.bind(this.toggle_btn, 'click', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
self.toggle();
|
|
|
|
});
|
2021-06-04 14:35:18 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domEvent.bind(this.run_btn, 'click', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
self.validate();
|
|
|
|
});
|
2021-06-09 15:34:06 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domEvent.bind(this.exit_btn, 'click', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2021-06-09 23:57:05 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
self.close();
|
|
|
|
});
|
2021-06-09 23:57:05 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domEvent.bind(this._parent, 'wheel', function (event) {
|
|
|
|
// stop propagation and handle scroll differently
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
2021-06-03 03:08:11 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// Check that the currently selected bpmn task is a script task and enable or disable coding window appropriately
|
|
|
|
eventBus.on('selection.changed', function (context) {
|
|
|
|
if (self.isOpen()) {
|
|
|
|
self.close();
|
|
|
|
}
|
|
|
|
domClasses(self._parent).remove('enabled');
|
|
|
|
if (context.newSelection.length > 0) {
|
|
|
|
if (context.newSelection[0].type == 'bpmn:ScriptTask' && true) { // TODO: Check if opened task is 'inline script'
|
|
|
|
domClasses(self._parent).add('enabled');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
eventBus.on('editor.validate.response', function (response) {
|
|
|
|
this.set_valid_state(response.passing ? 'passing' : 'failing');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return a list of objects containing both the script name and description in plain text
|
|
|
|
eventBus.on('editor.scripts.response', function (response) {
|
|
|
|
if (response.scripts && response.scripts.length > 0) {
|
|
|
|
self.scripts = response.scripts;
|
|
|
|
} else {
|
|
|
|
self.scripts = [{
|
|
|
|
name: 'No Scripts Available',
|
|
|
|
description: 'Contact your system administrator if this is abnormal'
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.scripts_menu.innerHTML = '';
|
|
|
|
self.scripts.forEach(element =>
|
|
|
|
self.scripts_menu.innerHTML += `<a class="dropdown-item">${element.name}</a>`);// <ul><li>${element.description}</ul></li>`); // <div class="description">${element.description}</div>
|
|
|
|
});
|
2021-06-04 14:35:18 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// Return a single object containing all data available to the open task
|
|
|
|
eventBus.on('editor.objects.response', function (response) {
|
|
|
|
self.objects = response.objects;
|
|
|
|
|
|
|
|
function recurseObject(data) {
|
|
|
|
var html = '';
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
|
|
|
|
if (typeof value == 'object' && value != undefined) {
|
|
|
|
html += `<li><a class="dropdown-item">${key}</a><ul class="dropdown-menu dropdown-submenu">` + recurseObject(value) + '</ul><li>';
|
|
|
|
} else {
|
|
|
|
let type = typeof value;
|
|
|
|
if (value == undefined) {
|
|
|
|
type = 'undefined';
|
|
|
|
}
|
|
|
|
html += `<li><a class="dropdown-item">${key}</a><div class="data-type ${type}">${type}</div></li>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.objects_menu.innerHTML = recurseObject(self.objects);
|
|
|
|
});
|
2021-06-03 01:45:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Editor.$inject = [
|
2021-07-08 15:17:27 -04:00
|
|
|
'config.editor',
|
|
|
|
'injector',
|
|
|
|
'eventBus',
|
|
|
|
'canvas',
|
|
|
|
'elementRegistry'
|
2021-06-03 01:45:51 -04:00
|
|
|
];
|
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
Editor.prototype.set_valid_state = function (state) {
|
|
|
|
if (state === 'passing') {
|
|
|
|
domClasses(this.run_btn).add('passing');
|
|
|
|
domClasses(this.run_btn).remove('failing');
|
|
|
|
domClasses(this.run_btn).remove('unknown');
|
|
|
|
} else if (state === 'failing') {
|
|
|
|
domClasses(this.run_btn).add('failing');
|
|
|
|
domClasses(this.run_btn).remove('passing');
|
|
|
|
domClasses(this.run_btn).remove('unknown');
|
|
|
|
} else {
|
|
|
|
domClasses(this.run_btn).add('unknown');
|
|
|
|
domClasses(this.run_btn).remove('passing');
|
|
|
|
domClasses(this.run_btn).remove('failing');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-09 23:57:05 -04:00
|
|
|
Editor.prototype.init = function () {
|
2021-07-08 15:17:27 -04:00
|
|
|
var canvas = this._canvas,
|
|
|
|
container = canvas.getContainer();
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
var template = `
|
2021-06-09 23:57:05 -04:00
|
|
|
<div class="djs-editor">
|
|
|
|
<div class="toggle" id="toggle"></div>
|
2021-07-08 15:17:27 -04:00
|
|
|
|
|
|
|
<nav class="navbar navbar-expand-sm navbar-light bg-light toolbar">
|
|
|
|
<div class="collapse navbar-collapse sidenav" id="navbarSupportedContent">
|
|
|
|
<ul class="navbar-nav mr-auto">
|
|
|
|
<li class="nav-item">
|
|
|
|
<a class="nav-link" id="run_btn">
|
|
|
|
<i class="fas fa-build"></i>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="nav-item dropdown">
|
|
|
|
<a class="nav-link dropdown-toggle" id="scriptsDropdown" role="button" data-toggle="dropdown" aria-haspopup="true"
|
|
|
|
aria-expanded="false">
|
|
|
|
Scripts
|
|
|
|
</a>
|
|
|
|
<div class="dropdown-menu" aria-labelledby="scriptsDropdown" id="scripts_list">
|
|
|
|
<a class="dropdown-item">
|
|
|
|
Validate to Load Scripts...
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
<li class="nav-item dropdown">
|
|
|
|
<a class="nav-link dropdown-toggle" id="dataDropdown" role="button" data-toggle="dropdown" aria-haspopup="true"
|
|
|
|
aria-expanded="false">
|
|
|
|
Data
|
|
|
|
</a>
|
|
|
|
<div class="dropdown-menu" aria-labelledby="dataDropdown" id="objects_list">
|
|
|
|
<a class="dropdown-item">
|
|
|
|
Validate to Load Data...
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
<li class="nav-item">
|
|
|
|
<a class="nav-link waves-effect waves-light" id="exit_btn">
|
|
|
|
<i class="fas fa-house"></i>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2021-06-09 23:57:05 -04:00
|
|
|
</div>
|
2021-07-08 15:17:27 -04:00
|
|
|
</nav>
|
2021-06-09 23:57:05 -04:00
|
|
|
<div class="ide" id="editor"></div>
|
2021-07-08 15:17:27 -04:00
|
|
|
</div>`;
|
|
|
|
let dom = new DOMParser()
|
|
|
|
.parseFromString(template, 'text/html');
|
|
|
|
|
|
|
|
// create parent div
|
|
|
|
var parent = this._parent = dom.body.firstElementChild;
|
|
|
|
this.toggle_btn = dom.getElementById('toggle');
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this.run_btn = dom.getElementById('run_btn');
|
|
|
|
this.exit_btn = dom.getElementById('exit_btn');
|
2021-06-03 10:09:36 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this.scripts_menu = dom.getElementById('scripts_list');
|
|
|
|
this.scripts = [];
|
|
|
|
this.objects_menu = dom.getElementById('objects_list');
|
|
|
|
this.objects = {};
|
2021-06-09 14:55:41 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
container.appendChild(parent);
|
2021-06-09 14:55:41 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._eventBus.fire('editor.toolbar.update');
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-06-03 01:45:51 -04:00
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.validate = function () {
|
2021-07-08 15:17:27 -04:00
|
|
|
this._eventBus.fire('editor.validate.request', {
|
|
|
|
code: document.getElementById('cam-script-val').value,
|
|
|
|
task_name: document.getElementById('camunda-id').value
|
|
|
|
});
|
|
|
|
};
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.open = function () {
|
2021-07-08 15:17:27 -04:00
|
|
|
assign(this._state, {isOpen: true});
|
|
|
|
|
|
|
|
domClasses(this._parent).add('open');
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
var translate = this._injector.get('translate', false) || function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domAttr(this.toggle_btn, 'title', translate('Close Editor'));
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// ? There should be some way to pass the closed editor to ace
|
|
|
|
this._ide = ace.edit('editor');
|
2021-06-03 10:09:36 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// this._ide.setTheme("ace/theme/monokai");
|
|
|
|
this._ide.session.setMode('ace/mode/python');
|
2021-06-03 10:09:36 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// grab script properties window
|
|
|
|
var codestore = document.getElementById('cam-script-val');
|
|
|
|
|
|
|
|
// Sync code window and a properties tab
|
2021-06-03 04:46:06 -04:00
|
|
|
this._ide.session.setValue(codestore.value);
|
2021-06-09 23:57:05 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
codestore.addEventListener('input', (event) => {
|
|
|
|
this._ide.session.setValue(codestore.value);
|
|
|
|
|
|
|
|
// TODO: Consolidate this bit
|
|
|
|
domClasses(this.run_btn).remove('passing');
|
|
|
|
domClasses(this.run_btn).remove('failing');
|
|
|
|
domClasses(this.run_btn).add('unknown');
|
|
|
|
});
|
2021-06-09 23:57:05 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._ide.addEventListener('input', (event) => {
|
|
|
|
codestore.value = this._ide.getValue();
|
|
|
|
|
|
|
|
domClasses(this.run_btn).remove('passing');
|
|
|
|
domClasses(this.run_btn).remove('failing');
|
|
|
|
domClasses(this.run_btn).add('unknown');
|
|
|
|
triggerEvent(codestore, 'change');
|
|
|
|
});
|
2021-06-04 15:49:56 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._eventBus.fire('editor.toggle', {open: true});
|
2021-06-04 15:49:56 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._eventBus.fire('editor.objects.request');
|
|
|
|
this._eventBus.fire('editor.scripts.request');
|
2021-06-03 01:45:51 -04:00
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.close = function () {
|
2021-06-03 06:56:40 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
assign(this._state, {isOpen: false});
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domClasses(this._parent).remove('open');
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
var translate = this._injector.get('translate', false) || function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
domAttr(this.toggle_btn, 'title', translate('Open Editor'));
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
this._eventBus.fire('editor.toggle', {open: false});
|
2021-06-03 01:45:51 -04:00
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.toggle = function (open) {
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
var currentOpen = this.isOpen();
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
if (typeof open === 'undefined') {
|
|
|
|
open = !currentOpen;
|
|
|
|
}
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
if (open == currentOpen) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-03 01:45:51 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
if (open) {
|
|
|
|
this.open();
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
2021-06-03 01:45:51 -04:00
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.isOpen = function () {
|
2021-07-08 15:17:27 -04:00
|
|
|
return this._state.isOpen;
|
2021-06-03 10:09:36 -04:00
|
|
|
};
|
|
|
|
|
2021-06-04 11:39:33 -04:00
|
|
|
/**
|
|
|
|
* Triggers a change event
|
|
|
|
*
|
|
|
|
* @param element on which the change should be triggered
|
|
|
|
* @param eventType type of the event (e.g. click, change, ...)
|
|
|
|
*/
|
|
|
|
var triggerEvent = function (element, eventType) {
|
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
var evt;
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
eventType = eventType || 'change';
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
try {
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// Chrome, Safari, Firefox
|
|
|
|
evt = new MouseEvent((eventType), {
|
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
// IE 11, PhantomJS (wat!)
|
|
|
|
evt = document.createEvent('MouseEvent');
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
evt.initEvent((eventType), true, true);
|
|
|
|
}
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
return element.dispatchEvent(evt);
|
2021-06-09 23:57:05 -04:00
|
|
|
};
|
|
|
|
|
2021-07-08 15:17:27 -04:00
|
|
|
|