-
Notifications
You must be signed in to change notification settings - Fork 1
/
mindmap.js
349 lines (278 loc) · 10.7 KB
/
mindmap.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*global Pablo*/
/*jshint newcap:false*/
var MindMap = (function(){
'use strict';
function MindMap(htmlContainer){
var mindmap = this;
// Create SVG root
this.svg = Pablo(htmlContainer).svg({
// Alternatively, specify `svg {width:100%; height:100%;}`
// in the CSS stylesheet
width: '100%',
height: '100%'
})
// Set up event handlers - see mindmap.onmousedown(), etc
.on('mousedown mousemove mouseup mouseout', function(event){
mindmap['on' + event.type](event);
});
// Create a simple object cache in memory
this.cache = {}; // data about each node
// Add instructions message
this.addInstructions();
}
MindMap.prototype = {
PADDING_X: 8,
PADDING_Y: 5,
CORNER_R: 5,
FONTSIZE: 20,
PATH_CURVE: 30,
idCounter: 1,
trim: function(str){
return str ? str.replace(/^\s\s*/, '').replace(/\s\s*$/, '') : '';
},
getId: function(node){
return node && node.attr('data-id');
},
// Add a <text> element with instructions
addInstructions: function(){
this.svg.text({x:'50%', y:'50%'})
.addClass('instructions')
.content('Click anywhere to create nodes');
return this;
},
// Remove the instructions <text> element
removeInstructions: function(){
this.svg.find('.instructions').remove();
return this;
},
// Ask the user what text to put in a new node
userCreate: function(x, y){
var title = this.trim(window.prompt('What\'s the text?')),
parentId = this.getId(this.selected);
if (title){
this.drawNode(parentId, x, y, title);
}
return this;
},
drawNode: function(parentId, x, y, title){
var nodeId, parent, path, node, nodeData, text, rect, textBBox;
// Generate a new id for the node
nodeId = this.idCounter ++;
// Remove the instructions text
if (!parentId) {
this.removeInstructions();
}
// Store data about the node in a lookup object
nodeData = this.cache[nodeId] = {
id:nodeId, parentId:parentId,
x:x, y:y,
title:title
};
// Update the node's text and position, and mark it `selected`
return this.createNodeElements(nodeData)
.updateText(nodeData, title)
.updatePosition(nodeData, x, y)
.makeSelected(nodeId);
},
createNodeElements: function(nodeData){
var parentId = nodeData.parentId,
parentData = this.cache[parentId],
parent, path, node, rect, text;
// If there's no parent (e.g. for the first node), append to the SVG root
parent = parentData ?
parentData.node : this.svg;
// Append a <g> group element to the parent to represent the
// mindmap node in the DOM
node = parent.g({'data-id': nodeData.id}).addClass('node');
// Append a <rect> rectangle element, with rounded corners
rect = node.rect({rx: this.CORNER_R});
// Append a <text> element, with padding
text = node.text({
x: this.PADDING_X,
y: this.FONTSIZE,
'font-size': this.FONTSIZE
});
// Create a <path> element to visually connect the parent and node.
// Its coordinates are set by the `updatePosition` method.
// We prepend it so that it appears beneath the parent's rectangle.
path = parentId ?
Pablo.path().prependTo(parent) : null;
// Extend the cached lookup object to give quick access to the elements
Pablo.extend(nodeData, {node:node, rect:rect, text:text, path:path});
return this;
},
updateText: function(nodeData, title){
var text = nodeData.text,
rect = nodeData.rect,
bbox, nodeDimensions;
// Set the text element's contents
text.content(title);
// Get the text's rendered dimensions. `getBBox()` is a native SVG DOM method
bbox = text[0].getBBox();
// Add padding for the rectangle's dimensions and update the node data
nodeDimensions = {
width: bbox.width + this.PADDING_X * 2,
height: bbox.height + this.PADDING_Y * 2
};
// Update the cached data and apply to the <rect> element
Pablo.extend(nodeData, nodeDimensions);
rect.attr(nodeDimensions);
return this;
},
updatePosition: function(nodeData, x, y){
var node = nodeData.node,
parentData = this.cache[nodeData.parentId],
pathData;
// Update stored data
nodeData.x = x;
nodeData.y = y;
// x and y are initially page coordinates -> make them relative to the parent
if (parentData){
x -= parentData.x;
y -= parentData.y;
}
// Translate the node to the new coordinates
node.transform('translate', x, y);
// Update the path drawn between the parent and node
return this.updatePath(parentData, nodeData);
},
updatePath: function(parentData, nodeData){
// Get the <path> element
var path = nodeData.path,
pathData;
if (path){
// Calculate the curve between the parent and node
pathData = this.getPathData(parentData, nodeData)
// Set the element's `d` (data) attribute with the path data
path.attr('d', pathData);
}
return this;
},
// Draw a path from the parent to the child
// `p` = parentData, `n` = nodeData
getPathData: function(p, n){
var isLeft = p.x < n.x,
isAbove = p.y < n.y,
x1 = isLeft ? p.width : 0,
x2 = n.x - p.x + (isLeft ? -x1 : n.width),
y1 = p.height / 2,
y2 = n.y - p.y,
curve = this.PATH_CURVE,
xCtrl = x2 / 2 + (isLeft ? -curve : curve),
yCtrl = y2 / 2 + (isAbove ? curve : -curve);
return 'm' + x1 + ' ' + y1 +
'q' + xCtrl + ' ' + yCtrl + ',' + x2 + ' ' + y2;
},
makeSelected: function(nodeId){
var node = this.cache[nodeId].node;
// De-selected currently selected node
if (this.selected){
this.selected.removeClass('selected');
}
// Store node as `mindmap.selected` property
this.selected = node
// Add a CSS class
.addClass('selected')
// Bring to front, to prevent the node being dragged behind another node
.appendTo(node.parent());
return this;
},
/*
removeNode: function(nodeData){
nodeData.node.remove();
nodeData.path.remove();
delete this.cache[nodeData.id];
return this;
},
*/
nearestNode: function(el){
var node = Pablo(el);
return node.hasClass('node') ?
node : node.parents('.node').first();
},
onmousedown: function(event){
var nodeId, node, x, y;
// left button click
if (event.which === 1){
node = this.nearestNode(event.target);
x = event.pageX;
y = event.pageY;
if (node.length){
nodeId = node.attr('data-id');
this.makeSelected(nodeId);
this.dragStart(node, x, y);
}
else {
this.userCreate(x, y);
}
}
},
onmousemove: function(event){
if (this.dragging){
this.drag(event.pageX, event.pageY);
}
},
onmouseup: function(){
if (this.dragging){
this.dragStop();
}
},
// When the mouse leaves the SVG element, stop dragging
// E.g. this prevents the mouse dragging out of the window, the mouse
// button released _outside_ the window and returning, still dragging
onmouseout: function(event){
var to, isSvg;
if (this.dragging){
// Which element is the mouse entering?
to = event.relatedTarget;
// Is that element the SVG root or one of its children?
isSvg = to && (to === this.svg[0] || to.ownerSVGElement === this.svg[0]);
// Stop dragging when the mouse leaves the SVG element
if (!isSvg){
this.dragStop();
}
}
},
dragStart: function(node, x, y){
var nodeId = this.getId(node),
nodeData = this.cache[nodeId];
// Store data about the node being dragged
// The offset is the distance between the node's x,y origin and the mouse cursor
this.dragging = {
nodeData: nodeData,
offsetX: x - nodeData.x,
offsetY: y - nodeData.y
};
return this;
},
drag: function(x, y){
// Retrieve the stored data about the node being dragged
var d = this.dragging;
// Remove the offset between the node's x,y origin and the mouse cursor
x -= d.offsetX;
y -= d.offsetY;
// Update the node's position
return this.updatePosition(d.nodeData, x, y);
},
dragStop: function(){
this.dragging = null;
return this;
}
};
/////
// Check browser support
if (Pablo.isSupported){
var mm = new MindMap('#mindmap');
// TEST DATA
/*
mm.drawNode(null, 220, 300, 'Trees')
.drawNode(1, 100, 100, 'Birch')
.drawNode(1, 150, 500, 'Oak')
.drawNode(3, 10, 400, 'Larch')
.drawNode(1, 310, 230, 'Pine');
window.mm = mm;
*/
}
/////
return MindMap;
}());