-
Notifications
You must be signed in to change notification settings - Fork 592
/
MindMapModel.js
235 lines (207 loc) · 6.2 KB
/
MindMapModel.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* <pre>
* Creates a new MindMapModel.
*
* This object represents the underlying mind map model and provides access
* to the document, the mind map and the currently selected node.
*
* All changes to the mind map pass through this object, either through calling
* methods directly or using the executeAction() method to perform NodeActions.
* </pre>
*
* @constructor
* @param {mindmaps.EventBus} eventBus
* @param {mindmaps.CommandRegistry} commandRegistry
*/
mindmaps.MindMapModel = function(eventBus, commandRegistry, undoController) {
var self = this;
this.document = null;
this.selectedNode = null;
/**
* Gets the current document.
*
* @returns {mindmaps.Document} the current document.
*/
this.getDocument = function() {
return this.document;
};
/**
* Sets the current document and will publish a DOCUMENT_OPENED or
* DOCUMENT_CLOSED event.
*
* @param {mindmaps.Document} doc or pass null to close the document
*/
this.setDocument = function(doc) {
this.document = doc;
if (doc) {
eventBus.publish(mindmaps.Event.DOCUMENT_OPENED, doc);
} else {
eventBus.publish(mindmaps.Event.DOCUMENT_CLOSED);
}
};
/**
* Gets the current mind map associated with the document.
*
* @returns {mindmaps.MindMap} the mind map or null
*/
this.getMindMap = function() {
if (this.document) {
return this.document.mindmap;
}
return null;
};
/**
* Initialise.
*
* @private
*/
this.init = function() {
var createNodeCommand = commandRegistry.get(mindmaps.CreateNodeCommand);
createNodeCommand.setHandler(this.createNode.bind(this));
var createSiblingNodeCommand = commandRegistry
.get(mindmaps.CreateSiblingNodeCommand);
createSiblingNodeCommand.setHandler(this.createSiblingNode.bind(this));
var deleteNodeCommand = commandRegistry.get(mindmaps.DeleteNodeCommand);
deleteNodeCommand.setHandler(this.deleteNode.bind(this));
eventBus.subscribe(mindmaps.Event.DOCUMENT_CLOSED, function() {
createNodeCommand.setEnabled(false);
createSiblingNodeCommand.setEnabled(false);
deleteNodeCommand.setEnabled(false);
});
eventBus.subscribe(mindmaps.Event.DOCUMENT_OPENED, function() {
createNodeCommand.setEnabled(true);
createSiblingNodeCommand.setEnabled(true);
deleteNodeCommand.setEnabled(true);
});
};
/**
* Deletes a node or the currently selected one if no argument is passed.
*
* @param {mindmaps.Node} [node] defaults to currently selected.
*/
this.deleteNode = function(node) {
if (!node) {
node = this.selectedNode;
}
var map = this.getMindMap();
var action = new mindmaps.action.DeleteNodeAction(node, map);
this.executeAction(action);
};
/**
* Attaches a new node the mind map. If invoked without arguments, it will
* add a new child to the selected node with an automatically generated
* position.
*
* @param {mindmaps.Node} node the new node
* @param {mindmaps.Node} parent
*/
this.createNode = function(node, parent) {
var map = this.getMindMap();
if (!(node && parent)) {
parent = this.selectedNode;
var action = new mindmaps.action.CreateAutoPositionedNodeAction(
parent, map);
} else {
var action = new mindmaps.action.CreateNodeAction(node, parent, map);
}
this.executeAction(action);
};
/**
* Creates a new auto positioned node as a sibling to the current selected
* node.
*/
this.createSiblingNode = function() {
var map = this.getMindMap();
var selected = this.selectedNode;
var parent = selected.getParent();
// root nodes dont have a parent
if (parent === null) {
return;
}
var action = new mindmaps.action.CreateAutoPositionedNodeAction(parent,
map);
this.executeAction(action);
};
/**
* Sets the node as the currently selected.
*
* @param {mindmaps.Node} node
*/
this.selectNode = function(node) {
if (node === this.selectedNode) {
return;
}
var oldSelected = this.selectedNode;
this.selectedNode = node;
eventBus.publish(mindmaps.Event.NODE_SELECTED, node, oldSelected);
};
/**
* Changes the caption for the passed node or for the selected one if node
* is null.
*
* @param {mindmaps.Node} node
* @param {String} caption
*/
this.changeNodeCaption = function(node, caption) {
if (!node) {
node = this.selectedNode;
}
var action = new mindmaps.action.ChangeNodeCaptionAction(node, caption);
this.executeAction(action);
};
/**
* Executes a node action. An executed action might raise an event over the
* event bus and cause an undo event to be emitted via
* MindMapModel#undoAction.
*
* @param {mindmaps.Action} action
*/
this.executeAction = function(action) {
// a composite action consists of multiple actions which are
// processed individually.
if (action instanceof mindmaps.action.CompositeAction) {
var execute = this.executeAction.bind(this);
action.forEachAction(execute);
return;
}
var executed = action.execute();
// cancel action if false was returned
if (executed !== undefined && !executed) {
return false;
}
// publish event
if (action.event) {
if (!Array.isArray(action.event)) {
action.event = [ action.event ];
}
eventBus.publish.apply(eventBus, action.event);
}
// register undo function if available
if (action.undo) {
var undoFunc = function() {
self.executeAction(action.undo());
};
// register redo function
if (action.redo) {
var redoFunc = function() {
self.executeAction(action.redo());
};
}
undoController.addUndo(undoFunc, redoFunc);
}
};
/**
* Saves a document to the localstorage and publishes DOCUMENT_SAVED event on success.
*
* @returns {Boolean} whether the save was successful.
*/
this.saveToLocalStorage = function() {
var doc = this.document.prepareSave();
var success = mindmaps.LocalDocumentStorage.saveDocument(doc);
if (success) {
eventBus.publish(mindmaps.Event.DOCUMENT_SAVED, doc);
}
return success;
}
this.init();
};