Mercurial > pub > Impl
comparison Lib/IMPL/Web/View/TTDocument.pm @ 236:2904da230022
DOM refactoring
| author | sergey |
|---|---|
| date | Mon, 15 Oct 2012 04:23:01 +0400 |
| parents | 2530d1bb9638 |
| children | b8c724f6de36 |
comparison
equal
deleted
inserted
replaced
| 235:a4d9126edcbb | 236:2904da230022 |
|---|---|
| 1 package IMPL::Web::View::TTDocument; | 1 package IMPL::Web::View::TTDocument; |
| 2 use strict; | 2 use strict; |
| 3 | 3 |
| 4 use IMPL::lang qw(:declare ); | |
| 5 use IMPL::DOM::Property qw(_dom); | |
| 6 use IMPL::Web::View::TTFactory(); | |
| 7 use IMPL::Web::View::TTControl(); | |
| 8 | |
| 9 use Scalar::Util qw(weaken); | 4 use Scalar::Util qw(weaken); |
| 10 | 5 use IMPL::Const qw(:prop); |
| 11 | 6 |
| 12 use parent qw( | 7 use IMPL::declare { |
| 13 IMPL::DOM::Document | 8 require => { |
| 14 IMPL::Web::View::TTControl | 9 TTFactory => 'IMPL::Web::View::TTFactory', |
| 15 ); | 10 TTControl => 'IMPL::Web::View::TTControl', |
| 11 Loader => 'IMPL::Code::Loader' | |
| 12 }, | |
| 13 base => [ | |
| 14 'IMPL::Web::View::TTControl' => sub { | |
| 15 my ($template,$contextOpts) = @_; | |
| 16 'document', | |
| 17 $_[0], # template | |
| 18 new Template::Context($_[1]) # context | |
| 19 }, | |
| 20 'IMPL::DOM::Document' => sub { | |
| 21 nodeName => 'document' | |
| 22 } | |
| 23 ], | |
| 24 props => [ | |
| 25 layout => PROP_RW, | |
| 26 opts => PROP_RO, | |
| 27 loader => PROP_RW, | |
| 28 controls => PROP_RO, | |
| 29 | |
| 30 # store the stash separately to make require() method to work correctly | |
| 31 # even when a stash of the context is modified during the processing | |
| 32 stash => PROP_RO | |
| 33 ] | |
| 34 }; | |
| 16 | 35 |
| 17 BEGIN { | 36 BEGIN { |
| 18 public _dom property layout => PROP_ALL; | 37 |
| 19 public property opts => PROP_GET | PROP_OWNERSET; | |
| 20 public property loader => PROP_ALL; | |
| 21 public property controls => PROP_GET | PROP_OWNERSET; | |
| 22 | |
| 23 # store the stash separately to make require() method to work correctly | |
| 24 # even when a stash of the context is modified during the processing | |
| 25 public property stash => PROP_GET | PROP_OWNERSET; | |
| 26 } | 38 } |
| 27 | 39 |
| 28 sub CTOR { | 40 sub CTOR { |
| 29 my ($this,$template,$refOpts,$loader,$vars) = @_; | 41 my ($this,$template,$refOpts,$loader,$vars) = @_; |
| 30 | 42 |
| 40 weaken($self); | 52 weaken($self); |
| 41 | 53 |
| 42 $this->templateVars('require', sub { | 54 $this->templateVars('require', sub { |
| 43 my $doc = $self; | 55 my $doc = $self; |
| 44 die new IMPL::Exception("A document is destroyed or invalid") unless $doc; | 56 die new IMPL::Exception("A document is destroyed or invalid") unless $doc; |
| 45 $doc->require(@_); | 57 $doc->RequireControl(@_); |
| 46 }); | 58 }); |
| 47 | 59 |
| 48 $this->templateVars('document', sub { $self } ); | 60 $this->templateVars('document', sub { $self } ); |
| 49 $this->InitInstance($vars); | 61 $this->InitInstance($vars); |
| 50 } | 62 } |
| 51 | |
| 52 our %CTOR = ( | |
| 53 'IMPL::Web::View::TTControl' => sub { | |
| 54 my ($template,$contextOpts) = @_; | |
| 55 'document', | |
| 56 $_[0], # template | |
| 57 new Template::Context($_[1]) # context | |
| 58 }, | |
| 59 'IMPL::DOM::Document' => sub { | |
| 60 nodeName => 'document' | |
| 61 } | |
| 62 ); | |
| 63 | 63 |
| 64 sub templateVars { | 64 sub templateVars { |
| 65 my $this = shift; | 65 my $this = shift; |
| 66 my $name = shift; | 66 my $name = shift; |
| 67 | 67 |
| 70 } else { | 70 } else { |
| 71 return $this->stash->get($name); | 71 return $this->stash->get($name); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 | 74 |
| 75 sub require { | 75 sub RequireControl { |
| 76 my ($this, $control, $nodeProps) = @_; | 76 my ($this, $control, $nodeProps) = @_; |
| 77 | 77 |
| 78 $nodeProps ||= {}; | 78 $nodeProps ||= {}; |
| 79 $nodeProps->{document} = $this; | 79 $nodeProps->{document} = $this; |
| 80 | 80 |
| 95 $template->class || typeof IMPL::Web::View::TTControl, | 95 $template->class || typeof IMPL::Web::View::TTControl, |
| 96 $template, | 96 $template, |
| 97 $ctx, | 97 $ctx, |
| 98 $opts | 98 $opts |
| 99 ); | 99 ); |
| 100 | |
| 101 if ($template->class) { | |
| 102 Loader->safe->Require($template->class); | |
| 103 } | |
| 100 | 104 |
| 101 my @parts = split(/\/+/,$control); | 105 my @parts = split(/\/+/,$control); |
| 102 | 106 |
| 103 $this->controls->{$control} = $factory; | 107 $this->controls->{$control} = $factory; |
| 104 | 108 |
| 135 sub RenderContent { | 139 sub RenderContent { |
| 136 my $this = shift; | 140 my $this = shift; |
| 137 return $this->SUPER::Render(@_); | 141 return $this->SUPER::Render(@_); |
| 138 } | 142 } |
| 139 | 143 |
| 140 | |
| 141 1; | 144 1; |
| 142 | 145 |
| 143 __END__ | 146 __END__ |
| 144 | 147 |
| 145 =pod | 148 =pod |
| 284 =item C<templateVars($name[,$newValue])> | 287 =item C<templateVars($name[,$newValue])> |
| 285 | 288 |
| 286 Получает или задает переменную для шаблона документа. Имя переменнной может быть составным, | 289 Получает или задает переменную для шаблона документа. Имя переменнной может быть составным, |
| 287 например C<'my.var.name'>, см. C<Template::Stash::set()>. | 290 например C<'my.var.name'>, см. C<Template::Stash::set()>. |
| 288 | 291 |
| 289 =item C<require($controlName)> | 292 =item C<RequireControl($controlName)> |
| 290 | 293 |
| 291 Загружает фабрику элемента управления, если она уже была загружена, возвращает на нее ссылку. | 294 Загружает фабрику элемента управления, если она уже была загружена, возвращает на нее ссылку. |
| 292 При загрузки фабрики для нее создается собственный контекст на основе параметров из свойства | 295 При загрузки фабрики для нее создается собственный контекст на основе параметров из свойства |
| 293 C<opts> и ее пространство имен наследуется от пространства имен документа из свойства C<stash>. | 296 C<opts> и ее пространство имен наследуется от пространства имен документа из свойства C<stash>. |
| 294 | 297 |
