1 import forEach from 'lodash.foreach';
3 const HIGH_PRIORITY = 10001;
15 this._readOnly = false;
16 this._eventBus = eventBus;
19 eventBus.on('readOnly.changed', HIGH_PRIORITY, function(e) {
20 self._readOnly = e.readOnly;
23 directEditing.cancel();
31 function intercept(obj, fnName, cb) {
33 obj[fnName] = function() {
34 return cb.call(this, fn, arguments);
38 function ignoreWhenReadOnly(obj, fnName) {
39 intercept(obj, fnName, function(fn, args) {
44 return fn.apply(this, args);
48 function throwIfReadOnly(obj, fnName) {
49 intercept(obj, fnName, function(fn, args) {
51 throw new Error('model is read-only');
54 return fn.apply(this, args);
58 ignoreWhenReadOnly(contextPad, 'open');
60 ignoreWhenReadOnly(dragging, 'init');
62 ignoreWhenReadOnly(directEditing, 'activate');
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');
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');
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');
99 intercept(paletteProvider, 'getPaletteEntries', function(fn, args) {
100 var entries = fn.apply(this, args);
101 if (self._readOnly) {
102 let allowedEntries = ['hand-tool'];
104 forEach(entries, function(value, key) {
105 if (allowedEntries.indexOf(key) === -1) {
125 module.exports = ReadOnly;
127 ReadOnly.prototype.readOnly = function(readOnly) {
128 var newValue = !!readOnly,
129 oldValue = !!this._readOnly;
131 if (readOnly === undefined || newValue === oldValue) {
135 this._readOnly = newValue;
136 this._eventBus.fire('readOnly.changed', { readOnly: newValue });