Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTControl.pm @ 265:6b6d4b2275a1
improved documentation
| author | cin |
|---|---|
| date | Thu, 10 Jan 2013 03:25:02 +0400 |
| parents | f48a1a9f4fa2 |
| children | bbc0da7ef90e |
| rev | line source |
|---|---|
| 181 | 1 package IMPL::Web::View::TTControl; |
| 2 use strict; | |
| 3 | |
| 234 | 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 | 7 use IMPL::declare { |
| 8 require => { | |
| 9 TTContext => 'Template::Context', | |
| 10 Exception => 'IMPL::Exception', | |
| 238 | 11 ArgumentException => '-IMPL::InvalidArgumentException', |
| 12 OperationException => '-IMPL::InvalidOperationException' | |
| 234 | 13 }, |
| 14 base => [ | |
|
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
15 'IMPL::Object' => undef |
| 234 | 16 ], |
| 17 props => [ | |
| 18 id => PROP_RO, | |
| 238 | 19 attributes => PROP_RW, |
| 20 name => PROP_RO, | |
| 234 | 21 context => PROP_RO, |
| 22 template => PROP_RO | |
| 23 ] | |
| 24 }; | |
| 25 | |
| 181 | 26 |
| 187 | 27 { |
| 194 | 28 my $nextId = 1; |
| 29 sub _GetNextId { | |
| 30 return $nextId++; | |
| 31 } | |
| 187 | 32 } |
| 181 | 33 |
| 238 | 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 | 36 |
| 181 | 37 sub CTOR { |
| 194 | 38 my ($this,$name,$template,$context,$refProps) = @_; |
| 39 | |
| 40 $name ||= "control"; | |
| 41 | |
| 42 $this->template( $template ) or die new IMPL::ArgumentException("A template is required"); | |
| 43 $this->context( $context ) or die new IMPL::ArgumentException("A context is required"); | |
| 44 | |
| 45 $this->id($name . "-" . _GetNextId()) unless $this->id; | |
| 46 | |
| 238 | 47 $this->name($name); |
| 48 $this->attributes({}); | |
| 194 | 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 | 56 } |
| 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 | 64 } |
| 65 | |
| 191 | 66 sub InitInstance { |
| 194 | 67 my ($this,$args) = @_; |
| 68 | |
| 69 $args ||= {}; | |
| 70 | |
| 71 if ( my $ctor = $this->template->blocks->{CTOR} ) { | |
| 212 | 72 $this->context->include($ctor, { %$args, this => $this, template => $this->template } ); |
| 194 | 73 } |
| 191 | 74 } |
| 75 | |
| 238 | 76 sub GetAttribute { |
| 77 my ($this,$name) = (shift,shift); | |
| 78 | |
| 79 if (my $method = $this->can($name)) { | |
| 80 unshift @_,$this; | |
| 81 goto &$method; | |
| 82 } else { | |
| 83 return $this->attributes->{$name}; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 sub SetAttribute { | |
| 88 my $this = shift; | |
| 89 my $name = shift; | |
| 90 | |
| 91 if (my $method = $this->can($name)) { | |
| 92 unshift @_, $this; | |
| 93 goto &$method; | |
| 94 } else { | |
| 95 return $this->attributes->{$name} = shift; | |
| 96 } | |
| 97 } | |
| 98 | |
| 234 | 99 sub GetRenderBlock { |
| 194 | 100 $_[0]->template->blocks->{RENDER} || $_[0]->template; |
| 187 | 101 } |
| 102 | |
| 103 sub Render { | |
| 194 | 104 my ($this,$args) = @_; |
| 105 | |
| 106 $args = {} unless ref $args eq 'HASH'; | |
| 107 | |
| 234 | 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 | 110 } else { |
| 111 return ""; | |
| 112 } | |
| 185 | 113 } |
| 114 | |
| 115 sub AUTOLOAD { | |
| 194 | 116 our $AUTOLOAD; |
| 117 | |
| 118 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0]; | |
| 119 | |
| 120 return if $method eq 'DESTROY'; | |
| 121 | |
| 238 | 122 if ($method =~ /$AutoloadRegex/) { |
| 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 | 126 |
| 127 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method); | |
| 128 } else { | |
| 129 die OperationException->new("The specified method '$method' doesn't exists"); | |
| 130 } | |
| 181 | 131 } |
| 132 | |
| 133 1; | |
| 134 | |
| 135 __END__ | |
| 136 | |
| 137 =pod | |
| 138 | |
| 139 =head1 NAME | |
| 140 | |
| 141 C<IMPL::Web::View::TTControl> | |
| 142 | |
| 143 =head1 SYNPOSIS | |
| 144 | |
| 265 | 145 =begin text |
| 146 | |
| 147 [% | |
| 148 META version = 1; | |
| 149 BLOCK INIT; | |
| 150 # this is a document scope | |
| 151 dojo.require.push( 'dijit/form/Input' ); | |
| 152 END; | |
| 153 BLOCK CTOR; | |
| 154 # local to this block | |
| 155 TPreview = require('My/Org/TextPreview'); | |
| 156 | |
| 157 # init control props | |
| 158 this.dojoClass = this.dojoClass || 'dijit.form.Input'; | |
| 159 this.visualClass = this.visualClass || 'classic'; | |
| 160 this.childNodes = []; | |
| 161 | |
| 162 # init content | |
| 163 FOREACH text IN this.data; | |
| 164 this.childNodes.push( TPreview.new('preview', nodeValue = text ) ); | |
| 165 END; | |
| 166 | |
| 167 END; | |
| 168 %] | |
| 169 | |
| 170 <div class="$this.visualClass" data-dojo-type="$this.dojoClass"> | |
| 171 [% FOREACH node IN this.childNodes %] | |
| 172 [% node.Render() %] | |
| 173 <hr /> | |
| 174 [% END %] | |
| 175 </div> | |
| 176 | |
| 177 =end text | |
| 178 | |
| 181 | 179 =head1 DESCRIPTION |
| 180 | |
| 265 | 181 Представляет собой фрагмент документа, который имеет шаблон для отображения, |
| 182 набор свойств и т.п. | |
| 183 | |
| 181 | 184 =head2 BLOCKS |
| 185 | |
| 265 | 186 =head3 META |
| 187 | |
| 188 Атрибуты C<META> C<layout>, C<title> будут перенесены в свойства элемента | |
| 189 управления. | |
| 190 | |
| 181 | 191 =head3 INIT |
| 192 | |
| 265 | 193 Данный блок шаблона управления выполняется один раз при создании первого |
| 194 экземпляра элемента управления, в пространстве имен документа. Может | |
| 195 использоваться для формирования заголовочной части документа, скрипта | |
| 196 подключающего C<js> модули и т.п. | |
| 197 | |
| 198 Выполнение данного блока производится фабрикой элементов управления. | |
| 181 | 199 |
| 200 =head3 CTOR | |
| 201 | |
| 265 | 202 данный блок выполняется каждый раз при создании нового экземпляра элемента |
| 203 управления, при этом переменная C<this> указывает на эземпляр элемента | |
| 204 упарвления. Данный блок можно использовать для инициализации свойств элемента | |
| 213 | 205 управления. |
| 181 | 206 |
| 207 =head3 RENDER | |
| 208 | |
| 265 | 209 Данный блок выполняется при вызове метода C<Render()>, вывод данного блока и |
| 210 есть результат отображения элемента управления. Если в шаблоне нет блока | |
| 211 C<RENDER>, то сам шаблон считается таковым. | |
| 187 | 212 |
| 213 =head2 TEMPLATE VARS | |
| 214 | |
| 265 | 215 Каждый шаблон имеет собственное пространство имен, вложенное в пространство имен |
| 216 фабрики элементов (которая разделяет пространство имен документа). В шаблоне | |
| 217 могут определяться новые переменные, однако они останутся локальными для блоков. | |
| 218 | |
| 219 Чтобы передать данные между блоками следует использовать ссылку на элемент | |
| 220 управления C<this>. | |
| 221 | |
| 222 =begin text | |
| 223 | |
| 224 [% | |
| 225 BLOCK CTOR; | |
| 226 this.extraCssClass = 'active'; | |
| 227 text = "this text will gone"; | |
| 228 END; | |
| 229 %] | |
| 230 | |
| 231 <div class="$this.extraCssClass">some text $text</div> | |
| 232 | |
| 233 =end text | |
| 234 | |
| 235 В примере выше переменная C<$text> установленная в конструкторе шаблона, при | |
| 236 отображении элемента управления будет неопределена. | |
| 187 | 237 |
| 238 =over | |
| 239 | |
| 265 | 240 =item * C<this> |
| 241 | |
| 242 ссылка на объект элемента управления | |
| 243 | |
| 244 =item * C<component> | |
| 187 | 245 |
| 265 | 246 ссылка на текущий шаблон, устанавливается автоматически в методе |
| 247 C<Template::Context::process>. | |
| 187 | 248 |
| 265 | 249 =item * C<template> |
| 250 | |
| 251 ссылка на шаблон элемента управления, для совместимости с C<TT> | |
| 187 | 252 |
| 253 =back | |
| 254 | |
| 255 =head1 MEMBERS | |
| 256 | |
| 257 =over | |
| 258 | |
| 259 =item * C<[get]context> | |
| 260 | |
| 265 | 261 Контекст элемента управления, хранит пременные шаблона. Передается в |
| 262 конструкторе. Фабрика элементов управления создает новый контекст пространство | |
| 263 имен которого вложено в пространство имен документа. | |
| 187 | 264 |
| 265 =item * C<[get,set]template> | |
| 266 | |
| 267 C<Template::Document> Шаблон элемента управления. | |
| 268 | |
| 269 =item * C<AUTOLOAD> | |
| 270 | |
| 265 | 271 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим |
| 272 свойствам через метод C<AUTOLOAD>. Имена свойств должны начинаться со строчной | |
| 273 буквы. | |
| 187 | 274 |
| 275 =back | |
| 181 | 276 |
| 277 =cut |
