Spreadsheet-HTML

 view release on metacpan or  search on metacpan

lib/Spreadsheet/HTML/Presets/Handson.pm  view on Meta::CPAN

package Spreadsheet::HTML::Presets::Handson;
use strict;
use warnings FATAL => 'all';

eval "use JSON";
our $NO_JSON = $@;

sub handson {
    my ($self,$data,$args);
    $self = shift if ref($_[0]) =~ /^Spreadsheet::HTML/;
    ($self,$data,$args) = $self ? $self->_args( @_ ) : Spreadsheet::HTML::_args( @_ );

    $args->{args}{rowHeaders} ||= 'true';
    $args->{args}{colHeaders} ||= 'true';
    $args->{json} = '';
    if ($NO_JSON) {
        $args->{json} = '{' . join( ', ', map "$_: $args->{args}{$_}", keys %{ $args->{args} } ) . '}';
    } else {
        my $json = JSON->new->allow_nonref;
        $args->{json} = $json->encode( $args->{args} );
        $args->{json} =~ s/"//g;
    }

    $args->{id} ||= 'handsontable';
    my @args = (
        @_,
        empty => undef,
        table => { %{ $args->{table} || {} }, class => $args->{id} },
    );

    my $table = $self ? $self->generate( @args ) : Spreadsheet::HTML::generate( @args );
    return _javascript( %$args ) . $args->{_auto}->tag( tag => 'div', cdata => $table, attr => { id => $args->{id} } );
}

sub _javascript {
    my %args = @_;

    my $js = sprintf _js_tmpl(), $args{id}, $args{json};

    $args{css} ||= 'http://handsontable.com/dist/handsontable.full.css';
    $args{handsonjs} ||= 'http://handsontable.com/dist/handsontable.full.js';
    $args{copyright} = 'Copyright (c) 2012-2014 Marcin Warpechowski | Copyright 2024 Handsoncode sp. z o.o.';

    return Spreadsheet::HTML::Presets::_js_wrapper( code => $js, %args );
}

sub _js_tmpl {
    return <<'END_JAVASCRIPT';

/* Copyright 2012-2014 Marcin Warpechowski */
/* Copyright 2024 Handsoncode sp. z o.o. */
/* install JavaScript::Minifier to minify this code */

var id = '%s';
var handson_args = %s;

$(document).ready( function () {

    var data = new Array;
    $('.' + id + ' tr').each( function () {
        var row = new Array;
        $.each( this.cells, function () {
            row.push( $(this).html() );
        });
        data.push( row );
    });

    $('#' + id).html( '' );
    handson_args['data'] = data;

    var hot = new Handsontable( document.getElementById( id ), handson_args );
});

END_JAVASCRIPT
}

1;

=head1 NAME

Spreadsheet::HTML::Presets::Handson - Generate Handsontable HTML tables.

=head1 DESCRIPTION

This is a container for L<Spreadsheet::HTML> preset methods.
These methods are not meant to be called from this package.
Instead, use the Spreadsheet::HTML interface:

  use Spreadsheet::HTML;
  my $generator = Spreadsheet::HTML->new( data => \@data );
  print $generator->handson;



( run in 1.338 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )