File Coverage

blib/lib/Spreadsheet/HTML/Presets/Calculator.pm
Criterion Covered Total %
statement 6 23 26.0
branch 0 14 0.0
condition n/a
subroutine 2 6 33.3
pod 1 1 100.0
total 9 44 20.4


line stmt bran cond sub pod time code
1             package Spreadsheet::HTML::Presets::Calculator;
2 5     5   26 use strict;
  5         11  
  5         157  
3 5     5   26 use warnings FATAL => 'all';
  5         11  
  5         2416  
4              
5             sub calculator {
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           $data = [
11             [ 'C', '±', '÷', '×' ],
12             [ 7, 8, 9, '−' ],
13             [ 4, 5, 6, '+' ],
14             [ 1, 2, 3, '=' ],
15             [ 0, '.' ],
16             ];
17              
18             my %attrs = (
19             height => 65,
20             width => 65,
21             align => 'center',
22 0 0         %{ $args->{td} || {} },
23             style => {
24             'font-size' => 'xx-large',
25             padding => 0,
26             margins => 0,
27 0 0         %{ $args->{td}{style} || {} },
  0            
28             },
29             );
30              
31 0           my $attrs = 'font-size: xx-large; font-weight: bold; font-family: monospace;';
32              
33             my @args = (
34             @_,
35             table => {
36             width => '20%',
37 0 0         %{ $args->{table} || {} },
38             style => {
39             border => 'thick outset',
40             padding => 0,
41             margins => 0,
42 0 0         %{ $args->{table}{style} || {} },
43             },
44             },
45             caption => qq(),
46 0     0     td => [ { %attrs }, sub { qq() } ],
  0            
47             -r3c3 => { rowspan => 2, %attrs },
48             -r4c0 => { colspan => 2, %attrs },
49             _layout => 1,
50             data => $data,
51             theta => 0,
52             flip => 0,
53             tgroups => 0,
54             headless => 0,
55             pinhead => 0,
56             wrap => 0,
57             matrix => 1,
58             );
59              
60 0           my $js = _javascript( %$args );
61 0 0         my $table = $self ? $self->generate( @args ) : Spreadsheet::HTML::generate( @args );
62 0           return $js . $table;
63             }
64              
65             sub _javascript {
66 0     0     return Spreadsheet::HTML::Presets::_js_wrapper( code => _js_tmpl(), @_ );
67             }
68              
69             sub _js_tmpl {
70 0     0     return <<'END_JAVASCRIPT';
71              
72             /* Copyright 2016 Jeff Anderson */
73             /* install JavaScript::Minifier to minify this code */
74              
75             var DISPLAY = [ 0 ];
76             var OPERAND = '';
77              
78             $(document).ready(function(){
79              
80             $('#display').prop( 'readonly', 'readonly' );
81              
82             $('button').click( function( data ) {
83              
84             var html = $(this).html();
85             var val = $('
').html(html).text();
86              
87             if (val === '.' && ( DISPLAY[0] === 0 || DISPLAY[0].indexOf('.') === -1 )) {
88              
89             DISPLAY[0] += val;
90              
91             } else if (+val === parseInt( val )) {
92              
93             if (DISPLAY[0] === "0.") {
94             DISPLAY[0] += val;
95             } else if (DISPLAY[0] == 0) {
96             DISPLAY[0] = val;
97             } else {
98             DISPLAY[0] += val;
99             }
100              
101             } else if (val === '+') {
102              
103             if (OPERAND) {
104             var value = eval( DISPLAY[1] + OPERAND + DISPLAY[0] );
105             DISPLAY = [ value ];
106             }
107              
108             update();
109             OPERAND = val;
110             DISPLAY.unshift( '' );
111             return;
112              
113             } else if (val.charCodeAt(0) == 8722) {
114              
115             if (OPERAND) {
116             var value = eval( DISPLAY[1] + OPERAND + DISPLAY[0] );
117             DISPLAY = [ value ];
118             }
119              
120             update();
121             OPERAND = '-';
122             DISPLAY.unshift( '' );
123             return;
124              
125             } else if (val.charCodeAt(0) == 215) {
126              
127             if (OPERAND) {
128             var value = eval( DISPLAY[1] + OPERAND + DISPLAY[0] );
129             DISPLAY = [ value ];
130             }
131              
132             update();
133             OPERAND = '*';
134             DISPLAY.unshift( '' );
135             return;
136              
137             } else if (val.charCodeAt(0) == 247) {
138              
139             if (OPERAND) {
140             var value = eval( DISPLAY[1] + OPERAND + DISPLAY[0] );
141             DISPLAY = [ value ];
142             }
143              
144             update();
145             OPERAND = '/';
146             DISPLAY.unshift( '' );
147             return;
148              
149             } else if (val.charCodeAt(0) == 177) {
150              
151             DISPLAY[0] *= -1;
152              
153             } else if (val === '=') {
154              
155             var value = eval( DISPLAY[1] + OPERAND + DISPLAY[0] );
156             OPERAND = '';
157             DISPLAY = [ value ];
158             update();
159             DISPLAY = [ 0 ];
160             return;
161              
162             } else if (val === 'C') {
163              
164             DISPLAY = [ 0 ];
165             }
166              
167             update();
168             });
169              
170             update();
171             });
172              
173             function update() { $('#display').val( DISPLAY[0] ) }
174              
175             END_JAVASCRIPT
176             }
177              
178             =head1 NAME
179              
180             Spreadsheet::HTML::Presets::Calculator - Generate HTML table basic calculator.
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;
190             print $generator->calculator;
191              
192             # or
193             use Spreadsheet::HTML qw( calculator );
194             print calculator();
195              
196             =head1 METHODS
197              
198             =over 4
199              
200             =item * C
201              
202             Generates a simple calculator.
203              
204             Uses Google's jQuery API unless you specify another URI via
205             the C param. Javascript will be minified
206             via L if it is installed.
207              
208             =back
209              
210             =head1 SEE ALSO
211              
212             =over 4
213              
214             =item L
215              
216             The interface for this functionality.
217              
218             =item L
219              
220             More presets.
221              
222             =back
223              
224             =head1 AUTHOR
225              
226             Jeff Anderson, C<< >>
227              
228             =head1 LICENSE AND COPYRIGHT
229              
230             Copyright 2016 Jeff Anderson.
231              
232             This program is free software; you can redistribute it and/or modify it
233             under the terms of the the Artistic License (2.0). You may obtain a
234             copy of the full license at:
235              
236             L
237              
238             Any use, modification, and distribution of the Standard or Modified
239             Versions is governed by this Artistic License. By using, modifying or
240             distributing the Package, you accept this license. Do not use, modify,
241             or distribute the Package, if you do not accept this license.
242              
243             If your Modified Version has been derived from a Modified Version made
244             by someone other than you, you are nevertheless required to ensure that
245             your Modified Version complies with the requirements of this license.
246              
247             This license does not grant you the right to use any trademark, service
248             mark, tradename, or logo of the Copyright Holder.
249              
250             This license includes the non-exclusive, worldwide, free-of-charge
251             patent license to make, have made, use, offer to sell, sell, import and
252             otherwise transfer the Package with respect to any patent claims
253             licensable by the Copyright Holder that are necessarily infringed by the
254             Package. If you institute patent litigation (including a cross-claim or
255             counterclaim) against any party alleging that the Package constitutes
256             direct or contributory patent infringement, then this Artistic License
257             to you shall terminate on the date that such litigation is filed.
258              
259             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
260             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
261             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
262             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
263             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
264             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
265             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
266             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267              
268             =cut
269              
270             1;