| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | package HTML::Widget::Plugin::JS; | ||
| 4 | 1 | $HTML::Widget::Plugin::JS::VERSION = '0.006'; | |
| 5 | # ABSTRACT: a JavaScript variable declaration emitter | ||
| 6 | |||
| 7 | 1 | ▶ | use parent qw(HTML::Widget::Plugin); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Data::JavaScript::Anon; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | ▶ | sub provided_widgets { qw(js_var js_vars js_anon) } |
|
Subroutine
provided_widgets: called CC=1 SCAR=0.0
Pod
documented
| |||
| 12 | |||
| 13 | ▶ | sub boolean_args {} | |
|
Subroutine
boolean_args: called CC=1 SCAR=0.0
Pod
documented
| |||
| 14 | ▶ | sub attribute_args {} | |
|
Subroutine
attribute_args: called CC=1 SCAR=0.0
Pod
documented
| |||
| 15 | |||
| 16 | #pod =head2 js_var | ||
| 17 | #pod | ||
| 18 | #pod =head2 js_vars | ||
| 19 | #pod | ||
| 20 | #pod These are two names for the same widget. Given a hashref, they will produce | ||
| 21 | #pod JavaScript code to assign the data in the hashref. | ||
| 22 | #pod | ||
| 23 | #pod In otherwords, this widget: | ||
| 24 | #pod | ||
| 25 | #pod $fac->js_vars({ | ||
| 26 | #pod foo => { a => 1, b => 2 }, | ||
| 27 | #pod bar => [ 4, 2, 3 ], | ||
| 28 | #pod }); | ||
| 29 | #pod | ||
| 30 | #pod ...will be rendered something like this: | ||
| 31 | #pod | ||
| 32 | #pod var foo = { a: 1, b: 2 }; | ||
| 33 | #pod var bar = [ 1, 2, 3 ]; | ||
| 34 | #pod | ||
| 35 | #pod =cut | ||
| 36 | |||
| 37 | sub js_vars { | ||
| 38 | 3 | ▶ | my ($self, $factory, $arg) = @_; |
|
Subroutine
js_vars: called CC=1 SCAR=0.0
Pod
documented
| |||
| 39 | |||
| 40 | my $str = | ||
| 41 | join "\n", | ||
| 42 | 3 | map { HTML::Widget::Plugin::JS::Encoder->var_dump($_ => $arg->{$_}) } | |
| 43 | keys %$arg; | ||
| 44 | |||
| 45 | 3 | return $str; | |
| 46 | } | ||
| 47 | |||
| 48 | 1 | ▶ | BEGIN { *js_var = \&js_vars } |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | |||
| 50 | #pod =head2 js_anon | ||
| 51 | #pod | ||
| 52 | #pod This widget converts a given data structure to an anonymous JavaScript | ||
| 53 | #pod structure. This basically just provides a widget factory interface to | ||
| 54 | #pod Data::JavaScript::Anon. | ||
| 55 | #pod | ||
| 56 | #pod It also escapes end-tag-like content in strings, using a JavaScript C<\u003c> | ||
| 57 | #pod form to avoid being interpreted as a real end tag in JavaScript embedded in | ||
| 58 | #pod HTML. | ||
| 59 | #pod | ||
| 60 | #pod Software is terrible. | ||
| 61 | #pod | ||
| 62 | #pod =cut | ||
| 63 | |||
| 64 | sub js_anon { | ||
| 65 | 0 | ▶ | my ($self, $factory, $arg) = @_; |
|
Subroutine
js_anon: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 66 | |||
| 67 | 0 | HTML::Widget::Plugin::JS::Encoder->anon_dump($arg); | |
| 68 | } | ||
| 69 | |||
| 70 | { | ||
| 71 | package | ||
| 72 | HTML::Widget::Plugin::JS::Encoder; | ||
| 73 | 1 | ▶ | use parent 'Data::JavaScript::Anon'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 74 | |||
| 75 | sub _escape { | ||
| 76 | 1 | ▶ | my ($self, $text) = @_; |
|
Subroutine
_escape: called CC=1 SCAR=0.0
| |||
| 77 | 1 | $text = $self->SUPER::_escape($text); | |
| 78 | 1 | $text =~ s/</\\u003c/g; | |
| 79 | 1 | return $text; | |
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1 | 1; | |
| 84 | |||
| 85 | __END__ | ||