]> iEval git - html-element-library.git/blobdiff - lib/HTML/Element/Library.pm
implement defmap
[html-element-library.git] / lib / HTML / Element / Library.pm
index 99abc3d39f7c534ce6eff10ac7cbba1cbedcbbeb..7573cbd26fda9a57265570e6c1a0ab503df7f06f 100644 (file)
@@ -12,6 +12,7 @@ use Array::Group qw(:all);
 use Carp qw(confess);
 use Data::Dumper;
 use HTML::Element;
+use List::Util qw(first);
 use List::MoreUtils qw/:all/;
 use Params::Validate qw(:all);
 use Scalar::Listify;
@@ -36,13 +37,73 @@ sub HTML::Element::siblings {
   $p->content_list;
 }
 
+sub HTML::Element::defmap {
+    my($tree,$attr,$hashref)=@_;
+
+    while (my ($k, $v) = (each %$hashref)) {
+       my $found = $tree->look_down($attr => $k);
+       if ($found) {
+           $found->replace_content( $v );
+       }
+    }
+
+}
+
+
+sub HTML::Element::hash_map {
+    my $container = shift;
+
+    my %p = validate(@_, {
+                         hash => { type => HASHREF },
+                         to_attr => 1,
+                         excluding => { type => ARRAYREF , default => [] },
+                         debug => { default => 0 },
+                        });
+
+    warn 'The container tag is ', $container->tag if $p{debug} ;
+    warn 'hash' . Dumper($p{hash}) if $p{debug} ;
+    warn 'at_under' . Dumper(\@_) if $p{debug} ;
+
+    my @same_as = $container->look_down( $p{to_attr} => qr/.+/ ) ;
+
+    warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug} ;
+
+
+    for my $same_as (@same_as) {
+       my $attr_val = $same_as->attr($p{to_attr}) ;
+       if (first { $attr_val eq $_ } @{$p{excluding}}) {
+           warn "excluding $attr_val" if $p{debug} ;
+           next;
+       }
+       warn "processing $attr_val" if $p{debug} ;
+       $same_as->replace_content( $p{hash}->{$attr_val} ) ;
+    }
+
+}
+
+sub HTML::Element::hashmap {
+    my ($container, $attr_name, $hashref, $excluding, $debug) = @_;
+
+    $excluding ||= [] ;
+
+    $container->hash_map(hash => $hashref, 
+                          to_attr => $attr_name,
+                          excluding => $excluding,
+                          debug => $debug);
+
+}
+
+
 sub HTML::Element::passover {
   my ($tree, $child_id) = @_;
   
-  warn "ARGS:   my ($tree, $child_id)";
+  warn "ARGS:   my ($tree, $child_id)" if $DEBUG;
+  warn $tree->as_HTML(undef, ' ') if $DEBUG;
 
   my $exodus = $tree->look_down(id => $child_id);
 
+  warn "E: $exodus" if $DEBUG;
+
   my @s = HTML::Element::siblings($exodus);
 
   for my $s (@s) {
@@ -188,6 +249,7 @@ sub HTML::Element::iter2 {
   warn "wrapper_data: " . Dumper $p{wrapper_data} if $p{debug} ;
 
   my $container = ref_or_ld($tree, $p{wrapper_ld});
+  warn "container: " . $container if $p{debug} ;
   warn "wrapper_(preproc): " . $container->as_HTML if $p{debug} ;
   $p{wrapper_proc}->($container) if defined $p{wrapper_proc} ;
   warn "wrapper_(postproc): " . $container->as_HTML if $p{debug} ;
@@ -619,24 +681,33 @@ sub HTML::Element::unroll_select {
 
   my $select = {};
 
+  warn "Select Hash: " . Dumper(\%select) if $select{debug};
+
   my $select_node = $s->look_down(id => $select{select_label});
+  warn "Select Node: " . $select_node if $select{debug};
 
-  my $option = $select_node->look_down('_tag' => 'option');
+  unless ($select{append}) {
+      for my $option ($select_node->look_down('_tag' => 'option')) {
+         $option->delete;
+      }
+  }
 
-#  warn $option;
 
+  my $option = HTML::Element->new('option');
+  warn "Option Node: " . $option if $select{debug};
 
   $option->detach;
 
   while (my $row = $select{data_iter}->($select{data}))
     {
-#      warn Dumper($row);
-      my $o = $option->clone;
-      $o->attr('value', $select{option_value}->($row));
-      $o->attr('SELECTED', 1) if ($select{option_selected}->($row)) ;
-
-      $o->replace_content($select{option_content}->($row));
-      $select_node->push_content($o);
+       warn "Data Row:" . Dumper($row) if $select{debug};
+       my $o = $option->clone;
+       $o->attr('value', $select{option_value}->($row));
+       $o->attr('SELECTED', 1) if (exists $select{option_selected} and $select{option_selected}->($row)) ;
+
+       $o->replace_content($select{option_content}->($row));
+       $select_node->push_content($o);
+       warn $o->as_HTML if $select{debug};
     }
 
 
@@ -725,6 +796,37 @@ One of these days, I'll around to writing a nice C<EXPORT> section.
 
 =head2 Tree Rewriting Methods
 
+=head3 $elem->hashmap($attr_name, \%hashref, \@excluded, $debug)
+
+This method is designed to take a hashref and populate a series of elements. For example:
+
+
+  <table>
+    <tr sclass="tr" class="alt" align="left" valign="top">
+      <td smap="people_id">1</td>
+      <td smap="phone">(877) 255-3239</td>
+      <td smap="password">*********</td>
+    </tr>
+  </table>
+
+In the table above, there are several attributes named C<< smap >>. If we have a hashref whose keys are the same:
+
+  my %data = (people_id => 888, phone => '444-4444', password => 'dont-you-dare-render');
+
+Then a single API call allows us to populate the HTML while excluding those ones we dont:
+
+  $tree->hashmap('sid' => \%data, ['password']);
+
+
+Note: the other way to prevent rendering some of the hash mapping is to not give that element the attr
+you plan to use for hash mapping.
+
+Also note: the function C<< hashmap >> has a simple easy-to-type API. Interally, it calls C<< hash_map >>
+(which has a more verbose keyword calling API). Thus, the above call to C<hashmap()> results in this call:
+
+  $tree->hash_map(hash => \%data, to_attr => 'sid', excluding => ['password']);
+
+
 =head3 $elem->replace_content(@new_elem)
 
 Replaces all of C<$elem>'s content with C<@new_elem>. 
@@ -962,7 +1064,11 @@ to give you a taste of how C<mute_attr> is used:
 
 
 
-=head2 Tree-Building Methods: Unrolling an array via a single sample element (<ul> container)
+=head2 Tree-Building Methods
+
+
+
+=head3 Unrolling an array via a single sample element (<ul> container)
 
 This is best described by example. Given this HTML:
 
@@ -993,7 +1099,7 @@ To produce this:
   </body>
  </html>
 
-=head2 Tree-Building Methods: Unrolling an array via n sample elements (<dl> container)
+=head3 Unrolling an array via n sample elements (<dl> container)
 
 C<iter()> was fine for awhile, but some things
 (e.g. definition lists) need a more general function to make them easy to
@@ -1150,7 +1256,9 @@ So now that we have documented the API, let's see the call we need:
  );
 
 
-=head2 Tree-Building Methods: Select Unrolling
+
+
+=head3 Select Unrolling
 
 The C<unroll_select> method has this API:
 
@@ -1161,6 +1269,8 @@ The C<unroll_select> method has this API:
       option_selected => $closure, # boolean to decide if SELECTED
       data         => $data        # the data to be put into the SELECT
       data_iter    => $closure     # the thing that will get a row of data
+      debug  => $boolean,
+      append => $boolean,   # remove the sample <OPTION> data or append?
     );
 
 Here's an example:
@@ -1171,8 +1281,10 @@ Here's an example:
    option_content   => sub { my $row = shift; $row->clan_name },
    option_selected  => sub { my $row = shift; $row->selected },
    data             => \@query_results, 
-   data_iter        => sub { my $data = shift; $data->next }
- )
+   data_iter        => sub { my $data = shift; $data->next },
+   append => 0,
+   debug => 0
+ );
 
 
 
@@ -1646,12 +1758,14 @@ down instead:
 
 L<HTML::Seamstress>
 
-=head1 AUTHOR
+=head1 AUTHOR / SOURCE
 
 Terrence Brannon, E<lt>tbone@cpan.orgE<gt>
 
 Many thanks to BARBIE for his RT bug report.
 
+The source is at L<http://github.com/metaperl/html-element-library/tree/master>
+
 =head1 COPYRIGHT AND LICENSE
 
 Copyright (C) 2004 by Terrence Brannon
This page took 0.029315 seconds and 4 git commands to generate.