1 |
package Perly::View; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Jifty::View::Declare -base; |
7 |
|
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
template '/' => page { |
11 |
h1 { _("Available code") } |
12 |
div { show 'code_list' } |
13 |
}; |
14 |
|
15 |
template '/edit' => page { |
16 |
h1 { _("Enter your code") } |
17 |
div { show 'code_editor' } |
18 |
}; |
19 |
|
20 |
private template 'code_list' => sub { |
21 |
ul { |
22 |
my $coll = Perly::Model::CodeCollection->new(); |
23 |
$coll->unlimit; |
24 |
while ( my $code = $coll->next ) { |
25 |
li { |
26 |
a { |
27 |
attr { href => '/edit/id=' . $code->id, }, |
28 |
$code->name, |
29 |
}, |
30 |
small { length( $code->source ), ' bytes ', $code->created_on }, |
31 |
a { |
32 |
attr { href => '/run/code=' . $code->id, }, 'run' |
33 |
}, |
34 |
pre { $code->source }, |
35 |
} |
36 |
} |
37 |
} |
38 |
}; |
39 |
|
40 |
private template 'code_editor' => sub { |
41 |
my $op = 'Create'; |
42 |
my $code = Perly::Model::Code->new(); |
43 |
if ( my $id = get 'id' ) { |
44 |
$code->load( $id ) or die "can't load $id"; |
45 |
warn "loaded ", length( $code->source ), " bytes"; |
46 |
$op = 'Update'; |
47 |
} |
48 |
form { |
49 |
my $action = new_action( class => $op . 'Code', arguments => { $code ? $code->as_hash : undef } ); |
50 |
render_param( $action => 'name' ); |
51 |
render_param( $action => 'source', cols => 80, rows => 25, language => 'perl', render_as => 'Jifty::Plugin::CodePress::Textarea' ); |
52 |
#render_action( $action => [ 'source' => { cols => 80, rows => 30 }, ] ); |
53 |
form_submit( |
54 |
label => _($op), |
55 |
onclick => [ |
56 |
{ beforeclick => "CodePress.beforeSubmit();" }, |
57 |
], |
58 |
); |
59 |
form_next_page( url => '/run/code=' . $action->argument_value('id') ); |
60 |
} |
61 |
}; |
62 |
|
63 |
template '/upload' => page { |
64 |
h1 { _("Upload new file") }, |
65 |
form { |
66 |
my $action = new_action( class => 'Upload' ); |
67 |
render_action( $action => [ 'filename', 'content', 'file', 'format' ] ); |
68 |
form_submit( |
69 |
label => _('Upload'), |
70 |
); |
71 |
} |
72 |
}; |
73 |
|
74 |
template '/run' => page { |
75 |
my $action = new_action( class => 'Run', arguments => { |
76 |
input => get('input'), |
77 |
code => get('code'), |
78 |
} ); |
79 |
warn "code = ",get('code'), " input = ", get('input'), dump( $action ); |
80 |
h1 { _("Run code on some input") }, |
81 |
form { |
82 |
render_action( $action => [ 'input', 'code' ] ); |
83 |
form_submit( |
84 |
label => _('Run'), |
85 |
); |
86 |
}; |
87 |
|
88 |
#warn dump( $action->result->content ); |
89 |
|
90 |
if ( my $input = $action->result->content( 'input' ) ) { |
91 |
h2 { _("Input data") }, |
92 |
pre { $input } |
93 |
} |
94 |
|
95 |
if ( my $output = $action->result->content( 'output' ) ) { |
96 |
h2 { _("Output result") }, |
97 |
pre { $output } |
98 |
} |
99 |
}; |
100 |
|
101 |
1; |