File: mod_wsf_graph.spl

package info (click to toggle)
spl 1.0~pre6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,364 kB
  • ctags: 2,070
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 167; xml: 156
file content (581 lines) | stat: -rw-r--r-- 14,463 bytes parent folder | download | duplicates (5)
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
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_wsf_graph.spl: A WSF Component for viewing and editing graphs
 */

/**
 * A module which implemnts a WSF Component for displaying and editing
 * graphs.
 */

load "wsf";

/**
 * A WSF Component for displaying and editing graphs.
 *
 * In order for this component to work properly, the browser must have support
 * for iframes and must allow javascripts to modify the DOM tree. This is the
 * case with most of todays graphical web browsers.
 *
 * For various technical reasons, the graph editor itself is displayed in an
 * iframe. This object creates an extra SPL task for this iframe.
 *
 * The constructor is passed a function which returns a list of node objects
 * which must implement the [[WsfGraphNode]] interface. If there is any
 * outside change of what that function would return, the 'dirty' attribute
 * of the WsfGraph instance must be set to '1'.
 *
 * This object is derived from [[wsf:WsfComponent]].
 */
object WsfGraph WsfComponent
{
	/**
	 * The function which returns the node list. This is set by the
	 * constructor to its first argument.
	 */
	var get_list;

	/**
	 * The (relative or absolute) URL to the directory in which the images
	 * used by this component can be found.
	 *
	 * This are the files 'images/wsf_graph_*' in the SPL sources.
	 */
	var ibase;

	/**
	 * Per default, nodes can be moved to any position. It is possible to
	 * use a raster to align the nodes. If you want that, set this variable
	 * to the size of the raster (e.g. 10 is a good value).
	 */
	var raster;

	/**
	 * Additional HTML text to be inserted in the HTML header of the iframe.
	 */
	var iframe_header;

	/**
	 * Additional HTML text to be inserted at the end of the HTML text in the
	 * iframe.
	 */
	var iframe_footer;

	/**
	 * Additional attributes to be added in the <iframe> tag. Usually this
	 * is something like 'style="width:100%; height:100%"'.
	 */
	var iframe_attributes;

	/**
	 * Some changes require a reload of the iframe.
	 * The current scroll position is then preserved using this attributes.
	 * It is also possible to adjust this values when setting the 'dirty'
	 * variable to '1'.
	 */
	var scroll_x, scroll_y;

	/**
	 * The main method for the iframe.
	 *
	 * This is called whenever the iframe content is reloaded and prints
	 * the new content of the iframe.
	 */
	method iframe_main()
	{
		write(iframe_header);

		write(
<>
<script><!--

var MouseX = 0, MouseX_Old = 0;
var MouseY = 0, MouseY_Old = 0;

var MouseMoveManager = 0;
var MouseUpManager = 0;

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    MouseX = event.clientX + document.body.scrollLeft;
    MouseY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    MouseX = e.pageX;
    MouseY = e.pageY;
  }
  if (MouseX < 0) MouseX = 0;
  if (MouseY < 0) MouseY = 0;
  if (MouseMoveManager) MouseMoveManager();
  MouseX_Old = MouseX;
  MouseY_Old = MouseY;
}

function MouseUp() {
  if (MouseUpManager) MouseUpManager();
}

var IE = document.all?true:false;
if (!IE) {
	document.captureEvents(Event.MOUSEMOVE);
	document.captureEvents(Event.MOUSEUP);
}

document.onmousemove = getMouseXY;
document.onmouseup = MouseUp;

//--></script>

<div id="dbg">
</div>

<script><!--

var gnodes = new Object();
var gnodes_id = new Array();

var gnode_move = 0;
var gconn_sel = 0;

var gnodes_xpos;
var gnodes_ypos;

var max_x = 0;
var max_y = 0;

function handle_down(id)
{
	gnode_move = id;
}

function handle_conn_enter(id)
{
	gconn_sel = id;
	draw();
}

function handle_conn_click(id, trg)
{
	var href = '${cgi.url}?sid=$sid&n=' + id + '&t=' + trg;
	
	if (!IE)
		href = href + "&sx=" + pageXOffset + "&sy=" + pageYOffset;

   <spl:if code="WsfDocument.domupdate ~== 'iframe'">
	parent.wsftarget.location.href = href;
   </spl:if>

   <spl:if code="WsfDocument.domupdate ~!= 'iframe'">
	parent.location.href = href;
   </spl:if>
}

function handle_conn_leave(id)
{
	if (gconn_sel == id) {
		gconn_sel = 0;
		draw();
	}
}

function handle_move()
{
	if (!gnode_move) return;

	gnodes[gnode_move]["top"]  += MouseY - MouseY_Old;
	gnodes[gnode_move]["left"] += MouseX - MouseX_Old;

	draw();
}

function handle_up()
{
	if (!gnode_move) return;
	var id = gnode_move;
	gnode_move = 0;

   <spl:if code="defined raster">
	gnodes[id]["top"]  = Math.round(gnodes[id]["top"]  / $raster) * $raster;
	gnodes[id]["left"] = Math.round(gnodes[id]["left"] / $raster) * $raster;
   </spl:if>

	var href =
		'${cgi.url}?sid=$sid&n=' + id + '&x=' +
		gnodes[id]["left"] + '&y=' + gnodes[id]["top"];

	if (!IE)
		href = href + "&sx=" + pageXOffset + "&sy=" + pageYOffset;

   <spl:if code="WsfDocument.domupdate ~== 'iframe'">
	parent.wsftarget.location.href = href;
   <spl:if code="defined raster">
	draw();
   </spl:if>
   </spl:if>

   <spl:if code="WsfDocument.domupdate ~!= 'iframe'">
	parent.location.href = href;
   </spl:if>
}

function setpos(id, top, left, height, width)
{
	document.getElementById(id).style.position   = "absolute";
	document.getElementById(id).style.overflow   = "visible";
	document.getElementById(id).style.top        = top  + "px";
	document.getElementById(id).style.left       = left + "px";
	document.getElementById(id).style.height     = height + "px";
	document.getElementById(id).style.width      = width  + "px";
	document.getElementById(id).style.visibility = "visible";

	if (id != "max_scroll") {
		if ( top + height > max_y ) max_y = top + height;
		if ( left + width > max_x ) max_x = left + width;
	}
}

function setpic(id, pic)
{
	if ( (gconn_sel + "_a") == id ) pic = pic + 'X';
	if ( (gconn_sel + "_b") == id ) pic = pic + 'X';
	if ( (gconn_sel + "_c") == id ) pic = pic + 'X';
	document.getElementById(id).setAttribute("src", "${ibase}" + pic + ".gif");
}

function register(id, top, left, height, width)
{
	gnodes[id] = new Object();
	gnodes[id]["links"]  = new Array();

	gnodes[id]["top"]    = top;
	gnodes[id]["left"]   = left;
	gnodes[id]["height"] = height;
	gnodes[id]["width"]  = width;

	gnodes_id.push(id);
}

function setlink_helper(id1, id2, id3, a, b)
{
	var y1, x1, y2, x2;
	var rel_y = 0;
	var rel_x = 0;

	var changedit = 1;
	var changecount = 0;

	while (changedit && changecount < 5) {
		changedit = 0;
		changecount++;

		if ( !rel_y ) {
			x1 = a.left + a.width/2;
			x2 = b.left;
		}

		if ( !rel_x ) {
			y1 = a.top + a.height;
			y2 = b.top + b.height/2;
		}

		var old_rel_y = rel_y;
		var old_rel_x = rel_x;

		rel_y = y1-2 < y2 ? 1 : y1-2 > y2+a.height ? -1 : 0;
		rel_x = x1+2 < x2 ? 1 : x1+2 > x2+b.width  ? -1 : 0;

		if ( old_rel_y != rel_y ) changedit = 1;
		if ( old_rel_x != rel_x ) changedit = 1;

		if ( rel_y ) {
			var offset = 5, dir = 1;
			while ( gnodes_xpos[x1] && gnodes_xpos[x1] != id1 ) {
				x1 += offset * dir;
				changedit = 1;
				offset += 5;
				dir *= -1;
			}
			gnodes_xpos[x1] = id1;
		}

		if ( rel_x ) {
			var offset = 5, dir = 1;
			while ( gnodes_ypos[y2] && gnodes_ypos[y2] != id2 ) {
				y2 += offset * dir;
				changedit = 1;
				offset += 5;
				dir *= -1;
			}
			gnodes_ypos[y2] = id2;
		}
	}

	if ( rel_y == 1 ) {
		if (!rel_x) y2 -= b.height/2;
		setpos(id1, y1, x1, (y2 - y1), 4);
		if (!rel_x) {
			setpos(id3, y2-7, x1-3, 10, 10);
			setpic(id3, "down");
		}
	} else
	if ( rel_y == -1 ) {
		y1 -= a.height;
		if (!rel_x) y2 += b.height/2;
		setpos(id1, y2, x1, (y1 - y2), 4);
		if (!rel_x) {
			setpos(id3, y2-3, x1-3, 10, 10);
			setpic(id3, "up");
		}
	} else {
		x1 += a.width/2;
		document.getElementById(id1).style.visibility = "hidden";
	}

	if ( rel_x == 1 ) {
		setpos(id2, y2, x1, 4, (x2 - x1));
		setpos(id3, y2-3, x2-7, 10, 10);
		setpic(id3, "right");
	} else
	if ( rel_x == -1 ) {
		x2 += b.width;
		x1 += !rel_y ? -a.width : 4;
		setpos(id2, y2, x2, 4, (x1 - x2));
		setpos(id3, y2-3, x2-3, 10, 10);
		setpic(id3, "left");
	} else {
		document.getElementById(id2).style.visibility = "hidden";
	}
}

function setlink(id1, id2)
{
	setpic(id1 + "_" + id2 + "_a", "line");
	setpic(id1 + "_" + id2 + "_b", "line");
	setlink_helper(id1 + "_" + id2 + "_a", id1 + "_" + id2 + "_b",
	               id1 + "_" + id2 + "_c", gnodes[id1], gnodes[id2]);
}

function draw()
{
	var i, j;

	gnodes_xpos = new Object();
	gnodes_ypos = new Object();

	for (i=0; i<gnodes_id.length; i++) {
		var id = gnodes_id[i];
		setpos(id, gnodes[id].top, gnodes[id].left,
		           gnodes[id].height, gnodes[id].width);
		for (j=0; j<gnodes[id].links.length; j++)
			setlink(id, gnodes[id].links[j]);
	}

	setpos("max_scroll", max_y, max_x, 20, 20);
}

function connect(id1, id2)
{
	gnodes[id1]["links"].push(id2);
}

MouseMoveManager = handle_move;
MouseUpManager = handle_up;

//--></script>

<img id="max_scroll" src="${ibase}trans.gif">

</>
			);

		var list = get_list();
		foreach i (list) {
			write(
<>
<table id="$i" cellspacing="0" cellpadding="0" onMouseDown="handle_down('$i')">
<tr><td <spl:if code="declared list[i].get_color">bgcolor="${list[i].get_color()}"</spl:if> align="center" valign="middle">
<spl:if code="declared list[i].get_html">${list[i].get_html()}</spl:if>
</td></tr></table>
</>
				);
		}

		foreach i (list) {
			if (declared list[i].get_links) {
				var links = list[i].get_links();
				foreach j (links) {
					write(
<>
<img id="${i}_${links[j]}_a" src="${ibase}line.gif" onClick="handle_conn_click('${i}', '${links[j]}')"
	onMouseOver="handle_conn_enter('${i}_${links[j]}')" onMouseOut="handle_conn_leave('${i}_${links[j]}')">
<img id="${i}_${links[j]}_b" src="${ibase}line.gif" onClick="handle_conn_click('${i}', '${links[j]}')"
	onMouseOver="handle_conn_enter('${i}_${links[j]}')" onMouseOut="handle_conn_leave('${i}_${links[j]}')">
<img id="${i}_${links[j]}_c" src="${ibase}line.gif" onClick="handle_conn_click('${i}', '${links[j]}')"
	onMouseOver="handle_conn_enter('${i}_${links[j]}')" onMouseOut="handle_conn_leave('${i}_${links[j]}')">
</>
						);
				}
			}
		}

		write('<script><!--\n');

		foreach i (list) {
			var pos = [ x: 10, y:10, w:10, h:10 ];
			if (declared list[i].get_xywh) pos = list[i].get_xywh();
			write('register("$i", $pos.y, $pos.x, $pos.h, $pos.w);\n');
		}

		foreach i (list) {
			if (declared list[i].get_links) {
				var links = list[i].get_links();
				foreach j (links) {
					write('connect("$i", "${links[j]}");\n');
				}
			}
		}

		write('draw();\n');
		if (defined scroll_x and defined scroll_y)
			write('scrollTo($scroll_x, $scroll_y);\n');
		write('//--></script>\n');
		write(iframe_footer);
	}

	/**
	 * Overloaded [[wsf:WsfComponent.get_html()]].
	 */
	method get_html()
	{
		return '<iframe $iframe_attributes id="$id" src="${cgi.url}?sid=${cgi.sid_vm}:${id}_iframe"></iframe>\n';
	}

	/**
	 * Overloaded [[wsf:WsfComponent.main()]].
	 */
	method main()
	{
		task_co_return();

		if (declared cgi.param.n)
		{
			var i = cgi.param.n;
			var list = get_list();

			if (declared cgi.param.sx) scroll_x = cgi.param.sx;
			if (declared cgi.param.sy) scroll_y = cgi.param.sy;

			if (declared cgi.param.x and declared cgi.param.y and declared list[i].set_xy)
				list[i].set_xy(cgi.param.x, cgi.param.y);

			if (declared cgi.param.t and declared list[i].set_link)
				list[i].set_link(cgi.param.t);
		}
	}

	/**
	 * Overloaded [[wsf:WsfComponent.destroy()]].
	 */
	method destroy()
	{
		task_kill("${id}_iframe");
		return *WsfComponent.destroy();
	}

	/**
	 * The Constructor.
	 *
	 * The parameters are copied to the [[.get_list]] and [[.ibase]]
	 * variables.
	 *
	 * The first parameter must be a function pointer. This function, when
	 * called, must return an array of objects implementing the
	 * [[WsfGraphNode]] interface. This objects describe the entire graph
	 * and also provide callbacks to react on user events.
	 */
	method init(_get_list, _ibase)
	{
		*WsfComponent.init();

		get_list = _get_list;
		ibase = _ibase;

		task_create("${id}_iframe", "while (1) { iframe_main(); task_pause(); }", this);
		task_public("${id}_iframe");

		return this;
	}
}

/**
 * The node objects used by [[WsfGraph]] must implement this interface.
 * Some of the methods in this interface are optional.
 */
// interface WsfGraphNode
// {
	/**
	 * This method returns the position and size of the node in pixel.
	 * The return value must be a hash with the keys 'x' and 'y' for
	 * the x- and y-coordinate of the left upper corner (0,0 is the
	 * left upper corner of the drawing area) and 'w' and 'h' for the
	 * width and height.
	 */
	// method get_xywh()

	/**
	 * When a node is moved or clicked, this callback is called. The new
	 * x- and y-coordinates are passed as parameters.
	 */
	// method set_xy(x, y)

	/**
	 * This method returns an array with the IDs of the nodes this node
	 * has connections to. The IDs must correspond with the keys in the
	 * array returned by [[WsfGraph.get_list]].
	 */
	// method get_links()

	/**
	 * This callback is called when one of the connections of the node
	 * are clicked. The ID (see [[.get_links]]) of the target node of the
	 * connection is passed as parameter.
	 *
	 * This method is optional. [[WsfGraph]] will not try to call it when
	 * is is not declared.
	 */
	// method set_link(trg)

	/**
	 * This method must return the background color for this node. The
	 * return value is a string in the usual HTML color '#RRGGBB' format.
	 *
	 * This method is optional. [[WsfGraph]] will not try to call it when
	 * it is not declared.
	 */
	// method get_color()

	/**
	 * This method must return the HTML content for this node. Usually
	 * this is just a label text, but it can any HTML code.
	 *
	 * This method is optional. [[WsfGraph]] will not try to call it when
	 * it is not declared.
	 */
	// method get_html()
// }