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