tweaks for SearchableListViewContainer
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / SearchableListViewContainer.pm
CommitLineData
e653a487 1package Reaction::UI::ViewPort::SearchableListViewContainer;
2use Reaction::Class;
3
4#use aliased 'Reaction::InterfaceModel::Search::Spec', 'SearchSpec';
5use aliased 'Reaction::InterfaceModel::Action::Search::UpdateSpec', 'UpdateSearchSpec';
6use aliased 'Reaction::UI::ViewPort::ListViewWithSearch';
7use aliased 'Reaction::UI::ViewPort::Action' => 'ActionVP';
eb09f78b 8use aliased 'Reaction::UI::ViewPort';
e653a487 9use aliased 'Reaction::UI::ViewPort::Collection::Role::Pager', 'PagerRole';
10
11use Method::Signatures::Simple;
12
13use namespace::clean -except => 'meta';
14
15extends 'Reaction::UI::ViewPort';
16
17has 'listview' => (
eb09f78b 18 isa => ViewPort,
e653a487 19 is => 'ro',
20 required => 1,
21);
22
eb09f78b 23has 'search_form' => (isa => ViewPort, is => 'ro', required => 1);
e653a487 24
25override BUILDARGS => sub {
26 my $args = super;
27 my $spec_event_id = $args->{location}.':search-spec';
28 my $spec_class = $args->{spec_class}
29 or confess "Argument spec_class is required";
eb09f78b 30 my $listview_class = $args->{'listview_class'} || ListViewWithSearch;
31 my $search_form_class = $args->{'search_form_class'} || ActionVP;
e653a487 32 my $action_class = $args->{action_class}
33 or confess "Argument action_class is required";
34# TODO: how do we autodiscover spec classes?
35# $spec_class =~ s/^::/${\SearchSpec}::/;
36 Class::MOP::load_class($spec_class);
37 my $spec = do {
38 if (my $string = $args->{ctx}->req->query_params->{$spec_event_id}) {
39 $spec_class->from_string($string, $args->{spec}||{});
40 } else {
41 $spec_class->new($args->{spec}||{});
42 }
43 };
44 my $listview_location = $args->{location}.'-listview';
eb09f78b 45 my $listview = $args->{listview} = $listview_class->new(
e653a487 46 %$args,
eb09f78b 47 layout => $args->{'listview_layout'} || 'list_view',
e653a487 48 search_spec => $spec,
49 location => $listview_location,
50 );
eb09f78b 51 $args->{search_form} = $search_form_class->new(
e653a487 52 model => $action_class->new(target_model => $spec),
53 location => $args->{location}.'-search_form',
54 apply_label => 'search',
55 ctx => $args->{ctx},
56 on_apply_callback => sub {
57 my ($vp, $spec) = @_;
58 my $req = $vp->ctx->req;
59 my $new_uri = $req->uri->clone;
60 my %query = %{$req->query_parameters};
61 delete @query{grep /^\Q${listview_location}\E/, keys %query};
62 $query{$spec_event_id} = $spec->to_string;
63 $new_uri->query_form(\%query);
64 $req->uri($new_uri);
65 $listview->clear_page;
66 $listview->clear_order_by;
67 },
68 %{$args->{search}||{}}
69 );
70 $args;
71};
72
73override child_event_sinks => method () {
74 ((map $self->$_, 'listview', 'search_form'), super);
75};
76
771;
78