[system] / trunk / pg / macros / parserRadioButtons.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/parserRadioButtons.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 4772 Revision 4773
37# generated by the radio buttons (if 37# generated by the radio buttons (if
38# the choice strings are too long, they 38# the choice strings are too long, they
39# will be trimmed and "..." inserted) 39# will be trimmed and "..." inserted)
40# Default: 25 40# Default: 25
41# 41#
42# uncheckable => 0 or 1 determines whether the radio buttons can
43# or "shift" be unchecked (requires JavaScript).
44# To uncheck, click a second time; when
45# set to "shift", unchecking requires the
46# shift key to be pressed.
47# Default: 0
48#
42# 49#
43# To insert the radio buttons into the problem text, use 50# To insert the radio buttons into the problem text, use
44# 51#
45# BEGIN_TEXT 52# BEGIN_TEXT
46# \{$radio->buttons\} 53# \{$radio->buttons\}
62# 69#
63# The package that implements RadioButtons 70# The package that implements RadioButtons
64# 71#
65package parserRadioButtons; 72package parserRadioButtons;
66our @ISA = qw(Value::String); 73our @ISA = qw(Value::String);
74
75my $jsPrinted = 0; # true when the JavaScript has been printed
67 76
68 77
69# 78#
70# Create a new RadioButtons object 79# Create a new RadioButtons object
71# 80#
76 main::set_default_options(\%options, 85 main::set_default_options(\%options,
77 labels => [], 86 labels => [],
78 separator => $main::BR, 87 separator => $main::BR,
79 checked => undef, 88 checked => undef,
80 maxLabelSize => 25, 89 maxLabelSize => 25,
90 uncheckable => 0,
81 @_, 91 @_,
82 ); 92 );
83 $options{labels} = [1..scalar(@$choices)] if $options{labels} eq "123"; 93 $options{labels} = [1..scalar(@$choices)] if $options{labels} eq "123";
84 $options{labels} = [@main::ALPHABET[0..scalar(@$choices)-1]] if $options{labels} eq "ABC"; 94 $options{labels} = [@main::ALPHABET[0..scalar(@$choices)-1]] if $options{labels} eq "ABC";
85 my $self = bless {%options, choices=>$choices}, $class; # temporary to so we can call our methods 95 my $self = bless {%options, choices=>$choices}, $class; # temporary to so we can call our methods
97 isValue => 1, context => $context, 107 isValue => 1, context => $context,
98 choices => $choices, 108 choices => $choices,
99 %options, 109 %options,
100 ), $class; 110 ), $class;
101 main::Context($oldContext); 111 main::Context($oldContext);
112 $self->JavaScript if $self->{uncheckable};
102 return $self; 113 return $self;
103} 114}
104 115
105# 116#
106# Locate the label of the correct answer 117# Locate the label of the correct answer
161 return -1 unless defined $index && $index =~ m/^\d$/; 172 return -1 unless defined $index && $index =~ m/^\d$/;
162 return $index; 173 return $index;
163} 174}
164 175
165# 176#
177# Print the JavaScript needed for uncheckable radio buttons
178#
179sub JavaScript {
180 return if $main::displayMode eq 'TeX';
181 return if $jsPrinted;
182 main::TEXT(
183 "\n<script>\n" .
184 "if (window.ww == null) {var ww = {}}\n" .
185 "if (ww.RadioButtons == null) {ww.RadioButtons = {}}\n" .
186 "if (ww.RadioButtons.selected == null) {ww.RadioButtons.selected = {}}\n" .
187 "ww.RadioButtons.Toggle = function (obj,event,shift) {\n" .
188 " if (!event) {event = window.event}\n" .
189 " if (shift && !event.shiftKey) {\n" .
190 " this.selected[obj.name] = obj\n" .
191 " return\n" .
192 " }\n" .
193 " var selected = this.selected[obj.name]\n" .
194 " if (selected && selected == obj) {\n".
195 " this.selected[obj.name] = null\n" .
196 " obj.checked = false\n" .
197 " } else {\n" .
198 " this.selected[obj.name] = obj\n".
199 " }\n" .
200 "}\n".
201 "</script>\n"
202 );
203 $jsSPrinted = 1;
204}
205
206sub makeUncheckable {
207 my $self = shift;
208 my $shift = ($self->{uncheckable} =~ m/shift/i ? ",1" : "");
209 my $onclick = "onclick=\"ww.RadioButtons.Toggle(this,event$shift)\"";
210 my @radio = @_;
211 foreach (@radio) {$_ =~ s/<INPUT/<INPUT $onclick/i}
212 return @radio;
213}
214
215#
166# Create the radio-buttons text 216# Create the radio-buttons text
167# 217#
168sub buttons { 218sub buttons {
169 my $self = shift; 219 my $self = shift;
170 my @radio = main::ans_radio_buttons($self->choiceHash); 220 my @radio = main::ans_radio_buttons($self->choiceHash);
221 @radio = $self->makeUncheckable(@radio) if $self->{uncheckable};
171 (wantarray) ? @radio : join($self->{separator}, @radio); 222 (wantarray) ? @radio : join($self->{separator}, @radio);
172} 223}
173sub named_buttons { 224sub named_buttons {
174 my $self = shift; my $name = shift; 225 my $self = shift; my $name = shift;
175 my @radio = NAMED_ANS_RADIO_BUTTONS($name,$self->choiceHash); 226 my @radio = NAMED_ANS_RADIO_BUTTONS($name,$self->choiceHash);
227 @radio = $self->makeUncheckable(@radio) if $self->{uncheckable};
176 # 228 #
177 # Taken from PGbasicmacros.pl 229 # Taken from PGbasicmacros.pl
178 # It is wrong to have \item in the radio buttons and to add itemize here, 230 # It is wrong to have \item in the radio buttons and to add itemize here,
179 # but that is the way PGbasicmacros.pl does it. 231 # but that is the way PGbasicmacros.pl does it.
180 # 232 #

Legend:
Removed from v.4772  
changed lines
  Added in v.4773

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9