summaryrefslogtreecommitdiff
path: root/httemplate/elements/selectlayers.html
blob: 82f5dd1a74144171c9b31d21dbbc9975fe8e8839 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<%doc>

Example:

  include( '/elements/selectlayers.html',
    'field'        => $key, # SELECT element NAME (passed as form field)
                            # also used as ID and a unique key for layers and
                            # functions
    'curr_value'   => $selected_layer,
    'options'      => [ 'option1', 'option2' ],
    'labels'       => { 'option1' => 'Option 1 Label',
                        'option2' => 'Option 2 Label',
                      },

    #XXX put this handling it its own selectlayers-fields.html element?
    'layer_prefix' => 'prefix_', #optional prefix for fieldnames
    'layer_fields' => [ 'layer'  => [ 'fieldname',
                                      { label => 'fieldname2',
                                        type  => 'text', #implemented:
                                                         # text, money, fixed,
                                                         # hidden, checkbox,
                                                         # checkbox-multiple,
                                                         # select, select-agent,
                                                         # select-pkg_class,
                                                         # select-part_referral,
                                                         # select-table,
                                                         #XXX tbd:
                                                         # more?
                                      },
                                      ...
                                    ],
                        'layer2' => [ 'l2fieldname',
                                      ...
                                    ],

    #current values for layer fields above
    'layer_values' => { 'layer'  => { 'fieldname'  => 'current_value',
                                      'fieldname2' => 'field2value',
                                      ...
                                    },
                        'layer2' => { 'l2fieldname' => 'l2value',
                                      ...
                                    },
                        ...
                      },

    #or manual control, instead of layer_fields and layer_values above
    #called with args: my( $layer, $layer_fields, $layer_values, $layer_prefix )
    'layer_callback' => 

    'html_between  => '', #optional HTML displayed between the SELECT and the
                          #layers, scalar or coderef ('field' passed as a param)
    'onchange'     => '', #javascript code run when the SELECT changes
                          # ("what" is the element)
    'js_only'      => 0, #set true to return only the JS portions
    'html_only'    => 0, #set true to return only the HTML portions
    'select_only'  => 0, #set true to return only the <SELECT> HTML
    'layers_only'  => 0, #set true to return only the layers <DIV> HTML
  )

</%doc>
% unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
    <SCRIPT TYPE="text/javascript">
% }
% unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
      //alert('start function define');
      function <% $key %>changed(what) {

        <% $opt{'onchange'} %>

        var <% $key %>layer = what.options[what.selectedIndex].value;

%       foreach my $layer ( keys %$options ) {

          if (<% $key %>layer == "<% $layer %>" ) {

%           foreach my $not ( grep { $_ ne $layer } keys %$options ) {
%             my $element = "document.getElementById('${key}d$not').style";
              <% $element %>.display = "none";
              <% $element %>.zIndex = 0;
%           }

%           my $element = "document.getElementById('${key}d$layer').style";
            <% $element %>.display = "";
            <% $element %>.zIndex = 1;

          }
%       }

        //<% $opt{'onchange'} %>

      }
      //alert('end function define');
% }
% unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
    </SCRIPT>
% }
%
% unless ( grep $opt{$_}, qw(js_only layers_only) ) {

    <SELECT NAME          = "<% $key %>"
            ID            = "<% $key %>"
            previousValue = "<% $selected %>"
            previousText  = "<% $options{$selected} %>"
            onChange="<% $key %>changed(this);"
    >

%     foreach my $option ( keys %$options ) {

        <OPTION VALUE="<% $option %>"
                <% $option eq $selected ? ' SELECTED' : '' %>
        ><% $options->{$option} %></OPTION>

%     }

    </SELECT>

% }
% unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {

<% ref($between) ? &{$between}($key) : $between %>

% }
%
% unless ( grep $opt{$_}, qw(js_only select_only) ) {

%   foreach my $layer ( keys %$options ) {

      <DIV ID="<% $key %>d<% $layer %>"
           STYLE="<% $layer eq $selected
                       ? 'display: ""  ; z-index: 1'
                       : 'display: none; z-index: 0'
                  %>"
      >

        <% &{$layer_callback}($layer, $layer_fields, $layer_values, $layer_prefix) %>

      </DIV>

%   }

% }
<%once>

my $conf = new FS::Conf;
my $money_char = $conf->config('money_char') || '$';

</%once>
<%init>

my %opt = @_;

#use Data::Dumper;
#warn Dumper(%opt);

my $key = $opt{field}; # || 'generate_one' #?

tie my %options, 'Tie::IxHash',
   map { $_ => $opt{'labels'}->{$_} }
       @{ $opt{'options'} }; #just arrayref for now

my $between = exists($opt{html_between}) ? $opt{html_between} : '';
my $options = \%options;

my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';

#XXX eek.  also eek $layer_fields in the layer_callback() call...
my $layer_fields = $opt{layer_fields};
my $layer_values = $opt{layer_values};
my $layer_prefix = $opt{layer_prefix};

my $layer_callback = $opt{layer_callback} || \&layer_callback;

sub layer_callback {
  my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;

  return  '' unless $layer && exists $layer_fields->{$layer};
  tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };

  #XXX this should become an element itself... (false laziness w/edit.html)
  # but at least all the elements inside are the shared mason elements now

  return '' unless keys %fields;
  my $html = "<TABLE>";

  foreach my $field ( keys %fields ) {

    my $lf = ref($fields{$field})
               ? $fields{$field}
               : { 'label'=>$fields{$field} };

    my $value = $layer_values->{$layer}{$field};

    my $type = $lf->{type} || 'text';

    my $include = $type;
    $include = "input-$include" if $include =~ /^(text|money)$/;
    $include = "tr-$include" unless $include eq 'hidden';

    $html .= include( "/elements/$include.html",
                        %$lf,
                        'field'      => "$layer_prefix$field",
                        'id'         => "$layer_prefix$field", #separate?
                        #don't want field0_label0...?
                        'label_id'   => $layer_prefix.$field."_label",

                        'value'      => ( $lf->{'value'} || $value ), #hmm.
                        'curr_value' => $value,
                    );

  }
  $html .= '</TABLE>';
  return $html;
}

</%init>