r23080@martha (orig r920): groditi | 2008-10-11 17:15:47 -0400
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Object.pm
index 7fb01d5..ed68f20 100644 (file)
@@ -39,10 +39,53 @@ sub BUILD {
   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;
@@ -59,7 +102,6 @@ sub _build_fields {
   return \@fields;
 }
 
-
 sub _build_computed_field_order {
   my ($self) = @_;
   my %excluded = map { $_ => undef } @{ $self->excluded_fields };
@@ -128,11 +170,11 @@ sub _build_simple_field {
 
   my $field_name = $args{attribute}->name;
   return $class->new(
-                     ctx       => $self->ctx,
-                     model     => $self->model,
-                     location  => join('-', $self->location, 'field', $field_name),
-                     %args
-                    );
+    ctx => $self->ctx,
+    model => $self->model,
+    location => join('-', $self->location, 'field', $field_name),
+    %args
+  );
 }
 
 sub _build_fields_for_type_Num {