distpicker.js
6.0 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*!
* Distpicker v1.0.4
* https://github.com/fengyuanchen/distpicker
*
* Copyright (c) 2014-2016 Fengyuan Chen
* Released under the MIT license
*
* Date: 2016-06-01T15:05:52.606Z
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module.
define(['jquery', 'ChineseDistricts'], factory);
} else if (typeof exports === 'object') {
// Node / CommonJS
factory(require('jquery'), require('ChineseDistricts'));
} else {
// Browser globals.
factory(jQuery, ChineseDistricts);
}
})(function ($, ChineseDistricts) {
'use strict';
if (typeof ChineseDistricts === 'undefined') {
throw new Error('The file "distpicker.data.js" must be included first!');
}
var NAMESPACE = 'distpicker';
var EVENT_CHANGE = 'change.' + NAMESPACE;
var PROVINCE = 'province';
var CIRY = 'city';
var DISTRICT = 'district';
function Distpicker(element, options) {
this.$element = $(element);
this.options = $.extend({}, Distpicker.DEFAULTS, $.isPlainObject(options) && options);
this.placeholders = $.extend({}, Distpicker.DEFAULTS);
this.active = false;
this.init();
}
Distpicker.prototype = {
constructor: Distpicker,
init: function () {
var options = this.options;
var $select = this.$element.find('select');
var length = $select.length;
var data = {};
$select.each(function () {
$.extend(data, $(this).data());
});
$.each([PROVINCE, CIRY, DISTRICT], $.proxy(function (i, type) {
if (data[type]) {
options[type] = data[type];
this['$' + type] = $select.filter('[data-' + type + ']');
} else {
this['$' + type] = length > i ? $select.eq(i) : null;
}
}, this));
this.bind();
// Reset all the selects (after event binding)
this.reset();
this.active = true;
},
bind: function () {
if (this.$province) {
this.$province.on(EVENT_CHANGE, (this._changeProvince = $.proxy(function () {
this.output(CIRY);
this.output(DISTRICT);
}, this)));
}
if (this.$city) {
this.$city.on(EVENT_CHANGE, (this._changeCity = $.proxy(function () {
this.output(DISTRICT);
}, this)));
}
},
unbind: function () {
if (this.$province) {
this.$province.off(EVENT_CHANGE, this._changeProvince);
}
if (this.$city) {
this.$city.off(EVENT_CHANGE, this._changeCity);
}
},
output: function (type) {
var options = this.options;
var placeholders = this.placeholders;
var $select = this['$' + type];
var districts = {};
var data = [];
var code;
var matched;
var value;
if (!$select || !$select.length) {
return;
}
value = options[type];
code = (
type === PROVINCE ? 86 :
type === CIRY ? this.$province && this.$province.find(':selected').data('code') :
type === DISTRICT ? this.$city && this.$city.find(':selected').data('code') : code
);
districts = $.isNumeric(code) ? ChineseDistricts[code] : null;
if ($.isPlainObject(districts)) {
$.each(districts, function (code, address) {
var selected = address === value;
if (selected) {
matched = true;
}
data.push({
code: code,
address: address,
selected: selected
});
});
}
if (!matched) {
if (data.length && (options.autoSelect || options.autoselect)) {
data[0].selected = true;
}
// Save the unmatched value as a placeholder at the first output
if (!this.active && value) {
placeholders[type] = value;
}
}
// Add placeholder option
if (options.placeholder) {
data.unshift({
code: '',
address: placeholders[type],
selected: false
});
}
$select.html(this.getList(data));
},
getList: function (data) {
var list = [];
$.each(data, function (i, n) {
list.push(
'<option' +
' value="' + (n.address && n.code ? n.address : '') + '"' +
' data-code="' + (n.code || '') + '"' +
(n.selected ? ' selected' : '') +
'>' +
(n.address || '') +
'</option>'
);
});
return list.join('');
},
reset: function (deep) {
if (!deep) {
this.output(PROVINCE);
this.output(CIRY);
this.output(DISTRICT);
} else if (this.$province) {
this.$province.find(':first').prop('selected', true).trigger(EVENT_CHANGE);
}
},
destroy: function () {
this.unbind();
this.$element.removeData(NAMESPACE);
}
};
Distpicker.DEFAULTS = {
autoSelect: true,
placeholder: true,
province: '—— 省 ——',
city: '—— 市 ——',
district: '—— 区 ——'
};
Distpicker.setDefaults = function (options) {
$.extend(Distpicker.DEFAULTS, options);
};
// Save the other distpicker
Distpicker.other = $.fn.distpicker;
// Register as jQuery plugin
$.fn.distpicker = function (option) {
var args = [].slice.call(arguments, 1);
return this.each(function () {
var $this = $(this);
var data = $this.data(NAMESPACE);
var options;
var fn;
if (!data) {
if (/destroy/.test(option)) {
return;
}
options = $.extend({}, $this.data(), $.isPlainObject(option) && option);
$this.data(NAMESPACE, (data = new Distpicker(this, options)));
}
if (typeof option === 'string' && $.isFunction(fn = data[option])) {
fn.apply(data, args);
}
});
};
$.fn.distpicker.Constructor = Distpicker;
$.fn.distpicker.setDefaults = Distpicker.setDefaults;
// No conflict
$.fn.distpicker.noConflict = function () {
$.fn.distpicker = Distpicker.other;
return this;
};
$(function () {
$('[data-toggle="distpicker"]').distpicker();
});
});