File: mod_wsf_dialog.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 (559 lines) | stat: -rw-r--r-- 16,406 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
/*
 *  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_dialog.spl: WSF Dialog Component
 */


/**
 * A module which implements a "Qt Designer"-like component for creating WSF
 * dialogs in a web browser.
 */

load "wsf";
load "format_xml";
load "encode_xml";

/**
 * A WSF component for creating simple WSF components in a web browser.
 *
 * A Dialog is simply a collection of form elements with small code snippets,
 * which are automatically executed when the content of the element  has been
 * modified (or the button been pushed).
 *
 * Such a dialog can be dumped as XML and later loaded again.
 *
 * If the "save" button should be functional, you eighter need to derive your
 * own object from this one or overwrite the method name after instanciating
 * this object.
 *
 * Derived from [[wsf:WsfComponent]].
 */
object WsfDialog WsfComponent
{
	/**
	 * The configuration of the dialog, in the format described in
	 * [[xml:format_xml_parse()]].
	 *
	 * All changes are done directly in this data structrue. So there is
	 * no need of calling any kind of convertation function in order to
	 * keep this structure up to date.
	 *
	 * Simply call [[xml:format_xml_dump()]] on this variable to create an
	 * XML file for this dialog (usually this is done in an overloaded
	 * [[.save()]] method.
	 */
	var xmltree;

	/**
	 * The current values of the elements. If there is an input box
	 * "foobar", "values.foobar" can be used to read and modify the value
	 * of that input box.
	 */
	var values;

	/**
	 * When this variable is set to '1', the dialog is currently in edit
	 * mode. Usually applications only allow privelidged users to switch
	 * to edit mode.
	 *
	 * The dirty variable ([[wsf:WsfComponent.dirty]]) must be set when
	 * this variable is changed. Usually the method [[.set_edit_mode()]] is
	 * used to set this variable.
	 */
	var edit_mode = 0;

	/**
	 * The name of the element which is open in the editor right now. If
	 * this is set to "", the screen with the generic dialog options is
	 * displayed.
	 *
	 * Usually this is only set internally by [[.main()]] when the user
	 * changes the current element in the web frontend.
	 */
	var edit_current = "";

	/**
	 * The frontend allows displaying the current XML tree so it can e.g.
	 * be copy&pasted to a file. This variable is set to '1' when displaying
	 * of the XML tree is currently active.
	 */
	var edit_show_xml = 0;

	/**
	 * Set this to '1' if you want this component to add an 'edit' button
	 * to the component. With this button, the user can switch to the editor
	 * view at any time. This should be used with care since someone with
	 * access to the editor may access any SPL code! Only privelidged
	 * users show have access to this feature.
	 */
	var show_edit_button = 0;

	/**
	 * Call this function if you want to switch this component to edit mode
	 * (parameter = '1') or back to executing the dialog (parameter = '0').
	 */
	method set_edit_mode(newmode)
	{
		if ( newmode != edit_mode ) {
			edit_mode = newmode;
			edit_current = "";
			edit_show_xml = 0;
			dirty = 1;
		}
	}

	method __WsfDialog_get_html_element(element)
	{
		var onchange = << EOT:
			:document.${id}_f.el.value = '${element.["A:name"]}';
			:submit(); return true;
		EOT;
		if ( element.["A:type"] ~== "text" ) {
			return << EOT:
				:<input
				:	name="${id}_${element.["A:name"]}"
				:	value="${xml::values.[element.["A:name"]]}"
				:	onChange="${xml::onchange}"
				:	style="width:100%;" />
			EOT;
		}
		else
		if ( element.["A:type"] ~== "textarea" ) {
			return << EOT:
				:<input
				:	name="${id}_${element.["A:name"]}"
				:	value="${xml::values.[element.["A:name"]]}"
				:	onChange="${xml::onchange}"
				:	style="width:100%; height:100%;" />
			EOT;
		}
		else
		if ( element.["A:type"] ~== "button" ) {
			return << EOT:
				:<input
				:	type="button"
				:	name="${id}_${element.["A:name"]}"
				:	value="${xml::values.[element.["A:name"]]}"
				:	onClick="${xml::onchange}"
				:	style="width:100%;" />
			EOT;
		}
		else
		if ( element.["A:type"] ~== "label" ) {
			return values.[element.["A:name"]];
		}
		
		return "";
	}

	method __WsfDialog_get_html_editor()
	{
		var d = xmltree.["E0:wsfdialog"];
		var html = "";

		html ~= '<input type=hidden name="current" value="${xml::edit_current}">\n';
		html ~= '<select style="width:100%" name="new_current" onchange="submit(); return true;">\n';
		html ~= '<option value="">Common Options</option>\n';
		foreach f (d) {
			if ( f =~ /^E\d+:field/ ) {
				var selected = edit_current ~== f ? "selected" : "";
				html ~= '<option $selected value="${xml::f}">${xml::d.[f].["A:name"]}</option>\n';
			}
		}
		html ~= '</select>\n';

		if ( edit_current ~== "" )
		{
			html ~= << EOT:
				:<p><table style="width:100%">
				:<tr><td colspan=2>New Element:</td></tr>
				:<tr><td><input name="new_element" style="width:80%">
				:</td><td>&nbsp;<input type="submit" value="Create">
				:</td></tr></table></p>
				:<p><table style="width:100%">
				:<tr><td>Width:</td><td><input style="width:80%" name="set_width"
				:	value="${xml::d.["A:width"]}"></td><td>cells</td></tr>
				:<tr><td>Height:</td><td><input style="width:80%" name="set_height"
				:	value="${xml::d.["A:height"]}"></td><td>cells</td></tr>
				:<tr><td>Step:</td><td><input style="width:80%" name="set_step"
				:	value="${xml::d.["A:step"]}"></td><td>pixel/cell</td></tr>
				:</table></p>
				:<p>Init code:<br/>
				:<textarea name="set_code" rows=5 cols=40>${xml::d.["E0:code"].["C0"]}</textarea></p>
				:<p><input type="submit" name="save" value="Save">
				:&nbsp;<input type="submit" name="run_dialog" value="Run Dialog">
				:&nbsp;<input type="submit" name="show_xml" value="${ edit_show_xml ? 'Hide' : 'Show'} XML"></p>
			EOT;
		}
		else
		{
			var e = d.[edit_current];

			var set_type = '<select name="set_type" style="width:80%">';
			var set_type_list = [ 'text', 'textarea', 'button', 'label' ];

			foreach i (set_type_list)
				set_type ~= '<option ${ e.["A:type"] ~== set_type_list.[i] ? 'selected' : '' } value="${xml::set_type_list.[i]}">${xml::set_type_list.[i]}</option>';

			set_type_list ~= '</select>';
 
			html ~= << EOT:
				:<p><table style="width:100%">
				:<tr><td>Name:</td><td colspan="2"><input style="width:80%" name="set_name"
				:	value="${xml::e.["A:name"]}"></td><td>text</td></tr>
				:<tr><td>Pos. X:</td><td width="80"><input style="width:50%" name="set_posx"
				:	value="${xml::e.["A:posx"]}"></td><td align="center" rowspan="2"><input type="button" value="GRAB"/ onClick="${id}_grab = 'xy';" /></td><td>cells</td></tr>
				:<tr><td>Pos. Y:</td><td width="80"><input style="width:50%" name="set_posy"
				:	value="${xml::e.["A:posy"]}"></td><td>cells</td></tr>
				:<tr><td>Width:</td><td width="80"><input style="width:50%" name="set_width"
				:	value="${xml::e.["A:width"]}"></td><td align="center" rowspan="2"><input type="button" value="GRAB" onClick="${id}_grab = 'wh';" /></td><td>cells</td></tr>
				:<tr><td>Height:</td><td width="80"><input style="width:50%" name="set_height"
				:	value="${xml::e.["A:height"]}"></td><td>cells</td></tr>
				:<tr><td>Type:</td><td colspan="2">$set_type</td><td>type</td></tr>
				:<tr><td>Value:</td><td colspan="2"><input style="width:80%" name="set_value"
				:	value="${xml::e.["E0:value"].["C0"]}"></td><td>text</td></tr>
				:</table></p>
				:<p>Action code:<br/>
				:<textarea name="set_code" rows=5 cols=40>${xml::e.["E0:code"].["C0"]}</textarea></p>
				:<p><input type="submit" name="update" value="Ok">
				:&nbsp;<input type="submit" name="delete" value="Delete">
				:&nbsp;<input type="submit" name="show_xml" value="${ edit_show_xml ? 'Hide' : 'Show'} XML"></p>
			EOT;
		}

		return html;
	}

	/**
	 * Overloaded [[wsf:WsfComponent.get_html()]].
	 */
	method get_html()
	{
		var html = "";
		var d = xmltree.["E0:wsfdialog"];

		html ~= << EOT:
			:<form id="$id" name="${id}_f" ${add_action(cgi.url)}>
			:<!-- Begin of WsfDialog $id -->
			:<input type=hidden name="sid" value="${sid}">
		EOT;
		if ( edit_mode )
			html ~= '<table cellpadding="10"><tr><td valign="top" bgcolor="#eeeeee">\n';
		else
			html ~= '<input type=hidden name="el" value="">\n';
		if (edit_mode )
			html ~= <<EOT:
				:<script>
				:var ${id}_grab = "";
				:function ${id}_clickhdl(x, y) {
				:	if ( ${id}_grab == "wh" ) {
				:		window.document.${id}_f.set_width.value = x-
				:		    window.document.${id}_f.set_posx.value+1;
				:		window.document.${id}_f.set_height.value = y-
				:		    window.document.${id}_f.set_posy.value+1;
				:		window.document.${id}_f.submit();
				:	}
				:	if ( ${id}_grab == "xy" ) {
				:		window.document.${id}_f.set_posx.value = x;
				:		window.document.${id}_f.set_posy.value = y;
				:		window.document.${id}_f.submit();
				:	}
				:	${id}_grab = "";
				:}
				:</script>
			EOT;
		html ~= << EOT:
			:<table width="${d.["A:width"] * d.["A:step"]}"
			:	height="${d.["A:height"] * d.["A:step"]}"
		EOT;
		if ( edit_mode )
			html ~= '\tbgcolor="#888888">\n';
		html ~= << EOT:
			:<tr>
		EOT;

		var m;

		foreach f (d) {
			if ( f =~ /^E\d+:field/ ) {
				var entry;

				entry.["type"] = "element";
				entry.["link"] = d.[f];
				entry.["ln_x"] = d.[f].["A:width"];
				entry.["ln_y"] = d.[f].["A:height"];
				entry.["done"] = 0;

				for (var y = d.[f].["A:posy"]; y < d.[f].["A:posy"]+d.[f].["A:height"]; y++)
				for (var x = d.[f].["A:posx"]; x < d.[f].["A:posx"]+d.[f].["A:width"]; x++)
					if (not declared m.[x].[y])
						m.[x].[y] = entry;
			}
		}

		for (var y=1; y <= d.["A:height"]; y++)
		for (var x=1; x <= d.["A:width"]; x++)
			if (not declared m.[x].[y])
			{
				var entry;
				var end_x = x+1, end_y = y+1;

				m.[x].[y] = entry;

				if ( !edit_mode )
				{
					for (; end_x <= d.["A:width"] && not declared m.[end_x].[y]; end_x++)
						m.[end_x].[y] = entry;

					var new_y_is_ok = 1;
					for (; end_y <= d.["A:height"] && new_y_is_ok; ) {
						for (var x2 = x; x2 < end_x; x2++)
							if (declared m.[x2].[end_y]) new_y_is_ok = 0;
						if (new_y_is_ok) {
							for (var x2 = x; x2 < end_x; x2++)
								m.[x2].[end_y] = entry;
							end_y++;
						}
					}
				}

				entry.["type"] = "filler";
				entry.["ln_x"] = end_x - x;
				entry.["ln_y"] = end_y - y;
				entry.["done"] = 0;
			}

		for (var y=1; y <= d.["A:height"]; y++) {
			for (var x=1; x <= d.["A:width"]; x++)
				if (not m.[x].[y].done)
				{
					html ~= << EOT:
				:<td width="${m.[x].[y].ln_x * d.["A:step"]}"
				:	height="${m.[x].[y].ln_y * d.["A:step"]}"
				:	colspan="${m.[x].[y].ln_x}"
				:	rowspan="${m.[x].[y].ln_y}"
				:	align="center" valign="middle"
					EOT;
					if ( m.[x].[y].type ~== "filler" ) {
						if ( edit_mode )
							html ~= '\tbgcolor="${ (x+y) % 2 ? "#aaaaaa" : "#dddddd" }" onclick="${id}_clickhdl($x,$y);"\n';
						html ~= '></td>\n';
					} else {
						if ( edit_mode )
							html ~= '\tbgcolor="#2FA68A">${m.[x].[y].link.["A:name"]}</td>\n';
						else
							html ~= '>${__WsfDialog_get_html_element(m.[x].[y].link)}</td>';
					}
					m.[x].[y].done = 1;
				}
			if ( y < d.["A:height"] )
				html ~= '</tr><tr>\n';
		}

		html ~= << EOT:
			:</tr>
			:</table>
		EOT;

		if ( edit_mode )
		{
			html ~= '</td><td valign="top" bgcolor="#eeeeee">\n';
			html ~= __WsfDialog_get_html_editor();
			if ( edit_show_xml )
				html ~= '</td></tr><tr><td bgcolor="#eeeeee" colspan="2">\n' ~
				        '<pre>${xml::(format_xml_dump(xmltree))}</pre>\n';
			html ~= '</td></tr></table>\n';
		}
		else
		if ( show_edit_button )
			html ~= '<input type="submit" name="switch_edit_mode" value="Switch to Edit Mode">\n';

		html ~= << EOT:
			:<!-- End of WsfDialog $id -->
			:</form>
		EOT;

		return html;
	}

	/**
	 * This method is called when the user clicks on the "save" button in
	 * the dialog editor. It does nothing in the default implementation. So
	 * you need to overload it in a derived object to add this
	 * functionality.
	 */
	method save()
	{
		/* To be overloaded */
		return;
	}

	/**
	 * Overloaded [[wsf:WsfComponent.main()]].
	 */
	method main()
	{
		while (1)
		{
			task_co_return();
			var d = xmltree.["E0:wsfdialog"];

			if ( edit_mode )
			{
				edit_current = cgi.param.new_current;

				if ( declared cgi.param.show_xml )
					edit_show_xml = not edit_show_xml;

				if ( cgi.param.current ~== "" )
				{
					d.["A:width"]  = cgi.param.set_width;
					d.["A:height"] = cgi.param.set_height;
					d.["A:step"]   = cgi.param.set_step;

					if ( cgi.param.set_code =~ /\S/ )
						d.["E0:code"].["C0"] = cgi.param.set_code;
					else
						delete d.["E0:code"];

					if ( cgi.param.new_element =~ /\S/ )
					{
						var id = 0;
						while ( declared d.["E${id}:field"] ) id++;
						edit_current = "E${id}:field";

						d.["E${id}:field"].["A:name"]   = cgi.param.new_element;
						d.["E${id}:field"].["A:width"]  = 1;
						d.["E${id}:field"].["A:height"] = 1;
						d.["E${id}:field"].["A:posx"]   = 0;
						d.["E${id}:field"].["A:posy"]   = 0;
						d.["E${id}:field"].["A:type"]   = "text";
					}

					if ( declared cgi.param.save )
						save();

					if ( declared cgi.param.run_dialog ) {
						set_edit_mode(0);
						show_edit_button = 1;
						reset_values();
					}
				}
				else
				if ( declared cgi.param.delete )
				{
					delete d.[cgi.param.current];
					edit_current = "";
				}
				else
				{
					var e = d.[cgi.param.current];

					e.["A:name"]   = cgi.param.set_name;
					e.["A:width"]  = cgi.param.set_width;
					e.["A:height"] = cgi.param.set_height;
					e.["A:posx"]   = cgi.param.set_posx;
					e.["A:posy"]   = cgi.param.set_posy;
					e.["A:type"]   = cgi.param.set_type;

					if ( cgi.param.set_code =~ /\S/ )
						e.["E0:code"].["C0"] = cgi.param.set_code;
					else
						delete e.["E0:code"];

					if ( cgi.param.set_value =~ /\S/ )
						e.["E0:value"].["C0"] = cgi.param.set_value;
					else
						delete e.["E0:value"];

					if ( declared cgi.param.update )
						edit_current = "";
				}
			}
			else
			if ( show_edit_button && declared cgi.param.switch_edit_mode ) {
				show_edit_button = 0;
				set_edit_mode(1);
			}
			else
			{
				foreach f (d) {
					var fn = d.[f].["A:name"];
					if ( declared cgi.param.["${id}_${fn}"] )
						values.[fn] = cgi.param.["${id}_${fn}"];
				}
				foreach f (d) {
					var fn = d.[f].["A:name"];
					if ( f =~ /^E\d+:field/ &&
							fn ~== cgi.param.el &&
							declared d.[f].["E0:code"].["C0"])
						task_eval(d.[f].["E0:code"].["C0"], this);
				}
			}

			dirty = 1;
		}
	}

	/**
	 * Reset the dialog elements to their default values. This can also be
	 * used in the code snippets assigned to the dialog elements. To reset
	 * the dialog.
	 */
	method reset_values()
	{
		var d = xmltree.["E0:wsfdialog"];
		foreach f (d) {
			if ( f =~ /^E\d+:field/ ) {
				values.[d.[f].["A:name"]] =
					declared d.[f].["E0:value"] ?
						d.[f].["E0:value"].["C0"] : undef;
			}
		}
		dirty = 1;
	}

	/**
	 * Overwrite xml data. The argument is the XML text. This might also be
	 * called by one of the code snippets attached to dialog elements to
	 * switch to another dialog. (E.g. when clicking on a "Next" button.)
	 */
	method import_xml(xmldata)
	{
		if (not defined xmldata)
			xmldata = '<wsfdialog width="10" height="10" step="20"></wsfdialog>';
		xmltree = format_xml_parse(xmldata);
		reset_values();
	}

	/**
	 * The constructor.
	 *
	 * The parameter is the XML text containing the dialog configuration.
	 * If it is undef (or not specified at all), an empty dialog will be
	 * created.
	 */
	method init(xmldata)
	{
		*WsfComponent.init();
		import_xml(xmldata);
		return this;
	}
}