moved handle for image buttons to UI::Window
[catagits/Reaction.git] / lib / Reaction / UI / Window.pm
index 8257c28..0289b39 100644 (file)
@@ -6,9 +6,9 @@ use Reaction::UI::FocusStack;
 use namespace::clean -except => [ qw(meta) ];
 
 
-has ctx => (isa => 'Catalyst', is => 'ro', required => 1);
+has ctx => (isa => 'Catalyst', is => 'ro', required => 1, weak_ref => 1);
 has view_name => (isa => 'Str', is => 'ro', lazy_fail => 1);
-has content_type => (isa => 'Str', is => 'ro', lazy_fail => 1);
+has content_type => (isa => 'Str', is => 'rw', lazy_fail => 1);
 has title => (isa => 'Str', is => 'rw', default => sub { 'Untitled window' });
 has view => (
   # XXX compile failure because the Catalyst::View constraint would be
@@ -27,6 +27,11 @@ sub _build_view {
 };
 sub flush {
   my ($self) = @_;
+  my $res = $self->ctx->res;
+  if ( $res->status =~ /^3/ || length($res->body) ) {
+      $res->content_type('text/plain') unless $res->content_type;
+      return;
+  }
   $self->flush_events;
   $self->flush_view;
 };
@@ -45,19 +50,30 @@ sub flush_events {
 
   foreach my $type (qw/query body/) {
     my $meth = "${type}_parameters";
-    my $param_hash = $ctx->req->$meth;
-    $self->focus_stack->apply_events($param_hash)
-      if keys %$param_hash;
+    my $req_param = $ctx->req->$meth;
+    my $param_hash = { 
+        map {
+            $_ =~ m/(^r.+\:\w+)\.(x|y)/ ? # for <input type="image"... buttons
+              ( $1 => $req_param->{$_} )
+              : ( $_ => $req_param->{$_} )
+        } keys %$req_param
+    }; # yeah, FocusStack deletes it
+    my @param_keys = keys %$param_hash;
+    if (@param_keys) {
+        for (@param_keys) {
+            utf8::decode($param_hash->{$_})
+                unless (utf8::is_utf8($param_hash->{$_}));
+        }
+        $self->focus_stack->apply_events($param_hash);
+    }
   }
 };
 sub flush_view {
   my ($self) = @_;
   my $res = $self->ctx->res;
-  if ( $res->status =~ /^3/ || length($res->body) ) {
-      $res->content_type('text/plain') unless $res->content_type;
-      return;
-  }
-  $res->body($self->view->render_window($self));
+  my $res_body = $self->view->render_window($self);
+  utf8::encode($res_body) if utf8::is_utf8($res_body);
+  $res->body($res_body);
   $res->content_type($self->content_type);
 };