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