aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/Object.pm
diff options
context:
space:
mode:
authorgroditi <groditi@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7>2008-08-21 00:38:47 +0000
committergroditi <groditi@03d0b0b2-0e1a-0410-a411-fdb2f4bd65d7>2008-08-21 00:38:47 +0000
commit599c1172394e3377d8e3e28c06557a99a1a10d1e (patch)
tree78e277ddc4cd5af9c43f9b033d82afc071e3f1e8 /lib/Reaction/UI/ViewPort/Object.pm
parent3671c9c3a3eb9640371d09a80ff88ae774d0a0b7 (diff)
downloadreaction-599c1172394e3377d8e3e28c06557a99a1a10d1e.tar.gz
reaction-599c1172394e3377d8e3e28c06557a99a1a10d1e.zip
container support built into fields and an example of usage in ComponentUI
Diffstat (limited to 'lib/Reaction/UI/ViewPort/Object.pm')
-rw-r--r--lib/Reaction/UI/ViewPort/Object.pm99
1 files changed, 79 insertions, 20 deletions
diff --git a/lib/Reaction/UI/ViewPort/Object.pm b/lib/Reaction/UI/ViewPort/Object.pm
index 35fa9c9..e7a2e3a 100644
--- a/lib/Reaction/UI/ViewPort/Object.pm
+++ b/lib/Reaction/UI/ViewPort/Object.pm
@@ -12,6 +12,7 @@ use aliased 'Reaction::UI::ViewPort::Field::RelatedObject';
use aliased 'Reaction::UI::ViewPort::Field::Array';
use aliased 'Reaction::UI::ViewPort::Field::Collection';
use aliased 'Reaction::UI::ViewPort::Field::File';
+use aliased 'Reaction::UI::ViewPort::Field::Container';
use aliased 'Reaction::InterfaceModel::Object' => 'IM_Object';
@@ -29,15 +30,62 @@ has field_order => (is => 'ro', isa => 'ArrayRef');
has builder_cache => (is => 'ro', isa => 'HashRef', lazy_build => 1);
has excluded_fields => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
has computed_field_order => (is => 'ro', isa => 'ArrayRef', lazy_build => 1);
+
+has containers => ( is => 'ro', isa => 'ArrayRef', lazy_build => 1);
+has container_layouts => ( is => 'rw', isa => 'ArrayRef' );
+
sub BUILD {
my ($self, $args) = @_;
if( my $field_args = delete $args->{Field} ){
$self->field_args( $field_args );
}
-};
+}
+
+sub _build_builder_cache { {} }
+sub _build_excluded_fields { [] }
+
+sub _build_containers {
+ my $self = shift;
+
+ my @container_layouts;
+ if( $self->has_container_layouts ){
+ #make sure we don't accidentally modify the original
+ @container_layouts = map { {%$_} }@{ $self->container_layouts };
+ } #we should always have a '_' container;
+ unless (grep {$_->{name} eq '_'} @container_layouts ){
+ unshift(@container_layouts, {name => '_'});
+ }
+
+ my %fields;
+ my $ordered_field_names = $self->computed_field_order;
+ @fields{ @$ordered_field_names } = @{ $self->fields };
+
+ my %containers;
+ my @container_order;
+ for my $layout ( @container_layouts ){
+ my @container_fields;
+ my $name = $layout->{name};
+ push(@container_order, $name);
+ if( my $field_names = delete $layout->{fields} ){
+ map{ push(@container_fields, $_) } grep { defined }
+ map { delete $fields{$_} } @$field_names;
+ }
+ $containers{$name} = Container->new(
+ ctx => $self->ctx,
+ location => join( '-', $self->location, 'container', $name ),
+ fields => \@container_fields,
+ %$layout,
+ );
+ }
+ if( keys %fields ){
+ my @leftovers = grep { exists $fields{$_} } @$ordered_field_names;
+ push(@{ $containers{_}->fields }, @fields{@leftovers} );
+ }
+
+ #only return containers with at least one field
+ return [ grep { scalar(@{ $_->fields }) } @containers{@container_order} ];
+}
-sub _build_excluded_fields { [] };
-sub _build_builder_cache { {} };
sub _build_fields {
my ($self) = @_;
my $obj = $self->model;
@@ -48,10 +96,11 @@ sub _build_fields {
my $attr = $param_attrs{$field_name};
my $meth = $self->builder_cache->{$field_name} ||= $self->get_builder_for($attr);
my $field = $self->$meth($attr, ($args->{$field_name} || {}));
- push(@fields, $field) if $field;
+ next unless $field;
+ push(@fields, $field);
}
return \@fields;
-};
+}
sub _build_computed_field_order {
my ($self) = @_;
@@ -60,7 +109,7 @@ sub _build_computed_field_order {
my @names = grep { $_ !~ /^_/ && !exists($excluded{$_})} map { $_->name }
grep { defined $_->get_read_method } $self->model->parameter_attributes;
return $self->sort_by_spec($self->field_order || [], \@names);
-};
+}
override child_event_sinks => sub {
return ( @{shift->fields}, super());
@@ -109,7 +158,8 @@ sub get_builder_for {
} else {
confess "Can't build field ${attr} without $builder method or type constraint";
}
-};
+}
+
sub _build_simple_field {
my ($self, %args) = @_;
my $class = delete $args{class};
@@ -125,20 +175,23 @@ sub _build_simple_field {
location => join('-', $self->location, 'field', $field_name),
%args
);
-};
+}
+
sub _build_fields_for_type_Num {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => Number, %$args);
-};
+}
+
sub _build_fields_for_type_Int {
my ($self, $attr, $args) = @_;
#XXX
$self->_build_simple_field(attribute => $attr, class => Integer, %$args);
-};
+}
+
sub _build_fields_for_type_Bool {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
-};
+}
#XXX
sub _build_fields_for_type_Reaction_Types_Core_Password { return };
@@ -147,41 +200,47 @@ sub _build_fields_for_type_Str {
my ($self, $attr, $args) = @_;
#XXX
$self->_build_simple_field(attribute => $attr, class => String, %$args);
-};
+}
+
sub _build_fields_for_type_Reaction_Types_Core_SimpleStr {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => String, %$args);
-};
+}
+
sub _build_fields_for_type_Reaction_Types_DateTime_DateTime {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
-};
+}
+
sub _build_fields_for_type_Enum {
my ($self, $attr, $args) = @_;
#XXX
$self->_build_simple_field(attribute => $attr, class => String, %$args);
-};
+}
+
sub _build_fields_for_type_ArrayRef {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => Array, %$args);
-};
+}
+
sub _build_fields_for_type_Reaction_Types_File_File {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => File, %$args);
-};
+}
+
sub _build_fields_for_type_Reaction_InterfaceModel_Object {
my ($self, $attr, $args) = @_;
#XXX
$self->_build_simple_field(attribute => $attr, class => RelatedObject, %$args);
-};
+}
+
sub _build_fields_for_type_Reaction_InterfaceModel_Collection {
my ($self, $attr, $args) = @_;
$self->_build_simple_field(attribute => $attr, class => Collection, %$args);
-};
+}
__PACKAGE__->meta->make_immutable;
-
1;
__END__;