~ubuntu-branches/ubuntu/lucid/libebox/lucid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<%args>
$tableName
$data
$cssClass => ''
$disabled => undef
</%args>
<%init>
  use Data::Dumper;
use Perl6::Junction qw(any);

#########################
# EVIL HACK
#########################
# This is done here, since it makes a deep recursion error on Select
# type class at options method. This is due to rows implementation
# which always ask for printableValues and it is not possible to ask
# for value rows. This does not work when the edit/add will be
# generated by JavaScript.
#
# We are very ashamed about this evil hack, we want to apologise for
# that. Time constraints make us do this crap. Sorry.

my $options = $data->options();
my @filteredOptions;
my @optionsAlreadyModel = ();

if ( $data->unique() ) {
    # Filter the options in other just to show the ones allowed
    my $model = $data->model();

    if ( defined ( $model )) {
        my $field  = $data->fieldName();

        foreach my $id (@{$model->ids()}) {
            # Check if the element exists to avoid nasty issues
            # with union types
	    my $row = $model->row($id);
            next unless ($row->elementByName($field));
            push( @optionsAlreadyModel,
                  $row->valueByName($field) );
        }

        # Difference among optionsAlreadyModel and options arrays
        @filteredOptions = grep { ! ($_->{value} eq any(@optionsAlreadyModel)) } @{$options};

        # Add the current value if the action is an edition
        if ( $data->isValueSet() ) {
            push ( @filteredOptions, {
                                      value => $data->value(),
                                      printableValue => $data->printableValue(),
                                     }
               );
            @filteredOptions = sort { $a->{value} cmp $b->{value} } ( @filteredOptions );
        }

        $options = \@filteredOptions;
    }
}
</%init>
% my $id = $tableName . '_' . $data->fieldName();
% # my $onChange = "eval('OnChangeOn$id(this)')";
% my $onChange = "OnChangeOn$id(this)";
<& /input/select.mas,
   'name'    => "$id" ,
   'value'   => $data->value(),
   'options' => $options,
   'id'      => "$id",
   'class'   => "$cssClass",
   'disabled' => $disabled,
&>
<% $data->trailingText() %>