-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cityentry.js
131 lines (108 loc) · 3.67 KB
/
jquery.cityentry.js
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
/* $Id: jquery.cityentry.js,v 1.4 2014/05/19 18:07:29 caran Exp $ */
/* global jQuery */
/* global ma_cities */
/* global nh_cities */
(function( $ ) {
'use strict';
$.fn.cityentry = function( initState, initCity ) {
var $cityentryObj = this;
$cityentryObj.children( 'select.city_list' ).chosen();
// Define what to do with cities, then call it
$cityentryObj.children( 'select.state_list' ).chosen().change( function() {
var state = $( this ).val();
var city_list = '';
if (state === 'MA') {
city_list = ma_cities;
}
else if (state === 'ME') {
city_list = me_cities;
}
else if (state === 'NH') {
city_list = nh_cities;
}
if (city_list) {
$( this ).siblings( 'input.city_input' ).val('').hide();
load_cities( $cityentryObj, city_list );
$( this ).siblings( 'select.city_list' ).next( '.chosen-container' ).show();
}
else {
$( this ).siblings( 'input.city_input' ).show();
$( this ).siblings( 'select.city_list' ).next( '.chosen-container' ).hide();
}
$( this ).trigger( 'chosen:updated' );
$( this ).siblings( 'select.city_list' ).trigger( 'chosen:updated' );
}).change();
validation_rules( $cityentryObj );
init_state_city( $cityentryObj, initState, initCity );
return this;
};
function init_state_city( $obj, state, city )
{
if ($obj === null || !state) {
return false;
}
$obj.children( 'select.state_list' ).val( state ).change();
if (state === 'MA' || state == 'ME' || state === 'NH') {
$obj.children( 'select.city_list' ).val( city ).change().trigger( 'chosen:updated' );
}
else {
$obj.children( 'input.city_input' ).val( city ).change();
}
}
function is_ma_nh( element )
{
var value = $( element ).siblings( 'select.state_list' ).val();
return (value === 'MA' || state == 'ME' || value === 'NH');
}
function load_cities( cityentryObj, list )
{
var $citylist = $( cityentryObj ).children( '.city_list' );
var option_html = [ '<option value=""></option>' ];
var disabled = false;
for (var i = 0; i < list.length; i++) {
option_html.push( '<option>', list[i], '</option>' );
}
if (!option_html.length) {
option_html.push( '<option>Non MA/ME/NH Location</option>' );
disabled = true;
}
$citylist.html( option_html.join( '' ) ).prop( 'disabled', disabled );
}
function validation_rules( $obj )
{
// Set-up validation rules
$obj.children( 'select.city_list' ).rules( 'add', {
required: {
depends: function( element ) {
return !$( element ).attr( 'disabled' ) && is_ma_nh( element );
}
}
});
$obj.children( 'input.city_input' ).rules( 'add', {
required: {
depends: function( element ) {
return !$( element ).attr( 'disabled' ) && !is_ma_nh( element );
}
}
});
$obj.children( 'select.state_list' ).rules( 'add', {
required: {
depends: function( element ) {
return !$( element ).attr( 'disabled' );
}
}
});
$obj.children( 'select.city_list' ).change( function() {
$( this ).valid();
});
$obj.children( '.city_list' ).each( function() {
$( this )[0].validate_highlight = function( element, highlight, errorClass ) {
// Highlight the chosen field (which is next) instead of the select
$( element ).next( '.chosen-container' ).toggleClass( errorClass, highlight );
};
});
// Used to validate chosen selects
$obj.parents( 'form' ).validate().settings.ignore =
':hidden:not(select), .chosen-search input';
}
}( jQuery ));