summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-state.html
blob: 2d60fde0f0f031ae02201e58b3f5ec5883a7b4b5 (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
<%doc>

Example:

  include( '/elements/select-state.html',
    #recommended
    country    => $current_country,
    state      => $current_state,

    #optional
    prefix        => $optional_unique_prefix,
    onchange      => $javascript,
    disabled      => 0, #bool
    disable_empty => 1, #defaults to 1, disable the empty option
    empty_label   => 'all', #label for empty option
    disable_countyupdate => 0, #bool - disabled update of the select-state.html
    style         => [ 'attribute:value', 'another:value' ],
  );

</%doc>

<SELECT NAME     = "<% $pre %>state"
        ID       = "<% $pre %>state"
        onChange = "<% $onchange %>"
        <% $opt{'disabled'} %>
        <% $style %>
>

% unless ( $opt{'disable_empty'} ) {
  <OPTION VALUE=""<% $opt{state} eq '' ? ' SELECTED' : '' %>><% $opt{empty_label} %>
% }

% foreach my $state ( keys %states ) { 

  <OPTION VALUE="<% $state |h %>"<% $state eq $opt{'state'} ? ' SELECTED' : '' %>><% $states{$state} || '(n/a)' |h %>

% } 


</SELECT>

<%init>

my %opt = @_;
foreach my $opt (qw( state country prefix onchange disabled empty_label )) {
  $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
}

$opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});

my $pre = $opt{'prefix'};

my $onchange =
  ( $opt{'disable_countyupdate'} ? '' : $pre.'state_changed(this); ' ).
  $opt{'onchange'};

$opt{'style'} ||= [];
my $style =
  scalar(@{$opt{style}})
    ? 'STYLE="'. join(';', @{$opt{style}}). '"'
    : '';

tie my %states, 'Tie::IxHash', states_hash( $opt{'country'} ); 

</%init>