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