Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTControl.pm @ 272:47db27ed5b43
sync
| author | sergey | 
|---|---|
| date | Mon, 28 Jan 2013 17:24:37 +0400 | 
| parents | bbc0da7ef90e | 
| children | 86ff93b34f2a | 
| 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 | |
| 267 | 99 sub GetMainBlock { | 
| 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 | |
| 267 | 108 if(my $body = $this->GetMainBlock ) { | 
| 
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 | |
| 267 | 115 sub RenderBlock { | 
| 116 my ($this,$block, $args) = @_; | |
| 117 | |
| 118 $args = {} unless ref $args eq 'HASH'; | |
| 119 | |
| 120 return $this->context->include( $block, { %$args, this => $this, template => $this->template } ); | |
| 121 } | |
| 122 | |
| 185 | 123 sub AUTOLOAD { | 
| 194 | 124 our $AUTOLOAD; | 
| 125 | |
| 126 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0]; | |
| 127 | |
| 128 return if $method eq 'DESTROY'; | |
| 129 | |
| 238 | 130 if ($method =~ /$AutoloadRegex/) { | 
| 131 my $this = shift; | |
| 
241
 
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
 
sergey 
parents: 
238 
diff
changeset
 | 
132 | 
| 
 
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
 
sergey 
parents: 
238 
diff
changeset
 | 
133 die OperationException->new("can't invoke method '$method' on an unblessed reference") unless blessed $this; | 
| 238 | 134 | 
| 135 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method); | |
| 136 } else { | |
| 137 die OperationException->new("The specified method '$method' doesn't exists"); | |
| 138 } | |
| 181 | 139 } | 
| 140 | |
| 141 1; | |
| 142 | |
| 143 __END__ | |
| 144 | |
| 145 =pod | |
| 146 | |
| 147 =head1 NAME | |
| 148 | |
| 149 C<IMPL::Web::View::TTControl> | |
| 150 | |
| 151 =head1 SYNPOSIS | |
| 152 | |
| 265 | 153 =begin text | 
| 154 | |
| 155 [% | |
| 156 META version = 1; | |
| 157 BLOCK INIT; | |
| 158 # this is a document scope | |
| 159 dojo.require.push( 'dijit/form/Input' ); | |
| 160 END; | |
| 161 BLOCK CTOR; | |
| 162 # local to this block | |
| 163 TPreview = require('My/Org/TextPreview'); | |
| 164 | |
| 165 # init control props | |
| 166 this.dojoClass = this.dojoClass || 'dijit.form.Input'; | |
| 167 this.visualClass = this.visualClass || 'classic'; | |
| 168 this.childNodes = []; | |
| 169 | |
| 170 # init content | |
| 171 FOREACH text IN this.data; | |
| 172 this.childNodes.push( TPreview.new('preview', nodeValue = text ) ); | |
| 173 END; | |
| 174 | |
| 175 END; | |
| 176 %] | |
| 177 | |
| 178 <div class="$this.visualClass" data-dojo-type="$this.dojoClass"> | |
| 179 [% FOREACH node IN this.childNodes %] | |
| 180 [% node.Render() %] | |
| 181 <hr /> | |
| 182 [% END %] | |
| 183 </div> | |
| 184 | |
| 185 =end text | |
| 186 | |
| 181 | 187 =head1 DESCRIPTION | 
| 188 | |
| 265 | 189 Представляет собой фрагмент документа, который имеет шаблон для отображения, | 
| 190 набор свойств и т.п. | |
| 191 | |
| 181 | 192 =head2 BLOCKS | 
| 193 | |
| 265 | 194 =head3 META | 
| 195 | |
| 196 Атрибуты C<META> C<layout>, C<title> будут перенесены в свойства элемента | |
| 197 управления. | |
| 198 | |
| 181 | 199 =head3 INIT | 
| 200 | |
| 265 | 201 Данный блок шаблона управления выполняется один раз при создании первого | 
| 202 экземпляра элемента управления, в пространстве имен документа. Может | |
| 203 использоваться для формирования заголовочной части документа, скрипта | |
| 204 подключающего C<js> модули и т.п. | |
| 205 | |
| 206 Выполнение данного блока производится фабрикой элементов управления. | |
| 181 | 207 | 
| 208 =head3 CTOR | |
| 209 | |
| 265 | 210 данный блок выполняется каждый раз при создании нового экземпляра элемента | 
| 211 управления, при этом переменная C<this> указывает на эземпляр элемента | |
| 212 упарвления. Данный блок можно использовать для инициализации свойств элемента | |
| 213 | 213 управления. | 
| 181 | 214 | 
| 215 =head3 RENDER | |
| 216 | |
| 265 | 217 Данный блок выполняется при вызове метода C<Render()>, вывод данного блока и | 
| 218 есть результат отображения элемента управления. Если в шаблоне нет блока | |
| 219 C<RENDER>, то сам шаблон считается таковым. | |
| 187 | 220 | 
| 221 =head2 TEMPLATE VARS | |
| 222 | |
| 265 | 223 Каждый шаблон имеет собственное пространство имен, вложенное в пространство имен | 
| 224 фабрики элементов (которая разделяет пространство имен документа). В шаблоне | |
| 225 могут определяться новые переменные, однако они останутся локальными для блоков. | |
| 226 | |
| 227 Чтобы передать данные между блоками следует использовать ссылку на элемент | |
| 228 управления C<this>. | |
| 229 | |
| 230 =begin text | |
| 231 | |
| 232 [% | |
| 233 BLOCK CTOR; | |
| 234 this.extraCssClass = 'active'; | |
| 235 text = "this text will gone"; | |
| 236 END; | |
| 237 %] | |
| 238 | |
| 239 <div class="$this.extraCssClass">some text $text</div> | |
| 240 | |
| 241 =end text | |
| 242 | |
| 243 В примере выше переменная C<$text> установленная в конструкторе шаблона, при | |
| 244 отображении элемента управления будет неопределена. | |
| 187 | 245 | 
| 246 =over | |
| 247 | |
| 265 | 248 =item * C<this> | 
| 249 | |
| 250 ссылка на объект элемента управления | |
| 251 | |
| 252 =item * C<component> | |
| 187 | 253 | 
| 265 | 254 ссылка на текущий шаблон, устанавливается автоматически в методе | 
| 255 C<Template::Context::process>. | |
| 187 | 256 | 
| 265 | 257 =item * C<template> | 
| 258 | |
| 259 ссылка на шаблон элемента управления, для совместимости с C<TT> | |
| 187 | 260 | 
| 261 =back | |
| 262 | |
| 263 =head1 MEMBERS | |
| 264 | |
| 265 =over | |
| 266 | |
| 267 =item * C<[get]context> | |
| 268 | |
| 265 | 269 Контекст элемента управления, хранит пременные шаблона. Передается в | 
| 270 конструкторе. Фабрика элементов управления создает новый контекст пространство | |
| 271 имен которого вложено в пространство имен документа. | |
| 187 | 272 | 
| 273 =item * C<[get,set]template> | |
| 274 | |
| 275 C<Template::Document> Шаблон элемента управления. | |
| 276 | |
| 277 =item * C<AUTOLOAD> | |
| 278 | |
| 265 | 279 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим | 
| 280 свойствам через метод C<AUTOLOAD>. Имена свойств должны начинаться со строчной | |
| 281 буквы. | |
| 187 | 282 | 
| 283 =back | |
| 181 | 284 | 
| 285 =cut | 
