summaryrefslogtreecommitdiffstats
path: root/sandbox/sebastien/cpp/apr-2/modules/js/htdocs/graph.js
blob: 883b3aa8011c15d39197c124259b6a35416b77cd (plain)
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

/**
 * SVG and VML component rendering functions.
 */

var graph = new Object();

/**
 * Detect browser VML support.
 */
graph.supportsVML = function() {
    if (typeof graph.supportsVML.supported != 'undefined')
        return graph.supportsVML.supported;
    graph.supportsVML.supported = navigator.appName == 'Microsoft Internet Explorer';
    return graph.supportsVML.supported;
};

/**
 * Detect browser SVG support.
 */
graph.supportsSVG = function() {
    if (typeof graph.supportsSVG.supported != 'undefined')
        return graph.supportsSVG.supported;
    graph.supportsSVG.supported = navigator.appName != 'Microsoft Internet Explorer';
    return graph.supportsSVG.supported;
};

/**
 * Basic colors
 */
graph.red = 'red';
graph.green = 'green';
graph.blue = 'blue';
graph.yellow = 'yellow';
graph.orange = '#ffa500';
graph.gray = 'gray'

/**
 * Base path class.
 */
graph.BasePath = function() {
    this.path = '';
    this.x = 0;
    this.y = 0;

    this.pos = function(x, y) {
        this.x = x;
        this.y = y;
        return this;
    };

    this.xpos = function() {
        return this.x;
    };

    this.ypos = function() {
        return this.y;
    };

    this.rmove = function(x, y) {
        return this.move(this.x + x, this.y + y);
    };

    this.rline = function(x, y) {
        return this.line(this.x + x, this.y + y);
    };

    this.rcurve = function(x1, y1, x, y) {
        return this.curve(this.x + x1, this.y + y1, this.x + x1 + x, this.y + y1 + y);
    };
    
    this.str = function() {
        return this.path;
    };
};

/**
 * Rendering functions that work both with VML and SVG.
 */
var graph;

/**
 * VML rendering.
 */
if (graph.supportsVML()) {

    graph.vmlns='urn:schemas-microsoft-com:vml';
    document.write('<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" />');

    /**
     * Make a graph.
     */
    graph.mkgraph = function() {
        var div = document.createElement('div');
        div.id = 'vmldiv';
        document.body.appendChild(div);

        var vmlg = document.createElement('v:group');
        vmlg.style.width = 500;
        vmlg.style.height = 500;
        vmlg.coordsize = '500,500';
        div.appendChild(vmlg);

        graph.dragging = null;

        function draggable(n) {
            if (n == vmlg)
                return null;
            if (n.nodeName == 'group')
                return n;
            return draggable(n.parentNode);
        }

        vmlg.onmousedown = function() {
            window.event.returnValue = false;
            graph.dragging = draggable(window.event.srcElement);
            if (graph.dragging == null)
                return false;
            graph.dragging.parentNode.appendChild(graph.dragging);
            graph.dragX = window.event.clientX;
            graph.dragY = window.event.clientY;
            vmlg.setCapture();
            return false;
        };

        vmlg.onmouseup = function() {
            if (graph.dragging == null)
                return false;
            graph.dragging = null;
            vmlg.releaseCapture();
            return false;
        };

        vmlg.onmousemove = function() {
            if (graph.dragging == null)
                return false;
            var origX = graph.dragging.coordorigin.X;
            var origY = graph.dragging.coordorigin.Y;
            var newX = origX - (window.event.clientX - graph.dragX);
            var newY = origY - (window.event.clientY - graph.dragY);
            graph.dragX = window.event.clientX;
            graph.dragY = window.event.clientY;
            graph.dragging.setAttribute('coordorigin', newX + ' ' + newY);
            return false;
        };

        graph.textWidthDiv = document.createElement('span');
        graph.textWidthDiv.style.visibility = 'hidden'
        div.appendChild(graph.textWidthDiv);

        return vmlg;
    };

    /**
     * Make a shape path.
     */
    graph.mkpath = function() {
        function Path() {
            this.BasePath = graph.BasePath;
            this.BasePath();

            this.move = function(x, y) {
                this.path += 'M ' + x + ',' + y + ' '; 
                return this.pos(x, y);
            };

            this.line = function(x, y) {
                this.path += 'L ' + x + ',' + y + ' ';
                return this.pos(x, y);
            };

            this.curve = function(x1, y1, x, y) {
                this.path += 'QB ' + x1 + ',' + y1 + ',' + x + ',' + y + ' ';
                return this.pos(x, y);
            };

            this.end = function() {
                this.path += 'X E';
                return this;
            };
        }

        return new Path();
    };

    /**
     * Make a title element.
     */
    graph.mktitle = function(t) {
        var title = document.createElement('v:textbox');
        title.style.left = '25';
        title.style.top = '5';
        title.style.position = 'absolute';
        var tnode = document.createTextNode(t);
        title.appendChild(tnode);
        return title;
    };

    /**
     * Return the width of a title.
     */
    graph.titlewidth = function(t) {
        graph.textWidthDiv.innerHTML = t;
        var twidth = graph.textWidthDiv.offsetWidth;
        graph.textWidthDiv.innerHTML = '';
        return twidth;
    };

    /**
     * Make a component shape.
     */
    graph.mkcompshape = function(comp, cassoc) {
        var name = scdl.name(comp);
        var title = graph.mktitle(name);

        var d = graph.mkcomppath(comp, cassoc).str();

        var shape = document.createElement('v:shape');
        shape.style.width = 500;
        shape.style.height = 500;
        shape.coordsize = '500,500';
        shape.path = d;
        shape.fillcolor = graph.color(comp);
        shape.stroked = 'false';

        var contour = document.createElement('v:shape');
        contour.style.width = 500;
        contour.style.height = 500;
        contour.coordsize = '500,500';
        contour.setAttribute('path', d);
        contour.filled = 'false';
        contour.strokecolor = graph.gray;
        contour.strokeweight = '3';
        contour.style.top = 1;
        contour.style.left = 1;
        var stroke = document.createElement('v:stroke');
        stroke.opacity = '20%';
        contour.appendChild(stroke);

        var g = document.createElement('v:group');
        g.style.width = 500;
        g.style.height = 500;
        g.coordsize = '500,500';
        g.appendChild(shape);
        g.appendChild(contour);
        g.appendChild(title);
        return g;
    };
}

/**
 * SVG rendering.
 */
if (graph.supportsSVG()) {

    graph.svgns='http://www.w3.org/2000/svg';

    /**
     * Make a graph.
     */
    graph.mkgraph = function() {
        var div = document.createElement('div');
        div.id = 'svgdiv';
        document.body.appendChild(div);

        var svg = document.createElementNS(graph.svgns, 'svg');
        svg.style.height = '100%';
        svg.style.width = '100%';
        div.appendChild(svg);

        graph.dragging = null;

        function draggable(n) {
            if (n == svg)
                return null;
            if (n.nodeName == 'g')
                return n;
            return draggable(n.parentNode);
        }

        svg.onmousedown = function(e) {
            if (e.preventDefault)
                e.preventDefault();
            else
                e.returnValue= false;
            graph.dragging = draggable(e.target);
            if (graph.dragging == null)
                return false;
            graph.dragging.parentNode.appendChild(graph.dragging);
            var pos = typeof e.touches != "undefined" ? e.touches[0] : e;
            graph.dragX = pos.clientX;
            graph.dragY = pos.clientY;
            return false;
        };

        svg.ontouchstart = svg.onmousedown;

        svg.onmouseup = function(e) {
            if (graph.dragging == null)
                return false;
            graph.dragging = null;
            return false;
        };

        svg.ontouchend = svg.onmouseup;

        svg.onmousemove = function(e) {
            if (graph.dragging == null)
                return false;
            var matrix = graph.dragging.getCTM();
            var pos = typeof e.touches != "undefined" ? e.touches[0] : e;
            var newX = Number(matrix.e) + (pos.clientX - graph.dragX);
            var newY = Number(matrix.f) + (pos.clientY - graph.dragY);
            graph.dragX = pos.clientX;
            graph.dragY = pos.clientY;
            graph.dragging.setAttribute('transform', 'translate(' + newX + ',' + newY + ')');
            return false;
        };

        svg.ontouchmove = svg.onmousemove;

        graph.textWidthSvg = document.createElementNS(graph.svgns, 'svg');
        graph.textWidthSvg.style.visibility = 'hidden';
        graph.textWidthSvg.style.height = '0px';
        graph.textWidthSvg.style.width = '0px';
        div.appendChild(graph.textWidthSvg);

        return svg;
    };

    /**
     * Make a shape path.
     */
    graph.mkpath = function() {
        function Path() {
            this.BasePath = graph.BasePath;
            this.BasePath();

            this.move = function(x, y) {
                this.path += 'M' + x + ',' + y + ' '; 
                return this.pos(x, y);
            };

            this.line = function(x, y) {
                this.path += 'L' + x + ',' + y + ' ';
                return this.pos(x, y);
            };

            this.curve = function(x1, y1, x, y) {
                this.path += 'Q' + x1 + ',' + y1 + ' ' + x + ',' + y + ' ';
                return this.pos(x, y);
            };

            this.end = function() {
                this.path += 'Z';
                return this;
            };
        }

        return new Path();
    };

    /**
     * Make a title element.
     */
    graph.mktitle = function(t) {
        var title = document.createElementNS(graph.svgns, 'text');
        title.setAttribute('text-anchor', 'start');
        title.setAttribute('x', 5);
        title.setAttribute('y', 15);
        title.appendChild(document.createTextNode(t));
        graph.textWidthSvg.appendChild(title);
        return title;
    };

    /**
     * Return a width of a title.
     */
    graph.titlewidth = function(t) {
        var title = graph.mktitle(t);
        var width = title.getBBox().width;
        graph.textWidthSvg.removeChild(title);
        return width;
    };

    /**
     * Make a component shape.
     */
    graph.mkcompshape = function(comp, cassoc) {
        var name = scdl.name(comp);
        var title = graph.mktitle(name);

        var d = graph.mkcomppath(comp, cassoc).str();

        var shape = document.createElementNS(graph.svgns, 'path');
        shape.setAttribute('d', d);
        shape.setAttribute('fill', graph.color(comp));

        var contour = document.createElementNS(graph.svgns, 'path');
        contour.setAttribute('d', d);
        contour.setAttribute('fill', 'none');
        contour.setAttribute('stroke', graph.gray);
        contour.setAttribute('stroke-width', '4');
        contour.setAttribute('stroke-opacity', '0.20');
        contour.setAttribute('transform', 'translate(1,1)');

        var g = document.createElementNS(graph.svgns, 'g');
        g.appendChild(shape);
        g.appendChild(contour);
        g.appendChild(title);
        return g;
    };
}

/**
 * Make a reference shape path, positioned to the right of a component shape.
 */
graph.mkrrefpath = function(ref, cassoc, path) {
    var height = graph.refheight(ref, cassoc);
    var ypos = path.ypos();
    return path.rline(0,10).rline(0,10).rcurve(0,5,-5,0).rcurve(-5,0,0,-5).rcurve(0,-5,-5,0).rcurve(-5,0,0,5).rline(0,20).rcurve(0,5,5,0).rcurve(5,0,0,-5).rcurve(0,-5,5,0).rcurve(5,0,0,5).line(path.xpos(),ypos + height);
};

/**
 * Make a reference shape path, positioned at the bottom of a component shape.
 */
graph.mkbrefpath = function(ref, cassoc, path) {
    var width = graph.refwidth(ref, cassoc);
    var xpos = path.xpos();
    return path.line(xpos - width + 60,path.ypos()).rline(-10,0).rline(-10,0).rcurve(-5,0,0,-5).rcurve(0,-5,5,0).rcurve(5,0,0,-5).rcurve(0,-5,-5,0).rline(-20,0).rcurve(-5,0,0,5).rcurve(0,5,5,0).rcurve(5,0,0,5).rcurve(0,5,-5,0).line(xpos - width,path.ypos());
};

/**
 * Make a service shape path, positioned to the left of a component shape.
 */
graph.mklsvcpath = function(svc, path) {
    var height = 60;
    var ypos = path.ypos();
    return path.rline(0,-10).rline(0, -10).rcurve(0,-5,-5,0).rcurve(-5,0,0,5).rcurve(0,5,-5,0).rcurve(-5,0,0,-5).rline(0,-20).rcurve(0,-5,5,0).rcurve(5,0,0,5).rcurve(0,5,5,0).rcurve(5,0,0,-5).line(path.xpos(), ypos - height);
};

/**
 * Make a service shape path, positioned at the top of a component shape.
 */
graph.mktsvcpath = function(svc, path) {
    var width = 60;
    var xpos = path.xpos();
    return path.rline(10,0).rline(10,0).rcurve(5,0,0,-5).rcurve(0,-5,-5,0).rcurve(-5,0,0,-5).rcurve(0,-5,5,0).rline(20,0).rcurve(5,0,0,5).rcurve(0,5,-5,0).rcurve(-5,0,0,5).rcurve(0,5,5,0).line(xpos + width,path.ypos());
};


/**
 * Return the services and references of a component.
 */
graph.tsvcs = function(comp) {
    return filter(function(s) { return scdl.align(s) == 'top'; }, scdl.services(comp));
};

graph.lsvcs = function(comp) {
    return filter(function(s) { var a = scdl.align(s); return a == null || a == 'left'; }, scdl.services(comp));
};

graph.brefs = function(comp) {
    return filter(function(r) { return scdl.align(r) == 'bottom'; }, scdl.references(comp));
};

graph.rrefs = function(comp) {
    return filter(function(r) { var a = scdl.align(r); return a == null || a == 'right'; }, scdl.references(comp));
};

/**
 * Return the color of a component.
 */
graph.color = function(comp) {
    var c = scdl.color(comp);
    return c == null? graph.blue : c;
}

/**
 * Return the height of a reference.
 */
graph.refheight = function(ref, cassoc) {
    var target = assoc(scdl.target(ref), cassoc);
    if (isNil(target))
        return 60;
    return graph.compheight(cadr(target), cassoc);
}

/**
 * Return the total height of a list of references.
 */
graph.refsheight = function(refs, cassoc) {
    if (isNil(refs))
        return 0;
    return graph.refheight(car(refs), cassoc) + graph.refsheight(cdr(refs), cassoc);
}

/**
 * Return the height of a component.
 */
graph.compheight = function(comp, cassoc) {
    var lsvcs = graph.lsvcs(comp);
    var lsvcsh = Math.max(1, length(lsvcs)) * 60 + 20;
    var rrefs = graph.rrefs(comp);
    var rrefsh = graph.refsheight(rrefs, cassoc) + 20;
    var height = Math.max(lsvcsh, rrefsh);
    return height;
};

/**
 * Return the width of a reference.
 */
graph.refwidth = function(ref, cassoc) {
    var target = assoc(scdl.target(ref), cassoc);
    if (isNil(target))
        return 60;
    return graph.compwidth(cadr(target), cassoc);
}

/**
 * Return the total width of a list of references.
 */
graph.refswidth = function(refs, cassoc) {
    if (isNil(refs))
        return 0;
    return graph.refwidth(car(refs), cassoc) + graph.refswidth(cdr(refs), cassoc);
}

/**
 * Return the width of a component.
 */
graph.compwidth = function(comp, cassoc) {
    var twidth = graph.titlewidth(scdl.name(comp)) + 20;
    var tsvcs = graph.tsvcs(comp);
    var tsvcsw = Math.max(1, length(tsvcs)) * 60 + 20;
    var brefs = graph.brefs(comp);
    var brefsw = graph.refswidth(brefs, cassoc) + 20;
    var width = Math.max(twidth, Math.max(tsvcsw, brefsw));
    return width;
};

/**
* Make a component shape path.
*/
graph.mkcomppath = function(comp, cassoc) {
    var tsvcs = graph.tsvcs(comp);
    var lsvcs = graph.lsvcs(comp);
    var brefs = graph.brefs(comp);
    var rrefs = graph.rrefs(comp);

    var height = graph.compheight(comp, cassoc);
    var width = graph.compwidth(comp, cassoc);

    var path = graph.mkpath().move(10,0);
    for (var s = 0; s < length(tsvcs); s++)
        path = graph.mktsvcpath(tsvcs[s], path);

    path = path.line(width - 10,path.ypos()).rcurve(10,0,0,10);
    for (var r = 0; r < length(rrefs); r++)
        path = graph.mkrrefpath(rrefs[r], cassoc, path);

    var boffset = 10 + graph.refswidth(brefs, cassoc);
    path = path.line(path.xpos(),height - 10).rcurve(0,10,-10,0).line(boffset, path.ypos());
    for (var r = 0; r < length(brefs); r++)
        path = graph.mkbrefpath(brefs[r], cassoc, path);

    var loffset = 10 + (length(lsvcs) * 60);
    path = path.line(10,path.ypos()).rcurve(-10,0,0,-10).line(path.xpos(), loffset);
    for (var s = 0; s < length(lsvcs); s++)
        path = graph.mklsvcpath(lsvcs[s], path);

    path = path.line(0,10).rcurve(0,-10,10,0);
    return path.end();
};

/**
 * Render a component.
 */
graph.component = function(comp, cassoc) {
    return graph.mkcompshape(comp, cassoc);
};

/**
 * Render a composite.
 */
graph.composite = function(compos) {
    var name = scdl.name(compos);
    var comps = scdl.components(compos);
    var cassoc = scdl.nameToElementAssoc(comps);

    function renderReference(ref, cassoc) {
        var target = assoc(scdl.target(ref), cassoc);
        if (isNil(target))
            return mklist();
        return renderComponent(cadr(target), cassoc);
    }

    function renderReferences(refs, cassoc) {
        if (isNil(refs))
            return mklist();
        var rref = renderReference(car(refs), cassoc);
        if (isNil(rref))
            return renderReferences(cdr(refs), cassoc);
        return append(rref, renderReferences(cdr(refs), cassoc));
    }

    function renderComponent(comp, cassoc) {
        return append(renderReferences(scdl.references(comp), cassoc), mklist(graph.component(comp, cassoc)));
    }

    function renderComponents(comps, cassoc) {
        if (isNil(comps))
            return mklist();
        return append(renderComponent(car(comps), cassoc), renderComponents(cdr(comps), cassoc));
    }

    var rcomps = renderComponents(comps, cassoc);
    return rcomps;
};