2021-06-03 01:45:51 -04:00
|
|
|
import {
|
|
|
|
attr as domAttr,
|
|
|
|
classes as domClasses,
|
|
|
|
event as domEvent,
|
|
|
|
query as domQuery
|
|
|
|
} from 'min-dom';
|
|
|
|
|
|
|
|
import {
|
|
|
|
assign,
|
|
|
|
every,
|
|
|
|
isNumber,
|
|
|
|
isObject
|
|
|
|
} from 'min-dash';
|
|
|
|
|
|
|
|
import cssEscape from 'css.escape';
|
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-06-04 11:39:33 -04:00
|
|
|
* A code editor that allows you to add syntax highlighting and tessting to bpmn properties panel
|
2021-06-03 01:45:51 -04:00
|
|
|
*/
|
|
|
|
export default function Editor(
|
2021-06-03 03:27:01 -04:00
|
|
|
config, injector, eventBus,
|
|
|
|
canvas, elementRegistry) {
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._canvas = canvas;
|
|
|
|
this._elementRegistry = elementRegistry;
|
|
|
|
this._eventBus = eventBus;
|
|
|
|
this._injector = injector;
|
|
|
|
|
|
|
|
this._state = {
|
|
|
|
isOpen: undefined,
|
2021-06-03 03:08:11 -04:00
|
|
|
isEnabled: undefined,
|
2021-06-03 01:45:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this._init();
|
|
|
|
|
|
|
|
this.toggle((config && config.open) || false);
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
domEvent.bind(this._toggle, 'click', function (event) {
|
2021-06-03 01:45:51 -04:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
self.toggle();
|
|
|
|
});
|
2021-06-04 14:35:18 -04:00
|
|
|
|
|
|
|
domEvent.bind(this._run_btn, 'click', function (event) {
|
2021-06-04 11:39:33 -04:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
self.validate();
|
|
|
|
});
|
2021-06-09 15:34:06 -04:00
|
|
|
|
2021-06-09 14:55:41 -04:00
|
|
|
domEvent.bind(this._parent, 'wheel', function(event) {
|
|
|
|
// stop propagation and handle scroll differently
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2021-06-03 05:20:57 -04:00
|
|
|
// Check that the currently selected bpmn task is a script task and enable or disable coding window appropriately
|
2021-06-03 03:27:01 -04:00
|
|
|
eventBus.on('selection.changed', function (context) {
|
2021-06-04 11:39:33 -04:00
|
|
|
if (self.isOpen()) {
|
|
|
|
self.close();
|
|
|
|
}
|
2021-06-09 15:34:06 -04:00
|
|
|
if (context.newSelection && self._parent) { // ? Try and replace self._parent with self.isOpen() cause idk why it works yet
|
2021-06-03 03:27:01 -04:00
|
|
|
if (context.newSelection[0].type == "bpmn:ScriptTask") {
|
2021-06-03 02:56:11 -04:00
|
|
|
domClasses(self._parent).add('enabled');
|
2021-06-03 03:08:11 -04:00
|
|
|
self._state.isEnabled = true;
|
|
|
|
return;
|
2021-06-03 03:27:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
domClasses(self._parent).remove('enabled');
|
|
|
|
self._state.isEnabled = false;
|
2021-06-04 11:39:33 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
eventBus.on('editor.validate.response', function (response) {
|
2021-06-09 14:55:41 -04:00
|
|
|
if(response.type == "error"){
|
2021-06-04 11:39:33 -04:00
|
|
|
} else {
|
2021-06-09 14:55:41 -04:00
|
|
|
if (response.passing){
|
|
|
|
// Update Status
|
|
|
|
}
|
2021-06-03 02:56:11 -04:00
|
|
|
}
|
2021-06-04 14:35:18 -04:00
|
|
|
self._eventBus.fire("editor.toolbar.update");
|
2021-06-03 02:56:11 -04:00
|
|
|
});
|
2021-06-03 03:08:11 -04:00
|
|
|
|
2021-06-04 11:39:33 -04:00
|
|
|
eventBus.on('editor.scripts.response', function (response) {
|
|
|
|
if(response.type == "error"){
|
2021-06-09 15:34:06 -04:00
|
|
|
self.scripts = [{name:"No Scripts Available", description:"Contact your system administrator if this is abnormal"}];
|
2021-06-04 11:39:33 -04:00
|
|
|
} else {
|
2021-06-04 14:35:18 -04:00
|
|
|
self.scripts = response.scripts;
|
2021-06-04 11:39:33 -04:00
|
|
|
}
|
2021-06-04 15:49:56 -04:00
|
|
|
self._eventBus.fire("editor.toolbar.update");
|
2021-06-04 11:39:33 -04:00
|
|
|
});
|
|
|
|
|
2021-06-04 14:35:18 -04:00
|
|
|
eventBus.on('editor.objects.response', function (response) {
|
|
|
|
if(response.type == "error"){
|
2021-06-09 15:34:06 -04:00
|
|
|
self.objects = [{name:"No Data Objects Available", description:"Contact your system administrator if this is abnormal"}];
|
2021-06-04 14:35:18 -04:00
|
|
|
} else {
|
|
|
|
self.objects = response.objects;
|
|
|
|
}
|
|
|
|
self._eventBus.fire("editor.toolbar.update");
|
|
|
|
});
|
|
|
|
|
|
|
|
eventBus.on('editor.toolbar.update', function () {
|
2021-06-04 15:49:56 -04:00
|
|
|
self._scripts_menu.innerHTML = "";
|
2021-06-04 14:35:18 -04:00
|
|
|
self.scripts.forEach(element =>
|
2021-06-04 15:49:56 -04:00
|
|
|
self._scripts_menu.innerHTML += `<div class="sub-trigger">${element.name}<div class="description">${element.description}</div></div>`);
|
2021-06-09 15:34:06 -04:00
|
|
|
|
2021-06-04 15:49:56 -04:00
|
|
|
|
|
|
|
self._objects_menu.innerHTML = "";
|
2021-06-09 15:34:06 -04:00
|
|
|
self.objects.forEach(element => {
|
|
|
|
self._objects_menu.innerHTML += `<div class="sub-trigger">${element.name}<div class="data-type">${element.type}</div>`;
|
|
|
|
// Add hierarchical stuff
|
|
|
|
self._objects_menu.innerHTML += "</div>";});
|
2021-06-04 11:39:33 -04:00
|
|
|
});
|
2021-06-03 01:45:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Editor.$inject = [
|
|
|
|
'config.editor',
|
|
|
|
'injector',
|
|
|
|
'eventBus',
|
|
|
|
'canvas',
|
|
|
|
'elementRegistry'
|
|
|
|
];
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype._init = function () {
|
2021-06-03 01:45:51 -04:00
|
|
|
var canvas = this._canvas,
|
2021-06-03 03:27:01 -04:00
|
|
|
container = canvas.getContainer();
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
// create parent div
|
|
|
|
var parent = this._parent = document.createElement('div');
|
2021-06-03 10:09:36 -04:00
|
|
|
|
2021-06-03 06:56:40 -04:00
|
|
|
// prevent drag propagation
|
2021-06-04 14:35:18 -04:00
|
|
|
domEvent.bind(parent, 'mousedown', function (event) {
|
2021-06-03 06:56:40 -04:00
|
|
|
event.stopPropagation();
|
|
|
|
});
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
domClasses(parent).add('djs-editor');
|
|
|
|
|
|
|
|
container.appendChild(parent);
|
|
|
|
|
|
|
|
// create toggle
|
|
|
|
var toggle = this._toggle = document.createElement('div');
|
|
|
|
|
|
|
|
domClasses(toggle).add('toggle');
|
|
|
|
|
|
|
|
parent.appendChild(toggle);
|
|
|
|
|
2021-06-03 10:09:36 -04:00
|
|
|
|
2021-06-04 11:39:33 -04:00
|
|
|
var toolbar = this._toolbar = document.createElement('div');
|
2021-06-03 10:09:36 -04:00
|
|
|
domClasses(toolbar).add('toolbar');
|
2021-06-03 06:56:40 -04:00
|
|
|
|
2021-06-04 11:39:33 -04:00
|
|
|
var run_btn = this._run_btn = document.createElement('div');
|
|
|
|
run_btn.setAttribute("id", "run_btn");
|
|
|
|
|
2021-06-04 15:49:56 -04:00
|
|
|
// ? I'm pretty sure I can just create a template string that will hold all these definitions but I'll have to get into that
|
2021-06-09 14:55:41 -04:00
|
|
|
|
|
|
|
this.scripts = [];
|
|
|
|
|
2021-06-04 14:35:18 -04:00
|
|
|
var scripts_menu_container = document.createElement('div');
|
|
|
|
domClasses(scripts_menu_container).add('dropdown');
|
|
|
|
|
|
|
|
var scripts_menu_trigger = document.createElement('div');
|
|
|
|
scripts_menu_trigger.innerHTML = "Scripts";
|
|
|
|
domClasses(scripts_menu_trigger).add('trigger');
|
|
|
|
|
|
|
|
scripts_menu_container.appendChild(scripts_menu_trigger);
|
|
|
|
|
2021-06-04 11:39:33 -04:00
|
|
|
var scripts_menu = this._scripts_menu = document.createElement('div');
|
2021-06-04 14:35:18 -04:00
|
|
|
domClasses(scripts_menu).add('dropdown-content');
|
2021-06-04 15:49:56 -04:00
|
|
|
self._scripts = [];
|
2021-06-04 14:35:18 -04:00
|
|
|
|
|
|
|
scripts_menu_container.appendChild(scripts_menu);
|
2021-06-04 15:49:56 -04:00
|
|
|
//--------------------------------------------------------//
|
2021-06-09 14:55:41 -04:00
|
|
|
this.objects = [];
|
|
|
|
|
2021-06-04 14:35:18 -04:00
|
|
|
var objects_menu_container = document.createElement('div');
|
|
|
|
domClasses(objects_menu_container).add('dropdown');
|
|
|
|
|
|
|
|
var objects_menu_trigger = document.createElement('div');
|
|
|
|
objects_menu_trigger.innerHTML = "Data";
|
|
|
|
domClasses(objects_menu_trigger).add('trigger');
|
|
|
|
|
|
|
|
objects_menu_container.appendChild(objects_menu_trigger);
|
|
|
|
|
2021-06-04 11:39:33 -04:00
|
|
|
var objects_menu = this._objects_menu = document.createElement('div');
|
2021-06-04 14:35:18 -04:00
|
|
|
domClasses(objects_menu).add('dropdown-content');
|
2021-06-04 15:49:56 -04:00
|
|
|
self._objects = [];
|
2021-06-04 14:35:18 -04:00
|
|
|
|
|
|
|
objects_menu_container.appendChild(objects_menu);
|
2021-06-04 11:39:33 -04:00
|
|
|
|
2021-06-04 15:49:56 -04:00
|
|
|
toolbar.appendChild(run_btn);
|
|
|
|
toolbar.appendChild(scripts_menu_container);
|
2021-06-04 16:19:37 -04:00
|
|
|
toolbar.appendChild(objects_menu_container);
|
2021-06-04 15:49:56 -04:00
|
|
|
|
2021-06-03 01:45:51 -04:00
|
|
|
// create ide textarea
|
2021-06-03 04:46:06 -04:00
|
|
|
var ide_window = document.createElement('div');
|
|
|
|
ide_window.setAttribute("id", "editor");
|
2021-06-03 10:09:36 -04:00
|
|
|
domClasses(ide_window).add('ide');
|
|
|
|
|
|
|
|
parent.appendChild(toolbar);
|
2021-06-03 04:46:06 -04:00
|
|
|
parent.appendChild(ide_window);
|
2021-06-04 11:39:33 -04:00
|
|
|
|
|
|
|
this._eventBus.fire('editor.toolbar.update');
|
2021-06-03 01:45:51 -04:00
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.validate = function () {
|
2021-06-04 11:39:33 -04:00
|
|
|
this._eventBus.fire('editor.validate.request', { code: this._codestore.value });
|
2021-06-03 01:45:51 -04:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.open = function () {
|
2021-06-03 01:45:51 -04:00
|
|
|
assign(this._state, { isOpen: true });
|
|
|
|
|
|
|
|
domClasses(this._parent).add('open');
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
var translate = this._injector.get('translate', false) || function (s) { return s; };
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
domAttr(this._toggle, 'title', translate('Close Editor'));
|
2021-06-04 11:39:33 -04:00
|
|
|
// ? There should be some way to pass the closed editor to ace
|
2021-06-03 10:09:36 -04:00
|
|
|
this._ide = ace.edit("editor");
|
|
|
|
//this._ide.setTheme("ace/theme/monokai");
|
|
|
|
this._ide.session.setMode("ace/mode/python");
|
|
|
|
|
|
|
|
// grab script properties window
|
|
|
|
var codestore = this._codestore = document.getElementById("cam-script-val");
|
|
|
|
// Sync code window and a properties tab
|
|
|
|
this._ide.session.setValue(codestore.value);
|
|
|
|
|
|
|
|
codestore.addEventListener("input", (event) => {
|
2021-06-03 04:46:06 -04:00
|
|
|
this._ide.session.setValue(codestore.value);
|
2021-06-03 03:27:01 -04:00
|
|
|
|
2021-06-03 10:09:36 -04:00
|
|
|
});
|
|
|
|
this._ide.addEventListener("input", (event) => {
|
|
|
|
codestore.value = this._ide.getValue();
|
2021-06-04 11:39:33 -04:00
|
|
|
triggerEvent(codestore, "change");
|
2021-06-03 10:09:36 -04:00
|
|
|
});
|
2021-06-04 15:49:56 -04:00
|
|
|
|
2021-06-03 01:45:51 -04:00
|
|
|
this._eventBus.fire('editor.toggle', { open: true });
|
2021-06-04 15:49:56 -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-06-03 01:45:51 -04:00
|
|
|
assign(this._state, { isOpen: false });
|
|
|
|
|
|
|
|
domClasses(this._parent).remove('open');
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
var translate = this._injector.get('translate', false) || function (s) { return s; };
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
domAttr(this._toggle, 'title', translate('Open Editor'));
|
|
|
|
|
|
|
|
this._eventBus.fire('editor.toggle', { open: false });
|
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.toggle = function (open) {
|
2021-06-03 01:45:51 -04:00
|
|
|
|
|
|
|
var currentOpen = this.isOpen();
|
|
|
|
|
|
|
|
if (typeof open === 'undefined') {
|
|
|
|
open = !currentOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open == currentOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open) {
|
|
|
|
this.open();
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-03 03:27:01 -04:00
|
|
|
Editor.prototype.isOpen = function () {
|
2021-06-03 01:45:51 -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) {
|
|
|
|
|
|
|
|
var evt;
|
|
|
|
|
|
|
|
eventType = eventType || 'change';
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Chrome, Safari, Firefox
|
|
|
|
evt = new MouseEvent((eventType), {
|
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
// IE 11, PhantomJS (wat!)
|
|
|
|
evt = document.createEvent('MouseEvent');
|
|
|
|
|
|
|
|
evt.initEvent((eventType), true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return element.dispatchEvent(evt);
|
|
|
|
};
|