annotate Lib/IMPL/Web/View/TTControl.pm @ 249:0057f48f7945

fixed simple auth package to work with utf8 strings
author sergey
date Tue, 06 Nov 2012 20:00:57 +0400
parents f48a1a9f4fa2
children 6b6d4b2275a1
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::TTControl;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
2 use strict;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
3
234
sergey
parents: 232
diff changeset
4 use IMPL::Const qw(:prop);
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
5 use IMPL::lang qw(:hash);
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
6 use Scalar::Util qw(blessed);
234
sergey
parents: 232
diff changeset
7 use IMPL::declare {
sergey
parents: 232
diff changeset
8 require => {
sergey
parents: 232
diff changeset
9 TTContext => 'Template::Context',
sergey
parents: 232
diff changeset
10 Exception => 'IMPL::Exception',
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
11 ArgumentException => '-IMPL::InvalidArgumentException',
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
12 OperationException => '-IMPL::InvalidOperationException'
234
sergey
parents: 232
diff changeset
13 },
sergey
parents: 232
diff changeset
14 base => [
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
15 'IMPL::Object' => undef
234
sergey
parents: 232
diff changeset
16 ],
sergey
parents: 232
diff changeset
17 props => [
sergey
parents: 232
diff changeset
18 id => PROP_RO,
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
19 attributes => PROP_RW,
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
20 name => PROP_RO,
234
sergey
parents: 232
diff changeset
21 context => PROP_RO,
sergey
parents: 232
diff changeset
22 template => PROP_RO
sergey
parents: 232
diff changeset
23 ]
sergey
parents: 232
diff changeset
24 };
sergey
parents: 232
diff changeset
25
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
26
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
27 {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
28 my $nextId = 1;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
29 sub _GetNextId {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
30 return $nextId++;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
31 }
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
32 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
33
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
34 our $AutoloadRegex = qr/^[a-z]/;
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
35 our @REFLECT_META = qw(title layout);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
36
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
37 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
38 my ($this,$name,$template,$context,$refProps) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
39
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
40 $name ||= "control";
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
41
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
42 $this->template( $template ) or die new IMPL::ArgumentException("A template is required");
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
43 $this->context( $context ) or die new IMPL::ArgumentException("A context is required");
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
44
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
45 $this->id($name . "-" . _GetNextId()) unless $this->id;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
46
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
47 $this->name($name);
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
48 $this->attributes({});
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
49
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
50 my %attrs;
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
51
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
52 foreach my $meta ( @REFLECT_META ) {
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
53 next if $meta =~ /^_/;
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
54 if( my $value = $template->$meta() ) {
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
55 $attrs{$meta} = $value;
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
56 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
57 }
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
58
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
59 hashApply(\%attrs,$refProps) if ref $refProps eq 'HASH';
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
60
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
61 while (my($key,$value) = each %attrs) {
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
62 $this->SetAttribute($key,$value);
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
63 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
64 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
65
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
66 sub InitInstance {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
67 my ($this,$args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
68
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
69 $args ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
70
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
71 if ( my $ctor = $this->template->blocks->{CTOR} ) {
212
292226770180 bugfixes
sergey
parents: 194
diff changeset
72 $this->context->include($ctor, { %$args, this => $this, template => $this->template } );
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
73 }
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
74 }
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
75
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
76 sub GetAttribute {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
77 my ($this,$name) = (shift,shift);
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
78
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
79 if (my $method = $this->can($name)) {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
80 unshift @_,$this;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
81 goto &$method;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
82 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
83 return $this->attributes->{$name};
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
84 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
85 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
86
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
87 sub SetAttribute {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
88 my $this = shift;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
89 my $name = shift;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
90
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
91 if (my $method = $this->can($name)) {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
92 unshift @_, $this;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
93 goto &$method;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
94 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
95 return $this->attributes->{$name} = shift;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
96 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
97 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
98
234
sergey
parents: 232
diff changeset
99 sub GetRenderBlock {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
100 $_[0]->template->blocks->{RENDER} || $_[0]->template;
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
101 }
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
102
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
103 sub Render {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
104 my ($this,$args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
105
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
106 $args = {} unless ref $args eq 'HASH';
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
107
234
sergey
parents: 232
diff changeset
108 if(my $body = $this->GetRenderBlock ) {
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
109 return $this->context->include( $body, { %$args, this => $this, template => $this->template } );
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
110 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
111 return "";
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
112 }
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
113 }
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
114
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
115 sub AUTOLOAD {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
116 our $AUTOLOAD;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
117
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
118 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0];
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
119
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
120 return if $method eq 'DESTROY';
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
121
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
122 if ($method =~ /$AutoloadRegex/) {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
123 my $this = shift;
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
124
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
125 die OperationException->new("can't invoke method '$method' on an unblessed reference") unless blessed $this;
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
126
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
127 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method);
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
128 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
129 die OperationException->new("The specified method '$method' doesn't exists");
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
130 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
131 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
132
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
133 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
134
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
135 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
136
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
137 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
138
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
141 C<IMPL::Web::View::TTControl>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
142
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143 =head1 SYNPOSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147 =head2 BLOCKS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149 =head3 INIT
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150
213
sergey
parents: 212
diff changeset
151 Данный блок шаблона управления выполняется один раз при создании первого экземпляра элемента управления,
sergey
parents: 212
diff changeset
152 может использоваться для формирования заголовочной части документа, скрипта подключающего ajax модули
sergey
parents: 212
diff changeset
153 при необходимости и т.п.
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155 =head3 CTOR
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157 данный блок выполняется каждый раз при создании нового экземпляра элемента управления, при этом переменная C<this>
213
sergey
parents: 212
diff changeset
158 указывает на эземпляр элемента упарвления. Данный блок можно использовать для инициализации свойств элемента
sergey
parents: 212
diff changeset
159 управления.
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161 =head3 RENDER
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
162
213
sergey
parents: 212
diff changeset
163 Данный блок выполняется при вызове метода C<Render()>, вывод данного блока и есть результат отображения элемента управления.
sergey
parents: 212
diff changeset
164 Если в шаблоне нет блока C<RENDER>, то сам шаблон считается таковым.
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
165
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
166 =head2 TEMPLATE VARS
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
167
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
168 Каждый шаблон имеет собственное пространство имен, унаследованное от пространства имен фабрики элементов (которая в свою очередь наследует контекст документа).
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
169 В шаблоне могут определяться новые переменные, которые разделяются между блоками. Также доступны стандартные переменные
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
170
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
171 =over
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
172
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
173 =item * C<this> ссылка на объект элемента управления
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
174
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
175 =item * C<component> ссылка на текущий шаблон, устанавливается автоматически в методе C<Template::Context::process>.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
176
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
177 =item * C<template> ссылка на шаблон элемента управления, для совместимости с C<TT>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
178
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
179 =back
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
180
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
181 =head1 MEMBERS
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
182
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
183 =over
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
184
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
185 =item * C<[get]context>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
186
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
187 Контекст элемента управления, хранит пременные шаблона. Наследуется от контекста фабрики элементов управления, который
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
188 наследуется от контекста документа.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
189
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
190 =item * C<[get,set]template>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
191
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
192 C<Template::Document> Шаблон элемента управления.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
193
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
194 =item * C<AUTOLOAD>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
195
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
196 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим свойствам через метод C<AUTOLOAD>.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
197
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
198 =back
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
199
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
200
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
201 C<lang ru>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
202
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
203 =cut