1:- module('html_form/radio', [
    2			    image_radio_set//1
    3			   ]).

Tools for radio buttons

radio

Weblog Licensed under LGPL */

   12:- use_module(library(http/html_write)).   13
   14:- meta_predicate image_radio_set(1, ?, ?).
 image_radio_set(+Generator:goal)// is det
Creates a set of radio buttons with images instead of the default rendering

Generator will be called with one additional argument, of form

*/

   34image_radio_set(Generator) -->
   35	{
   36	    (	call(Generator, set_name(SetName)) ; gensym(radioset, SetName)),
   37	    (	bagof(ID, call(Generator, id(ID)), IDList) ; IDList = [] )
   38	},
   39	html([
   40	    \html_post(head,
   41script(type('text/javascript'), [
   42                               \['function ~wReset() {~n'-[SetName]],
   43			       \reset_button_code(Generator, IDList),
   44                               \['}\n']
   45				])
   46	     )]),
   47	image_radio_buttons(SetName, Generator, IDList).
   48
   49reset_button_code(_, []) --> [].
   50reset_button_code(Generator, [H|T]) -->
   51	{
   52	   call(Generator, image(H, Image))
   53	},
   54	html([
   55	    \['document.getElementById(\'~w\').checked =false;~n
   56document.getElementById(\'~wimage\').src = \'~w\';~n'-[H, H, Image]]
   57	     ]),
   58	reset_button_code(Generator, T).
   59
   60image_radio_buttons(_, _, []) --> [].
   61image_radio_buttons(SetName, Generator, [H|T]) -->
   62	{
   63	    call(Generator, image(H, Image)),
   64	    call(Generator, selected_image(H, SelectedImage)),
   65	    !,
   66	    format(atom(Click),
   67               '~wReset(); document.getElementById(\'~wimage\').src = \'~w\';
   68document.getElementById(\'~w\').checked =true; ',
   69               [SetName, H, SelectedImage, H]),
   70	    (
   71	        call(Generator, default(H))
   72	    ->
   73	        StartImage = SelectedImage,
   74	        Attribs = [type=radio, id=H, value=H,
   75			   checked=true, name=SetName]
   76	    ;
   77	        StartImage = Image,
   78	        Attribs = [type=radio, id=H, value=H, name=SetName]
   79	    )
   80	},
   81	html([
   82	    span(style='display:none', input(Attribs, [])),
   83	    img([id=H+image,
   84		 src=StartImage,
   85		 style='cursor:pointer;',
   86		 onclick=Click], [])
   87	     ]),
   88	image_radio_buttons(SetName, Generator, T).
   89image_radio_buttons(SetName, Generator, [H|T]) -->
   90	html([
   91	    span(['?', '<!-- missing or invalid props for ~w -->'-[H]])
   92	     ]),
   93	image_radio_buttons(SetName, Generator, T)