Mercurial > pub > Impl
comparison Lib/Form/ValueItem/List.pm @ 0:03e58a454b20
Создан репозитарий
author | Sergey |
---|---|
date | Tue, 14 Jul 2009 12:54:37 +0400 |
parents | |
children | 16ada169ca75 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:03e58a454b20 |
---|---|
1 package Form::ValueItem::List; | |
2 use Common; | |
3 use base qw(Form::ValueItem); | |
4 | |
5 BEGIN { | |
6 DeclareProperty ListValues => ACCESS_READ; | |
7 DeclareProperty CurrentItem => ACCESS_READ; | |
8 } | |
9 | |
10 sub CTOR { | |
11 my $this = shift; | |
12 $this->SUPER::CTOR(@_); | |
13 | |
14 $this->{$ListValues} = []; | |
15 | |
16 my $source = $this->Form->Bindings->{$this->Attributes->{source}}; | |
17 | |
18 if (ref $source eq 'CODE') { | |
19 $this->LoadList($source->()); | |
20 } elsif (ref $source and (UNIVERSAL::isa($source,'HASH') or UNIVERSAL::isa($source,'ARRAY'))){ | |
21 $this->LoadList($source); | |
22 } else { | |
23 if (not $source) { | |
24 warn "a source isn't specified for the listvalue ".$this->Id->Canonical; | |
25 } else { | |
26 warn "an unsupported source type ".(ref $source)." for the listvalue".$this->Id->Canonical; | |
27 } | |
28 } | |
29 } | |
30 | |
31 sub Value { | |
32 my $this = shift; | |
33 | |
34 if (@_) { | |
35 my $newValue = shift; | |
36 | |
37 $this->{$CurrentItem}->{active} = 0 if $this->{$CurrentItem}; | |
38 | |
39 my ($item) = (defined $newValue ? grep {defined $_->{id} and $_->{id} eq $newValue} @{$this->{$ListValues}} : undef); | |
40 | |
41 if ($item) { | |
42 $this->{$CurrentItem} = $item; | |
43 $item->{active} = 1; | |
44 return $this->SUPER::Value($newValue); | |
45 } else { | |
46 undef $this->{$CurrentItem}; | |
47 return $this->SUPER::Value(undef); | |
48 } | |
49 } else { | |
50 return $this->SUPER::Value; | |
51 } | |
52 } | |
53 | |
54 sub LoadList { | |
55 my ($this,$refList) = @_; | |
56 | |
57 if (ref $refList and UNIVERSAL::isa($refList,'HASH')) { | |
58 $this->{$CurrentItem} = undef; | |
59 $this->{$ListValues} = [ sort { $a->{name} cmp $b->{name} } map { Form::ValueItem::List::Item->new($_,ref $refList->{$_} eq 'ARRAY' ? @{$refList->{$_}} : $refList->{$_})} keys %{$refList}]; | |
60 $this->SUPER::Value(undef); | |
61 } elsif (ref $refList and UNIVERSAL::isa($refList,'ARRAY')) { | |
62 $this->{$CurrentItem} = undef; | |
63 $this->{$ListValues} = [map { Form::ValueItem::List::Item->new(ref $_ eq 'ARRAY' ? @$_ : $_ )} @$refList]; | |
64 $this->SUPER::Value(undef); | |
65 } else { | |
66 die new Exception('An unexpected list type'); | |
67 } | |
68 } | |
69 | |
70 package Form::ValueItem::List::Item; | |
71 use fields qw( | |
72 id | |
73 description | |
74 name | |
75 active | |
76 ); | |
77 | |
78 sub new { | |
79 my ($class,$id,$name,$desc) = @_; | |
80 | |
81 my $this=fields::new($class); | |
82 $this->{id} = $id; | |
83 $this->{name} = $name; | |
84 $this->{description} = $desc; | |
85 | |
86 return $this; | |
87 } | |
88 | |
89 #compatibility with TToolkit | |
90 | |
91 sub Id { | |
92 $_[0]->{id}; | |
93 } | |
94 | |
95 sub Description { | |
96 $_[0]->{description}; | |
97 } | |
98 | |
99 sub Active { | |
100 $_[0]->{active}; | |
101 } | |
102 | |
103 sub Name { | |
104 $_[0]->{name}; | |
105 } | |
106 | |
107 1; |