diff 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
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/Metadata/FormMeta.pm	Tue Dec 10 03:02:01 2013 +0400
+++ b/Lib/IMPL/Web/View/Metadata/FormMeta.pm	Fri Dec 13 16:49:47 2013 +0400
@@ -1,13 +1,15 @@
 package IMPL::Web::View::Metadata::FormMeta;
 use strict;
 
+use IMPL::lang;
 use IMPL::Const qw(:prop);
 use IMPL::declare {
 	require => {
 		Exception => 'IMPL::Exception',
 		ArgException => '-IMPL::InvalidArgumentException',
 		OpException => '-IMPL::InvalidOperationException',
-		SchemaNavigator => 'IMPL::DOM::Navigator::SchemaNavigator'
+		SchemaNavigator => 'IMPL::DOM::Navigator::SchemaNavigator',
+		DOMNode => '-IMPL::DOM::Node'
 	},
 	base => [
 		'IMPL::Web::View::Metadata::BaseMeta' => '@_'
@@ -36,6 +38,39 @@
 		foreach qw(schema);
 }
 
+sub GetSchemaProperty {
+	my ($this,$name) = @_;
+	
+	return $this->decl ? $this->decl->nodeProperty($name) || $this->schema->nodeProperty($name) : $this->schema->nodeProperty($name);
+}
+
+sub template {
+	shift->GetSchemaProperty('template');
+}
+
+sub label {
+	shift->GetSchemaProperty('display');
+}
+
+sub inputType {
+	shift->GetSchemaProperty('inputType');
+}
+
+sub inputValue {
+	my ($this) = @_;
+	
+	if($this->isMultiple) {
+		return [
+			map {
+				$_ ? $_->nodeValue || $_->nodeProperty('rawValue') : undef
+			}
+			@{$this->model || []}
+		]
+	} else {
+		return $this->model ? $this->model->nodeValue || $this->model->nodeProperty('rawValue') : undef;
+	}
+}
+
 sub isMultiple {
 	my ($this) = @_;
 	$this->decl && $this->decl->isMultiple;
@@ -51,15 +86,17 @@
 	
 	my $nodes = $this->nodes;
 	
-	return [
+	my $errors = [
 		grep _IsOwnError($nodes,$this->decl,$_), @{$this->errors || []}
 	];
+	
+	return $errors;
 }
 
 sub _IsOwnError {
     my ($nodes,$source,$err) = @_;
-    
-    return 1 if ($err->node && grep($err->node == $_, @$nodes)) || (not(@$nodes) && $err->schema == $source );
+
+ 	return 1 if ($err->node && grep($err->node == $_, @$nodes)) || (not(@$nodes) && $err->schema == $source );
     
     return 0;
 }
@@ -102,14 +139,14 @@
 			name => $decl->name,
 			decl => $decl,
 			schema => $schema,
-			nodes => \@nodes,
+			nodes => [@nodes],
 			errors => [grep _IsErrorRelates(\@nodes,$decl,$_), @{$this->errors || []}]
 		);
 		
 		my ($model,$type);
 		
 		if ($decl->isMultiple) {
-			$model = \@nodes; 
+			$model = [@nodes]; 
 			$type = 'ARRAY';
 			$args{holdingType} = $schema->type;
 		} else {
@@ -166,6 +203,28 @@
 	);
 }
 
+sub GetMetadataForModel {
+	my ($self,$model,$args) = @_;
+	
+	$args ||= {};
+	
+	my $modelType = delete $args->{modelType};
+	
+	if($model) {
+		die ArgException->new(model => "A node is required")
+			unless is($model,DOMNode);
+		
+		$args->{decl} ||= $model->schemaSource;
+		$args->{schema} ||= $model->schema; 
+	}
+	
+	return $self->new(
+		$model,
+		$modelType,
+		$args
+	);
+}
+
 1;
 
 __END__
@@ -243,9 +302,9 @@
 	my ($index,$tmpl) = @_;
 	
 	if ($index =~ /^\d+$/) {
-		return render($tmpl, meta => { $meta->GetItem($index) });
+		return render($tmpl, metadata => { $meta->GetItem($index) });
 	} else {
-		return render($tmpl, meta => { $meta->GetProperty($index) });
+		return render($tmpl, metadata => { $meta->GetProperty($index) });
 	}
 }