File Coverage

blib/lib/Spreadsheet/HTML/Presets/Handson.pm
Criterion Covered Total %
statement 7 33 21.2
branch 0 10 0.0
condition 0 10 0.0
subroutine 3 6 50.0
pod 1 1 100.0
total 11 60 18.3


line stmt bran cond sub pod time code
1             package Spreadsheet::HTML::Presets::Handson;
2 5     5   16 use strict;
  5         5  
  5         123  
3 5     5   14 use warnings FATAL => 'all';
  5         6  
  5         1853  
4              
5 5     5   784 eval "use JSON";
  0            
  0            
6             our $NO_JSON = $@;
7              
8             sub handson {
9 0     0 1   my ($self,$data,$args);
10 0 0         $self = shift if ref($_[0]) =~ /^Spreadsheet::HTML/;
11 0 0         ($self,$data,$args) = $self ? $self->_args( @_ ) : Spreadsheet::HTML::_args( @_ );
12              
13 0   0       $args->{args}{rowHeaders} ||= 'true';
14 0   0       $args->{args}{colHeaders} ||= 'true';
15 0           $args->{json} = '';
16 0 0         if ($NO_JSON) {
17 0           $args->{json} = '{' . join( ', ', map "$_: $args->{args}{$_}", keys %{ $args->{args} } ) . '}';
  0            
18             } else {
19 0           my $json = JSON->new->allow_nonref;
20 0           $args->{json} = $json->encode( $args->{args} );
21 0           $args->{json} =~ s/"//g;
22             }
23              
24 0   0       $args->{id} ||= 'handsontable';
25             my @args = (
26             @_,
27             empty => undef,
28 0 0         table => { %{ $args->{table} || {} }, class => $args->{id} },
29 0           );
30              
31 0 0         my $table = $self ? $self->generate( @args ) : Spreadsheet::HTML::generate( @args );
32 0           return _javascript( %$args ) . $args->{_auto}->tag( tag => 'div', cdata => $table, attr => { id => $args->{id} } );
33             }
34              
35             sub _javascript {
36 0     0     my %args = @_;
37              
38 0           my $js = sprintf _js_tmpl(), $args{id}, $args{json};
39              
40 0   0       $args{css} ||= 'http://handsontable.com/dist/handsontable.full.css';
41 0   0       $args{handsonjs} ||= 'http://handsontable.com/dist/handsontable.full.js';
42 0           $args{copyright} = 'Copyright (c) 2012-2014 Marcin Warpechowski | Copyright 2016 Handsoncode sp. z o.o.';
43              
44 0           return Spreadsheet::HTML::Presets::_js_wrapper( code => $js, %args );
45             }
46              
47             sub _js_tmpl {
48 0     0     return <<'END_JAVASCRIPT';
49              
50             /* Copyright 2012-2014 Marcin Warpechowski */
51             /* Copyright 2016 Handsoncode sp. z o.o. */
52             /* install JavaScript::Minifier to minify this code */
53              
54             var id = '%s';
55             var handson_args = %s;
56              
57             $(document).ready( function () {
58              
59             var data = new Array;
60             $('.' + id + ' tr').each( function () {
61             var row = new Array;
62             $.each( this.cells, function () {
63             row.push( $(this).html() );
64             });
65             data.push( row );
66             });
67              
68             $('#' + id).html( '' );
69             handson_args['data'] = data;
70              
71             var hot = new Handsontable( document.getElementById( id ), handson_args );
72             });
73              
74             END_JAVASCRIPT
75             }
76              
77             1;
78              
79             =head1 NAME
80              
81             Spreadsheet::HTML::Presets::Handson - Generate Handsontable HTML tables.
82              
83             =head1 DESCRIPTION
84              
85             This is a container for L preset methods.
86             These methods are not meant to be called from this package.
87             Instead, use the Spreadsheet::HTML interface:
88              
89             use Spreadsheet::HTML;
90             my $generator = Spreadsheet::HTML->new( data => \@data );
91             print $generator->handson;
92              
93             # or
94             use Spreadsheet::HTML qw( handson );
95             print handson( data => \@data );
96              
97             =head1 METHODS
98              
99             =over 4
100              
101             =item * C
102              
103             Styles table with Handsontable, a "data grid component with
104             and Excel-like appearance."
105              
106             Generate an empty 100x20 data grid:
107              
108             handson( fill => '100x20' )
109              
110             Uses Handsontable's JS API unless you specify another URI via
111             the C param. Also uses their CSS unless you
112             override with the C param.
113              
114             handson(
115             handsonjs => '/dist/handsontable.full.js',
116             css => '/dist/handsontable.full.css',
117             )
118              
119             Uses Google's jQuery API unless you specify another URI via
120             the C param. Javascript will be minified
121             via L if it is installed.
122              
123             =back
124              
125             =head1 SEE ALSO
126              
127             =over 4
128              
129             =item L
130              
131             The interface for this functionality.
132              
133             =item L
134              
135             More presets.
136              
137             =back
138              
139             =head1 AUTHOR
140              
141             Jeff Anderson, C<< >>
142              
143             =head1 LICENSE AND COPYRIGHT
144              
145             The MIT License effective as of January 12, 2015.
146             Copyright 2012-2014 Marcin Warpechowski
147             Copyright 2016 Handsoncode sp. z o.o.
148             Permission is hereby granted, free of charge, to any person obtaining
149             a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
150             The above copyright notice and this permission notice shall be
151             included in all copies or substantial portions of the Software.
152             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
153             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.