-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.js
360 lines (325 loc) · 9.15 KB
/
view.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
350
351
352
353
354
355
356
357
358
359
"use strict";
// configuration objects
var nodeStyle = {
// These usually need to contain 5's and stuff to avoid decimal errors when converted to pixels
width: 100,
height: 30,
fontSize: 10
};
var config = {
canvasId: "maindraw",
width: 10000,
height: 9000
};
// Shared objects initialization
var canv, ctx;
function init() {
canv = document.getElementById(config.canvasId);
canv.height = config.height;
canv.width = config.width;
ctx = canv.getContext('2d');
paths = paths.map(function(path) {
return new Path(
path.from,
path.to
);
});
nodes = nodes.map(function(node) {
return new Node(
node.name,
node.alterNames,
node.gender,
node.gridX,
node.gridY
);
});
}
function renderPaths() {
paths.forEach(function(path) {
// Begin sketching path
var pathPoints = path.convertToSteps(nodeStyle);
if (path.isMarriage())
ctx.setLineDash([1, 0, 1, 1]);
else
ctx.setLineDash([1, 0]);
ctx.beginPath();
ctx.moveTo(pathPoints[0][0], pathPoints[0][1]);
pathPoints
.slice(1)
.forEach(function(point) {
ctx.lineTo(point[0], point[1]);
});
ctx.stroke();
});
}
function renderNodes() {
// Set some common properties for rendering
var font = nodeStyle.fontSize + "px sans serif";
ctx.textAlign = "center";
// Start render
nodes.forEach(function(node) {
// First render the rectangle
var rect = node.convertToRect(nodeStyle);
ctx.fillStyle = node.gender === "m" ?
"steelblue" : "#6f6";
ctx.fillRect.apply(ctx, rect);
//Add a node border
ctx.strokeStyle = "black";
ctx.strokeRect.apply(ctx, rect);
// Now render the text
ctx.font = font;
ctx.fillStyle = "black";
ctx.setLineDash([1,0]);
ctx.fillText(node.name,
nodeStyle.width * node.gridX,
nodeStyle.height * node.gridY - nodeStyle.fontSize/2
);
ctx.font = "italic " + font;
ctx.fillText(node.alterNames
.filter(function(elem) {
return elem;
})
.join(", "),
nodeStyle.width * node.gridX,
nodeStyle.height * node.gridY +nodeStyle.fontSize/2
);
});
}
function render() {
ctx.clearRect(0, 0, config.width, config.height);
renderPaths();
renderNodes();
}
var trackNodePath = function(e) {
return track(e, true);
};
var trackNode = function(e) {
return track(e, false);
};
function elementClickedOn(e, nodeStyle) {
var elementsClickedOn = nodes.filter(function (elem) {
var boundX = (elem.gridX - 0.5) * nodeStyle.width;
var boundY = (elem.gridY - 0.5) * nodeStyle.height;
return (e.pageY > boundY &&
e.pageY < boundY+nodeStyle.height &&
e.pageX > boundX &&
e.pageX < boundX + nodeStyle.width)
});
return elementsClickedOn.length === 1 ?
elementsClickedOn[0] : null;
}
function startNodeAdd(e) {
// Make a new Node
nodes.push( new Node("New Node", [], "f", 0, 0));
// Do I also need to initialize a new Path?
if (elementClickedOn(e, nodeStyle)) {
paths.push(new Path(
elementClickedOn(e, nodeStyle),
nodes[nodes.length - 1]
));
canv.addEventListener("mousemove", trackNodePath);
}
else {
canv.addEventListener("mousemove", trackNode);
}
canv.removeEventListener("click", startNodeAdd);
canv.addEventListener("click", fixNode);
}
function startNodeModify(e) {
// First, we need to be on top of a node to do anything about it
var elem = elementClickedOn(e, nodeStyle);
if (elem === null) return;
canv.removeEventListener("click", startNodeModify);
populateFromUser(elem).then(function resolve(node) {
elem = node;
render();
canv.addEventListener("click", startNodeModify);
});
}
function startNodeDeletion(e) {
var elem = elementClickedOn(e, nodeStyle);
if (elem === null) return;
nodes = nodes.filter(function(node) {
return !(elem.gridX == node.gridX && elem.gridY == node.gridY);
});
paths = paths.filter(function(path) {
return !(
((path.to.gridX === elem.gridX) && (path.to.gridY === elem.gridY)) ||
((path.from.gridX === elem.gridX) && path.from.gridY === elem.gridY)
);
});
render();
}
function fixNode(e) {
canv.removeEventListener("click", fixNode);
canv.removeEventListener("mousemove", trackNode);
canv.removeEventListener("mousemove", trackNodePath);
var oldNode = nodes.pop();
populateFromUser(oldNode).then(function resolve(node) {
canv.addEventListener("click", startNodeAdd);
nodes.push(node);
// Deal with paths
paths.forEach(function(path) {
if (path.from === oldNode)
path.from = node;
if (path.to === oldNode)
path.to = node;
});
render();
});
}
// TODO: Refactor to track in an elegant way
function track(e, path) {
var snapToGrid = function(x, y) {
return [
Math.round(x/nodeStyle.width) * nodeStyle.width,
Math.round(y/nodeStyle.height) * nodeStyle.height
];
};
var gridCoords = snapToGrid(e.pageX, e.pageY);
var newNode = new Node("New Node", [], "f",
gridCoords[0]/nodeStyle.width + 0.5,
gridCoords[1]/nodeStyle.height + 0.5);
var oldNode = nodes.pop();
nodes.push(newNode);
if (path) {
var oldPath = paths.pop();
paths.push(new Path(
oldPath.from,
newNode
));
}
if (JSON.stringify(newNode) !== JSON.stringify(oldNode))
render();
}
// Get user input when needed
function populateFromUser(node) {
var divProperties = {
left: node.gridX * nodeStyle.width,
top: node.gridY * nodeStyle.height
};
// Some ugly DOM manipulation follows
var questionDiv = document.createElement("div");
questionDiv.id = "questionDiv";
questionDiv.style.left = divProperties.left + "px";
questionDiv.style.top = divProperties.top + "px";
// Create input elements
var createTextInput = function(id, placeholder, value) {
var inp = document.createElement("input");
inp.type = "text"
inp.id = id;
inp.placeholder = placeholder;
if (value)
inp.value = value;
return inp;
}
var nameInput = createTextInput("nameInput", "Name", node.name);
var nickNameInput = createTextInput("nickNameInput", "Optional nickname", node.alterNames[1]);
var maidenNameInput = createTextInput("maidenNameInput", "Optional maiden name", node.alterNames[0]);
var genderInput = createTextInput("genderInput", "Gender - m or f", node.gender);
var submitInput = document.createElement("input");
submitInput.type = "submit";
submitInput.value = "Done";
// Add all to questionDiv
[ nameInput,
nickNameInput,
maidenNameInput,
genderInput,
submitInput
].forEach(function(elem) {
questionDiv.appendChild(elem);
});
document.body.appendChild(questionDiv);
return new Promise(function(resolve, reject) {
submitInput.addEventListener("click", function(){
node.name = nameInput.value;
node.gender = genderInput.value;
node.alterNames = [];
if (maidenNameInput.value)
node.alterNames.push(maidenNameInput.value);
else
node.alterNames.push("");
if (nickNameInput.value)
node.alterNames.push(nickNameInput.value);
else
node.alterNames.push("");
questionDiv.parentNode.removeChild(questionDiv);
questionDiv = null; // necessary?
return resolve(node);
});
});
}
// Main entry point code
// Need to process initial data
var nodeClickListeners = [startNodeAdd, startNodeDeletion, startNodeModify, fixNode];
function clearListeners() {
nodeClickListeners.forEach(function(listener) {
canv.removeEventListener("click", listener);
});
}
function startAddMode() {
clearListeners();
canv.addEventListener("click", startNodeAdd);
}
function startDeleteMode() {
clearListeners();
canv.addEventListener("click", startNodeDeletion);
}
function startModifyMode() {
clearListeners();
canv.addEventListener("click", startNodeModify);
}
function trimNodes(nodeList, pathList) {
var getGridX = function(elem) {
return elem.gridX;
};
var getGridY = function(elem) {
return elem.gridY;
};
var leftMin = Math.min.apply(null, nodeList.map(getGridX));
leftMin -= 1;
var topMin = Math.min.apply(null, nodeList.map(getGridY));
topMin -= 1;
nodeList.forEach(function trim(node) {
node.gridX -= leftMin;
node.gridY -= topMin;
});
pathList.forEach(function trim(path) {
path.from.gridX -= leftMin;
path.from.gridY -= topMin;
path.to.gridX -= leftMin;
path.to.gridY -= topMin;
});
}
function startViewMode() {
clearListeners();
trimNodes(nodes, paths);
canv.onmousemove=function(e){mouse={x:e.pageX-this.offsetLeft,y:e.pageY-this.offsetTop};}
canv.onmousemove=function(e){mouse={x:e.pageX-this.offsetLeft,y:e.pageY-this.offsetTop};}
var isDown = false;
var startCoords = [];
var last = [0, 0];
canv.onmousedown = function(e) {
isDown = true;
startCoords = [
e.offsetX - last[0],
e.offsetY - last[1]
];
};
canv.onmouseup = function(e) {
isDown = false;
last = [
e.offsetX - startCoords[0], // set last coordinates
e.offsetY - startCoords[1]
];
};
canv.onmousemove = function(e)
{
if(!isDown) return;
var x = e.offsetX;
var y = e.offsetY;
ctx.setTransform(1, 0, 0, 1,
x - startCoords[0], y - startCoords[1]);
render();
}
}