annotate Lib/IMPL/Web/View/TTFactory.pm @ 298:78f767765706

TT view refactoring
author cin
date Tue, 19 Mar 2013 02:02:37 +0400
parents 86ff93b34f2a
children bf3af33b9003
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
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
6 use IMPL::lang qw(:hash);
181
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
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
11 use IMPL::Const qw(:prop);
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
12 use IMPL::declare {
298
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
13 require => {
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
14 Loader => 'IMPL::Code::Loader'
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
15 },
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
16 base => [
298
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
17 'IMPL::Object::Factory' => sub {
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
18 shift->class || 'IMPL::Web::View::TTControl'
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
19 }
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
20 ],
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
21 props => [
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
22 template => PROP_RW,
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
23 context => PROP_RW,
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
24 instances => PROP_RW,
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
25 base => PROP_RW,
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
26 require => PROP_RO
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
27 ]
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
28 };
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
29
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
30 sub CTOR {
298
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
31 my ($this,$template,$context,$base,$require) = @_;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
32
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
33 die IMPL::ArgumentException("A template is required") unless $template;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
34
298
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
35 Loader->safe->Require($this->factory)
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
36 if $this->factory and not ref $this->factory;
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
37
287
2d253e6e4a88 *TTView refactoring
cin
parents: 280
diff changeset
38 $context ||= new Template::Context();
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
39
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
40 $this->template($template);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
41 $this->context($context);
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
42 $this->base($base);
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
43 $this->instances(0);
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
44 $this->require($require);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
45 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
46
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
47 sub MergeParameters {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
48 my ($this,$name,$refProps) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
49
291
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
50 if (ref $name) {
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
51 $refProps = $name;
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
52 $name = (ref $refProps eq 'HASH' and ($refProps->{name} || $refProps->{id})) || '*anonymous*';
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
53 }
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
54
298
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
55 $refProps->{factory} = $this;
78f767765706 TT view refactoring
cin
parents: 296
diff changeset
56
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
57 my $base = $this->base;
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
58
287
2d253e6e4a88 *TTView refactoring
cin
parents: 280
diff changeset
59 $this->context->localise();
2d253e6e4a88 *TTView refactoring
cin
parents: 280
diff changeset
60
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
61 my $ctx = _clone_context($this->context);
287
2d253e6e4a88 *TTView refactoring
cin
parents: 280
diff changeset
62
2d253e6e4a88 *TTView refactoring
cin
parents: 280
diff changeset
63 $this->context->delocalise();
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
64
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
65 my $stash = $ctx->stash;
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
66 my $require = $this->require;
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
67
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
68
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
69 $stash->update({
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
70 require => sub {
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
71 my ($module) = @_;
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
72
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
73 $module =~ s/^\.\//$base\//;
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
74 return $require->($module);
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
75 }
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
76 });
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
77
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
78 return ($name, $this->template, $ctx, $refProps);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
79 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
80
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
81 sub CreateObject {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
82 my $this = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
83
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
84 my $count = $this->instances;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
85
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
86 unless($count) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
87 # нужно выполнить именно блок INIT шаблона при создании первого экземпляра
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
88 if (my $init = $this->template->blocks->{INIT}) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
89 $this->context->process($init);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
90 }
296
cin
parents: 291
diff changeset
91
cin
parents: 291
diff changeset
92 $this->context->visit($this->template->blocks);
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
93 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
94
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
95 my $instance = $this->SUPER::CreateObject(@_);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
96
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
97 $instance->InitInstance();
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
98
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
99 $count++;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
100 $this->instances($count);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
101
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
102 return $instance;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
104
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
105 sub _clone_context {
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
106 my $args = { %{shift || {}} };
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
107 delete $args->{CONFIG};
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
108
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
109 return Template::Context->new($args);
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
110 }
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
111
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112 sub save {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
113 die new IMPL::NotImplementedException("This class doesn't support serialization");
181
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 sub restore {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
117 die new IMPL::NotImplementedException("This class doesn't support serialization");
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
119
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
120 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
121
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
122 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
123
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
124 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
125
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
126 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
127
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
128 C<IMPL::Web::View::TTFactory> - фабрика элементов управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
129
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
130 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
131
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
132 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
133
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
134 my $factory = new IMPL::Web::View::TTFactory(
280
c6d0f889ef87 +IMPL::declare now supports meta attributes
cin
parents: 267
diff changeset
135 'IMPL::Web::View::TTControl',
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
136 $doc,
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
137 $context,
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
138 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
139 TRIM => 1
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
140 },
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
141 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
142 myprop => 'my value'
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
143 },
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146 my $input1 = $factory->new('login', { class => "required" } );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148 my $propval = $input->nodeProperty('myprop'); # 'my value'
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
152 =begin text
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154 [%
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
155 this.appendChild(
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
156 my.org.input.new('login', class = this.errors('login') ? "invalid" : "" )
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
157 );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
158 %]
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160 =end text
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
162 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
163
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
164 C< Inherits L<IMPL::Object::Factory> >
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
165
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
166 =head1 MEMBERS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
167
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
168 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
169
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
170 =item C<[get,set]template>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
171
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172 Документ C<Template::Document> который описывает элемент управления. См. C<IMPL::Web::View::TTControl>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
173
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174 =item C<[get,set]context>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
175
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
176 Контекст фабрики элементов управления, в этом контексте выполняет шаблон элемента управления при загрузке.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
177 Далее в этом контексте будет выполнен блок инициализации при создании первого элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
178
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
179 =item C<[get,set]opts>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
180
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
181 Параметры контекста элемента управления (ссылка на хеш). Каждый элемент управления при создании получает свой контекст,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
182 который создает с данными параметрами и хранилищем переменных, дочерним к контексту фабрики.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
183
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
184 =item C<[get,set]nodeProperties>
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
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
188 =item C<[get]instances>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
189
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
190 Количество созданных элементов управления данной фабрикой
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
191
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
192 =item C<[override]MergeParameters($name,$nodeProps)>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
193
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
194 Превращает значения переданные методу C<new> фабрики в параметры для создания элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
195
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
196 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
197
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
198 =item C<$name>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
199
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
200 Имя создаваемого узла (C<nodeName>).
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
201
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
202 =item C<$nodeProps>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
203
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
204 Ссылка на шех со значениями свойств узла. Данные значения будут совмещены со значениями из свойства C<nodeProperties>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
205
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
206 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
207
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
208 =item C<[override]CreateObject(@params)>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
209
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
210 Создает экземпляр элемента управления стандартным образом. Учитывает количество экземпляров и если это первый,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
211 то производит дополнительную инициализацию контекста выполнив блок шаблона C<INIT>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
212
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
213 =item C<[inherited]new($name,$nodeProps)>
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
214
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
215 Создает элемент управления с указанным именем и набором свойств.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
216
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
217 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
218
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
219 =cut