diff Lib/IMPL/Web/TT/Document.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents d1676be8afcc
children
line wrap: on
line diff
--- a/Lib/IMPL/Web/TT/Document.pm	Tue Apr 10 08:13:22 2012 +0400
+++ b/Lib/IMPL/Web/TT/Document.pm	Tue Apr 10 20:08:29 2012 +0400
@@ -30,29 +30,29 @@
 );
 
 sub CTOR {
-	my ($this,%args) = @_;
-	
-	$this->_controlClassMap({});
-	$this->registerControlClass( Control => 'IMPL::Web::TT::Control' );
-	$this->appendChild( $this->Create(body => 'IMPL::Web::TT::Collection') );
-	$this->appendChild( $this->Create(head => 'IMPL::Web::TT::Collection') );
-	$this->cache($args{cache}) if $args{cache};
-	$this->preprocess($args{preprocess}) if $args{preprocess};
+    my ($this,%args) = @_;
+    
+    $this->_controlClassMap({});
+    $this->registerControlClass( Control => 'IMPL::Web::TT::Control' );
+    $this->appendChild( $this->Create(body => 'IMPL::Web::TT::Collection') );
+    $this->appendChild( $this->Create(head => 'IMPL::Web::TT::Collection') );
+    $this->cache($args{cache}) if $args{cache};
+    $this->preprocess($args{preprocess}) if $args{preprocess};
 }
 
 sub CreateControl {
-	my ($this,$name,$class,$args) = @_;
-	
-	$args = {} unless ref $args eq 'HASH';
-	
-	if (my $info = $this->_controlClassMap->{$class}) {
-		my %nodeArgs = (%{$info->{args}},%$args);
-		$nodeArgs{controlClass} = $class;
-		
-		return $this->Create($name,$info->{type},\%nodeArgs);
-	} else {
-		die new IMPL::Exception('A control is\'t registered', $class, $name);
-	}
+    my ($this,$name,$class,$args) = @_;
+    
+    $args = {} unless ref $args eq 'HASH';
+    
+    if (my $info = $this->_controlClassMap->{$class}) {
+        my %nodeArgs = (%{$info->{args}},%$args);
+        $nodeArgs{controlClass} = $class;
+        
+        return $this->Create($name,$info->{type},\%nodeArgs);
+    } else {
+        die new IMPL::Exception('A control is\'t registered', $class, $name);
+    }
 }
 
 sub provider {
@@ -79,16 +79,16 @@
                     document => $this,
                     this => $this,
                     render => sub {
-                    	$this->_process(@_);
+                        $this->_process(@_);
                     },
                     encode => sub {
-                    	Encode::encode('utf8',shift);
+                        Encode::encode('utf8',shift);
                     },
                     dump => sub {
-                    	Dumper(shift);
+                        Dumper(shift);
                     },
                     as_list => sub {
-                    	[ map ref($_) eq 'ARRAY' ? @$_ : $_, @_ ]
+                        [ map ref($_) eq 'ARRAY' ? @$_ : $_, @_ ]
                     }
                 },
                 RECURSION => 1,
@@ -99,75 +99,75 @@
 }
 
 sub resolveVar {
-	my ($this,$var) = @_;
-	
-	return $this->context->stash->get($var);
+    my ($this,$var) = @_;
+    
+    return $this->context->stash->get($var);
 }
 
 sub registerControlClass {
-	my ($this, $controlClass, $type, $args) = @_;
-	
-	$type ||= 'IMPL::Web::TT::Control';
-	
-	die new IMPL::InvalidArgumentException("A controlClass must be a single word",$controlClass) unless $controlClass =~ /^\w+$/;
-	
-	eval "require $type; 1;" or die new IMPL::Exception("Failed to load a module",$type,"$@") unless eval { $type->can('new') };
-	
-	die new IMPL::InvalidArgumentException("A type must be subclass of IMPL::DOM::Node",$type) unless $type->isa('IMPL::DOM::Node');
-	
-	# resolve template name to a real template
-	$args->{template} = $this->context->template($args->{template}) if $args->{template};
-	
-	$this->_controlClassMap->{$controlClass} = {
-		controlClass => $controlClass,
-		type => $type,
-		args => ref $args eq 'HASH' ? $args : {}
-	};
+    my ($this, $controlClass, $type, $args) = @_;
+    
+    $type ||= 'IMPL::Web::TT::Control';
+    
+    die new IMPL::InvalidArgumentException("A controlClass must be a single word",$controlClass) unless $controlClass =~ /^\w+$/;
+    
+    eval "require $type; 1;" or die new IMPL::Exception("Failed to load a module",$type,"$@") unless eval { $type->can('new') };
+    
+    die new IMPL::InvalidArgumentException("A type must be subclass of IMPL::DOM::Node",$type) unless $type->isa('IMPL::DOM::Node');
+    
+    # resolve template name to a real template
+    $args->{template} = $this->context->template($args->{template}) if $args->{template};
+    
+    $this->_controlClassMap->{$controlClass} = {
+        controlClass => $controlClass,
+        type => $type,
+        args => ref $args eq 'HASH' ? $args : {}
+    };
 }
 
 sub require {
-	my ($this,$template) = @_;
-	
-	my $doc = $this->context->template($template);
-	
-	die new IMPL::InvalidOperationException("A specified template isn't a document",$template) unless eval{ $doc -> isa('Template::Document') };
-	
-	my $controlClass = $doc->class;
-	my $type = $doc->nativeType;
-	my $controlTemplate;
-	my $out = "";
-	
-	die new IMPL::InvalidOperationException("A specified template isn't a control",$template) unless $controlClass;
-	
-	if (not $this->isControlClass($controlClass)) {
-		if ($doc->template) {
-			$controlTemplate = $doc->blocks()->{$doc->template} || $this->context->template($doc->template);
-			$out = $this->context->include($doc);
-		} else {
-			$controlTemplate = $doc;
-		}
-		$this->registerControlClass($controlClass,$type,{ template => $controlTemplate } );
-	}
-	
-	return $out;
+    my ($this,$template) = @_;
+    
+    my $doc = $this->context->template($template);
+    
+    die new IMPL::InvalidOperationException("A specified template isn't a document",$template) unless eval{ $doc -> isa('Template::Document') };
+    
+    my $controlClass = $doc->class;
+    my $type = $doc->nativeType;
+    my $controlTemplate;
+    my $out = "";
+    
+    die new IMPL::InvalidOperationException("A specified template isn't a control",$template) unless $controlClass;
+    
+    if (not $this->isControlClass($controlClass)) {
+        if ($doc->template) {
+            $controlTemplate = $doc->blocks()->{$doc->template} || $this->context->template($doc->template);
+            $out = $this->context->include($doc);
+        } else {
+            $controlTemplate = $doc;
+        }
+        $this->registerControlClass($controlClass,$type,{ template => $controlTemplate } );
+    }
+    
+    return $out;
 }
 
 sub isControlClass {
-	my ($this,$name) = @_;
-	return $this->_controlClassMap->{$name} ? 1 : 0;
+    my ($this,$name) = @_;
+    return $this->_controlClassMap->{$name} ? 1 : 0;
 }
 
 sub _getControls {
-	my ($this) = @_;
-	
-	my ($node) = $this->selectNodes('controls');
-	return $node;
+    my ($this) = @_;
+    
+    my ($node) = $this->selectNodes('controls');
+    return $node;
 }
 
 sub _validatePresenter {
-	my ($this,$value) = @_;
-	
-	die new IMPL::InvalidArgumentException("A view object is required") unless blessed($value) and $value->isa('Template::View');
+    my ($this,$value) = @_;
+    
+    die new IMPL::InvalidArgumentException("A view object is required") unless blessed($value) and $value->isa('Template::View');
 }
 
 sub LoadFile {
@@ -183,9 +183,9 @@
     $this->_provider(undef);
     
     if (not ref $src) {
-    	my ($vol,$dir,$fileName) = File::Spec->splitpath($src);
-    	unshift @$includes, File::Spec->catpath($vol,$dir,'');
-    	$src = $fileName;
+        my ($vol,$dir,$fileName) = File::Spec->splitpath($src);
+        unshift @$includes, File::Spec->catpath($vol,$dir,'');
+        $src = $fileName;
     }
     
     $this->provider(
@@ -200,9 +200,9 @@
     );
     
     if ($vars) {
-    	while ( my ($var,$val) = each %$vars ) {
-    		$this->AddVar($var,$val);
-    	}
+        while ( my ($var,$val) = each %$vars ) {
+            $this->AddVar($var,$val);
+        }
     }
     
     $this->context->process($_) foreach $this->preprocess;
@@ -210,18 +210,18 @@
     my $template = $this->context->template($src);
     $this->title($template->title);
     if ( $template->template ) {
-    	$this->context->process($template);
-    	$this->template($template->template);
+        $this->context->process($template);
+        $this->template($template->template);
     } else {
-    	$this->template($template);
+        $this->template($template);
     }
     
 }
 
 sub AddVar {
-	my ($this,$name,$value) = @_;
-	
-	$this->context->stash->set($name,$value);
+    my ($this,$name,$value) = @_;
+    
+    $this->context->stash->set($name,$value);
 }
 
 sub Render {
@@ -232,42 +232,42 @@
 
 # Формирует представление для произвольных объектов 
 sub _process {
-	my ($this,@items) = @_;
-	
-	my @result;
-	
-	foreach my $item (@items) {
-		if (blessed($item) and $item->isa('IMPL::Web::TT::Control')) {
-			push @result, $item->Render();	
-		} elsif(blessed($item)) {
-			if ($this->presenter) {
-				push @result, $this->presenter->print($item);
-			} else {
-				push @result, $this->toString;
-			}
-		} else {
-			push @result, $item;
-		}
-	}
-	
-	return join '',@result;
+    my ($this,@items) = @_;
+    
+    my @result;
+    
+    foreach my $item (@items) {
+        if (blessed($item) and $item->isa('IMPL::Web::TT::Control')) {
+            push @result, $item->Render();    
+        } elsif(blessed($item)) {
+            if ($this->presenter) {
+                push @result, $this->presenter->print($item);
+            } else {
+                push @result, $this->toString;
+            }
+        } else {
+            push @result, $item;
+        }
+    }
+    
+    return join '',@result;
 }
 
 our $AUTOLOAD;
 sub AUTOLOAD {
-	my $this = shift;
-	my ($method) = ($AUTOLOAD =~ /(\w+)$/);
-	
-	if($method =~ /^create(\w+)/) {
-		my ($name,$args) = @_;
-		return $this->CreateControl($name,$1,$args);
-	}
-	
-	my @result = $this->selectNodes($method);
-	
-	return $result[0] if @result;
-	carp "Looks like you have a mistake, the document doesn't have a such property or child: $method";
-	return;
+    my $this = shift;
+    my ($method) = ($AUTOLOAD =~ /(\w+)$/);
+    
+    if($method =~ /^create(\w+)/) {
+        my ($name,$args) = @_;
+        return $this->CreateControl($name,$1,$args);
+    }
+    
+    my @result = $this->selectNodes($method);
+    
+    return $result[0] if @result;
+    carp "Looks like you have a mistake, the document doesn't have a such property or child: $method";
+    return;
 }
 
 sub Dispose {
@@ -360,19 +360,19 @@
 [% table = document.сreateTable('env') %]
 
 [% FOEACH item in document.result %]
-	[% table.rows.Add( item.get('name','value') ) %]
+    [% table.rows.Add( item.get('name','value') ) %]
 [% END %]
 
 [% form = document.createForm('login') %]
 [% form.template = 'LOGIN_FORM'%]
 
 [% FOREACH item IN document.childNodes %]
-	[%render(item)%]
+    [%render(item)%]
 [% END %]
-	
+    
 [% BLOCK LOGIN_FORM %]
 <form method="POST" action='/login.pl'>
-	user: [% render(this.item('name')) %] password: [% render(this.item('password')) %] <input type="submit"/>
+    user: [% render(this.item('name')) %] password: [% render(this.item('password')) %] <input type="submit"/>
 </form>
 [% END %]