annotate Lib/IMPL/Web/View/TTControl.pm @ 236:2904da230022

DOM refactoring
author sergey
date Mon, 15 Oct 2012 04:23:01 +0400
parents 2530d1bb9638
children b8c724f6de36
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
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
4 use Scalar::Util qw(weaken);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
5
234
sergey
parents: 232
diff changeset
6 use IMPL::Const qw(:prop);
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',
sergey
parents: 232
diff changeset
11 ArgumentException => '-IMPL::InvalidArgumentException'
sergey
parents: 232
diff changeset
12 },
sergey
parents: 232
diff changeset
13 base => [
sergey
parents: 232
diff changeset
14 'IMPL::DOM::Node' => sub {
sergey
parents: 232
diff changeset
15 nodeName => $_[0],
sergey
parents: 232
diff changeset
16 %{ $_[3] || {} }
sergey
parents: 232
diff changeset
17 }
sergey
parents: 232
diff changeset
18 ],
sergey
parents: 232
diff changeset
19 props => [
sergey
parents: 232
diff changeset
20 id => PROP_RO,
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
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
34 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
35 my ($this,$name,$template,$context,$refProps) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
36
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
37 $name ||= "control";
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
38
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
39 $this->template( $template ) or die new IMPL::ArgumentException("A template is required");
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
40 $this->context( $context ) or die new IMPL::ArgumentException("A context is required");
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
41
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
42 $this->id($name . "-" . _GetNextId()) unless $this->id;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
43
236
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
44 #TODO: deprecated, cleanup
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
45 #weaken($this); # prevent cyclic references produced by the code below
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
46
236
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
47 #$context->stash->set('append', sub { $this->appendChild(@_); undef; } );
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
48 #$context->stash->set('select', sub { $this->selectNodes(@_); } );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
49 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
50
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
51 sub InitInstance {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
52 my ($this,$args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
53
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
54 $args ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
55
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
56 if ( my $ctor = $this->template->blocks->{CTOR} ) {
212
292226770180 bugfixes
sergey
parents: 194
diff changeset
57 $this->context->include($ctor, { %$args, this => $this, template => $this->template } );
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
58 }
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
59 }
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
60
234
sergey
parents: 232
diff changeset
61 sub GetRenderBlock {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
62 $_[0]->template->blocks->{RENDER} || $_[0]->template;
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
63 }
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
64
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
65 sub Render {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
66 my ($this,$args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
67
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
68 $args = {} unless ref $args eq 'HASH';
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
69
234
sergey
parents: 232
diff changeset
70 if(my $body = $this->GetRenderBlock ) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
71 return $this->context->include( $body, { %$args, this => $this, template => $this->template, document => $this->document } );
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
72 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
73 return "";
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
74 }
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
75 }
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
76
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
77 sub AUTOLOAD {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
78 our $AUTOLOAD;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
79
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
80 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0];
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
81
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
82 return if $method eq 'DESTROY';
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
83
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
84 my $this = shift;
212
292226770180 bugfixes
sergey
parents: 194
diff changeset
85
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
86 $this->nodeProperty($method,@_);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
87 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
88
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
89 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
90
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
91 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
92
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
93 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
94
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
95 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
96
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
97 C<IMPL::Web::View::TTControl>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
98
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
99 =head1 SYNPOSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
100
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
101 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103 =head2 BLOCKS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
104
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
105 =head3 INIT
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
106
213
sergey
parents: 212
diff changeset
107 Данный блок шаблона управления выполняется один раз при создании первого экземпляра элемента управления,
sergey
parents: 212
diff changeset
108 может использоваться для формирования заголовочной части документа, скрипта подключающего ajax модули
sergey
parents: 212
diff changeset
109 при необходимости и т.п.
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
110
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111 =head3 CTOR
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113 данный блок выполняется каждый раз при создании нового экземпляра элемента управления, при этом переменная C<this>
213
sergey
parents: 212
diff changeset
114 указывает на эземпляр элемента упарвления. Данный блок можно использовать для инициализации свойств элемента
sergey
parents: 212
diff changeset
115 управления.
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117 =head3 RENDER
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118
213
sergey
parents: 212
diff changeset
119 Данный блок выполняется при вызове метода C<Render()>, вывод данного блока и есть результат отображения элемента управления.
sergey
parents: 212
diff changeset
120 Если в шаблоне нет блока C<RENDER>, то сам шаблон считается таковым.
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
121
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
122 =head2 TEMPLATE VARS
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
123
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
124 Каждый шаблон имеет собственное пространство имен, унаследованное от пространства имен фабрики элементов (которая в свою очередь наследует контекст документа).
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
125 В шаблоне могут определяться новые переменные, которые разделяются между блоками. Также доступны стандартные переменные
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
126
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
127 =over
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
128
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
129 =item * C<this> ссылка на объект элемента управления
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
130
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
131 =item * C<component> ссылка на текущий шаблон, устанавливается автоматически в методе C<Template::Context::process>.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
132
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
133 =item * C<template> ссылка на шаблон элемента управления, для совместимости с C<TT>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
134
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
135 =back
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
136
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
137 =head1 MEMBERS
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
138
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
139 =over
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
140
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
141 =item * C<[get]context>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
142
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
143 Контекст элемента управления, хранит пременные шаблона. Наследуется от контекста фабрики элементов управления, который
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
144 наследуется от контекста документа.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
145
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
146 =item * C<[get,set]template>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
147
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
148 C<Template::Document> Шаблон элемента управления.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
149
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
150 =item * C<AUTOLOAD>
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
151
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
152 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим свойствам через метод C<AUTOLOAD>.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
153
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
154 =back
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157 C<lang ru>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
158
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159 =cut