-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_Graph.js
487 lines (454 loc) · 14.7 KB
/
DB_Graph.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
var paper = null;
/**
* Create a line/conection between two elements
* @param {Number} relations_counter: Relations with other tables
* @param {Number} inPosition: Table to draw
* @param {array} obj1: Origin table
* @param {array} obj2: Destiny table
* @param {String} line: Line color
* @param {String} bg: Table background
* @return {JSON}: Relation data from Raphael
*/
Raphael.fn.connection = function (relations_counter, inPosition, obj1, obj2, line, bg) {
if (obj1.line && obj1.from && obj1.to) {
line = obj1;
obj1 = line.from;
obj2 = line.to;
}
var position = inPosition + 1;
var bb1 = obj1.getBBox(),
bb2 = obj2.getBBox(),
//Here is where lines are created
p = [
{x: bb1.x + (bb1.width / relations_counter) * (position / 2), y: bb1.y - 1},
{x: bb1.x + (bb1.width / relations_counter) * (position / 2), y: bb1.y + bb1.height + 1},
{x: bb1.x - 1, y: bb1.y + (bb1.height / relations_counter) * (position / 2)},
{x: bb1.x + bb1.width + 1, y: bb1.y + (bb1.height / relations_counter) * (position / 2)},
{x: bb2.x + (bb2.width / relations_counter) * (position / 2), y: bb2.y - 1},
{x: bb2.x + (bb2.width / relations_counter) * (position / 2), y: bb2.y + bb2.height + 1},
{x: bb2.x - 1, y: bb2.y + (bb2.height / relations_counter) * (position / 2)},
{x: bb2.x + bb2.width + 1, y: bb2.y + (bb2.height / relations_counter) * (position / 2)}
],
d = {}, dis = [];
for (var i = 0; i < 4; i++) {
for (var j = 4; j < 8; j++) {
var dx = Math.abs(p[i].x - p[j].x),
dy = Math.abs(p[i].y - p[j].y);
if ((i == j - 4) || (((i != 3 && j != 6) || p[i].x < p[j].x) && ((i != 2 && j != 7) || p[i].x > p[j].x) && ((i != 0 && j != 5) || p[i].y > p[j].y) && ((i != 1 && j != 4) || p[i].y < p[j].y))) {
dis.push(dx + dy);
d[dis[dis.length - 1]] = [i, j];
}
}
}
if (dis.length == 0) {
var res = [0, 4];
} else {
res = d[Math.min.apply(Math, dis)];
}
var x1 = p[res[0]].x,
y1 = p[res[0]].y,
x4 = p[res[1]].x,
y4 = p[res[1]].y;
dx = Math.max(Math.abs(x1 - x4) / 2, 10);
dy = Math.max(Math.abs(y1 - y4) / 2, 10);
var x2 = [x1, x1, x1 - dx, x1 + dx][res[0]].toFixed(3),
y2 = [y1 - dy, y1 + dy, y1, y1][res[0]].toFixed(3),
x3 = [0, 0, 0, 0, x4, x4, x4 - dx, x4 + dx][res[1]].toFixed(3),
y3 = [0, 0, 0, 0, y1 + dy, y1 - dy, y4, y4][res[1]].toFixed(3);
var path = ["M", x1.toFixed(3), y1.toFixed(3), "L", x2, y2, x3, y3, x4.toFixed(3), y4.toFixed(3)].join(",");
if (line && line.line) {
line.bg && line.bg.attr({path: path});
line.line.attr({path: path});
} else {
var color = typeof line == "string" ? line : "#000";
return {
bg: bg && bg.split && this.path(path).attr({stroke: bg.split("|")[0], fill:"square", "stroke-width": bg.split("|")[1] || 3}),
line: this.path(path).attr({stroke: color, fill: "none"}),
from: obj1,
to: obj2
};
}
};
/**
* Allows to move a table and changes its connections possitions
* @param {array} obj1: Table in movement
* @param {Number} relations_counter: Relations with other tables
* @param {Number} position: Position of the line (first, second, third)
*/
Raphael.fn.DragConnection = function (obj1, relations_counter, position) {
var line;
var bg;
if (obj1.line && obj1.from && obj1.to) {
line = obj1;
obj1 = line.from;
obj2 = line.to;
}
relations_counter++;
var bb1 = obj1.getBBox(),
bb2 = obj2.getBBox(),
//Here is where lines are created
p = [
{x: bb1.x + (bb1.width / relations_counter) * ((position / 2) + 1), y: bb1.y - 1},
{x: bb1.x + (bb1.width / relations_counter) * ((position / 2) + 1), y: bb1.y + bb1.height + 1},
{x: bb1.x - 1, y: bb1.y + (bb1.height / relations_counter) * ((position / 2) + 1)},
{x: bb1.x + bb1.width + 1, y: bb1.y + (bb1.height / relations_counter) * ((position / 2) + 1)},
{x: bb2.x + (bb2.width / relations_counter) * ((position / 2) + 1), y: bb2.y - 1},
{x: bb2.x + (bb2.width / relations_counter) * ((position / 2) + 1), y: bb2.y + bb2.height + 1},
{x: bb2.x - 1, y: bb2.y + (bb2.height / relations_counter) * ((position / 2) + 1)},
{x: bb2.x + bb2.width + 1, y: bb2.y + (bb2.height / relations_counter) * ((position / 2) + 1)}
],
d = {}, dis = [];
for (var i = 0; i < 4; i++) {
for (var j = 4; j < 8; j++) {
var dx = Math.abs(p[i].x - p[j].x),
dy = Math.abs(p[i].y - p[j].y);
if ((i == j - 4) || (((i != 3 && j != 6) || p[i].x < p[j].x) && ((i != 2 && j != 7) || p[i].x > p[j].x) && ((i != 0 && j != 5) || p[i].y > p[j].y) && ((i != 1 && j != 4) || p[i].y < p[j].y))) {
dis.push(dx + dy);
d[dis[dis.length - 1]] = [i, j];
}
}
}
if (dis.length == 0) {
var res = [0, 4];
} else {
res = d[Math.min.apply(Math, dis)];
}
var x1 = p[res[0]].x,
y1 = p[res[0]].y,
x4 = p[res[1]].x,
y4 = p[res[1]].y;
dx = Math.max(Math.abs(x1 - x4) / 2, 10);
dy = Math.max(Math.abs(y1 - y4) / 2, 10);
var x2 = [x1, x1, x1 - dx, x1 + dx][res[0]].toFixed(3),
y2 = [y1 - dy, y1 + dy, y1, y1][res[0]].toFixed(3),
x3 = [0, 0, 0, 0, x4, x4, x4 - dx, x4 + dx][res[1]].toFixed(3),
y3 = [0, 0, 0, 0, y1 + dy, y1 - dy, y4, y4][res[1]].toFixed(3);
var path = ["M", x1.toFixed(3), y1.toFixed(3), "L", x2, y2, x3, y3, x4.toFixed(3), y4.toFixed(3)].join(",");
if (line && line.line) {
line.bg && line.bg.attr({path: path});
line.line.attr({path: path});
} else {
var color = typeof line == "string" ? line : "#000";
return {
bg: bg && bg.split && this.path(path).attr({stroke: bg.split("|")[0], fill:"square", "stroke-width": bg.split("|")[1] || 3}),
line: this.path(path).attr({stroke: color, fill: "none"}),
from: obj1,
to: obj2
};
}
};
/**
* Initializes the table elements
* @param {array} tables: List of tables to be drawed
*/
var TableListPresenter = function(tables) {
var gotX = false;
if(tables[0].x){
gotX = true;
}
var dragger = function () {
this.set.oBB = this.set.getBBox();
},
move = function (dx, dy) {
var bb = this.set.getBBox();
this.set.translate(this.set.oBB.x - bb.x + dx, this.set.oBB.y - bb.y + dy);
// Here is where relations are made when a table is moved. They are drawn all together
// Now I am going to validate the maximum number of relations to distribute the lines
var relations_counter = 0;
if(tables[0].relations){
var relations = tables[0].relations;
relations_counter = relations.length;
for(var tables_counter = 0; tables_counter < tables.length; tables_counter++){
current_relations = tables[tables_counter].relations;
if(current_relations.length > relations_counter){
relations_counter = current_relations.length;
}
}
}
if(relations_counter == 0){
relations_counter = 1;
}
for (var i = connections.length; i--;) {
paper.DragConnection(connections[i], relations_counter, i);
}
paper.safari();
},
up = function () {
},
connections = [];
var sets = [];
var settings;
settings = {
paper: {
width: 500,
height: 800,
table: {
gridColumns: 2,
topOffset: 50,
leftOffset: 50
}
},
name: {
rect: {
width: 80,
height: 20
},
text: {
topOffset: 10,
leftOffset: 5
}
},
column: {
rect: {
width: 80,
height: 20
},
text: {
topOffset: 10,
leftOffset: 5
}
}
};
var tableGrid = [];
for (var i = settings.paper.table.gridColumns - 1; i >= 0; i--) {
tableGrid.push(0);
}
var currentGridColumn = 0;
this.draw = function(element) {
paper = new Raphael(
element,
settings.paper.height,
settings.paper.width
);
for(var tableIndex = 0; tableIndex < tables.length; tableIndex++) {
drawTable(tables[tableIndex]);
}
//Gets total relations number
var total_relations = 0;
if(tables[0].relations){
var relations = tables[0].relations;
total_relations = relations.length;
for(var tables_counter = 0; tables_counter < tables.length; tables_counter++){
current_relations = tables[tables_counter].relations;
if(current_relations.length > total_relations){
total_relations = current_relations.length;
}
}
}
if(total_relations == 0){
total_relations = 1;
}
for(var sets_counter = 0; sets_counter < sets.length; sets_counter++){
var relations = tables[sets_counter].relations;
for(var relations_counter = 0; relations_counter < relations.length; relations_counter++){
//Creates a new relation
connections.push(paper.connection(total_relations, sets_counter, sets[sets_counter], sets[relations[relations_counter]], '#000'));
//Draws the new relation
for (var i = 0, ii = sets.length; i < ii; i++) {
var color = Raphael.getColor();
sets[i].attr({cursor: "move"});
sets[i].drag(move, dragger, up);
}
}
}
}
var leftTopPosition = 50;
function drawTable(table) {
var rectLeft;
if(table.x){
rectLeft = table.x;
}else{
rectLeft = leftTopPosition;
}
var set = paper.set();
var rectTop;
if(table.y){
rectTop = table.y;
}else{
rectTop = leftTopPosition;
}
//Adds table name
var name = table.name;
var name_rect = paper.rect(
rectLeft,
rectTop,
settings.name.rect.width,
settings.name.rect.height
).attr({'fill': 'white'});
var name_text = paper
.text(
rectLeft + settings.name.text.leftOffset,
rectTop + settings.name.text.topOffset,
name
).attr({'text-anchor': 'start', 'font-weight': '900'});
rectTop += settings.name.rect.height;
set.push(name_rect);
set.push(name_text);
name_rect.set = set;
name_text.set = set;
//Adds table columns
for (var i=0; i<table.columns.length;i++) {
var column = table.columns[i];
var rect = paper.rect(
rectLeft,
rectTop,
settings.column.rect.width,
settings.column.rect.height
).attr({'fill': 'white'});
var text = paper
.text(
rectLeft + settings.column.text.leftOffset,
rectTop + settings.column.text.topOffset,
column
).attr({'text-anchor': 'start'});
rectTop += settings.column.rect.height;
set.push(rect);
set.push(text);
rect.set = set;
text.set = set;
}
sets.push(set);
updateTableGrid(rectTop);
leftTopPosition = leftTopPosition + 50;
}
/**
* Update the current column height in grid
* and choose the shortest one for the next table
*/
function updateTableGrid(rectTop) {
tableGrid[currentGridColumn]
+= rectTop + settings.paper.table.topOffset;
var minColumnHeight = tableGrid[0];
var shortestColumn = 0;
for (var i = 1; i < tableGrid.length; i++) {
if (tableGrid[i] < minColumnHeight) {
shortestColumn = i;
minColumnHeight = tableGrid[i];
}
};
currentGridColumn = shortestColumn;
}
};
/**
* Creates the TableListPresenter to draw the tables
*/
window.onload = function() {
// First JSON with the tables data
var tableListPresenter = new TableListPresenter([
{
name: '1. Person',
columns: [
'id',
'name',
'fk_id_company',
'fk_id_degree'
],
relations: [2]
},
{
name: '2. Company',
columns: [
'id',
'name',
'type',
'employees',
'average_salary',
'country',
'net_income'
],
relations: [0, 2, 3]
},
{
name: '3. Degree',
columns: [
'id',
'name',
'department'
],
relations: [3]
},
{
name: '4. New',
columns: [
'id',
'base',
'multiplier'
],
relations: []
}
]);
tableListPresenter.draw(document.getElementById('canvas_container'));
}
/**
* Prints the current tables possitions in a JSON
*/
function saveState(){
// Save
var json = paper.toJSON();
console.log('Paper:' + json);
}
/**
* Restores a diagram from JSON input. The method uses tableListPresenter to load. It could be improved
*/
function restoreState(){
//Clear current canvas
var row = document.getElementById("row");
row.removeChild(document.getElementById('canvas_container'));
var element = document.createElement('div');
element.id = 'canvas_container';
var objBody = document.getElementById("row");
objBody.appendChild(element);
//Load
var tableListPresenter = new TableListPresenter([
{
name: '1. Person',
columns: [
'id',
'name',
'fk_id_company',
'fk_id_degree'
],
relations: [2],
x: 66,
y: 171
},
{
name: '2. Company',
columns: [
'id',
'name',
'type',
'employees',
'average_salary',
'country',
'net_income'
],
relations: [0, 2, 3],
x: 400,
y: 81
},
{
name: '3. Degree',
columns: [
'id',
'name',
'department'
],
relations: [3],
x: 239,
y: 290
},
{
name: '4. New',
columns: [
'id',
'base',
'multiplier'
],
relations: [],
x: 696,
y: 143
}
]);
tableListPresenter.draw(document.getElementById('canvas_container'));
}