aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/readOnly.js
diff options
context:
space:
mode:
Diffstat (limited to 'sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/readOnly.js')
-rw-r--r--sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/readOnly.js134
1 files changed, 134 insertions, 0 deletions
diff --git a/sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/readOnly.js b/sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/readOnly.js
new file mode 100644
index 00000000..f1da7dcf
--- /dev/null
+++ b/sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/readOnly.js
@@ -0,0 +1,134 @@
+import forEach from 'lodash.foreach';
+
+const HIGH_PRIORITY = 10001;
+
+function ReadOnly(
+ eventBus,
+ contextPad,
+ dragging,
+ directEditing,
+ editorActions,
+ modeling,
+ palette,
+ paletteProvider
+) {
+ this._readOnly = false;
+ this._eventBus = eventBus;
+
+ let self = this;
+ eventBus.on('readOnly.changed', HIGH_PRIORITY, function(e) {
+ self._readOnly = e.readOnly;
+
+ if (e.readOnly) {
+ directEditing.cancel();
+ contextPad.close();
+ dragging.cancel();
+ }
+
+ palette._update();
+ });
+
+ function intercept(obj, fnName, cb) {
+ var fn = obj[fnName];
+ obj[fnName] = function() {
+ return cb.call(this, fn, arguments);
+ };
+ }
+
+ function ignoreWhenReadOnly(obj, fnName) {
+ intercept(obj, fnName, function(fn, args) {
+ if (self._readOnly) {
+ return;
+ }
+
+ return fn.apply(this, args);
+ });
+ }
+
+ function throwIfReadOnly(obj, fnName) {
+ intercept(obj, fnName, function(fn, args) {
+ if (self._readOnly) {
+ throw new Error('model is read-only');
+ }
+
+ return fn.apply(this, args);
+ });
+ }
+
+ ignoreWhenReadOnly(contextPad, 'open');
+
+ ignoreWhenReadOnly(dragging, 'init');
+
+ ignoreWhenReadOnly(directEditing, 'activate');
+
+ ignoreWhenReadOnly(editorActions._actions, 'undo');
+ ignoreWhenReadOnly(editorActions._actions, 'redo');
+ ignoreWhenReadOnly(editorActions._actions, 'copy');
+ ignoreWhenReadOnly(editorActions._actions, 'paste');
+ ignoreWhenReadOnly(editorActions._actions, 'removeSelection');
+ // BpmnEditorActions
+ ignoreWhenReadOnly(editorActions._actions, 'spaceTool');
+ ignoreWhenReadOnly(editorActions._actions, 'lassoTool');
+ ignoreWhenReadOnly(editorActions._actions, 'globalConnectTool');
+ ignoreWhenReadOnly(editorActions._actions, 'distributeElements');
+ ignoreWhenReadOnly(editorActions._actions, 'alignElements');
+ ignoreWhenReadOnly(editorActions._actions, 'directEditing');
+
+ throwIfReadOnly(modeling, 'moveShape');
+ throwIfReadOnly(modeling, 'updateAttachment');
+ throwIfReadOnly(modeling, 'moveElements');
+ throwIfReadOnly(modeling, 'moveConnection');
+ throwIfReadOnly(modeling, 'layoutConnection');
+ throwIfReadOnly(modeling, 'createConnection');
+ throwIfReadOnly(modeling, 'createShape');
+ throwIfReadOnly(modeling, 'createLabel');
+ throwIfReadOnly(modeling, 'appendShape');
+ throwIfReadOnly(modeling, 'removeElements');
+ throwIfReadOnly(modeling, 'distributeElements');
+ throwIfReadOnly(modeling, 'removeShape');
+ throwIfReadOnly(modeling, 'removeConnection');
+ throwIfReadOnly(modeling, 'replaceShape');
+ throwIfReadOnly(modeling, 'pasteElements');
+ throwIfReadOnly(modeling, 'alignElements');
+ throwIfReadOnly(modeling, 'resizeShape');
+ throwIfReadOnly(modeling, 'createSpace');
+ throwIfReadOnly(modeling, 'updateWaypoints');
+ throwIfReadOnly(modeling, 'reconnectStart');
+ throwIfReadOnly(modeling, 'reconnectEnd');
+
+ intercept(paletteProvider, 'getPaletteEntries', function(fn, args) {
+ var entries = fn.apply(this, args);
+ if (self._readOnly) {
+ forEach(entries, function(value, key) {
+ delete entries[key];
+ });
+ }
+ return entries;
+ });
+}
+
+ReadOnly.$inject = [
+ 'eventBus',
+ 'contextPad',
+ 'dragging',
+ 'directEditing',
+ 'editorActions',
+ 'modeling',
+ 'palette',
+ 'paletteProvider'
+];
+
+module.exports = ReadOnly;
+
+ReadOnly.prototype.readOnly = function(readOnly) {
+ var newValue = !!readOnly,
+ oldValue = !!this._readOnly;
+
+ if (readOnly === undefined || newValue === oldValue) {
+ return oldValue;
+ }
+
+ this._readOnly = newValue;
+ this._eventBus.fire('readOnly.changed', { readOnly: newValue });
+ return newValue;
+};