annotate Lib/IMPL/Web/View/TTFactory.pm @ 192:a9faf883cdce

IMPL::Web::View refactoring
author cin
date Fri, 06 Apr 2012 16:22:38 +0400
parents 78a18a2b6266
children 4d0e1962161c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
1 package IMPL::Web::View::TTFactory;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
2 use strict;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
3
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
4 use Template::Context();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
5
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
6 use IMPL::lang qw(:hash :declare :constants);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
7 use IMPL::Exception();
192
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
8 use Scalar::Util qw(weaken);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
9
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
10
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
11 use parent qw(IMPL::Object::Factory);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
12
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
13 BEGIN {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
14 public property template => PROP_ALL;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
15 public property context => PROP_ALL;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
16 public property opts => PROP_ALL;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
17 public property nodeProperties => PROP_ALL;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
18 public property instances => PROP_GET | PROP_OWNERSET;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
19 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
20
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
21 __PACKAGE__->PassThroughArgs;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
22
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
23 sub CTOR {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
24 my ($this,$factory,$template,$context,$options,$nodeProps) = @_;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
25
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
26 die IMPL::ArgumentException("A template is required") unless $template;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
27
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
28 $options ||= {};
192
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
29 $context ||= new Template::Context($options);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
30
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
31 $this->template($template);
192
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
32 $this->context($context);
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
33 $this->opts($options);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
34 $this->nodeProperties($nodeProps || {});
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
35 $this->instances(0);
192
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
36
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
37 my $doc = delete $nodeProps->{document};
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
38 weaken($doc);
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
39
a9faf883cdce IMPL::Web::View refactoring
cin
parents: 191
diff changeset
40 $context->stash->set('require', sub { $doc->require(@_); } );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
41 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
42
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
43 our %CTOR = (
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
44 'IMPL::Object::Factory' => sub {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
45 $_[0]
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
46 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
47 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
48
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
49 sub MergeParameters {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
50 my ($this,$name,$refProps) = @_;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
51
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 181
diff changeset
52 my $opts = { %{ $this->opts } };
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
53 $opts->{STASH} = $opts->{STASH}->clone() if $opts->{STASH};
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
54
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
55 my $ctx = new Template::Context($opts);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
56
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
57 return ($name, $this->template, $ctx, hashMerge($this->nodeProperties,$refProps));
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
58 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
59
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
60 sub CreateObject {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
61 my $this = shift;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
62
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
63 my $count = $this->instances;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
64
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
65 unless($count) {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
66 # нужно выполнить именно блок INIT шаблона при создании первого экземпляра
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
67 if (my $init = $this->template->blocks->{INIT}) {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
68 $this->context->process($init);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
69 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
70 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
71
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
72 my $instance = $this->SUPER::CreateObject(@_);
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
73
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
74 $instance->InitInstance();
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
75
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
76 $count++;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
77 $this->instances($count);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
78
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
79 return $instance;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
80 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
81
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
82 sub save {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
83 die new IMPL::NotImplementedException("This class doesn't support serialization");
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
84 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
85
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
86 sub restore {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
87 die new IMPL::NotImplementedException("This class doesn't support serialization");
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
88 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
89
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
90 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
91
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
92 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
93
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
94 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
95
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
96 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
97
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
98 C<IMPL::Web::View::TTFactory> - фабрика элементов управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
99
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
100 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
101
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
104 my $factory = new IMPL::Web::View::TTFactory(
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
105 typeof IMPL::Web::View::TTControl,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
106 $doc,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
107 $context,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
108 {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
109 TRIM => 1
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
110 },
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111 {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112 myprop => 'my value'
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113 },
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
114 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
115
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116 my $input1 = $factory->new('login', { class => "required" } );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118 my $propval = $input->nodeProperty('myprop'); # 'my value'
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
119
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
120 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
121
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
122 =begin text
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
123
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
124 [%
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
125 this.appendChild(
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
126 my.org.input.new('login', class = this.errors('login') ? "invalid" : "" )
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
127 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
128 %]
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
129
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
130 =end text
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
131
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
132 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
133
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
134 C< Inherits L<IMPL::Object::Factory> >
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
135
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
136 =head1 MEMBERS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
137
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
138 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140 =item C<[get,set]template>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
141
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
142 Документ C<Template::Document> который описывает элемент управления. См. C<IMPL::Web::View::TTControl>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144 =item C<[get,set]context>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146 Контекст фабрики элементов управления, в этом контексте выполняет шаблон элемента управления при загрузке.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147 Далее в этом контексте будет выполнен блок инициализации при создании первого элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149 =item C<[get,set]opts>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151 Параметры контекста элемента управления (ссылка на хеш). Каждый элемент управления при создании получает свой контекст,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
152 который создает с данными параметрами и хранилищем переменных, дочерним к контексту фабрики.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154 =item C<[get,set]nodeProperties>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156 Ссылка на хеш со значениями свойств по умолчанию для создаваемого элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
158 =item C<[get]instances>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160 Количество созданных элементов управления данной фабрикой
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
162 =item C<[override]MergeParameters($name,$nodeProps)>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
163
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
164 Превращает значения переданные методу C<new> фабрики в параметры для создания элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
165
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
166 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
167
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
168 =item C<$name>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
169
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
170 Имя создаваемого узла (C<nodeName>).
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
171
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172 =item C<$nodeProps>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
173
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174 Ссылка на шех со значениями свойств узла. Данные значения будут совмещены со значениями из свойства C<nodeProperties>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
175
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
176 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
177
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
178 =item C<[override]CreateObject(@params)>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
179
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
180 Создает экземпляр элемента управления стандартным образом. Учитывает количество экземпляров и если это первый,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
181 то производит дополнительную инициализацию контекста выполнив блок шаблона C<INIT>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
182
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
183 =item C<[inherited]new($name,$nodeProps)>
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
184
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
185 Создает элемент управления с указанным именем и набором свойств.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
186
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
187 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
188
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
189 =cut