-
Notifications
You must be signed in to change notification settings - Fork 1
/
depage-address.js
156 lines (140 loc) · 4.21 KB
/
depage-address.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
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
/**
* @require framework/shared/jquery-1.8.3.js
*
* @file depage-address
*
* Depage Address plugin to handle client-side address fields.
*
* copyright (c) 2006-2012 Frank Hellenkamp [[email protected]]
*
* @author Ben Wallis
*/
(function($){
if(!$.depage){
$.depage = {};
};
/**
* datalist
*
* @param el - file input
* @param index
* @param options
*/
$.depage.address = function(el, index, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.address", base);
var $state = null;
var $country = null;
// {{{ init
/**
* Init
*
* Get the plugin options.
*
* @return void
*/
base.init = function(){
base.options = $.extend({}, $.depage.address.defaultOptions, options);
$country = $(base.options.country_selector, base.$el);
$state = $(base.options.state_selector, base.$el);
if ($country.length == 0 || $state.length == 0) {
return;
}
base.address();
};
// }}}
// {{{ address()
/**
* address
*
* @return void
*/
base.address = function(){
if (typeof $.fn.selectize !== 'undefined') {
base.addressSelectized();
} else {
base.addressHtml();
}
};
/// }}}
// {{{ addressHtml()
/**
* address
*
* @return void
*/
base.addressHtml = function(){
var $optgroups = $state.find('optgroup');
var filter = function() {
$optgroup = $optgroups.filter("[label='" + $country.val() + "']");
if ($optgroup.length){
$(base.options.state_selector, base.$el).html($optgroup);
$state.parents('.skin-select').show();
} else {
$state.val([]);
$state.parents('.skin-select').hide();
}
};
$country.change(filter);
filter();
};
/// }}}
// {{{ addressSelectized()
/**
* address
*
* @return void
*/
base.addressSelectized = function(){
var oldState = $state.val();
var stateSel = $state[0].selectize;
var allStates = JSON.parse(JSON.stringify(stateSel.options));
var filter = function() {
var country = $country.val();
stateSel.clearOptions();
if (country == "") {
return;
}
for (var s in allStates) {
if (allStates[s].optgroup == country) {
stateSel.addOption(allStates[s]);
}
}
if (Object.keys(stateSel.options).length > 0) {
stateSel.addItem(oldState, false);
$state.parents('.skin-select').show();
} else {
$state.parents('.skin-select').hide();
}
};
$country.change(filter);
filter();
};
/// }}}
base.init();
};
// }}}
/**
* Default Options
*
* city_selector - jQuery selector for the city element
* state_selector - jQuery selector for the state element
* country_selector - jQuery selector for the country element
*/
$.depage.address.defaultOptions = {
city_selector : 'input[name="city"]',
state_selector : 'select[name="state"]',
country_selector : 'select[name="country"]'
};
$.fn.depageAddress = function(options){
return this.each(function(index){
(new $.depage.address(this, index, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */