# See bottom of file for license and copyright information

package Foswiki::Configure::UIs::Root;

=begin TML

---+ package Foswiki::Configure::UIs::Root

Specialisation of Foswiki::Configure::UIs::Section for the root
of the model.

The root differs from other sections in that it carries the
Foswiki::Configure::Valuer used to give values to configuration items,
and is itself a visitor over the model. As each node in the model
is visited, the UI for that node is loaded.

=cut

use strict;
use warnings;

use Foswiki::Configure::UI ();

use Foswiki::Configure::UIs::Section ();
use Foswiki::Configure::Visitor ();    # pure virtual base class

our @ISA =
  ( 'Foswiki::Configure::UIs::Section', 'Foswiki::Configure::Visitor' );

sub new {
    my $class = shift;

    my $this = $class->SUPER::new(@_);
    $this->{tabs}    = [];
    $this->{message} = ''
      ; # Set to contain the results of the prior save, or other messages to display on next iteration

    return $this;
}

=begin TML

---++ ObjectMethod messages($message)

Set and retrieve the messages from the previous operation. Also passes through sanity errors.

If called with a message, sets and returns the message, otherwise just returns the message

=cut

sub messages {
    my ( $this, $message ) = @_;
    if ( defined $message && $message ) {
        $this->{message} = $message;
    }
    return $this->{message};
}

=begin TML

---++ ObjectMethod createUI( $tree, $valuer )

Visit the nodes in a tree of configuration items, and generate
their UIs.

=cut

sub createUI {
    my ( $this, $tree, $valuer ) = @_;
    $this->{valuer} = $valuer;
    $this->{output} = '';
    @{ $this->{stack} } = ();
    $tree->visit($this);
    return $this->{output};
}

# See Foswiki::Configure::Visitor
sub startVisit {
    my ( $this, $item ) = @_;

    # Stack the output from previously visited nodes
    # SMELL: we could probably print it

    my $class = ref($item);
    $class =~ s/.*:://;
    return 1
      if ( $class eq 'Value' )
      ;    # rendering of values - en groupe - is done in Section object

    push( @{ $this->{stack} }, $this->{output} );
    $this->{output} = '';
    return 1;
}

# See Foswiki::Configure::Visitor
# The last visit to a node in the model generates the UI for
# that node by wrapping the output already generated by
# visits to the subtree in containing HTML.
sub endVisit {
    my ( $this, $item ) = @_;

    my $class = ref($item);
    $class =~ s/.*:://;
    return 1
      if ( $class eq 'Value' )
      ;    # rendering of values - en groupe - is done in Section object

    my $ui = Foswiki::Configure::UI::loadUI( $class, $item );
    die "Fatal Error - Could not load UI for $class - $@" unless $ui;
    $this->{output} =
      pop( @{ $this->{stack} } )
      . $ui->renderHtml( $item, $this, $this->{output} );

    return 1;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/

Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.

Additional copyrights apply to some or all of the code in this
file as follows:

Copyright (C) 2000-2006 TWiki Contributors. All Rights Reserved.
TWiki Contributors are listed in the AUTHORS file in the root
of this distribution. NOTE: Please extend that file, not this notice.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

As per the GPL, removal of this notice is prohibited.
