File Coverage

blib/lib/HTML/JQuery.pm
Criterion Covered Total %
statement 26 70 37.1
branch 1 2 50.0
condition n/a
subroutine 6 21 28.5
pod 17 18 94.4
total 50 111 45.0


line stmt bran cond sub pod time code
1             package HTML::JQuery;
2              
3             =head1 NAME
4              
5             HTML::JQuery - Generate jQuery/Javascript code in Perl
6              
7             =head1 DESCRIPTION
8              
9             This module is used to generate jQuery/Javascript code in Perl. What you do with it is up to you. I designed it for a work project where I needed to
10             display certain Perl variables to the page using Javascript, so, instead of ajax calls I designed C, which obviously took longer. Go figure.
11             You can quite easily use it in a Perl Web Framework of your choice and display the Javascript it generates into a template, meaning you don't have to code
12             any Javascript (Unless you need something extra).
13              
14             =head1 SYNOPSIS
15              
16             An example from a Catalyst point of view.
17              
18             ## Inside the Controller
19              
20             my $j = jquery sub {
21             function 'testfunc' => sub {
22             my $name = shift;
23             alert "Test func called: $name";
24             };
25              
26             function 'init' => sub {
27             my $name = shift;
28             alert "Document Loaded. Inside $name";
29             };
30              
31             onclick '#heading' => sub { fadeout shift; };
32              
33             dialog '#test' => (
34             title => 'Click Box!',
35             body => 'Thanks for clicking :-)',
36             modal => 1,
37             autoOpen => 0,
38             buttons => {
39             "OK" => function(sub {
40             dialog '#test', 'close';
41             alert 'closed!';
42             }),
43             "Nah" => function(sub {
44             alert "Fine. I won't close";
45             }),
46             q{'Test Func'} => function(sub {
47             func 'testfunc';
48             }),
49             },
50             );
51              
52             onclick '.button' => sub {
53             dialog '#test', 'open';
54             };
55              
56             onclick '.slidey' => sub {
57             fadein ('#slide_text', 1000,
58             function(sub {
59             fadeout '#slide_text', 1000;
60             })
61             );
62             };
63              
64             keystrokes '*' => ( keys => [qw( alt+ctrl+a )], event => function(sub { alert 'ALT+CTRL+A pressed' }) );
65             };
66              
67             $c->stash->{jquery} = $j;
68              
69             ## Inside the template (.tt)
70             [% jquery %]
71              
72             =cut
73              
74 2     2   64644 use Sub::Mage ':Class';
  2         31387  
  2         16  
75             extends 'HTML::JQuery::Data';
76              
77             $HTML::JQuery::VERSION = '0.005';
78             $HTML::JQuery::Inline = 0;
79             my $CLASS = __PACKAGE__;
80              
81             sub import {
82 2     2   22 my ($class, @args) = @_;
83              
84 2         11 $CLASS->_import_defs (qw/
85             jquery
86             jquery_inline
87             onclick
88             alert
89             fadeout
90             fadein
91             dialog
92             function
93             func
94             keystrokes
95             slidetoggle
96             rel
97             hide
98             show
99             dom_remove
100             datepicker
101             appendhtml
102             code
103             /);
104             }
105              
106             sub _import_defs {
107 2     2   10 my ($self, @defs) = @_;
108 2         5 my $pkg = caller(1);
109 2         7 for (@defs) {
110 36         3097 exports $_ => ( into => $pkg );
111             }
112             }
113              
114             sub code {
115 0     0 0 0 my $self = shift;
116 0         0 return join '', @{$HTML::JQuery::Data::JQuery};
  0         0  
117             }
118              
119             =head2 jquery
120              
121             All your HTML::JQuery must be wrapped between the jquery subroutine, like so
122              
123             my $j = jquery sub {
124             ...
125             };
126              
127             Then you can pass the C<$j> variable to whatever output you need. For example, in Catalyst you might do:
128              
129             my $j = jquery sub {
130             function 'init' => sub { alert 'Loaded!'; };
131             };
132              
133             $c->stash->{jquery} = $j;
134              
135             # then in the template
136             [% jquery %]
137              
138             =cut
139              
140             sub jquery {
141 1     1 1 15 my $sub = shift;
142 1         19 $CLASS->jquery_add("\n");
148 1         2 return join '', @{$HTML::JQuery::Data::JQuery};
  1         7  
149             }
150              
151             =head2 jquery_inline
152              
153             This is emulates an anonymous Javascript function. Like, C
154             Normally you would use these in callbacks.
155              
156             onclick '#test' => sub {
157             my $this = shift;
158             hide $this, 2000, jquery_inline(sub { alert '#test is now hidden'; });
159             };
160              
161             =cut
162              
163             sub jquery_inline {
164 0     0 1 0 my $sub = shift;
165 0         0 $HTML::JQuery::Inline = 1;
166 0         0 $HTML::JQuery::Data::Inline = [];
167 0         0 $sub->(@_);
168 0         0 $HTML::JQuery::Inline = 0;
169 0         0 return join '', @{$HTML::JQuery::Data::Inline};
  0         0  
170             }
171              
172             =head2 onclick
173              
174             As the name states, this event will be triggered when a given element is clicked.
175             The name of the element is passed in the first argument, if you need it.
176              
177             onclick '.myclass' => sub {
178             my $this = shift;
179             alert "$this was called";
180             };
181              
182             =cut
183              
184             sub onclick {
185 0     0 1 0 my ($sel, $code) = @_;
186 0         0 $CLASS->jquery_add($CLASS->jquery_onclick($sel));
187 0         0 $code->($sel);
188 0         0 $CLASS->jquery_add($CLASS->jquery_end);
189             }
190              
191             =head2 alert
192              
193             A very basic Javascript alert box.
194              
195             alert 'Huzzah!';
196              
197             =cut
198              
199             sub alert {
200 1     1 1 7 my $message = shift;
201              
202 1         5 $message =~ s/"/\\"/g;
203 1         11 $CLASS->js_alert($message);
204             }
205              
206             =head2 function
207              
208             Creates a standard Javascript function. Currently arguments are not supported, but will be in the future.
209             The first argument in the subroutine is the name of the function.
210              
211             function 'boo' => sub {
212             my $name = shift;
213             alert "$name was called!";
214             };
215              
216             Also, if we create a function called C, then HTML::JQuery will run it once the document has loaded.
217              
218             function 'init' => sub {
219             alert "The page has successfully loaded";
220             alert "We can now do stuff";
221             };
222              
223             As of 0.005, calling C with no name and just a code reference results in a Javascript callback (like jquery_inline, but with a more relevant name).
224              
225             =cut
226              
227             sub function {
228 1     1 1 17 my ($name, $function) = @_;
229 1 50       5 if ($function) {
230 1         6 $CLASS->jquery_add("function $name() {");
231 1         4 $function->(@_);
232 1         5 $CLASS->jquery_add("}");
233             }
234             else {
235 0           $HTML::JQuery::Inline = 1;
236 0           $HTML::JQuery::Data::Inline = [];
237 0           $name->(@_);
238 0           $HTML::JQuery::Inline = 0;
239 0           return join '', @{$HTML::JQuery::Data::Inline};
  0            
240             }
241             }
242              
243             =head2 func
244              
245             Simply calls a Javascript function.
246              
247             function 'myfunc' => sub { alert "Help! I've been ran!"; };
248             onclick '#runIt' => sub { func 'myfunc'; };
249              
250             =cut
251              
252             sub func {
253 0     0 1   my $func = shift;
254 0           $CLASS->js_callfunc($func);
255             }
256              
257             =head2 fadeout
258              
259             Hides the specified element with a "fade" effect. You can set the speed and even provide a callback
260             for when the command has completed. The last two options are completely optional, though.
261              
262             function 'fadeText' => sub {
263             fadeout '#text';
264             fadeout '#text2', 1000;
265             fadeout '#text3', 'slow';
266             fadeout '#text4', 2000, function(sub { alert '#text4 is now hidden' });
267             };
268              
269             =cut
270              
271             sub fadeout {
272 0     0 1   my ($sel, $duration, $after) = @_;
273            
274 0           $CLASS->jquery_fade('Out', $sel, $duration, $after);
275             }
276              
277             =head2 fadein
278              
279             fadein is the exact same as C, except it makes an element "re-appear". I won't repeat myself with example code.
280              
281             =cut
282              
283             sub fadein {
284 0     0 1   my ($sel, $duration, $after) = @_;
285            
286 0           $CLASS->jquery_fade('In', $sel, $duration, $after);
287             }
288              
289             =head2 slidetoggle
290              
291             Binds itself to an element so when you click said element it appears by sliding out, then, when clicked again will disappear by sliding in.
292              
293             onclick '#paragraph' => sub {
294             slidetoggle '.text', 1000;
295             };
296              
297             C also has a duration and callback feature, much the same as C and C.
298              
299             =cut
300              
301             sub slidetoggle {
302 0     0 1   my ($sel, $duration, $after) = @_;
303 0           $CLASS->jquery_slidetoggle($sel, $duration, $after);
304             }
305              
306             =head2 keystrokes
307              
308             JQuery keybindings - a truly fun extension to JQuery. This requires a Javascript file that is included with this module.
309             An example event to make an alert box appear after typing the word alert into your browser..
310              
311             keystrokes '*' => ( keys => [qw( a l e r t )], event => function(sub { alert 'You typed a l e r t' }) );
312              
313             Not only can it be triggered by keys pressed one after another, but you can make it work with multiple keys pressed at the same time.
314              
315             keystrokes '*' => ( keys => [ 'alt+m' ], event => function(sub { alert 'Alt+M was pressed' }) );
316              
317             It's also possible to mix multiple key presses with single ones if you wish.
318              
319             =cut
320              
321             sub keystrokes {
322 0     0 1   my ($sel, %args) = @_;
323 0           $CLASS->jquery_keystrokes($sel, \%args);
324             }
325              
326             =head2 dialog
327              
328             Runs a jQuery dialog box. Let's take a look.
329              
330             dialog '#test' => (
331             title => 'My dialog title',
332             autoOpen => 1, # run when the document has loaded?
333             modal => 1, # focuses on the window and blocks out everything else until its closed
334             body => '

This is the content within the dialog

',
335             buttons => {
336             OK => function(sub {
337             dialog '#test', 'close';
338             }),
339             Fade => function(sub {
340             fadeout 'p', 1000;
341             alert 'Text faded';
342             }),
343             },
344             );
345              
346             =cut
347              
348             sub dialog {
349 0     0 1   my ($sel, %opts) = @_;
350 0           $CLASS->jquery_dialog($sel, \%opts);
351             }
352              
353             =head2 rel
354              
355             Just retrieves the C attribute from an element.
356              
357             rel '.somelink';
358              
359             =cut
360              
361             sub rel {
362 0     0 1   my $sel = shift;
363 0           $CLASS->jquery_rel($sel);
364             }
365              
366             =head2 hide
367              
368             Similar to C, but without the actual "fade" effect. It simply hides an element, but doesn't permanently remove it.
369             It will do a CSS equivalent to C.
370             Like most of these types of functions the second argument is the duration and the third is a callback. Both are optional.
371              
372             hide '#test', 1000, function(sub{ alert 'Hidden #test' });
373              
374             =cut
375              
376             sub hide {
377 0     0 1   my ($sel, $duration, $after) = @_;
378 0           $CLASS->jquery_hide($sel, $duration);
379             }
380              
381             =head2 show
382              
383             The same as C, only shows the element instead if it's hidden
384              
385             =cut
386              
387             sub show {
388 0     0 1   my ($sel, $duration, $after) = @_;
389 0           $CLASS->jquery_show($sel);
390             }
391              
392             =head2 dom_remove
393              
394             Completely removes the given element from the DOM. This means it won't be able to be used again once it has been removed, unless you
395             reload the page, of course.
396              
397             onclick 'div' => sub {
398             hide 'this', 2000, function(sub {
399             dom_remove 'this'
400             });
401             };
402              
403             =cut
404              
405             sub dom_remove {
406 0     0 1   my $sel = shift;
407 0           $CLASS->jquery_remove($sel);
408             }
409              
410             =head2 datepicker
411              
412             Binds a fancy calendar to a specific element (Usually an input field).
413             If you pass C<>> 1 in the hash then it will append C to
414             the datepicker options plus anything you specify.
415              
416             datepicker '.datefield' => ( dateFormat => 'mm-dd-yy', currentText => 'Now' );
417              
418             You can see a list of options on jQuery UI's website for datepicker.
419              
420             =cut
421              
422             sub datepicker {
423 0     0 1   my ($sel, %args) = @_;
424 0           $CLASS->jquery_datepicker($sel, \%args);
425             }
426              
427             =head2 appendhtml
428              
429             Dynamically appends html to a div.
430              
431             innerhtml '#mydiv', 'Hello, World!';
432              
433             =cut
434              
435             sub appendhtml {
436 0     0 1   my ($sel, $text) = @_;
437 0           $CLASS->jquery_innerhtml($sel, $text);
438             }
439              
440             =head1 BUGS
441              
442             Please e-mail brad@geeksware.net
443              
444             =head1 AUTHOR
445              
446             Brad Haywood
447              
448             =head1 COPYRIGHT & LICENSE
449              
450             Copyright 2011 the above author(s).
451              
452             This sofware is free software, and is licensed under the same terms as perl itself.
453              
454             =cut
455              
456             1;
457