| blib/lib/Rest/HtmlVis/Content.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % | 
| statement | 18 | 76 | 23.6 | 
| branch | 0 | 20 | 0.0 | 
| condition | 0 | 28 | 0.0 | 
| subroutine | 6 | 14 | 42.8 | 
| pod | 6 | 6 | 100.0 | 
| total | 30 | 144 | 20.8 | 
| line | stmt | bran | cond | sub | pod | time | code | 
|---|---|---|---|---|---|---|---|
| 1 | package Rest::HtmlVis::Content; | ||||||
| 2 | |||||||
| 3 | 1 | 1 | 651 | use 5.006; | |||
| 1 | 8 | ||||||
| 1 | 32 | ||||||
| 4 | 1 | 1 | 4 | use strict; | |||
| 1 | 1 | ||||||
| 1 | 25 | ||||||
| 5 | 1 | 1 | 3 | use warnings FATAL => 'all'; | |||
| 1 | 2 | ||||||
| 1 | 32 | ||||||
| 6 | |||||||
| 7 | 1 | 1 | 4 | use parent qw( Rest::HtmlVis::Key ); | |||
| 1 | 1 | ||||||
| 1 | 4 | ||||||
| 8 | |||||||
| 9 | 1 | 1 | 524 | use Plack::Request; | |||
| 1 | 53330 | ||||||
| 1 | 29 | ||||||
| 10 | 1 | 1 | 583 | use YAML::Syck; | |||
| 1 | 2115 | ||||||
| 1 | 807 | ||||||
| 11 | |||||||
| 12 | =head1 NAME | ||||||
| 13 | |||||||
| 14 | Rest::HtmlVis::Content - Return base block for keys links and form. | ||||||
| 15 | |||||||
| 16 | =head1 VERSION | ||||||
| 17 | |||||||
| 18 | Version 0.01 | ||||||
| 19 | |||||||
| 20 | =cut | ||||||
| 21 | |||||||
| 22 | our $VERSION = '0.01'; | ||||||
| 23 | |||||||
| 24 | |||||||
| 25 | =head1 SYNOPSIS | ||||||
| 26 | |||||||
| 27 | Quick summary of what the module does. | ||||||
| 28 | |||||||
| 29 | Perhaps a little code snippet. | ||||||
| 30 | |||||||
| 31 | use Rest::HtmlVis::Content; | ||||||
| 32 | |||||||
| 33 | my $foo = Rest::HtmlVis::Content->new(); | ||||||
| 34 | ... | ||||||
| 35 | |||||||
| 36 | =head1 KEYS | ||||||
| 37 | |||||||
| 38 | =head2 links | ||||||
| 39 | |||||||
| 40 | Convert default strcuture of links. Each link should consists of: | ||||||
| 41 | |||||||
| 42 | =over 4 | ||||||
| 43 | |||||||
| 44 | =item * href | ||||||
| 45 | |||||||
| 46 | URL of target.Can be absolute or relative. | ||||||
| 47 | |||||||
| 48 | =item * name | ||||||
| 49 | |||||||
| 50 | Name of the link. | ||||||
| 51 | |||||||
| 52 | =item * id | ||||||
| 53 | |||||||
| 54 | Identifier of the link | ||||||
| 55 | |||||||
| 56 | =back | ||||||
| 57 | |||||||
| 58 | Example: | ||||||
| 59 | |||||||
| 60 | links => [ | ||||||
| 61 | { | ||||||
| 62 | href => '/api/test', | ||||||
| 63 | name => 'Test resource', | ||||||
| 64 | id => 'api.test' | ||||||
| 65 | } | ||||||
| 66 | ] | ||||||
| 67 | |||||||
| 68 | =head2 form | ||||||
| 69 | |||||||
| 70 | Define elements of formular for html. | ||||||
| 71 | |||||||
| 72 | =cut | ||||||
| 73 | |||||||
| 74 | sub setStruct { | ||||||
| 75 | 0 | 0 | 1 | my ($self, $key, $struct, $env) = @_; | |||
| 76 | 0 | $self->{struct} = $struct; | |||||
| 77 | 0 | $self->{env} = $env; | |||||
| 78 | |||||||
| 79 | 0 | return 1; | |||||
| 80 | } | ||||||
| 81 | |||||||
| 82 | sub getOrder { | ||||||
| 83 | 0 | 0 | 1 | return 99999999999; | |||
| 84 | } | ||||||
| 85 | |||||||
| 86 | sub newRow { | ||||||
| 87 | 0 | 0 | 1 | return 1; | |||
| 88 | } | ||||||
| 89 | |||||||
| 90 | sub head { | ||||||
| 91 | 0 | 0 | 1 | ' | |||
| 92 | |||||||
| 98 | ' | ||||||
| 99 | } | ||||||
| 100 | |||||||
| 101 | sub onload { | ||||||
| 102 | 0 | 0 | 1 | 'prettyPrint();' | |||
| 103 | } | ||||||
| 104 | |||||||
| 105 | sub html { | ||||||
| 106 | 0 | 0 | 1 | my ($self) = @_; | |||
| 107 | 0 | my $struct = $self->getStruct; | |||||
| 108 | |||||||
| 109 | ### Links | ||||||
| 110 | 0 | my $links = ''; | |||||
| 111 | 0 | 0 | 0 | if (exists $struct->{links} && ref $struct->{links} eq 'ARRAY'){ | |||
| 112 | 0 | foreach my $link (@{$struct->{links}}) { | |||||
| 0 | |||||||
| 113 | 0 | $links .= ' | |||||
| 114 | } | ||||||
| 115 | 0 | delete $struct->{links}; | |||||
| 116 | } | ||||||
| 117 | |||||||
| 118 | ### Content | ||||||
| 119 | 0 | my $content = ''; | |||||
| 120 | { | ||||||
| 121 | 0 | local $Data::Dumper::Indent=1; | |||||
| 0 | |||||||
| 122 | 0 | local $Data::Dumper::Quotekeys=0; | |||||
| 123 | 0 | local $Data::Dumper::Terse=1; | |||||
| 124 | 0 | local $Data::Dumper::Sortkeys=1; | |||||
| 125 | |||||||
| 126 | 0 | $content = Dump($struct); | |||||
| 127 | } | ||||||
| 128 | |||||||
| 129 | ### Form | ||||||
| 130 | 0 | my $form = {}; | |||||
| 131 | # if (exists $struct->{form} && ref $struct->{form} eq 'HASH'){ | ||||||
| 132 | 0 | 0 | 0 | if (exists $struct->{form} && ref $struct->{form} eq 'HASH'){ | |||
| 0 | 0 | ||||||
| 133 | 0 | $form = _formToHtml($struct->{form}); | |||||
| 134 | 0 | delete $struct->{form}; | |||||
| 135 | }elsif( exists $self->getEnv()->{'restapi.class'} && $self->getEnv()->{'restapi.class'}->can('GET_FORM')){ | ||||||
| 136 | 0 | my $req = Plack::Request->new($self->getEnv()); | |||||
| 137 | 0 | my $par = $req->parameters; | |||||
| 138 | 0 | $par->add('content', $content); | |||||
| 139 | 0 | $form = _formToHtml($self->getEnv()->{'restapi.class'}->GET_FORM($par)); | |||||
| 140 | } | ||||||
| 141 | |||||||
| 142 | " | ||||||
| 143 |  | ||||||
| 144 |  | ||||||
| 145 | $links | ||||||
| 146 | |||||||
| 147 | |||||||
| 148 |  | ||||||
| 149 |  | ||||||
| 150 | $content | ||||||
| 151 | |||||||
| 152 | |||||||
| 153 |  | ||||||
| 154 | |||||||
| 155 | |||||||
| 156 |  | ||||||
| 157 |  | ||||||
| 158 |  | ||||||
| 159 |  | ||||||
| 160 |  | ||||||
| 161 | |||||||
| 162 | |||||||
| 163 | |||||||
| 164 |  | ||||||
| 165 |  | ||||||
| 166 | |||||||
| 167 | 0 | 0 | ".($form->{GET}||'  Not allowed ')." | ||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 168 | |||||||
| 169 | |||||||
| 170 |  | ||||||
| 171 | |||||||
| 172 | ".($form->{POST}||'  Not allowed ')." | ||||||
| 173 | |||||||
| 174 | |||||||
| 175 |  | ||||||
| 176 | |||||||
| 177 | ".($form->{PUT}||'  Not allowed ')." | ||||||
| 178 | |||||||
| 179 | |||||||
| 180 |  | ||||||
| 181 | |||||||
| 182 | ".($form->{DELETE}||'  Not allowed ')." | ||||||
| 183 | |||||||
| 184 | |||||||
| 185 | |||||||
| 186 | |||||||
| 187 | |||||||
| 188 | " | ||||||
| 189 | } | ||||||
| 190 | |||||||
| 191 | sub _getAjaxCall { | ||||||
| 192 | 0 | 0 | my ($self, $methodType) = @_; | ||||
| 193 | 0 | "\$.ajax({ | |||||
| 194 | type: '$methodType', | ||||||
| 195 | url: '".$self->getEnv()->{REQUEST_URI}."', | ||||||
| 196 | success: function(data) { | ||||||
| 197 | alert('Success'); | ||||||
| 198 | var newDoc = document.open('text/html', 'replace'); | ||||||
| 199 | newDoc.write(data); | ||||||
| 200 | newDoc.close(); | ||||||
| 201 | }, | ||||||
| 202 | error: function(data) { | ||||||
| 203 | alert(data.responseText); | ||||||
| 204 | }, | ||||||
| 205 | data: \$(this).serialize() | ||||||
| 206 | }); return false;" | ||||||
| 207 | } | ||||||
| 208 | |||||||
| 209 | |||||||
| 210 | my $defaultForm = { | ||||||
| 211 | GET => "", | ||||||
| 212 | |||||||
| 213 | POST => " | ||||||
| 214 | |||||||
| 215 | |||||||
| 216 | |||||||
| 217 | |||||||
| 218 | |||||||
| 219 | ", | ||||||
| 220 | |||||||
| 221 | PUT => " | ||||||
| 222 | |||||||
| 223 | |||||||
| 224 | |||||||
| 225 | |||||||
| 226 | |||||||
| 227 | ", | ||||||
| 228 | |||||||
| 229 | DELETE => "", | ||||||
| 230 | }; | ||||||
| 231 | |||||||
| 232 | sub _formToHtml { | ||||||
| 233 | 0 | 0 | my ($struct) = @_; | ||||
| 234 | |||||||
| 235 | 0 | my $form = {}; | |||||
| 236 | 0 | foreach my $method (keys %{$struct}) { | |||||
| 0 | |||||||
| 237 | 0 | 0 | 0 | if (exists $struct->{$method}{params} && ref $struct->{$method}{params} eq 'ARRAY'){ | |||
| 0 | |||||||
| 238 | 0 | my $html = ''; | |||||
| 239 | 0 | foreach my $param (@{$struct->{$method}{params}}) { | |||||
| 0 | |||||||
| 240 | 0 | my $type = $param->{type}; | |||||
| 241 | 0 | my $name = $param->{name}; | |||||
| 242 | |||||||
| 243 | 0 | 0 | next unless $name; | ||||
| 244 | |||||||
| 245 | 0 | 0 | if ($type eq 'text'){ | ||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 246 | 0 | 0 | my $default = ($param->{default}||''); | ||||
| 247 | 0 | $html .= ''; | |||||
| 248 | }elsif ($type eq 'textarea'){ | ||||||
| 249 | 0 | 0 | my $rows = ($param->{rows}||20); | ||||
| 250 | 0 | 0 | my $cols = ($param->{cols}||3); | ||||
| 251 | 0 | 0 | my $default = ($param->{default}||''); | ||||
| 252 | 0 | $html .= ''; | |||||
| 253 | }elsif ($type eq 'checkbox'){ | ||||||
| 254 | }elsif ($type eq 'radio'){ | ||||||
| 255 | } | ||||||
| 256 | } | ||||||
| 257 | 0 | $form->{$method} .= $html; | |||||
| 258 | }elsif(exists $struct->{$method}{default}){ | ||||||
| 259 | 0 | my $html = ''; | |||||
| 260 | 0 | $html .= ''; | |||||
| 261 | 0 | $form->{$method} .= $html; | |||||
| 262 | } | ||||||
| 263 | 0 | $form->{$method} .= $defaultForm->{$method}; | |||||
| 264 | } | ||||||
| 265 | |||||||
| 266 | 0 | return $form; | |||||
| 267 | } | ||||||
| 268 | |||||||
| 269 | =head1 AUTHOR | ||||||
| 270 | |||||||
| 271 | Vaclav Dovrtel, C<< | ||||||
| 272 | |||||||
| 273 | =head1 BUGS | ||||||
| 274 | |||||||
| 275 | Please report any bugs or feature requests to github repository. | ||||||
| 276 | |||||||
| 277 | =head1 ACKNOWLEDGEMENTS | ||||||
| 278 | |||||||
| 279 | Inspired by L | ||||||
| 280 | |||||||
| 281 | =head1 REPOSITORY | ||||||
| 282 | |||||||
| 283 | L | ||||||
| 284 | |||||||
| 285 | =head1 LICENSE AND COPYRIGHT | ||||||
| 286 | |||||||
| 287 | Copyright 2015 Vaclav Dovrtel. | ||||||
| 288 | |||||||
| 289 | This program is free software; you can redistribute it and/or modify it | ||||||
| 290 | under the terms of either: the GNU General Public License as published | ||||||
| 291 | by the Free Software Foundation; or the Artistic License. | ||||||
| 292 | |||||||
| 293 | See L | ||||||
| 294 | |||||||
| 295 | =cut | ||||||
| 296 | |||||||
| 297 | 1; # End of Rest::HtmlVis::Content |