add a tr- for just the select bit of a selectlayers...
[freeside.git] / httemplate / elements / selectlayers.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/selectlayers.html',
6     'field'        => $key, # SELECT element NAME (passed as form field)
7                             # also used as ID and a unique key for layers and
8                             # functions
9     'curr_value'   => $selected_layer,
10     'options'      => [ 'option1', 'option2' ],
11     'labels'       => { 'option1' => 'Option 1 Label',
12                         'option2' => 'Option 2 Label',
13                       },
14
15     #XXX put this handling it its own selectlayers-fields.html element?
16     'layer_prefix' => 'prefix_', #optional prefix for fieldnames
17     'layer_fields' => [ 'layer'  => [ 'fieldname',
18                                       { label => 'fieldname2',
19                                         type  => 'text', #implemented:
20                                                          # text, money, fixed,
21                                                          # hidden, checkbox,
22                                                          # checkbox-multiple,
23                                                          # select, select-agent,
24                                                          # select-pkg_class,
25                                                          # select-part_referral,
26                                                          # select-table,
27                                                          #XXX tbd:
28                                                          # more?
29                                       },
30                                       ...
31                                     ],
32                         'layer2' => [ 'l2fieldname',
33                                       ...
34                                     ],
35
36     #current values for layer fields above
37     'layer_values' => { 'layer'  => { 'fieldname'  => 'current_value',
38                                       'fieldname2' => 'field2value',
39                                       ...
40                                     },
41                         'layer2' => { 'l2fieldname' => 'l2value',
42                                       ...
43                                     },
44                         ...
45                       },
46
47     'html_between  => '', #optional HTML displayed between the SELECT and the
48                           #layers, scalar or coderef ('field' passed as a param)
49     'onchange'     => '', #javascript code run when the SELECT changes
50                           # ("what" is the element)
51     'js_only'      => 0, #set true to return only the JS portions
52     'html_only'    => 0, #set true to return only the HTML portions
53     'select_only'  => 0, #set true to return only the <SELECT> HTML
54     'layers_only'  => 0, #set true to return only the layers <DIV> HTML
55   )
56
57 </%doc>
58 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
59     <SCRIPT TYPE="text/javascript">
60 % }
61 % unless ( grep $opt{$_}, qw(html_only select_only layers_only) ) {
62       //alert('start function define');
63       function <% $key %>changed(what) {
64
65         <% $opt{'onchange'} %>
66
67         var <% $key %>layer = what.options[what.selectedIndex].value;
68
69 %       foreach my $layer ( keys %$options ) {
70
71           if (<% $key %>layer == "<% $layer %>" ) {
72
73 %           foreach my $not ( grep { $_ ne $layer } keys %$options ) {
74 %             my $element = "document.getElementById('${key}d$not').style";
75               <% $element %>.display = "none";
76               <% $element %>.zIndex = 0;
77 %           }
78
79 %           my $element = "document.getElementById('${key}d$layer').style";
80             <% $element %>.display = "";
81             <% $element %>.zIndex = 1;
82
83           }
84 %       }
85
86         //<% $opt{'onchange'} %>
87
88       }
89       //alert('end function define');
90 % }
91 % unless ( grep $opt{$_}, qw(html_only js_only select_only layers_only) ) {
92     </SCRIPT>
93 % }
94 %
95 % unless ( grep $opt{$_}, qw(js_only layers_only) ) {
96
97     <SELECT NAME          = "<% $key %>"
98             ID            = "<% $key %>"
99             previousValue = "<% $selected %>"
100             previousText  = "<% $options{$selected} %>"
101             onChange="<% $key %>changed(this);"
102     >
103
104 %     foreach my $option ( keys %$options ) {
105
106         <OPTION VALUE="<% $option %>"
107                 <% $option eq $selected ? ' SELECTED' : '' %>
108         ><% $options->{$option} %></OPTION>
109
110 %     }
111
112     </SELECT>
113
114 % }
115 % unless ( grep $opt{$_}, qw(js_only select_only layers_only) ) {
116
117 <% ref($between) ? &{$between}($key) : $between %>
118
119 % }
120 %
121 % unless ( grep $opt{$_}, qw(js_only select_only) ) {
122
123 %   foreach my $layer ( keys %$options ) {
124
125       <DIV ID="<% $key %>d<% $layer %>"
126            STYLE="<% $layer eq $selected
127                        ? 'display: ""  ; z-index: 1'
128                        : 'display: none; z-index: 0'
129                   %>"
130       >
131
132         <% layer_callback($layer, $layer_fields, $layer_values, $layer_prefix) %>
133
134       </DIV>
135
136 %   }
137
138 % }
139 <%once>
140
141 my $conf = new FS::Conf;
142 my $money_char = $conf->config('money_char') || '$';
143
144 </%once>
145 <%init>
146
147 my %opt = @_;
148
149 #use Data::Dumper;
150 #warn Dumper(%opt);
151
152 my $key = $opt{field}; # || 'generate_one' #?
153
154 tie my %options, 'Tie::IxHash',
155    map { $_ => $opt{'labels'}->{$_} }
156        @{ $opt{'options'} }; #just arrayref for now
157
158 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
159 my $options = \%options;
160
161 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
162
163 #XXX eek.  also eek $layer_fields in the layer_callback() call...
164 my $layer_fields = $opt{layer_fields};
165 my $layer_values = $opt{layer_values};
166 my $layer_prefix = $opt{layer_prefix};
167
168 sub layer_callback {
169   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
170
171   return  '' unless $layer && exists $layer_fields->{$layer};
172   tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
173
174   #XXX this should become an element itself... (false laziness w/edit.html)
175   # but at least all the elements inside are the shared mason elements now
176
177   return '' unless keys %fields;
178   my $html = "<TABLE>";
179
180   foreach my $field ( keys %fields ) {
181
182     my $lf = ref($fields{$field})
183                ? $fields{$field}
184                : { 'label'=>$fields{$field} };
185
186     my $value = $layer_values->{$layer}{$field};
187
188     my $type = $lf->{type} || 'text';
189
190     my $include = $type;
191     $include = "input-$include" if $include =~ /^(text|money)$/;
192     $include = "tr-$include" unless $include eq 'hidden';
193
194     $html .= include( "/elements/$include.html",
195                         %$lf,
196                         'field'      => "$layer_prefix$field",
197                         'id'         => "$layer_prefix$field", #separate?
198                         #don't want field0_label0...?
199                         'label_id'   => $layer_prefix.$field."_label",
200
201                         'value'      => ( $lf->{'value'} || $value ), #hmm.
202                         'curr_value' => $value,
203                     );
204
205   }
206   $html .= '</TABLE>';
207   return $html;
208 }
209
210 </%init>