Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTControl.pm @ 346:f05634287ac7
working on the view concept
| author | cin |
|---|---|
| date | Mon, 30 Sep 2013 01:37:50 +0400 |
| parents | 9bdccdf1f50b |
| children | 675cd1829255 |
| rev | line source |
|---|---|
| 181 | 1 package IMPL::Web::View::TTControl; |
| 2 use strict; | |
| 3 | |
| 234 | 4 use IMPL::Const qw(:prop); |
| 334 | 5 use IMPL::lang qw(:hash :base); |
| 299 | 6 use Scalar::Util qw(blessed reftype); |
| 234 | 7 use IMPL::declare { |
| 8 require => { | |
| 296 | 9 TemplateDocument => 'Template::Document', |
| 343 | 10 TTContext => 'IMPL::Web::View::TTContext', |
| 234 | 11 Exception => 'IMPL::Exception', |
| 238 | 12 ArgumentException => '-IMPL::InvalidArgumentException', |
| 13 OperationException => '-IMPL::InvalidOperationException' | |
| 234 | 14 }, |
| 15 base => [ | |
|
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
16 'IMPL::Object' => undef |
| 234 | 17 ], |
| 18 props => [ | |
| 19 id => PROP_RO, | |
| 238 | 20 attributes => PROP_RW, |
| 234 | 21 context => PROP_RO, |
| 346 | 22 template => PROP_RO, |
| 23 parents => PROP_RO | |
| 234 | 24 ] |
| 25 }; | |
| 26 | |
| 181 | 27 |
| 187 | 28 { |
| 194 | 29 my $nextId = 1; |
| 30 sub _GetNextId { | |
| 299 | 31 return '_' . $nextId++; |
| 194 | 32 } |
| 187 | 33 } |
| 181 | 34 |
| 300 | 35 our $AUTOLOAD_REGEX = qr/^[a-z]/; |
| 238 | 36 |
| 334 | 37 my %mapSkipAttributes = map { $_, 1 } qw(attributes context); |
| 38 | |
| 181 | 39 sub CTOR { |
| 299 | 40 my ($this,$template,$context,$attrs) = @_; |
| 194 | 41 |
| 299 | 42 |
| 194 | 43 $this->template( $template ) or die new IMPL::ArgumentException("A template is required"); |
| 334 | 44 |
| 45 die IMPL::ArgumentException->new(context => "A context is required, supplied: $context") | |
| 46 unless is($context,TTContext); | |
| 47 | |
| 48 $this->context( $context ); | |
| 194 | 49 |
| 238 | 50 $this->attributes({}); |
| 194 | 51 |
| 301 | 52 if(ref($attrs) eq 'HASH') { |
| 299 | 53 while (my($key,$value) = each %$attrs) { |
| 334 | 54 next if $mapSkipAttributes{$key}; |
| 299 | 55 $this->SetAttribute($key,$value); |
| 56 } | |
| 307 | 57 } |
|
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
58 |
| 299 | 59 $this->id(_GetNextId()) unless $this->id; |
| 191 | 60 } |
| 61 | |
| 238 | 62 sub GetAttribute { |
| 63 my ($this,$name) = (shift,shift); | |
| 64 | |
| 65 if (my $method = $this->can($name)) { | |
| 66 unshift @_,$this; | |
| 67 goto &$method; | |
| 68 } else { | |
| 69 return $this->attributes->{$name}; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 sub SetAttribute { | |
| 74 my $this = shift; | |
| 75 my $name = shift; | |
| 76 | |
| 77 if (my $method = $this->can($name)) { | |
| 78 unshift @_, $this; | |
| 79 goto &$method; | |
| 80 } else { | |
| 81 return $this->attributes->{$name} = shift; | |
| 82 } | |
| 83 } | |
| 84 | |
| 187 | 85 sub Render { |
| 194 | 86 my ($this,$args) = @_; |
| 87 | |
| 88 $args = {} unless ref $args eq 'HASH'; | |
| 89 | |
| 299 | 90 return $this->context->include( |
| 301 | 91 $this->template->block, |
| 299 | 92 { |
| 93 %$args, | |
| 94 this => $this, | |
| 95 template => $this->template | |
| 96 } | |
| 97 ); | |
| 267 | 98 } |
| 99 | |
| 302 | 100 sub GetTemplate { |
| 101 my ($this,$name) = @_; | |
| 102 | |
| 103 return eval { $this->context->template($name) }; | |
| 104 } | |
| 105 | |
| 106 sub Include { | |
| 107 my ($this,$template, $args) = @_; | |
| 108 | |
| 109 my $tpl = $this->GetTemplate($template) | |
| 110 or die OperationException->new("The specified template isn't found", $template); | |
| 111 | |
| 112 return $this->context->include( | |
| 113 $tpl, | |
| 114 $args | |
| 115 ); | |
| 116 } | |
| 117 | |
| 314 | 118 sub HasBlock { |
| 119 my ($this,$block) = @_; | |
| 120 | |
| 121 $this->GetTemplate ? 1 : 0; | |
| 122 } | |
| 123 | |
| 185 | 124 sub AUTOLOAD { |
| 194 | 125 our $AUTOLOAD; |
| 126 | |
| 127 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0]; | |
| 128 | |
| 129 return if $method eq 'DESTROY'; | |
| 130 | |
| 300 | 131 if ($method =~ /$AUTOLOAD_REGEX/) { |
| 238 | 132 my $this = shift; |
|
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
133 |
|
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
134 die OperationException->new("can't invoke method '$method' on an unblessed reference") unless blessed $this; |
| 238 | 135 |
| 136 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method); | |
| 137 } else { | |
| 138 die OperationException->new("The specified method '$method' doesn't exists"); | |
| 139 } | |
| 181 | 140 } |
| 141 | |
|
342
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
142 sub CreateControlFromTemplate { |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
143 my ($this,$template,$args) = @_; |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
144 |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
145 if (not ref($template)) { |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
146 return $this->context->stash->get([ |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
147 require => [ |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
148 $template |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
149 ] |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
150 ])->new($args); |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
151 } else { |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
152 return $this->new( |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
153 $template, |
| 343 | 154 $this->context->clone(), |
|
342
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
155 $args |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
156 ); |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
157 } |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
158 } |
|
1090c1dd7429
TTControl: added a helper method 'CreateControlFromTemlpate'
cin
parents:
334
diff
changeset
|
159 |
| 181 | 160 1; |
| 161 | |
| 162 __END__ | |
| 163 | |
| 164 =pod | |
| 165 | |
| 166 =head1 NAME | |
| 167 | |
| 168 C<IMPL::Web::View::TTControl> | |
| 169 | |
| 170 =head1 SYNPOSIS | |
| 171 | |
| 265 | 172 =begin text |
| 173 | |
| 174 [% | |
| 175 META version = 1; | |
| 176 BLOCK INIT; | |
| 177 # this is a document scope | |
| 299 | 178 dojo.modules.push( 'dijit/form/Input' ); |
| 265 | 179 END; |
| 299 | 180 |
| 181 # local to this block | |
| 182 TPreview = require('My/Org/TextPreview'); | |
| 265 | 183 |
| 299 | 184 # init control props |
| 185 visualClass = this.visualClass || 'classic'; | |
| 186 %] | |
| 187 <div id="$id" class="$visualClass" data-dojo-type="dijit/form/Input"> | |
| 188 [% FOREACH item IN model %] | |
| 189 <div class="itemContainer"> | |
| 190 [% Display(item) %] | |
| 191 </div> | |
| 265 | 192 [% END %] |
| 193 </div> | |
| 194 | |
| 195 =end text | |
| 196 | |
| 181 | 197 =head1 DESCRIPTION |
| 198 | |
| 299 | 199 Легкая обертка вокруг шаблона, позволяет изолировать пространство имен шаблона, |
| 200 а также реализовать собственные методы по представлению данных (в случае если | |
| 201 это проще сделать в виде методов класса). | |
| 265 | 202 |
| 181 | 203 =head2 BLOCKS |
| 204 | |
| 265 | 205 =head3 META |
| 206 | |
| 207 Атрибуты C<META> C<layout>, C<title> будут перенесены в свойства элемента | |
| 208 управления. | |
| 209 | |
| 181 | 210 =head3 INIT |
| 211 | |
| 265 | 212 Данный блок шаблона управления выполняется один раз при создании первого |
| 213 экземпляра элемента управления, в пространстве имен документа. Может | |
| 214 использоваться для формирования заголовочной части документа, скрипта | |
| 215 подключающего C<js> модули и т.п. | |
| 216 | |
| 217 Выполнение данного блока производится фабрикой элементов управления. | |
| 181 | 218 |
| 187 | 219 =head2 TEMPLATE VARS |
| 220 | |
| 265 | 221 Каждый шаблон имеет собственное пространство имен, вложенное в пространство имен |
| 222 фабрики элементов (которая разделяет пространство имен документа). В шаблоне | |
| 223 могут определяться новые переменные, однако они останутся локальными для блоков. | |
| 224 | |
| 225 Чтобы передать данные между блоками следует использовать ссылку на элемент | |
| 226 управления C<this>. | |
| 227 | |
| 228 =begin text | |
| 229 | |
| 230 [% | |
| 231 BLOCK CTOR; | |
| 232 this.extraCssClass = 'active'; | |
| 233 text = "this text will gone"; | |
| 234 END; | |
| 235 %] | |
| 236 | |
| 237 <div class="$this.extraCssClass">some text $text</div> | |
| 238 | |
| 239 =end text | |
| 240 | |
| 241 В примере выше переменная C<$text> установленная в конструкторе шаблона, при | |
| 242 отображении элемента управления будет неопределена. | |
| 187 | 243 |
| 244 =over | |
| 245 | |
| 265 | 246 =item * C<this> |
| 247 | |
| 248 ссылка на объект элемента управления | |
| 249 | |
| 250 =item * C<component> | |
| 187 | 251 |
| 265 | 252 ссылка на текущий шаблон, устанавливается автоматически в методе |
| 253 C<Template::Context::process>. | |
| 187 | 254 |
| 265 | 255 =item * C<template> |
| 256 | |
| 257 ссылка на шаблон элемента управления, для совместимости с C<TT> | |
| 187 | 258 |
| 259 =back | |
| 260 | |
| 261 =head1 MEMBERS | |
| 262 | |
| 300 | 263 =head2 C<[get]context> |
| 187 | 264 |
| 300 | 265 Контекст элемента управления, хранит пременные шаблона. Фабрика элементов |
| 266 управления создает новый контекст пространство имен которого вложено в | |
| 267 пространство имен документа. | |
| 187 | 268 |
| 300 | 269 Контекст следует использовать только при рендеринге документа. |
| 270 | |
| 271 =head2 C<[get,set]template> | |
| 187 | 272 |
| 273 C<Template::Document> Шаблон элемента управления. | |
| 274 | |
| 300 | 275 =head2 C<AUTOLOAD> |
| 187 | 276 |
| 265 | 277 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим |
| 278 свойствам через метод C<AUTOLOAD>. Имена свойств должны начинаться со строчной | |
| 279 буквы. | |
| 187 | 280 |
| 181 | 281 =cut |
