/
DisableModeling.js
126 lines (101 loc) · 3.16 KB
/
DisableModeling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {
TOGGLE_MODE_EVENT
} from '../../util/EventHelper';
const HIGH_PRIORITY = 10001;
export default function DisableModeling(
eventBus,
contextPad,
dragging,
directEditing,
editorActions,
modeling,
palette) {
let modelingDisabled = false;
eventBus.on(TOGGLE_MODE_EVENT, HIGH_PRIORITY, event => {
modelingDisabled = event.active;
if (modelingDisabled) {
directEditing.cancel();
dragging.cancel();
}
palette._update();
});
function intercept(obj, fnName, cb) {
const fn = obj[fnName];
obj[fnName] = function() {
return cb.call(this, fn, arguments);
};
}
function ignoreIfModelingDisabled(obj, fnName) {
intercept(obj, fnName, function(fn, args) {
if (modelingDisabled) {
return;
}
return fn.apply(this, args);
});
}
function throwIfModelingDisabled(obj, fnName) {
intercept(obj, fnName, function(fn, args) {
if (modelingDisabled) {
throw new Error('model is read-only');
}
return fn.apply(this, args);
});
}
ignoreIfModelingDisabled(dragging, 'init');
ignoreIfModelingDisabled(directEditing, 'activate');
ignoreIfModelingDisabled(dragging, 'init');
ignoreIfModelingDisabled(directEditing, 'activate');
throwIfModelingDisabled(modeling, 'moveShape');
throwIfModelingDisabled(modeling, 'updateAttachment');
throwIfModelingDisabled(modeling, 'moveElements');
throwIfModelingDisabled(modeling, 'moveConnection');
throwIfModelingDisabled(modeling, 'layoutConnection');
throwIfModelingDisabled(modeling, 'createConnection');
throwIfModelingDisabled(modeling, 'createShape');
throwIfModelingDisabled(modeling, 'createLabel');
throwIfModelingDisabled(modeling, 'appendShape');
throwIfModelingDisabled(modeling, 'removeElements');
throwIfModelingDisabled(modeling, 'distributeElements');
throwIfModelingDisabled(modeling, 'removeShape');
throwIfModelingDisabled(modeling, 'removeConnection');
throwIfModelingDisabled(modeling, 'replaceShape');
throwIfModelingDisabled(modeling, 'pasteElements');
throwIfModelingDisabled(modeling, 'alignElements');
throwIfModelingDisabled(modeling, 'resizeShape');
throwIfModelingDisabled(modeling, 'createSpace');
throwIfModelingDisabled(modeling, 'updateWaypoints');
throwIfModelingDisabled(modeling, 'reconnectStart');
throwIfModelingDisabled(modeling, 'reconnectEnd');
intercept(editorActions, 'trigger', function(fn, args) {
const action = args[0];
if (modelingDisabled && isAnyAction([
'undo',
'redo',
'copy',
'paste',
'removeSelection',
'spaceTool',
'lassoTool',
'globalConnectTool',
'distributeElements',
'alignElements',
'directEditing',
], action)) {
return;
}
return fn.apply(this, args);
});
}
DisableModeling.$inject = [
'eventBus',
'contextPad',
'dragging',
'directEditing',
'editorActions',
'modeling',
'palette'
];
// helpers //////////
function isAnyAction(actions, action) {
return actions.indexOf(action) > -1;
}