File Coverage

blib/lib/Spreadsheet/HTML/Presets/Scroll.pm
Criterion Covered Total %
statement 6 32 18.7
branch 0 12 0.0
condition 0 30 0.0
subroutine 2 5 40.0
pod 1 1 100.0
total 9 80 11.2


line stmt bran cond sub pod time code
1             package Spreadsheet::HTML::Presets::Scroll;
2 5     5   17 use strict;
  5         4  
  5         123  
3 5     5   15 use warnings FATAL => 'all';
  5         5  
  5         1961  
4              
5             sub scroll {
6 0     0 1   my ($self,$data,$args);
7 0 0         $self = shift if ref($_[0]) =~ /^Spreadsheet::HTML/;
8 0 0         ($self,$data,$args) = $self ? $self->_args( @_ ) : Spreadsheet::HTML::_args( @_ );
9              
10 0 0 0       $args->{fgdirection} ||= ($args->{bgdirection} || $args->{bx} || $args->{by}) ? '' : 'right';
      0        
11 0   0       $args->{bgdirection} ||= '';
12 0   0       $args->{interval} ||= 200;
13              
14 0           my @cells;
15 0           for my $r ( 0 .. $args->{_max_rows} - 1 ) {
16 0           for my $c ( 0 .. $args->{_max_cols} - 1 ) {
17 0           my $cell = sprintf '-r%sc%s', $r, $c;
18 0           push @cells, $cell => {
19             id => join( '-', $r, $c ),
20             class => 'scroll',
21             };
22             }
23             }
24              
25 0           my @args = (
26             caption => { '' => { align => 'bottom' } },
27             @_,
28             @cells,
29             );
30              
31 0           my $js = _javascript( %$args );
32 0 0         return( $js, @args ) if $args->{scroll};
33              
34 0 0         my $table = $self ? $self->generate( @args ) : Spreadsheet::HTML::generate( @args );
35 0           return $js . $table;
36             }
37              
38             sub _javascript {
39 0     0     my %args = @_;
40              
41 0           my %fmap = (
42             right => { fx => 1, fy => 0 },
43             left => { fx => -1, fy => 0 },
44             up => { fy => 1, fx => 0 },
45             down => { fy => -1, fx => 0 },
46             );
47              
48 0           my %bmap = (
49             right => { bx => 1, by => 0 },
50             left => { bx => -1, by => 0 },
51             up => { by => 1, bx => 0 },
52             down => { by => -1, bx => 0 },
53             );
54              
55 0 0 0       $args{fx} = (defined $args{fx} or defined $args{fy}) ? $args{fx} : $fmap{ $args{fgdirection} }{fx};
56 0   0       $args{fy} ||= $fmap{ $args{fgdirection} }{fy};
57 0   0       $args{bx} ||= $bmap{ $args{bgdirection} }{bx};
58 0   0       $args{by} ||= $bmap{ $args{bgdirection} }{by};
59              
60             my $js = sprintf _js_tmpl(),
61             $args{_max_rows},
62             $args{_max_cols},
63             $args{fx} || 0,
64             $args{fy} || 0,
65             $args{bx} || 0,
66             $args{by} || 0,
67             $args{interval},
68 0   0       ;
      0        
      0        
      0        
69              
70 0           return Spreadsheet::HTML::Presets::_js_wrapper( code => $js, %args );
71             }
72              
73             sub _js_tmpl {
74 0     0     return <<'END_JAVASCRIPT';
75              
76             /* Copyright 2017 Jeff Anderson */
77             /* install JavaScript::Minifier to minify this code */
78             var ROW = %s;
79             var COL = %s;
80             var FGX = %s;
81             var FGY = %s;
82             var BGX = %s;
83             var BGY = %s;
84             var INTERVAL = %s;
85             var tid;
86              
87             function toggle() {
88             if ($('#toggle').html() === 'Start') {
89             tid = setInterval( move, INTERVAL );
90             $('#toggle').html( 'Stop' );
91             } else {
92             clearInterval( tid );
93             $('#toggle').html( 'Start' );
94             }
95             }
96              
97             function move() {
98              
99             if (FGX) {
100             for (var row = 0; row < ROW; row++) {
101             var vals = new Array();
102             for (var col = 0; col < COL; col++) {
103             vals.push( $('#' + row + '-' + col ).clone() );
104             }
105              
106             if (FGX > 0) {
107             vals.unshift( vals.pop() );
108             } else {
109             vals.push( vals.shift() );
110             }
111              
112             for (var col = 0; col < COL; col++) {
113             $('#' + row + '-' + col ).html( vals[col].html() );
114             }
115             }
116             }
117              
118             if (FGY) {
119             for (var col = 0; col < COL; col++) {
120             var vals = new Array();
121             for (var row = 0; row < ROW; row++) {
122             vals.push( $('#' + row + '-' + col ).clone() );
123             }
124              
125             if (FGY > 0) {
126             vals.push( vals.shift() );
127             } else {
128             vals.unshift( vals.pop() );
129             }
130              
131             for (var row = 0; row < ROW; row++) {
132             $('#' + row + '-' + col ).html( vals[row].html() );
133             }
134             }
135             }
136              
137             if (BGX) {
138             for (var row = 0; row < ROW; row++) {
139             var vals = new Array();
140             for (var col = 0; col < COL; col++) {
141             vals.push( $('#' + row + '-' + col ).clone() );
142             }
143              
144             if (BGX > 0) {
145             vals.unshift( vals.pop() );
146             } else {
147             vals.push( vals.shift() );
148             }
149              
150             for (var col = 0; col < COL; col++) {
151             $('#' + row + '-' + col ).attr( 'style', vals[col].attr( 'style' ) );
152             }
153             }
154             }
155              
156             if (BGY) {
157             for (var col = 0; col < COL; col++) {
158             var vals = new Array();
159             for (var row = 0; row < ROW; row++) {
160             vals.push( $('#' + row + '-' + col ).clone() );
161             }
162              
163             if (BGY > 0) {
164             vals.push( vals.shift() );
165             } else {
166             vals.unshift( vals.pop() );
167             }
168              
169             for (var row = 0; row < ROW; row++) {
170             $('#' + row + '-' + col ).attr( 'style', vals[row].attr( 'style' ) );
171             }
172             }
173             }
174             }
175             END_JAVASCRIPT
176             }
177              
178             =head1 NAME
179              
180             Spreadsheet::HTML::Presets::Scroll - Generate scrolling HTML table cells and backgrounds.
181              
182             =head1 DESCRIPTION
183              
184             This is a container for L preset methods.
185             These methods are not meant to be called from this package.
186             Instead, use the Spreadsheet::HTML interface:
187              
188             use Spreadsheet::HTML;
189             my $generator = Spreadsheet::HTML->new( data => \@data );
190             print $generator->scroll;
191              
192             # or
193             use Spreadsheet::HTML qw( scroll );
194             print scroll( data => \@data );
195              
196             =head1 METHODS
197              
198             =over 4
199              
200             =item * C
201              
202             Moves the contents (C for CDATA, C
203             for attributes) of each cell in the direction specified.
204             Valid values are C, C, C and C, or
205             you can optionally use C and/or C instead of C
206             to specify which axis(es) to scroll, as well as C and
207             C instead of C.
208              
209             scroll( fgdirection => 'left' )
210              
211             # same as
212             scroll( fx => -1 )
213              
214             # produce diagonal (left and up)
215             scroll( fx => -1, fy => -1 )
216              
217             Set the timer with C (defaults to 200 miliseconds).
218              
219             scroll( fgdirection => 'right', interval => 500 )
220              
221             Uses Google's jQuery API unless you specify another URI via
222             the C param. Javascript will be minified via
223             L if it is installed.
224              
225             Virtually all other Spreadsheet::HTML generating methods/procedures
226             also can additionally specify C et. al. as a literal parameters:
227              
228             print $generator->landscape( scroll => 1, by => -1, bx => 1 )
229              
230             =back
231              
232             =head1 SEE ALSO
233              
234             =over 4
235              
236             =item L
237              
238             The interface for this functionality.
239              
240             =item L
241              
242             More presets.
243              
244             =back
245              
246             =head1 AUTHOR
247              
248             Jeff Anderson, C<< >>
249              
250             =head1 LICENSE AND COPYRIGHT
251              
252             Copyright 2017 Jeff Anderson.
253              
254             This program is free software; you can redistribute it and/or modify it
255             under the terms of the the Artistic License (2.0). You may obtain a
256             copy of the full license at:
257              
258             L
259              
260             Any use, modification, and distribution of the Standard or Modified
261             Versions is governed by this Artistic License. By using, modifying or
262             distributing the Package, you accept this license. Do not use, modify,
263             or distribute the Package, if you do not accept this license.
264              
265             If your Modified Version has been derived from a Modified Version made
266             by someone other than you, you are nevertheless required to ensure that
267             your Modified Version complies with the requirements of this license.
268              
269             This license does not grant you the right to use any trademark, service
270             mark, tradename, or logo of the Copyright Holder.
271              
272             This license includes the non-exclusive, worldwide, free-of-charge
273             patent license to make, have made, use, offer to sell, sell, import and
274             otherwise transfer the Package with respect to any patent claims
275             licensable by the Copyright Holder that are necessarily infringed by the
276             Package. If you institute patent litigation (including a cross-claim or
277             counterclaim) against any party alleging that the Package constitutes
278             direct or contributory patent infringement, then this Artistic License
279             to you shall terminate on the date that such litigation is filed.
280              
281             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
282             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
283             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
284             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
285             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
286             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
287             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
288             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289              
290             =cut
291              
292             1;