| 1 | package HTML::Widget::JavaScript::Result; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use base 'HTML::Widget::Result'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | __PACKAGE__->mk_attr_accessors(qw/onsubmit/); | |||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | HTML::Widget::JavaScript::Result - Result Class which supports JavaScript parameter validation | ||||||||||
| 12 | |||||||||||
| 13 | =head1 VERSION | ||||||||||
| 14 | |||||||||||
| 15 | Version 0.02 | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 2 | our $VERSION = '0.02'; | |||||||||
| 20 | |||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||
| 22 | |||||||||||
| 23 | This module adds the ability to output JavaScript validation code to | ||||||||||
| 24 | L<HTML::Widget::Result>. | ||||||||||
| 25 | |||||||||||
| 26 | =head1 FUNCTIONS | ||||||||||
| 27 | |||||||||||
| 28 | See L<HTML::Widget::Result>. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | =head2 $self->as_xml | ||||||||||
| 33 | |||||||||||
| 34 | Returns the widget's XML representation, including JavaScript validation code. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub as_xml { | ||||||||
|
Subroutine
as_xml: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 39 | 0 | my $self = shift; | |||||||||
| 40 | |||||||||||
| 41 | 0 | my $id = $self->name; | |||||||||
| 42 | |||||||||||
| 43 | # stop function taken from prototype.js | ||||||||||
| 44 | 0 | my $js_code = <<EOJS; | |||||||||
| 45 | <script type="text/javascript"> | ||||||||||
| 46 | function __html__widget__validate__$id (event) { | ||||||||||
| 47 | var form = document.getElementById('$id'); | ||||||||||
| 48 | var stop = function() { | ||||||||||
| 49 | if (event.preventDefault) { | ||||||||||
| 50 | event.preventDefault(); | ||||||||||
| 51 | event.stopPropagation(); | ||||||||||
| 52 | } else { | ||||||||||
| 53 | event.returnValue = false; | ||||||||||
| 54 | event.cancelBubble = true; | ||||||||||
| 55 | } | ||||||||||
| 56 | }; | ||||||||||
| 57 | |||||||||||
| 58 | EOJS | ||||||||||
| 59 | |||||||||||
| 60 | 0 | for my $constraint ( @{ $self->{_constraints} } ) { | |||||||||
| 61 | 0 | ▶ | next unless UNIVERSAL::can($constraint, 'emit_javascript'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | for my $js_const ($constraint->emit_javascript('form')) { | |||||||||
| 63 | 0 | my $msg = $constraint->message; | |||||||||
| 64 | 0 | $msg =~ s/'/\\'/g; | |||||||||
| 65 | 0 | $msg =~ s/\\$/\\\\/; | |||||||||
| 66 | 0 | $js_code .= " if $js_const { alert('$msg'); stop(); return false; }\n"; | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | $js_code .= "\n"; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | $js_code .= <<EOJS; | |||||||||
| 72 | |||||||||||
| 73 | return true; | ||||||||||
| 74 | } | ||||||||||
| 75 | </script> | ||||||||||
| 76 | EOJS | ||||||||||
| 77 | |||||||||||
| 78 | 0 | $self->onsubmit("__html__widget__validate__$id(event)"); | |||||||||
| 79 | 0 | return $js_code . $self->SUPER::as_xml; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head1 AUTHOR | ||||||||||
| 83 | |||||||||||
| 84 | Nilson Santos Figueiredo Júnior, C<< <nilsonsfj at cpan.org> >> | ||||||||||
| 85 | |||||||||||
| 86 | =head1 BUGS | ||||||||||
| 87 | |||||||||||
| 88 | Please report any bugs or feature requests directly to the author. | ||||||||||
| 89 | If you ask nicely it will probably get fixed or implemented. | ||||||||||
| 90 | |||||||||||
| 91 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 92 | |||||||||||
| 93 | Copyright 2006, 2009 Nilson Santos Figueiredo Júnior, all rights reserved. | ||||||||||
| 94 | |||||||||||
| 95 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 96 | under the same terms as Perl itself. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | 2 | 1; # End of HTML::Widget::JavaScript::Result | |||||||||