composition readonly implementation
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / version / composition / readOnly.js
1 import forEach from 'lodash.foreach';
2
3 const HIGH_PRIORITY = 10001;
4
5 function ReadOnly(
6     eventBus,
7     contextPad,
8     dragging,
9     directEditing,
10     editorActions,
11     modeling,
12     palette,
13     paletteProvider
14 ) {
15     this._readOnly = false;
16     this._eventBus = eventBus;
17
18     let self = this;
19     eventBus.on('readOnly.changed', HIGH_PRIORITY, function(e) {
20         self._readOnly = e.readOnly;
21
22         if (e.readOnly) {
23             directEditing.cancel();
24             contextPad.close();
25             dragging.cancel();
26         }
27
28         palette._update();
29     });
30
31     function intercept(obj, fnName, cb) {
32         var fn = obj[fnName];
33         obj[fnName] = function() {
34             return cb.call(this, fn, arguments);
35         };
36     }
37
38     function ignoreWhenReadOnly(obj, fnName) {
39         intercept(obj, fnName, function(fn, args) {
40             if (self._readOnly) {
41                 return;
42             }
43
44             return fn.apply(this, args);
45         });
46     }
47
48     function throwIfReadOnly(obj, fnName) {
49         intercept(obj, fnName, function(fn, args) {
50             if (self._readOnly) {
51                 throw new Error('model is read-only');
52             }
53
54             return fn.apply(this, args);
55         });
56     }
57
58     ignoreWhenReadOnly(contextPad, 'open');
59
60     ignoreWhenReadOnly(dragging, 'init');
61
62     ignoreWhenReadOnly(directEditing, 'activate');
63
64     ignoreWhenReadOnly(editorActions._actions, 'undo');
65     ignoreWhenReadOnly(editorActions._actions, 'redo');
66     ignoreWhenReadOnly(editorActions._actions, 'copy');
67     ignoreWhenReadOnly(editorActions._actions, 'paste');
68     ignoreWhenReadOnly(editorActions._actions, 'removeSelection');
69     // BpmnEditorActions
70     ignoreWhenReadOnly(editorActions._actions, 'spaceTool');
71     ignoreWhenReadOnly(editorActions._actions, 'lassoTool');
72     ignoreWhenReadOnly(editorActions._actions, 'globalConnectTool');
73     ignoreWhenReadOnly(editorActions._actions, 'distributeElements');
74     ignoreWhenReadOnly(editorActions._actions, 'alignElements');
75     ignoreWhenReadOnly(editorActions._actions, 'directEditing');
76
77     throwIfReadOnly(modeling, 'moveShape');
78     throwIfReadOnly(modeling, 'updateAttachment');
79     throwIfReadOnly(modeling, 'moveElements');
80     throwIfReadOnly(modeling, 'moveConnection');
81     throwIfReadOnly(modeling, 'layoutConnection');
82     throwIfReadOnly(modeling, 'createConnection');
83     throwIfReadOnly(modeling, 'createShape');
84     throwIfReadOnly(modeling, 'createLabel');
85     throwIfReadOnly(modeling, 'appendShape');
86     throwIfReadOnly(modeling, 'removeElements');
87     throwIfReadOnly(modeling, 'distributeElements');
88     throwIfReadOnly(modeling, 'removeShape');
89     throwIfReadOnly(modeling, 'removeConnection');
90     throwIfReadOnly(modeling, 'replaceShape');
91     throwIfReadOnly(modeling, 'pasteElements');
92     throwIfReadOnly(modeling, 'alignElements');
93     throwIfReadOnly(modeling, 'resizeShape');
94     throwIfReadOnly(modeling, 'createSpace');
95     throwIfReadOnly(modeling, 'updateWaypoints');
96     throwIfReadOnly(modeling, 'reconnectStart');
97     throwIfReadOnly(modeling, 'reconnectEnd');
98
99     intercept(paletteProvider, 'getPaletteEntries', function(fn, args) {
100         var entries = fn.apply(this, args);
101         if (self._readOnly) {
102             let allowedEntries = ['hand-tool'];
103
104             forEach(entries, function(value, key) {
105                 if (allowedEntries.indexOf(key) === -1) {
106                     delete entries[key];
107                 }
108             });
109         }
110         return entries;
111     });
112 }
113
114 ReadOnly.$inject = [
115     'eventBus',
116     'contextPad',
117     'dragging',
118     'directEditing',
119     'editorActions',
120     'modeling',
121     'palette',
122     'paletteProvider'
123 ];
124
125 module.exports = ReadOnly;
126
127 ReadOnly.prototype.readOnly = function(readOnly) {
128     var newValue = !!readOnly,
129         oldValue = !!this._readOnly;
130
131     if (readOnly === undefined || newValue === oldValue) {
132         return oldValue;
133     }
134
135     this._readOnly = newValue;
136     this._eventBus.fire('readOnly.changed', { readOnly: newValue });
137     return newValue;
138 };