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 |
pre { $code->source }, |
32 |
} |
33 |
} |
34 |
} |
35 |
}; |
36 |
|
37 |
private template 'code_editor' => sub { |
38 |
my $content; |
39 |
my $op = 'Create'; |
40 |
my $id; |
41 |
if ( $id = get 'id' ) { |
42 |
my $code = Perly::Model::Code->new(); |
43 |
$code->load( $id ) or die "can't load $id"; |
44 |
$content = $code->source; |
45 |
warn "loaded ", length( $content ), " bytes"; |
46 |
$op = 'Update'; |
47 |
} |
48 |
form { |
49 |
my $action = new_action( class => $op . 'Code', arguments => { id => $id, source => $content }, ); |
50 |
render_param( $action => 'source', cols => 80, rows => 25, language => 'perl', render_as => 'Jifty::Plugin::CodePress::Textarea' ); |
51 |
#render_action( $action => [ 'source' => { cols => 80, rows => 30 }, ] ); |
52 |
form_submit( |
53 |
label => _($op), |
54 |
onclick => [ |
55 |
{ beforeclick => "CodePress.beforeSubmit();" }, |
56 |
], |
57 |
); |
58 |
} |
59 |
}; |
60 |
|
61 |
template '/upload' => page { |
62 |
h1 { _("Upload new file") }, |
63 |
form { |
64 |
my $action = new_action( class => 'Upload' ); |
65 |
render_action( $action => [ 'content', 'file', 'format' ] ); |
66 |
form_submit( |
67 |
label => _('Upload'), |
68 |
); |
69 |
} |
70 |
}; |
71 |
|
72 |
template '/run' => page { |
73 |
my $action = new_action( class => 'Run' ); |
74 |
h1 { _("Run code on some input") }, |
75 |
form { |
76 |
render_action( $action => [ 'input', 'code' ] ); |
77 |
form_submit( |
78 |
label => _('Run'), |
79 |
); |
80 |
}; |
81 |
if ( $action->result->content ) { |
82 |
h1 { _("Output") }, |
83 |
pre { |
84 |
$action->result->content( 'output' ) |
85 |
} |
86 |
} |
87 |
}; |
88 |
|
89 |
1; |