Revision 71878bf7
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
SL/Template/Plugin/JavaScript.pm | ||
---|---|---|
6 | 6 |
use strict; |
7 | 7 |
|
8 | 8 |
sub new { |
9 |
my $class = shift; |
|
10 |
my $context = shift; |
|
9 |
my ($class, $context, @args) = @_; |
|
11 | 10 |
|
12 |
bless { }, $class; |
|
11 |
return bless { |
|
12 |
CONTEXT => $context, |
|
13 |
}, $class; |
|
13 | 14 |
} |
14 | 15 |
|
16 |
# |
|
17 |
# public interface |
|
18 |
# |
|
19 |
|
|
15 | 20 |
sub escape { |
16 | 21 |
my $self = shift; |
17 | 22 |
my $text = shift; |
18 | 23 |
|
24 |
$text =~ s|\\|\\\\|g; |
|
19 | 25 |
$text =~ s|\"|\\\"|g; |
26 |
$text =~ s|\n|\\n|g; |
|
20 | 27 |
|
21 | 28 |
return $text; |
22 | 29 |
} |
23 | 30 |
|
31 |
sub replace_with { |
|
32 |
return _replace_helper('replaceWith', @_); |
|
33 |
} |
|
34 |
|
|
35 |
sub replace_html_with { |
|
36 |
return _replace_helper('html', @_); |
|
37 |
} |
|
38 |
|
|
39 |
# |
|
40 |
# private methods |
|
41 |
# |
|
42 |
|
|
43 |
sub _context { |
|
44 |
die 'not an accessor' if @_ > 1; |
|
45 |
return $_[0]->{CONTEXT}; |
|
46 |
} |
|
47 |
|
|
48 |
sub _replace_helper { |
|
49 |
my ($method, $self, $selector, $template, $locals) = @_; |
|
50 |
|
|
51 |
$template .= '.html' unless $template =~ m/\.html$/; |
|
52 |
my $html = $self->escape($self->_context->process($template, %{ $locals || { } })); |
|
53 |
my $code = <<CODE; |
|
54 |
\$('${selector}').${method}("$html"); |
|
55 |
CODE |
|
56 |
|
|
57 |
return $code; |
|
58 |
} |
|
59 |
|
|
24 | 60 |
1; |
25 | 61 |
|
26 | 62 |
|
63 |
__END__ |
|
64 |
|
|
65 |
=pod |
|
66 |
|
|
67 |
=encoding utf8 |
|
68 |
|
|
69 |
=head1 NAME |
|
70 |
|
|
71 |
SL::Template::Plugin::JavaScript - Template plugin for JavaScript helper functions |
|
72 |
|
|
73 |
=head1 FUNCTIONS |
|
74 |
|
|
75 |
=over 4 |
|
76 |
|
|
77 |
=item C<escape $value> |
|
78 |
|
|
79 |
Returns C<$value> escaped for inclusion in a JavaScript string. The |
|
80 |
value is not wrapped in quotes. Example: |
|
81 |
|
|
82 |
<input type="submit" value="Delete" |
|
83 |
onclick="if (confirm('Do you really want to delete this: [% JavaScript.escape(obj.description) %]') return true; else return false;"> |
|
84 |
|
|
85 |
=item C<replace_with $selector, $template, %locals> |
|
86 |
|
|
87 |
Returns code replacing the DOM elements matched by C<$selector> with |
|
88 |
the content rendered by Template's I<PROCESS> directive applied to |
|
89 |
C<$template>. C<%locals> are passed as local parameters to I<PROCESS>. |
|
90 |
|
|
91 |
Uses jQuery's C<obj.replaceWith()> function. Requires jQuery to be loaded. |
|
92 |
|
|
93 |
Example: |
|
94 |
|
|
95 |
<div>TODO:</div> |
|
96 |
<ul> |
|
97 |
<li id="item1">First item</li> |
|
98 |
<li id="item2">Second item</li> |
|
99 |
<li id="item3">Another item</li> |
|
100 |
</ul> |
|
101 |
|
|
102 |
<script type="text/javascript"> |
|
103 |
function do_work() { |
|
104 |
[% JavaScript.replace_with('#item2', 'todo/single_item', item => current_todo_item) %] |
|
105 |
} |
|
106 |
</script> |
|
107 |
|
|
108 |
<input type="submit" onclick="do_work(); return false;" value="Replace single item"> |
|
109 |
|
|
110 |
=item C<replace_html_with $selector, $template, %locals> |
|
111 |
|
|
112 |
Returns code replacing the inner HTML of the DOM elements matched by |
|
113 |
C<$selector> with the content rendered by Template's I<PROCESS> |
|
114 |
directive applied to C<$template>. C<%locals> are passed as local |
|
115 |
parameters to I<PROCESS>. |
|
116 |
|
|
117 |
Uses jQuery's C<obj.html()> function. Requires jQuery to be loaded. |
|
118 |
|
|
119 |
<div>TODO:</div> |
|
120 |
<ul id="todo_list"> |
|
121 |
<li id="item1">First item</li> |
|
122 |
<li id="item2">Second item</li> |
|
123 |
<li id="item3">Another item</li> |
|
124 |
</ul> |
|
125 |
|
|
126 |
<script type="text/javascript"> |
|
127 |
function do_work() { |
|
128 |
[% JavaScript.replace_html_with('#todo_list', 'todo/full_list', items => todo_items) %] |
|
129 |
} |
|
130 |
</script> |
|
131 |
|
|
132 |
<input type="submit" onclick="do_work(); return false;" value="Replace list"> |
|
133 |
|
|
134 |
=back |
|
135 |
|
|
136 |
=head1 BUGS |
|
137 |
|
|
138 |
Nothing here yet. |
|
139 |
|
|
140 |
=head1 AUTHOR |
|
141 |
|
|
142 |
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt> |
|
143 |
|
|
144 |
=cut |
Auch abrufbar als: Unified diff
Helferfunktionen für AJAX-Aufrufe/DOM-Modifikationen mit jQuery: Elemente ersetzen