comparison Lib/IMPL/Web/View/Metadata/FormMeta.pm @ 371:d5c8b955bf8d

refactoring
author cin
date Fri, 13 Dec 2013 16:49:47 +0400
parents 010ceafd0c5a
children 2f16f13b000c
comparison
equal deleted inserted replaced
370:cbf4febf0930 371:d5c8b955bf8d
1 package IMPL::Web::View::Metadata::FormMeta; 1 package IMPL::Web::View::Metadata::FormMeta;
2 use strict; 2 use strict;
3 3
4 use IMPL::lang;
4 use IMPL::Const qw(:prop); 5 use IMPL::Const qw(:prop);
5 use IMPL::declare { 6 use IMPL::declare {
6 require => { 7 require => {
7 Exception => 'IMPL::Exception', 8 Exception => 'IMPL::Exception',
8 ArgException => '-IMPL::InvalidArgumentException', 9 ArgException => '-IMPL::InvalidArgumentException',
9 OpException => '-IMPL::InvalidOperationException', 10 OpException => '-IMPL::InvalidOperationException',
10 SchemaNavigator => 'IMPL::DOM::Navigator::SchemaNavigator' 11 SchemaNavigator => 'IMPL::DOM::Navigator::SchemaNavigator',
12 DOMNode => '-IMPL::DOM::Node'
11 }, 13 },
12 base => [ 14 base => [
13 'IMPL::Web::View::Metadata::BaseMeta' => '@_' 15 'IMPL::Web::View::Metadata::BaseMeta' => '@_'
14 ], 16 ],
15 props => [ 17 props => [
34 36
35 $this->$_() || die ArgException->new($_ => "The $_ is required") 37 $this->$_() || die ArgException->new($_ => "The $_ is required")
36 foreach qw(schema); 38 foreach qw(schema);
37 } 39 }
38 40
41 sub GetSchemaProperty {
42 my ($this,$name) = @_;
43
44 return $this->decl ? $this->decl->nodeProperty($name) || $this->schema->nodeProperty($name) : $this->schema->nodeProperty($name);
45 }
46
47 sub template {
48 shift->GetSchemaProperty('template');
49 }
50
51 sub label {
52 shift->GetSchemaProperty('display');
53 }
54
55 sub inputType {
56 shift->GetSchemaProperty('inputType');
57 }
58
59 sub inputValue {
60 my ($this) = @_;
61
62 if($this->isMultiple) {
63 return [
64 map {
65 $_ ? $_->nodeValue || $_->nodeProperty('rawValue') : undef
66 }
67 @{$this->model || []}
68 ]
69 } else {
70 return $this->model ? $this->model->nodeValue || $this->model->nodeProperty('rawValue') : undef;
71 }
72 }
73
39 sub isMultiple { 74 sub isMultiple {
40 my ($this) = @_; 75 my ($this) = @_;
41 $this->decl && $this->decl->isMultiple; 76 $this->decl && $this->decl->isMultiple;
42 } 77 }
43 78
49 sub GetOwnErrors { 84 sub GetOwnErrors {
50 my ($this) = @_; 85 my ($this) = @_;
51 86
52 my $nodes = $this->nodes; 87 my $nodes = $this->nodes;
53 88
54 return [ 89 my $errors = [
55 grep _IsOwnError($nodes,$this->decl,$_), @{$this->errors || []} 90 grep _IsOwnError($nodes,$this->decl,$_), @{$this->errors || []}
56 ]; 91 ];
92
93 return $errors;
57 } 94 }
58 95
59 sub _IsOwnError { 96 sub _IsOwnError {
60 my ($nodes,$source,$err) = @_; 97 my ($nodes,$source,$err) = @_;
61 98
62 return 1 if ($err->node && grep($err->node == $_, @$nodes)) || (not(@$nodes) && $err->schema == $source ); 99 return 1 if ($err->node && grep($err->node == $_, @$nodes)) || (not(@$nodes) && $err->schema == $source );
63 100
64 return 0; 101 return 0;
65 } 102 }
66 103
67 sub _IsErrorRelates { 104 sub _IsErrorRelates {
100 137
101 my %args = ( 138 my %args = (
102 name => $decl->name, 139 name => $decl->name,
103 decl => $decl, 140 decl => $decl,
104 schema => $schema, 141 schema => $schema,
105 nodes => \@nodes, 142 nodes => [@nodes],
106 errors => [grep _IsErrorRelates(\@nodes,$decl,$_), @{$this->errors || []}] 143 errors => [grep _IsErrorRelates(\@nodes,$decl,$_), @{$this->errors || []}]
107 ); 144 );
108 145
109 my ($model,$type); 146 my ($model,$type);
110 147
111 if ($decl->isMultiple) { 148 if ($decl->isMultiple) {
112 $model = \@nodes; 149 $model = [@nodes];
113 $type = 'ARRAY'; 150 $type = 'ARRAY';
114 $args{holdingType} = $schema->type; 151 $args{holdingType} = $schema->type;
115 } else { 152 } else {
116 $model = shift @nodes; 153 $model = shift @nodes;
117 $type = $schema->type; 154 $type = $schema->type;
164 nodes => \@nodes 201 nodes => \@nodes
165 } 202 }
166 ); 203 );
167 } 204 }
168 205
206 sub GetMetadataForModel {
207 my ($self,$model,$args) = @_;
208
209 $args ||= {};
210
211 my $modelType = delete $args->{modelType};
212
213 if($model) {
214 die ArgException->new(model => "A node is required")
215 unless is($model,DOMNode);
216
217 $args->{decl} ||= $model->schemaSource;
218 $args->{schema} ||= $model->schema;
219 }
220
221 return $self->new(
222 $model,
223 $modelType,
224 $args
225 );
226 }
227
169 1; 228 1;
170 229
171 __END__ 230 __END__
172 231
173 =pod 232 =pod
241 300
242 sub display_for { 301 sub display_for {
243 my ($index,$tmpl) = @_; 302 my ($index,$tmpl) = @_;
244 303
245 if ($index =~ /^\d+$/) { 304 if ($index =~ /^\d+$/) {
246 return render($tmpl, meta => { $meta->GetItem($index) }); 305 return render($tmpl, metadata => { $meta->GetItem($index) });
247 } else { 306 } else {
248 return render($tmpl, meta => { $meta->GetProperty($index) }); 307 return render($tmpl, metadata => { $meta->GetProperty($index) });
249 } 308 }
250 } 309 }
251 310
252 =end code 311 =end code
253 312