File Coverage

blib/lib/Spreadsheet/HTML/Presets/Scroll.pm
Criterion Covered Total %
statement 6 34 17.6
branch 0 12 0.0
condition 0 30 0.0
subroutine 2 6 33.3
pod 2 2 100.0
total 10 84 11.9


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