comparison Lib/IMPL/Web/View/TTControl.pm @ 238:b8c724f6de36

DOM model refactoring TT view refactoring, controls are no longer derived from DOM nodes bugfixes
author sergey
date Tue, 16 Oct 2012 01:33:06 +0400
parents 2904da230022
children f48a1a9f4fa2
comparison
equal deleted inserted replaced
237:61db68166c37 238:b8c724f6de36
1 package IMPL::Web::View::TTControl; 1 package IMPL::Web::View::TTControl;
2 use strict; 2 use strict;
3
4 use Scalar::Util qw(weaken);
5 3
6 use IMPL::Const qw(:prop); 4 use IMPL::Const qw(:prop);
7 use IMPL::declare { 5 use IMPL::declare {
8 require => { 6 require => {
9 TTContext => 'Template::Context', 7 TTContext => 'Template::Context',
10 Exception => 'IMPL::Exception', 8 Exception => 'IMPL::Exception',
11 ArgumentException => '-IMPL::InvalidArgumentException' 9 ArgumentException => '-IMPL::InvalidArgumentException',
10 OperationException => '-IMPL::InvalidOperationException'
12 }, 11 },
13 base => [ 12 base => [
14 'IMPL::DOM::Node' => sub { 13 'IMPL::Object' => '@_'
15 nodeName => $_[0],
16 %{ $_[3] || {} }
17 }
18 ], 14 ],
19 props => [ 15 props => [
20 id => PROP_RO, 16 id => PROP_RO,
17 attributes => PROP_RW,
18 name => PROP_RO,
21 context => PROP_RO, 19 context => PROP_RO,
22 template => PROP_RO 20 template => PROP_RO
23 ] 21 ]
24 }; 22 };
25 23
29 sub _GetNextId { 27 sub _GetNextId {
30 return $nextId++; 28 return $nextId++;
31 } 29 }
32 } 30 }
33 31
32 our $AutoloadRegex = qr/^[a-z]/;
33
34 sub CTOR { 34 sub CTOR {
35 my ($this,$name,$template,$context,$refProps) = @_; 35 my ($this,$name,$template,$context,$refProps) = @_;
36 36
37 $name ||= "control"; 37 $name ||= "control";
38 38
39 $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");
40 $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");
41 41
42 $this->id($name . "-" . _GetNextId()) unless $this->id; 42 $this->id($name . "-" . _GetNextId()) unless $this->id;
43 43
44 #TODO: deprecated, cleanup 44 $this->name($name);
45 #weaken($this); # prevent cyclic references produced by the code below 45 $this->attributes({});
46 46
47 #$context->stash->set('append', sub { $this->appendChild(@_); undef; } ); 47 if (ref $refProps eq 'HASH') {
48 #$context->stash->set('select', sub { $this->selectNodes(@_); } ); 48 while (my($key,$value) = each %$refProps) {
49 $this->SetAttribute($key,$value);
50 }
51 }
49 } 52 }
50 53
51 sub InitInstance { 54 sub InitInstance {
52 my ($this,$args) = @_; 55 my ($this,$args) = @_;
53 56
54 $args ||= {}; 57 $args ||= {};
55 58
56 if ( my $ctor = $this->template->blocks->{CTOR} ) { 59 if ( my $ctor = $this->template->blocks->{CTOR} ) {
57 $this->context->include($ctor, { %$args, this => $this, template => $this->template } ); 60 $this->context->include($ctor, { %$args, this => $this, template => $this->template } );
61 }
62 }
63
64 sub GetAttribute {
65 my ($this,$name) = (shift,shift);
66
67 if (my $method = $this->can($name)) {
68 unshift @_,$this;
69 goto &$method;
70 } else {
71 return $this->attributes->{$name};
72 }
73 }
74
75 sub SetAttribute {
76 my $this = shift;
77 my $name = shift;
78
79 if (my $method = $this->can($name)) {
80 unshift @_, $this;
81 goto &$method;
82 } else {
83 return $this->attributes->{$name} = shift;
58 } 84 }
59 } 85 }
60 86
61 sub GetRenderBlock { 87 sub GetRenderBlock {
62 $_[0]->template->blocks->{RENDER} || $_[0]->template; 88 $_[0]->template->blocks->{RENDER} || $_[0]->template;
79 105
80 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0]; 106 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0];
81 107
82 return if $method eq 'DESTROY'; 108 return if $method eq 'DESTROY';
83 109
84 my $this = shift; 110 if ($method =~ /$AutoloadRegex/) {
85 111 my $this = shift;
86 $this->nodeProperty($method,@_); 112
113 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method);
114 } else {
115 die OperationException->new("The specified method '$method' doesn't exists");
116 }
87 } 117 }
88 118
89 1; 119 1;
90 120
91 __END__ 121 __END__