annotate Lib/IMPL/Web/View/TTFactory.pm @ 267:bbc0da7ef90e

*IMPL::Web::View refactoring
author cin
date Thu, 17 Jan 2013 02:39:44 +0400
parents 5c82eec23bb6
children c6d0f889ef87
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 {
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
13 base => [
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
14 'IMPL::Object::Factory' => '@_'
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
15 ],
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
16 props => [
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
17 template => PROP_RW,
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
18 context => PROP_RW,
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
19 opts => PROP_RW,
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
20 instances => PROP_RW,
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
21 base => PROP_RW
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
22 ]
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
23 };
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
24
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
25 sub CTOR {
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
26 my ($this,$factory,$template,$context,$options,$base) = @_;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
27
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
28 die IMPL::ArgumentException("A template is required") unless $template;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
29
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
30 $options ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
31 $context ||= new Template::Context($options);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
32
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
33 $this->template($template);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
34 $this->context($context);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
35 $this->opts($options);
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
36 $this->base($base);
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
37 $this->instances(0);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
38 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
39
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
40 our %CTOR = (
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
41 'IMPL::Object::Factory' => sub {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
42 $_[0]
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
43 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
44 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
45
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
46 sub MergeParameters {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
47 my ($this,$name,$refProps) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
48
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
49 my $opts = { %{ $this->opts } };
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
50 $opts->{STASH} = $opts->{STASH}->clone() if $opts->{STASH};
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
51
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
52 my $base = $this->base;
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
53
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
54 my $ctx = new Template::Context($opts);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
55
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
56 my $stash = $ctx->stash;
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
57 weaken($stash);
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
58
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
59 $stash->update({
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
60 require => sub {
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
61 my ($module) = @_;
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
62
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
63 $module =~ s/^\.\//$base\//;
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
64 return $stash->get('document')->RequireControl($module);
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
65 }
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
66 });
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
67
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 232
diff changeset
68 return ($name, $this->template, $ctx, hashApply({ factory => $this },$refProps));
181
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 sub CreateObject {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
72 my $this = shift;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
73
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
74 my $count = $this->instances;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
75
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
76 unless($count) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
77 # нужно выполнить именно блок INIT шаблона при создании первого экземпляра
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
78 if (my $init = $this->template->blocks->{INIT}) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
79 $this->context->process($init);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
80 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
81 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
82
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
83 my $instance = $this->SUPER::CreateObject(@_);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
84
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
85 $instance->InitInstance();
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
86
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
87 $count++;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
88 $this->instances($count);
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
89
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
90 return $instance;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
91 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
92
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
93 sub save {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
94 die new IMPL::NotImplementedException("This class doesn't support serialization");
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
95 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
96
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
97 sub restore {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
98 die new IMPL::NotImplementedException("This class doesn't support serialization");
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
99 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
100
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
101 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
104
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
105 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
106
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
107 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
108
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
109 C<IMPL::Web::View::TTFactory> - фабрика элементов управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
110
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
114
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
115 my $factory = new IMPL::Web::View::TTFactory(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
116 typeof IMPL::Web::View::TTControl,
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
117 $doc,
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
118 $context,
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
119 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
120 TRIM => 1
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
121 },
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
122 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
123 myprop => 'my value'
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
124 },
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
125 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
126
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
127 my $input1 = $factory->new('login', { class => "required" } );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
128
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
129 my $propval = $input->nodeProperty('myprop'); # 'my value'
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
130
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
131 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
132
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
133 =begin text
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
134
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
135 [%
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
136 this.appendChild(
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
137 my.org.input.new('login', class = this.errors('login') ? "invalid" : "" )
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
138 );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139 %]
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
141 =end text
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
142
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145 C< Inherits L<IMPL::Object::Factory> >
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147 =head1 MEMBERS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151 =item C<[get,set]template>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
152
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153 Документ C<Template::Document> который описывает элемент управления. См. C<IMPL::Web::View::TTControl>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155 =item C<[get,set]context>
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 Далее в этом контексте будет выполнен блок инициализации при создании первого элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160 =item C<[get,set]opts>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
162 Параметры контекста элемента управления (ссылка на хеш). Каждый элемент управления при создании получает свой контекст,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
163 который создает с данными параметрами и хранилищем переменных, дочерним к контексту фабрики.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
164
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
165 =item C<[get,set]nodeProperties>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
166
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
167 Ссылка на хеш со значениями свойств по умолчанию для создаваемого элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
168
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
169 =item C<[get]instances>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
170
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
171 Количество созданных элементов управления данной фабрикой
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
173 =item C<[override]MergeParameters($name,$nodeProps)>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
175 Превращает значения переданные методу C<new> фабрики в параметры для создания элемента управления.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
176
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
177 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
178
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
179 =item C<$name>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
180
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
181 Имя создаваемого узла (C<nodeName>).
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
182
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
183 =item C<$nodeProps>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
184
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
185 Ссылка на шех со значениями свойств узла. Данные значения будут совмещены со значениями из свойства C<nodeProperties>
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 =item C<[override]CreateObject(@params)>
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 то производит дополнительную инициализацию контекста выполнив блок шаблона C<INIT>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
193
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 186
diff changeset
194 =item C<[inherited]new($name,$nodeProps)>
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
195
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
196 Создает элемент управления с указанным именем и набором свойств.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
197
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
198 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
199
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
200 =cut