aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort
diff options
context:
space:
mode:
authorphaylon <phaylon@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7>2009-03-27 02:40:02 +0000
committerphaylon <phaylon@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7>2009-03-27 02:40:02 +0000
commite653a48785a1942da650254c8fba30706ca18333 (patch)
treee0478436bd4a61b1d34af9cf1e770e7e02dba99f /lib/Reaction/UI/ViewPort
parent0feb9d76b60db643a717dded5c19efb8f049f642 (diff)
downloadreaction-e653a48785a1942da650254c8fba30706ca18333.tar.gz
reaction-e653a48785a1942da650254c8fba30706ca18333.zip
search spec components factored out of T365
Diffstat (limited to 'lib/Reaction/UI/ViewPort')
-rw-r--r--lib/Reaction/UI/ViewPort/Collection/Role/UseSearchSpec.pm35
-rw-r--r--lib/Reaction/UI/ViewPort/ListViewWithSearch.pm14
-rw-r--r--lib/Reaction/UI/ViewPort/SearchableListViewContainer.pm77
3 files changed, 126 insertions, 0 deletions
diff --git a/lib/Reaction/UI/ViewPort/Collection/Role/UseSearchSpec.pm b/lib/Reaction/UI/ViewPort/Collection/Role/UseSearchSpec.pm
new file mode 100644
index 0000000..5949056
--- /dev/null
+++ b/lib/Reaction/UI/ViewPort/Collection/Role/UseSearchSpec.pm
@@ -0,0 +1,35 @@
+package Reaction::UI::ViewPort::Collection::Role::UseSearchSpec;
+
+use Reaction::Role;
+use aliased 'Reaction::InterfaceModel::Search::Spec' => 'SearchSpecRole';
+use Scalar::Util qw(weaken);
+use Method::Signatures::Simple;
+use signatures;
+use namespace::clean -except => 'meta';
+
+has 'search_spec' => (isa => SearchSpecRole, is => 'ro', required => 1);
+
+has '_search_spec_cb' => (is => 'ro', lazy_build => 1);
+
+method _build__search_spec_cb () {
+ my $object = $self;
+ weaken($object);
+ my $cb = sub { $object->clear_current_collection };
+}
+
+method _filter_collection_using_search_spec($coll) {
+ $self->search_spec->filter_collection($coll);
+}
+
+method _register_self_with_search_spec () {
+ my $cb = $self->_search_spec_cb;
+ $self->search_spec->register_dependent($self, $cb);
+}
+
+around _build_current_collection => sub ($orig, $self, @rest) {
+ my $coll = $self->$orig(@rest);
+ return $self->_filter_collection_using_search_spec($coll);
+};
+
+1;
+
diff --git a/lib/Reaction/UI/ViewPort/ListViewWithSearch.pm b/lib/Reaction/UI/ViewPort/ListViewWithSearch.pm
new file mode 100644
index 0000000..ee5f738
--- /dev/null
+++ b/lib/Reaction/UI/ViewPort/ListViewWithSearch.pm
@@ -0,0 +1,14 @@
+package Reaction::UI::ViewPort::ListViewWithSearch;
+
+use Reaction::Class;
+use namespace::clean -except => [ qw(meta) ];
+extends 'Reaction::UI::ViewPort::Collection::Grid';
+
+with 'Reaction::UI::ViewPort::Collection::Role::UseSearchSpec';
+with 'Reaction::UI::ViewPort::Collection::Role::Order';
+with 'Reaction::UI::ViewPort::Collection::Role::Pager';
+with 'Reaction::UI::ViewPort::Role::Actions';
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/lib/Reaction/UI/ViewPort/SearchableListViewContainer.pm b/lib/Reaction/UI/ViewPort/SearchableListViewContainer.pm
new file mode 100644
index 0000000..b9f919d
--- /dev/null
+++ b/lib/Reaction/UI/ViewPort/SearchableListViewContainer.pm
@@ -0,0 +1,77 @@
+package Reaction::UI::ViewPort::SearchableListViewContainer;
+use Reaction::Class;
+
+#use aliased 'Reaction::InterfaceModel::Search::Spec', 'SearchSpec';
+use aliased 'Reaction::InterfaceModel::Action::Search::UpdateSpec', 'UpdateSearchSpec';
+use aliased 'Reaction::UI::ViewPort::ListViewWithSearch';
+use aliased 'Reaction::UI::ViewPort::Action' => 'ActionVP';
+use aliased 'Reaction::UI::ViewPort::Collection::Role::Pager', 'PagerRole';
+
+use Method::Signatures::Simple;
+
+use namespace::clean -except => 'meta';
+
+extends 'Reaction::UI::ViewPort';
+
+has 'listview' => (
+ isa => ListViewWithSearch,
+ is => 'ro',
+ required => 1,
+);
+
+has 'search_form' => (isa => ActionVP, is => 'ro', required => 1);
+
+override BUILDARGS => sub {
+ my $args = super;
+ my $spec_event_id = $args->{location}.':search-spec';
+ my $spec_class = $args->{spec_class}
+ or confess "Argument spec_class is required";
+ my $action_class = $args->{action_class}
+ or confess "Argument action_class is required";
+# TODO: how do we autodiscover spec classes?
+# $spec_class =~ s/^::/${\SearchSpec}::/;
+ Class::MOP::load_class($spec_class);
+ my $spec = do {
+ if (my $string = $args->{ctx}->req->query_params->{$spec_event_id}) {
+ $spec_class->from_string($string, $args->{spec}||{});
+ } else {
+ $spec_class->new($args->{spec}||{});
+ }
+ };
+ my $listview_location = $args->{location}.'-listview';
+ # should this maybe use the listview class in $args->{listview}?
+ my $listview = $args->{listview} = ListViewWithSearch->new(
+ %$args,
+ layout => 'list_view',
+ search_spec => $spec,
+ location => $listview_location,
+ );
+ # same as with listview wrt. class name
+ $args->{search_form} = ActionVP->new(
+ model => $action_class->new(target_model => $spec),
+ location => $args->{location}.'-search_form',
+ apply_label => 'search',
+ ctx => $args->{ctx},
+ on_apply_callback => sub {
+ my ($vp, $spec) = @_;
+ my $req = $vp->ctx->req;
+ my $new_uri = $req->uri->clone;
+ my %query = %{$req->query_parameters};
+ delete @query{grep /^\Q${listview_location}\E/, keys %query};
+ $query{$spec_event_id} = $spec->to_string;
+ $new_uri->query_form(\%query);
+ $req->uri($new_uri);
+ $listview->clear_page;
+ $listview->clear_order_by;
+ },
+ %{$args->{search}||{}}
+ );
+ $args;
+};
+
+override child_event_sinks => method () {
+ ((map $self->$_, 'listview', 'search_form'), super);
+};
+
+1;
+