File Coverage

blib/lib/JQuery.pm
Criterion Covered Total %
statement 6 78 7.6
branch 0 24 0.0
condition 0 3 0.0
subroutine 2 10 20.0
pod 8 8 100.0
total 16 123 13.0


line stmt bran cond sub pod time code
1             package JQuery;
2              
3 1     1   27611 use warnings;
  1         3  
  1         37  
4 1     1   7 use strict;
  1         2  
  1         1104  
5              
6             our $VERSION = '1.00';
7              
8              
9             # The expected parameters
10             # jqueryDir - the javascript directory
11             # usePacked - use the compressed library if available
12              
13             sub new {
14 0     0 1   my $this = shift;
15 0   0       my $class = ref($this) || $this;
16 0           my $my = {@_} ;
17 0 0         $my->{usePacked} = 0 unless defined $my->{usePacked} ;
18 0 0         $my->{useDump} = 0 unless defined $my->{useDump} ;
19 0           $my->{css} = [] ;
20 0           $my->{csslast} = [] ;
21 0           $my->{objectList} = [] ;
22 0           bless $my, $class;
23              
24 0           return $my ;
25             }
26              
27             sub getJQueryDir {
28 0     0 1   my $my = shift ;
29 0           return $my->{jqueryDir} ;
30             }
31              
32              
33             sub usePacked {
34 0     0 1   my $my = shift ;
35 0           return $my->{usePacked} ;
36             }
37              
38             # Allow css to be set
39             sub add_css {
40 0     0 1   my $my = shift ;
41 0           my $css = shift ;
42 0           push @{$my->{css}}, $css ;
  0            
43             }
44              
45             sub add_css_last {
46 0     0 1   my $my = shift ;
47 0           my $css = shift ;
48 0           push @{$my->{csslast}}, $css ;
  0            
49             }
50              
51             sub add {
52 0     0 1   my $my = shift ;
53 0           my $object = shift ;
54 0           push @{$my->{objectList}},$object ;
  0            
55 0           my @packagesNeeded = $object->packages_needed($my->{usePacked}) ;
56             # Keep track of which packages to include in the header
57 0           grep { $my->{packages}{$_} ++ } @packagesNeeded ;
  0            
58              
59 0           return $object ;
60             }
61              
62             sub get_css {
63 0     0 1   my $my = shift ;
64 0           my $style ;
65 0           for my $css (@{$my->{css}}) {
  0            
66 0 0         if ($css->can('output_text')) {
67 0           $style .= $css->output_text ;
68             }
69             }
70              
71 0           my @objects = @{$my->{objectList}} ;
  0            
72 0           for my $object (@objects) {
73 0           my $id = $object->id ;
74 0 0         next unless $object->can('get_css') ;
75 0           my $css = $object->get_css ;
76             # Either an object is returned or a string
77 0 0         if (ref($css) =~ /\S/) {
78 0           for my $c (@$css) {
79 0 0         if (ref($c) =~ /\S/) {
80 0           $style .= $c->output_text($id) . "\n" ;
81             } else {
82 0 0         $style .= qq[\n" if $c =~ /\S/ ; # HTML Validator doesn't like empty styles
83             }
84             }
85             } else {
86 0 0         $style .= qq[\n" if $css =~ /\S/; # HTML Validator doesn't like empty styles
87             }
88             }
89 0           for my $css (@{$my->{csslast}}) {
  0            
90 0 0         if ($css->can('output_text')) {
91 0           $style .= $css->output_text ;
92             }
93             }
94 0           return $style ;
95             }
96              
97             sub get_jquery_code {
98 0     0 1   my $my = shift ;
99 0           my @packages = keys %{$my->{packages}} ;
  0            
100 0           my $jqueryDir = $my->{jqueryDir} ;
101 0 0         my $pack = $my->{usePacked} ? ".pack" : "" ;
102 0           my $code = qq[] ."\n" ;
103 0 0         if ($my->{useDump}) {
104 0           push @packages,"dumper/jquery.dump.js" ;
105             }
106              
107 0           for my $package (@packages) {
108 0           $code .= qq[] . "\n" ;
109             }
110              
111 0           $code .= qq[\n" ;
122 0           return $code ;
123             }
124              
125             =head1 NAME
126              
127             JQuery - Interface to Jquery, a language based on Javascript
128              
129             =head1 VERSION
130              
131             Version 1.00
132              
133             =head1 SYNOPSIS
134              
135             JQuery provides some of the functionality provided by the JQuery language.
136              
137             use JQuery;
138              
139             my $jquery = new JQuery(jqueryDir => '/jquery_js') ;
140              
141             my $accordion = JQuery::Accordion->new(id => 'myAccordion',
142             headers => \@headers,
143             texts => \@texts,
144             panelHeight => 200,
145             panelWidth => '400px'
146             ) ;
147              
148             $jquery->add_css_last(new JQuery::CSS( hash => {'#myAccordion' => {width => '600px'}})) ;
149              
150             my $data = [['Id','Total','Ip','Time','US Short Date','US Long Date'],
151             ['66672', '$22.79','172.78.200.124','08:02','12-24-2000','Jul 6, 2006 8:14 AM'],
152             ['66672','$2482.79','172.78.200.124','15:10','12-12-2001','Jan 6, 2006 8:14 AM']
153             ] ;
154              
155             my $tableHTML = $jquery->Add(JQuery::TableSorter->new(id => 'table1',
156             data => $data,
157             headerClass => 'largeHeaders',
158             dateFormat=>'dd/mm/yyyy' ))->HTML ;
159              
160             $jquery->add($accordion) ;
161             my $html = $accordion->HTML . $tableHTML ;
162             my $jquery_code = $jquery->get_jquery_code ;
163             my $css = $jquery->get_css ;
164            
165             =head1 DESCRIPTION
166              
167             JQuery is a frontend for the jQuery language. I use B to
168             refer to the Perl part or the package, and B to reference
169             the javascript part or the package.
170              
171             A quote from L: jQuery is a fast, concise,
172             JavaScript Library that simplifies how you traverse HTML documents,
173             handle events, perform animations, and add Ajax interactions to your
174             web pages.
175              
176             JQuery.pm is the main module. There are other modules such as Form,
177             TableSorter, Splitter, Taconite ..., all of which provide different
178             functionality. The main module needs to be instantiated, and each
179             instance of the other modules needs to be registered with the main
180             module. It is then the responsibility of JQuery.pm to produce the
181             relevant HTML, css and javascript code.
182              
183             One of the objectives is to produce javascript functioniality with as
184             little user code as possible and to provide reasonable
185             defaults. "Reasonable" is of course in the sight of the beholder. Most
186             defaults are provided by css, and can be changed easily.
187              
188             Another objective is to allow module writers to be able to add new
189             functionality within this framework.
190              
191             =head2 Using JQuery
192              
193             JQuery comes packaged with jQuery javascript files. Since the
194             javascript directory is going to be needed by the web server, you will
195             probably need to copy the whole of the jquery_js directory to
196             somewhere the web server can access. Remember to change the web server
197             config file if necessary.
198              
199             =head2 JQuery::CSS
200              
201             JQuery::CSS is a helper module that helps create CSS objects uses CSS
202             module from CPAN.
203              
204             CSS can be created in the following ways:
205              
206             my $css = new JQuery::CSS(text => 'body {font-family: Arial, Sans-Serif;font-size: 10px;}') ;
207              
208             my $css = new JQuery::CSS( hash => {
209             '.odd' => {'background-color' => "#FFF"} ,
210             '.even' => {'background-color' => "#D7FF00"} ,
211             }) ;
212              
213             my $css = new JQuery::CSS(file => 'dates/default.css') ;
214              
215             =over 8
216              
217             =item text
218              
219             The "text" form allows plain text to be used to create the CSS object.
220              
221             =item hash
222              
223             The "hash" form allows a Perl hash to be used.
224              
225             =item file
226              
227             The "file" form allows a file to be specified.
228              
229             =back
230              
231             =head2 Initialization
232              
233             JQuery needs to be initialized.
234              
235             my $jquery = new JQuery(jqueryDir => '/jquery_js', usePacked => 1) ;
236              
237             The parameter jqueryDir specifies where the javascript files can be found.
238             When using a web server, this refers to the directory defined by the web server.
239              
240             If usePacked is set, the compressed jQuery files are used if available.
241              
242             =head2 Adding CSS
243              
244             CSS objects can be added to JQuery using
245             $jquery->add_css($css)
246             or
247             $jquery->add_css_last($css)
248            
249             add_css outputs css before the css of the modules. add_css_last
250             outputs css after the css of the modules. This is useful if you
251             want to change the default css supplied by the package.
252              
253             =head2 Functions
254              
255             =over 4
256              
257             =item add
258              
259             This adds a new object from JQuery::* to JQuery. Typically, this is used in a class
260             implementing new functionality, and adds the new class to the
261             controlling JQuery class. It is not normally called directly by the
262             user.
263              
264             =item add_css
265              
266             Add css to JQuery. All css elements added are installed before that of
267             css implemented by JQuery::*. This is used if the user wants to install CSS.
268              
269             =item add_css_last
270              
271             Similar to add_css, but the css gets added after the ccs implemented
272             by JQuery::*. This means that the css installed by JQuery::* can be
273             over-ridden by the user. Typically this would be used when changing
274             colours, urls, backgrounds etc.
275              
276             =item getJQueryDir
277              
278             All JQuery::* objects get passed the a variable pointing to
279             JQuery. Within JQuery::*, there may be a need to reference a path, eg
280             an image, and the base path can be modified by using this directory.
281              
282             This parameter is the jqueryDir passed by
283              
284             my $jquery = new JQuery(jqueryDir => '/jquery_js') ;
285            
286             =item get_css
287              
288             This is normally called very late in the program when you want to
289             produce all the css. It goes though all css registered with JQuery,
290             then it gets all css registered by JQuery::* objects, and then it gets
291             the css added add_css_last.
292              
293             =item get_jquery_code
294              
295             Similar to get_css, except that it produces the javascript to be
296             included in the page.
297              
298             =item new
299              
300             Used to instantiate the JQuery object. The path is the path seen by
301             the web browser, and not the local path.
302              
303             my $jquery = new JQuery(jqueryDir => '/jquery_js') ;
304              
305             =item usePacked
306              
307             JQuery modules can come in an unpacked or packed form. The former is
308             the original source, while the second, although not compiled, is
309             highly dense and obfuscated, while being more efficient.
310              
311             =back
312              
313             =head1 JQuery::Examples
314              
315             There are a number of working examples in the cgi-bin directory, which
316             can be found in the Perl distribution under the JQuery directory. The
317             examples are mostly written using CGI::Application , so you will need
318             to install CGI-Application to run the examples. This is not a
319             restriction, as the modules will work using CGI and mod-apache as
320             well, and hopefully the framework of yor choice.
321              
322             =head2 Demo.pm
323              
324             The examples mostly use Demo.pm. This is a very simple module which
325             initializes JQuery, calls get_jquery_code, get_css
326              
327             The module Demo.pm simply does some of the repetitive work.
328              
329             The setupfunction initiates $jquery.
330             cgiapp_postrun gets runs $jquery->get_jquery_code, $jquery->get_css and puts both of these, and
331             the HTML, into a very basic template.
332              
333             =head2 Ajax
334              
335             Let's start with the Ajax.
336              
337             Suppose you have a button, not neccessarily in a form, and you want
338             some action to happen when the user presses the button.
339              
340             Firstly, the JQuery module needs to be initialized.
341              
342             use JQuery ;
343             use JQuery::Taconite ;
344             my $jquery = new JQuery(jqueryDir => '/jquery_js') ;
345              
346             The button to be pressed needs an id, as it is going to accessed by javascript. So the HTML fragment could read:
347            
348            
349            
350             JQuery::Taconite->new(id => 'ex1', remoteProgram => '/cgi-bin/jquery_taconite_results.pl', rm => 'myRunMode', addToJQuery => $jquery) ;
351              
352             You may or may not need to set the run mode. CGI-Applications normally need
353             them, to define which function is to be executed in the CGI program.
354              
355             When the button is pressed, some output will be shown, and a placeholder is needed to display the text.
356              
357             The HTML fragment might be:
358            
359            
360             Initially this div is hidden.
361            
362              
363             This is a div where, initially, the text is not shown.
364              
365              
366             =head1 Example Programs
367              
368             =over
369              
370             =item jquery_accordion.pl
371              
372             Shows an example of an accordion
373              
374             =item jquery_clickmenu.pl
375              
376             An example of a menu in a normal desktop application
377              
378             Shows an example of an accordion
379              
380             =item jquery_form.pl
381              
382             This is a small example showing how a from is constructed, and how the
383             Ajax reply is sent, causing only the specified fields to be updated.
384              
385             =item jquery_splitter1.pl, jquery_splitter2.pl, jquery_splitter3.pl
386              
387             Examples showing how to split an area into two or three panes with a
388             bar allowing the user to resize them.
389              
390             =item jquery_tabs1.pl jquery_tabs2.pl jquery_tabs_results.pl
391              
392             Examples showing a tabbing of a pane. When the tab is pressed a
393             different page is shown. The user can download the page remotely from
394             a server.
395              
396             =item jquery_taconite1.pl and jquery_taconite_results1.pl jquery_taconite2.pl
397              
398             Taconite is a word that doesn't sound very interesting, but this
399             module allows you update your screen very easily without needing to
400             know anything about the DOM. This is Ajax at its easiest. You really
401             want to use this.
402              
403             jquery_taconite1.pl sets up the page and jquery_taconite_results1.pl
404             does the reply. This example show a variety of things to do with
405             Taconite, by adding radio buttons, wiring a button on the fly, adding
406             and removing items.
407              
408             =item jquery_taconite2.pl
409              
410             jquery_taconite2.pl does the same sort of thing as
411             jquery_taconite1.pl, except that it uses a run mode to define the
412             reply.
413              
414             Examples of split windows with a bar for resizing
415              
416             =item jquery_treeview.pl
417              
418             Show an expandable tree. You can choose grid lines or folder icons.
419              
420             =item jquery_heartbeat.pl
421              
422             Show how to update a page every second.
423              
424             =back
425              
426             =head1 WRITING NEW MODULES
427              
428             A module needs to provide the following methods
429              
430             new - to create the object
431              
432             id - the id of the object. There are some modules that don't need this. This only happens in the case of where an instance does not have any css related to the id.
433              
434             get_css - returns the css for the instance. The return value may be an array reference. css may be plain text or a JQuery:CSS object.
435              
436             HTML - returns the HTML text for the instance
437              
438             packages_needed - returns a list of jquery packages needed for the javascript to run
439              
440             get_jquery_code - returns the jQuery code
441              
442              
443             =head1 AUTHOR
444              
445             Peter Gordon, C<< >>
446              
447             =head1 ACKNOWLEDGMENTS
448              
449             Thanks to Brent Pedersen for pointing me in the direction of JQuery and to all
450             contibutors to jQuery from whom css/images/whatever have been plagiarized.
451              
452             =head1 BUGS
453              
454             Please report any bugs or feature requests to
455             C, or through the web interface at
456             L.
457             I will be notified, and then you'll automatically be notified of progress on
458             your bug as I make changes.
459              
460             =head1 SUPPORT
461              
462             You can find documentation for this module with the perldoc command.
463              
464             perldoc JQuery
465              
466             You can also look for information at:
467              
468             =over 4
469              
470             =item * AnnoCPAN: Annotated CPAN documentation
471              
472             L
473              
474             =item * CPAN Ratings
475              
476             L
477              
478             =item * RT: CPAN's request tracker
479              
480             L
481              
482             =item * Search CPAN
483              
484             L
485              
486             =back
487              
488             =head1 COPYRIGHT & LICENSE
489              
490             Copyright 2007 Peter Gordon, all rights reserved.
491              
492             This program is free software; you can redistribute it and/or modify it
493             under the same terms as Perl itself.
494              
495             =cut
496              
497             1; # End of JQuery