event refactor, landing on HEAD!
[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   )
54
55 </%doc>
56 % unless ( $opt{html_only} || $opt{js_only} ) {
57     <SCRIPT TYPE="text/javascript">
58 % }
59 % unless ( $opt{html_only} ) {
60       //alert('start function define');
61       function <% $key %>changed(what) {
62
63         <% $opt{'onchange'} %>
64
65         var <% $key %>layer = what.options[what.selectedIndex].value;
66
67 %       foreach my $layer ( keys %$options ) {
68
69           if (<% $key %>layer == "<% $layer %>" ) {
70
71 %           foreach my $not ( grep { $_ ne $layer } keys %$options ) {
72 %             my $element = "document.getElementById('${key}d$not').style";
73               <% $element %>.display = "none";
74               <% $element %>.zIndex = 0;
75 %           }
76
77 %           my $element = "document.getElementById('${key}d$layer').style";
78             <% $element %>.display = "";
79             <% $element %>.zIndex = 1;
80
81           }
82 %       }
83
84         //<% $opt{'onchange'} %>
85
86       }
87       //alert('end function define');
88 % }
89 % unless ( $opt{html_only} || $opt{js_only} ) {
90     </SCRIPT>
91 % }
92 %
93 % unless ( $opt{js_only} ) {
94
95     <SELECT NAME          = "<% $key %>"
96             ID            = "<% $key %>"
97             previousValue = "<% $selected %>"
98             previousText  = "<% $options{$selected} %>"
99             onChange="<% $key %>changed(this);"
100     >
101
102 %     foreach my $option ( keys %$options ) {
103
104         <OPTION VALUE="<% $option %>"
105                 <% $option eq $selected ? ' SELECTED' : '' %>
106         ><% $options->{$option} %></OPTION>
107
108 %     }
109
110     </SELECT>
111
112 <% ref($between) ? &{$between}($key) : $between %>
113
114 %   foreach my $layer ( keys %$options ) {
115
116       <DIV ID="<% $key %>d<% $layer %>"
117            STYLE="<% $layer eq $selected
118                        ? 'display: ""  ; z-index: 1'
119                        : 'display: none; z-index: 0'
120                   %>"
121       >
122
123         <% layer_callback($layer, $layer_fields, $layer_values, $layer_prefix) %>
124
125       </DIV>
126
127 %   }
128
129 % }
130 <%once>
131
132 my $conf = new FS::Conf;
133 my $money_char = $conf->config('money_char') || '$';
134
135 </%once>
136 <%init>
137
138 my %opt = @_;
139
140 #use Data::Dumper;
141 #warn Dumper(%opt);
142
143 my $key = $opt{field}; # || 'generate_one' #?
144
145 tie my %options, 'Tie::IxHash',
146    map { $_ => $opt{'labels'}->{$_} }
147        @{ $opt{'options'} }; #just arrayref for now
148
149 my $between = exists($opt{html_between}) ? $opt{html_between} : '';
150 my $options = \%options;
151
152 my $selected = exists($opt{curr_value}) ? $opt{curr_value} : '';
153
154 #XXX eek.  also eek $layer_fields in the layer_callback() call...
155 my $layer_fields = $opt{layer_fields};
156 my $layer_values = $opt{layer_values};
157 my $layer_prefix = $opt{layer_prefix};
158
159 sub layer_callback {
160   my( $layer, $layer_fields, $layer_values, $layer_prefix ) = @_;
161
162   return  '' unless $layer && exists $layer_fields->{$layer};
163   tie my %fields, 'Tie::IxHash', @{ $layer_fields->{$layer} };
164
165   #XXX this should become an element itself... (false laziness w/edit.html)
166   # but at least all the elements inside are the shared mason elements now
167
168   return '' unless keys %fields;
169   my $html = "<TABLE>";
170
171   foreach my $field ( keys %fields ) {
172
173     my $lf = ref($fields{$field})
174                ? $fields{$field}
175                : { 'label'=>$fields{$field} };
176
177     my $value = $layer_values->{$layer}{$field};
178
179     my $type = $lf->{type} || 'text';
180
181     my $include = $type;
182     $include = "input-$include" if $include =~ /^(text|money)$/;
183     $include = "tr-$include" unless $include eq 'hidden';
184
185     $html .= include( "/elements/$include.html",
186                         %$lf,
187                         'field'      => "$layer_prefix$field",
188                         'id'         => "$layer_prefix$field", #separate?
189                         #don't want field0_label0...?
190                         'label_id'   => $layer_prefix.$field."_label",
191
192                         'value'      => ( $lf->{'value'} || $value ), #hmm.
193                         'curr_value' => $value,
194                     );
195
196   }
197   $html .= '</TABLE>';
198   return $html;
199 }
200
201 </%init>