comparison Lib/IMPL/Web/View/TTControl.pm @ 234:2530d1bb9638

sync
author sergey
date Thu, 11 Oct 2012 20:11:45 +0400
parents 5c82eec23bb6
children 2904da230022
comparison
equal deleted inserted replaced
233:3cebcf6fdb9b 234:2530d1bb9638
1 package IMPL::Web::View::TTControl; 1 package IMPL::Web::View::TTControl;
2 use strict; 2 use strict;
3 3
4 use IMPL::DOM::Property qw(_dom);
5 use IMPL::lang qw(:declare );
6
7 use Template::Context();
8 use Scalar::Util qw(weaken); 4 use Scalar::Util qw(weaken);
9 5
10 use parent qw( 6 use IMPL::Const qw(:prop);
11 IMPL::DOM::Node 7 use IMPL::declare {
12 ); 8 require => {
9 TTContext => 'Template::Context',
10 Exception => 'IMPL::Exception',
11 ArgumentException => '-IMPL::InvalidArgumentException'
12 },
13 base => [
14 'IMPL::DOM::Node' => sub {
15 nodeName => $_[0],
16 %{ $_[3] || {} }
17 }
18 ],
19 props => [
20 id => PROP_RO,
21 context => PROP_RO,
22 template => PROP_RO
23 ]
24 };
25
13 26
14 { 27 {
15 my $nextId = 1; 28 my $nextId = 1;
16 sub _GetNextId { 29 sub _GetNextId {
17 return $nextId++; 30 return $nextId++;
18 } 31 }
19 } 32 }
20
21
22 BEGIN {
23 public _dom property id => PROP_ALL;
24
25 public property context => PROP_GET | PROP_OWNERSET;
26 public property template => PROP_ALL;
27 }
28
29 33
30 sub CTOR { 34 sub CTOR {
31 my ($this,$name,$template,$context,$refProps) = @_; 35 my ($this,$name,$template,$context,$refProps) = @_;
32 36
33 $name ||= "control"; 37 $name ||= "control";
35 $this->template( $template ) or die new IMPL::ArgumentException("A template is required"); 39 $this->template( $template ) or die new IMPL::ArgumentException("A template is required");
36 $this->context( $context ) or die new IMPL::ArgumentException("A context is required"); 40 $this->context( $context ) or die new IMPL::ArgumentException("A context is required");
37 41
38 $this->id($name . "-" . _GetNextId()) unless $this->id; 42 $this->id($name . "-" . _GetNextId()) unless $this->id;
39 43
40 weaken($this); # prevent cyclic references produces by the code below 44 weaken($this); # prevent cyclic references produced by the code below
41 45
42 $context->stash->set('append', sub { $this->appendChild(@_); undef; } ); 46 $context->stash->set('append', sub { $this->appendChild(@_); undef; } );
43 $context->stash->set('select', sub { $this->selectNodes(@_); } ); 47 $context->stash->set('select', sub { $this->selectNodes(@_); } );
44
45 } 48 }
46
47 our %CTOR = (
48 'IMPL::DOM::Node' => sub {
49 nodeName => $_[0],
50 %{ $_[3] || {} }
51 }
52 );
53 49
54 sub InitInstance { 50 sub InitInstance {
55 my ($this,$args) = @_; 51 my ($this,$args) = @_;
56 52
57 $args ||= {}; 53 $args ||= {};
59 if ( my $ctor = $this->template->blocks->{CTOR} ) { 55 if ( my $ctor = $this->template->blocks->{CTOR} ) {
60 $this->context->include($ctor, { %$args, this => $this, template => $this->template } ); 56 $this->context->include($ctor, { %$args, this => $this, template => $this->template } );
61 } 57 }
62 } 58 }
63 59
64 sub renderBlock { 60 sub GetRenderBlock {
65 $_[0]->template->blocks->{RENDER} || $_[0]->template; 61 $_[0]->template->blocks->{RENDER} || $_[0]->template;
66 } 62 }
67 63
68 sub Render { 64 sub Render {
69 my ($this,$args) = @_; 65 my ($this,$args) = @_;
70 66
71 $args = {} unless ref $args eq 'HASH'; 67 $args = {} unless ref $args eq 'HASH';
72 68
73 if(my $body = $this->renderBlock ) { 69 if(my $body = $this->GetRenderBlock ) {
74 return $this->context->include( $body, { %$args, this => $this, template => $this->template, document => $this->document } ); 70 return $this->context->include( $body, { %$args, this => $this, template => $this->template, document => $this->document } );
75 } else { 71 } else {
76 return ""; 72 return "";
77 } 73 }
78 } 74 }