city_picker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 92a6471dbed552f1c5ca28b52c64f6f71d6ba2402b3096d14c6e9cac6e271c54
4
+ data.tar.gz: 7fff61be12750734c0a45f163fdaab5fb6bb28371004ebd8cfe5c2001f2c8365
5
+ SHA512:
6
+ metadata.gz: 135742e929bebc1d7ce33cd9b6077fd1c09f517465cba2be28f14178be29ac8dbaf2d06f20ba609484f3f8373f1686c11defff225c46004a4106c74be1c59ddb
7
+ data.tar.gz: 0ade2393813a15cb0308d7bf2ccc94adad55e3f5173783215fbb07f2357b9f3664225814f98a6c1b1f7594d0c3112b136980af5ce0bb09c0e03477c15aecec0a
@@ -0,0 +1,20 @@
1
+ Copyright 2018 doabit
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ # CityPicker
2
+ 中国省市区街道四级联地址选择,js代码来自 [city-picker](https://github.com/huangchanghuan/city-picker), 数据来自 [Administrative-divisions-of-China](https://github.com/modood/Administrative-divisions-of-China)
3
+
4
+ ## 安装
5
+
6
+ ### Gemfile
7
+
8
+ ```ruby
9
+ gem 'city_picker'
10
+ ```
11
+
12
+ ### app/assets/javascripts/application.js
13
+
14
+ ```javascript
15
+ //= require city_picker/city_picker
16
+ ```
17
+
18
+ ### app/assets/stylesheets/application.css
19
+
20
+ ```javascript
21
+ *= require city_picker/city_picker
22
+ ```
23
+
24
+ config/routes.rb
25
+ ```ruby
26
+ mount CityPicker::Engine => "/city_picker"
27
+ ```
28
+
29
+ ## 使用
30
+
31
+ 在页面中加入
32
+
33
+ ```html
34
+ <div style="position: relative;">
35
+ <!-- container -->
36
+ <input readonly type="text" placeholder="请选择省/市" id="target" style="width:100%">
37
+ </div>
38
+ ```
39
+
40
+ ```javascript
41
+ <script>
42
+ $(function() {
43
+ $('#target').citypicker({
44
+ responsive: true,
45
+ simple: true,
46
+ // level: 'district'
47
+ // addrElm: '#addrValue1'
48
+ });
49
+
50
+ $('#target').on('cp:updated', function(e) {
51
+ console.log($(this).val())
52
+ })
53
+ })
54
+ </script>
55
+ ```
56
+
57
+ 如果要在选择后取得城市code,可在页面添加
58
+
59
+ ```html
60
+ <input type="text" id="addrValue">
61
+ ```
62
+
63
+ 或者 添加初始化选项 `addrElm: '#my_address_elm`
64
+
65
+ ```html
66
+ <input type="text" id="my_address_elm">
67
+ ```
68
+
69
+
70
+ ## Contributing
71
+ Contribution directions go here.
72
+
73
+ ## License
74
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'CityPicker'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/city_picker .js
2
+ //= link_directory ../stylesheets/city_picker .css
@@ -0,0 +1,693 @@
1
+ /*!
2
+ * CityPicker v@VERSION
3
+ * https://github.com/tshi0912/citypicker
4
+ *
5
+ * Copyright (c) 2015-@YEAR Tao Shi
6
+ * Released under the MIT license
7
+ *
8
+ * Date: @DATE
9
+ */
10
+
11
+ ChineseDistricts={
12
+ 86: {
13
+ 'A-G': [
14
+ { code: '34', address: '安徽省' },
15
+ { code: '11', address: '北京市' },
16
+ { code: '50', address: '重庆市' },
17
+ { code: '35', address: '福建省' },
18
+ { code: '62', address: '甘肃省' },
19
+ { code: '44', address: '广东省' },
20
+ { code: '45', address: '广西壮族自治区' },
21
+ { code: '52', address: '贵州省' }],
22
+ 'H-K': [
23
+ { code: '46', address: '海南省' },
24
+ { code: '13', address: '河北省' },
25
+ { code: '23', address: '黑龙江省' },
26
+ { code: '41', address: '河南省' },
27
+ { code: '42', address: '湖北省' },
28
+ { code: '43', address: '湖南省' },
29
+ { code: '32', address: '江苏省' },
30
+ { code: '36', address: '江西省' },
31
+ { code: '22', address: '吉林省' }],
32
+ 'L-S': [
33
+ { code: '21', address: '辽宁省' },
34
+ { code: '15', address: '内蒙古自治区' },
35
+ { code: '64', address: '宁夏回族自治区' },
36
+ { code: '63', address: '青海省' },
37
+ { code: '37', address: '山东省' },
38
+ { code: '31', address: '上海市' },
39
+ { code: '14', address: '山西省' },
40
+ { code: '61', address: '陕西省' },
41
+ { code: '51', address: '四川省' }],
42
+ 'T-Z': [
43
+ { code: '12', address: '天津市' },
44
+ { code: '65', address: '新疆维吾尔自治区' },
45
+ { code: '54', address: '西藏自治区' },
46
+ { code: '53', address: '云南省' },
47
+ { code: '33', address: '浙江省' }]
48
+ },
49
+ };
50
+
51
+ (function (factory) {
52
+ if (typeof define === 'function' && define.amd) {
53
+ // AMD. Register as anonymous module.
54
+ define(['jquery', 'ChineseDistricts'], factory);
55
+ } else if (typeof exports === 'object') {
56
+ // Node / CommonJS
57
+ factory(require('jquery'), require('ChineseDistricts'));
58
+ } else {
59
+ // Browser globals.
60
+ factory(jQuery, ChineseDistricts);
61
+ }
62
+ })(function ($, ChineseDistricts) {
63
+
64
+ 'use strict';
65
+ if (typeof ChineseDistricts === 'undefined') {
66
+ throw new Error('The file "city-picker.data.js" must be included first!');
67
+ }
68
+ var NAMESPACE = 'citypicker';
69
+ var EVENT_CHANGE = 'change.' + NAMESPACE;
70
+ var PROVINCE = 'province';
71
+ var CITY = 'city';
72
+ var DISTRICT = 'district';
73
+ var COUNTY = 'county';
74
+
75
+ function CityPicker(element, options) {
76
+ this.$element = $(element);
77
+ this.$dropdown = null;
78
+ this.options = $.extend({}, CityPicker.DEFAULTS, $.isPlainObject(options) && options);
79
+ this.active = false;
80
+ this.dems = [];
81
+ this.needBlur = false;
82
+ this.init();
83
+ }
84
+
85
+ CityPicker.prototype = {
86
+ constructor: CityPicker,
87
+
88
+ init: function () {
89
+
90
+ this.defineDems();
91
+
92
+ this.render();
93
+
94
+ this.bind();
95
+
96
+ this.active = true;
97
+ },
98
+ //界面显示处理
99
+ render: function () {
100
+ var p = this.getPosition(),
101
+ placeholder = this.$element.attr('placeholder') || this.options.placeholder,
102
+ textspan = '<span class="city-picker-span" style="' +
103
+ this.getWidthStyle(p.width) + 'height:' +
104
+ p.height + 'px;line-height:' + (p.height - 1) + 'px;">' +
105
+ (placeholder ? '<span class="placeholder">' + placeholder + '</span>' : '') +
106
+ '<span class="title"></span><div class="arrow"></div>' + '</span>',
107
+
108
+ dropdown = '<div class="city-picker-dropdown" style="left:0px;top:100%;' +
109
+ this.getWidthStyle(p.width, true) + '">' +
110
+ '<div class="city-select-wrap">' +
111
+ '<div class="city-select-tab">' +
112
+ '<a class="active" data-count="province">省份</a>' +
113
+ (this.includeDem('city') ? '<a data-count="city">城市</a>' : '') +
114
+ (this.includeDem('district') ? '<a data-count="district">区县</a>' : '') +
115
+ (this.includeDem('county') ? '<a data-count="county">乡镇</a>' : '') +
116
+ '</div>' +
117
+ '<div class="city-select-content">' +
118
+ '<div class="city-select province" data-count="province"></div>' +
119
+ (this.includeDem('city') ? '<div class="city-select city" data-count="city"></div>' : '') +
120
+ (this.includeDem('district') ? '<div class="city-select district" data-count="district"></div>' : '') +
121
+ (this.includeDem('county') ? '<div class="city-select county" data-count="county"></div>' : '') +
122
+ '</div></div>';
123
+
124
+ this.$element.addClass('city-picker-input');
125
+ this.$textspan = $(textspan).insertAfter(this.$element);
126
+ this.$dropdown = $(dropdown).insertAfter(this.$textspan);
127
+ var $select = this.$dropdown.find('.city-select');
128
+
129
+ // setup this.$province, this.$city and/or this.$district object
130
+ $.each(this.dems, $.proxy(function (i, type) {
131
+ this['$' + type] = $select.filter('.' + type + '');
132
+ }, this));
133
+
134
+ this.refresh();
135
+ },
136
+
137
+ refresh: function (force) {
138
+ // clean the data-item for each $select
139
+ var $select = this.$dropdown.find('.city-select');
140
+ $select.data('item', null);
141
+ // parse value from value of the target $element
142
+ var val = this.$element.val() || '';
143
+ val = val.split('/');
144
+ $.each(this.dems, $.proxy(function (i, type) {//遍历dems
145
+ if (val[i] && i < val.length) {
146
+ this.options[type] = val[i];//把当前显示值赋值给options
147
+ } else if (force) {
148
+ this.options[type] = '';
149
+ }
150
+ this.output(type);//输出下拉框显示数据
151
+ }, this));
152
+ this.tab(PROVINCE);
153
+ this.feedText();//界面显示选择的内容
154
+ this.feedVal();//input标签value赋值
155
+ },
156
+ //dems赋值
157
+ defineDems: function () {
158
+ var stop = false;
159
+ $.each([PROVINCE, CITY, DISTRICT,COUNTY], $.proxy(function (i, type) {
160
+ if (!stop) {
161
+ this.dems.push(type);
162
+ }
163
+ if (type === this.options.level) {
164
+ stop = true;
165
+ }
166
+ }, this));
167
+ },
168
+
169
+ includeDem: function (type) {
170
+ return $.inArray(type, this.dems) !== -1;
171
+ },
172
+
173
+ getPosition: function () {
174
+ var p, h, w, s, pw;
175
+ p = this.$element.position();
176
+ s = this.getSize(this.$element);
177
+ h = s.height;
178
+ w = s.width;
179
+ if (this.options.responsive) {
180
+ pw = this.$element.offsetParent().width();
181
+ if (pw) {
182
+ w = w / pw;
183
+ if (w > 0.99) {
184
+ w = 1;
185
+ }
186
+ w = w * 100 + '%';
187
+ }
188
+ }
189
+
190
+ return {
191
+ top: p.top || 0,
192
+ left: p.left || 0,
193
+ height: h,
194
+ width: w
195
+ };
196
+ },
197
+
198
+ getSize: function ($dom) {
199
+ var $wrap, $clone, sizes;
200
+ if (!$dom.is(':visible')) {
201
+ $wrap = $("<div />").appendTo($("body"));
202
+ $wrap.css({
203
+ "position": "absolute !important",
204
+ "visibility": "hidden !important",
205
+ "display": "block !important"
206
+ });
207
+
208
+ $clone = $dom.clone().appendTo($wrap);
209
+
210
+ sizes = {
211
+ width: $clone.outerWidth(),
212
+ height: $clone.outerHeight()
213
+ };
214
+
215
+ $wrap.remove();
216
+ } else {
217
+ sizes = {
218
+ width: $dom.outerWidth(),
219
+ height: $dom.outerHeight()
220
+ };
221
+ }
222
+
223
+ return sizes;
224
+ },
225
+
226
+ getWidthStyle: function (w, dropdown) {
227
+ if (this.options.responsive && !$.isNumeric(w)) {
228
+ return 'width:' + w + ';';
229
+ } else {
230
+ return 'width:' + (dropdown ? Math.max(320, w) : w) + 'px;';
231
+ }
232
+ },
233
+ //绑定事件
234
+ bind: function () {
235
+ var $this = this;
236
+ $(document).on('click', (this._mouteclick = function (e) {
237
+ var $target = $(e.target);
238
+ var $dropdown, $span, $input;
239
+ if ($target.is('.city-picker-span')) {
240
+ $span = $target;
241
+ } else if ($target.is('.city-picker-span *')) {
242
+ $span = $target.parents('.city-picker-span');
243
+ }
244
+ if ($target.is('.city-picker-input')) {
245
+ $input = $target;
246
+ }
247
+ if ($target.is('.city-picker-dropdown')) {
248
+ $dropdown = $target;
249
+ } else if ($target.is('.city-picker-dropdown *')) {
250
+ $dropdown = $target.parents('.city-picker-dropdown');
251
+ }
252
+ if ((!$input && !$span && !$dropdown) ||
253
+ ($span && $span.get(0) !== $this.$textspan.get(0)) ||
254
+ ($input && $input.get(0) !== $this.$element.get(0)) ||
255
+ ($dropdown && $dropdown.get(0) !== $this.$dropdown.get(0))) {
256
+ $this.close(true);
257
+ }
258
+ }));
259
+ this.$element.on('change', (this._changeElement = $.proxy(function () {
260
+ this.close(true);
261
+ this.refresh(true);
262
+ }, this))).on('focus', (this._focusElement = $.proxy(function () {
263
+ this.needBlur = true;
264
+ this.open();
265
+ }, this))).on('blur', (this._blurElement = $.proxy(function () {
266
+ if (this.needBlur) {
267
+ this.needBlur = false;
268
+ this.close(true);
269
+ }
270
+ }, this)));
271
+ this.$textspan.on('click', function (e) {
272
+ var $target = $(e.target), type;
273
+ $this.needBlur = false;
274
+ if ($target.is('.select-item')) {
275
+ type = $target.data('count');
276
+ $this.open(type);
277
+ } else {
278
+ if ($this.$dropdown.is(':visible')) {
279
+ $this.close();
280
+ } else {
281
+ $this.open();
282
+ }
283
+ }
284
+ }).on('mousedown', function () {
285
+ $this.needBlur = false;
286
+ });
287
+ this.$dropdown.on('click', '.city-select a', function () {
288
+ var $select = $(this).parents('.city-select');
289
+ var $active = $select.find('a.active');
290
+ var last = $select.next().length === 0;
291
+ $active.removeClass('active');
292
+ $(this).addClass('active');
293
+ if ($active.data('code') !== $(this).data('code')) {
294
+ $select.data('item', {
295
+ address: $(this).attr('title'), code: $(this).data('code')
296
+ });
297
+ $(this).trigger(EVENT_CHANGE);
298
+ $this.feedText();
299
+ $this.feedVal(true);
300
+ if (last) {
301
+ $this.close();
302
+ }
303
+ }
304
+ }).on('click', '.city-select-tab a', function () {
305
+ if (!$(this).hasClass('active')) {
306
+ var type = $(this).data('count');
307
+ $this.tab(type);
308
+ }
309
+ }).on('mousedown', function () {
310
+ $this.needBlur = false;
311
+ });
312
+ if (this.$province) {
313
+ this.$province.on(EVENT_CHANGE, (this._changeProvince = $.proxy(function () {
314
+ if(this.output(CITY)){//判断下一个tab是否有数据,没有则关闭下拉
315
+ $this.close();
316
+ return;
317
+ };
318
+ this.output(CITY);
319
+ this.output(DISTRICT);
320
+ this.output(COUNTY);
321
+ this.tab(CITY);
322
+ }, this)));
323
+ }
324
+ if (this.$city) {
325
+ this.$city.on(EVENT_CHANGE, (this._changeCity = $.proxy(function () {
326
+ if(this.output(DISTRICT)){
327
+ $this.close();
328
+ return;
329
+ };
330
+ this.output(COUNTY);
331
+ this.tab(DISTRICT);
332
+ }, this)));
333
+ }
334
+
335
+ if (this.$district) {
336
+ this.$district.on(EVENT_CHANGE, (this._changeDistrict = $.proxy(function () {
337
+ if(this.output(COUNTY)){
338
+ $this.close();
339
+ return;
340
+ };
341
+ this.tab(COUNTY);
342
+ }, this)));
343
+ }
344
+ },
345
+ //显示下拉
346
+ open: function (type) {
347
+ type = type || PROVINCE;
348
+ this.$dropdown.show();
349
+ this.$textspan.addClass('open').addClass('focus');
350
+ this.tab(type);
351
+ },
352
+ //关闭下拉
353
+ close: function (blur) {
354
+ this.$dropdown.hide();
355
+ this.$textspan.removeClass('open');
356
+ if (blur) {
357
+ this.$textspan.removeClass('focus');
358
+ }
359
+ },
360
+ //解绑事件
361
+ unbind: function () {
362
+
363
+ $(document).off('click', this._mouteclick);
364
+
365
+ this.$element.off('change', this._changeElement);
366
+ this.$element.off('focus', this._focusElement);
367
+ this.$element.off('blur', this._blurElement);
368
+
369
+ this.$textspan.off('click');
370
+ this.$textspan.off('mousedown');
371
+
372
+ this.$dropdown.off('click');
373
+ this.$dropdown.off('mousedown');
374
+
375
+ if (this.$province) {
376
+ this.$province.off(EVENT_CHANGE, this._changeProvince);
377
+ }
378
+
379
+ if (this.$city) {
380
+ this.$city.off(EVENT_CHANGE, this._changeCity);
381
+ }
382
+
383
+ if (this.$district) {
384
+ this.$district.off(EVENT_CHANGE, this._changeDistrict);
385
+ }
386
+ },
387
+ //获取选择项信息
388
+ getText: function () {
389
+ var text = '';
390
+ this.$dropdown.find('.city-select')
391
+ .each(function () {
392
+ var item = $(this).data('item'),
393
+ type = $(this).data('count');
394
+ if (item) {
395
+ text += ($(this).hasClass('province') ? '' : '/') + '<span class="select-item" data-count="' +
396
+ type + '" data-code="' + item.code + '">' + item.address + '</span>';
397
+ }
398
+ });
399
+ return text;
400
+ },
401
+ getPlaceHolder: function () {
402
+ return this.$element.attr('placeholder') || this.options.placeholder;
403
+ },
404
+ //显示placeholder或者选择的区域
405
+ feedText: function () {
406
+ var text = this.getText();
407
+ if (text) {
408
+ this.$textspan.find('>.placeholder').hide();
409
+ this.$textspan.find('>.title').html(this.getText()).show();
410
+ } else {
411
+ this.$textspan.find('>.placeholder').text(this.getPlaceHolder()).show();
412
+ this.$textspan.find('>.title').html('').hide();
413
+ }
414
+ },
415
+ getCode: function (count) {
416
+ var obj = {}, arr = [];
417
+ this.$textspan.find('.select-item')
418
+ .each(function () {
419
+ var code = $(this).data('code');
420
+ var count = $(this).data('count');
421
+ obj[count] = code;
422
+ arr.push(code);
423
+ });
424
+ return count ? obj[count] : arr.join('/');
425
+ },
426
+ getVal: function () {
427
+ var text = '';
428
+ var code='';
429
+ var that = this;
430
+ this.$dropdown.find('.city-select')
431
+ .each(function () {
432
+ var item = $(this).data('item');
433
+ if (item) {
434
+ text += ($(this).hasClass('province') ? '' : '/') + item.address;
435
+ code += ($(this).hasClass('province') ? '' : that.options.delimiter) + item.code;
436
+ }
437
+ });
438
+ $(this.options.addrElm).val(code);
439
+ return text;
440
+ },
441
+ //input的value赋值
442
+ feedVal: function (trigger) {
443
+ this.$element.val(this.getVal());
444
+ if(trigger) {
445
+ this.$element.trigger('cp:updated');
446
+ }
447
+ },
448
+ //输出数据
449
+ output: function (type) {
450
+ var $this = this;
451
+ var options = this.options;
452
+ //var placeholders = this.placeholders;
453
+ var $select = this['$' + type];
454
+ var data = type === PROVINCE ? {} : [];
455
+ var item;
456
+ var districts;
457
+ var code;
458
+ var matched = null;
459
+ var value;
460
+ if (!$select || !$select.length) {
461
+ return;
462
+ }
463
+ item = $select.data('item');
464
+ value = (item ? item.address : null) || options[type];
465
+ code = (
466
+ type === PROVINCE ? 86 :
467
+ type === CITY ? this.$province && this.$province.find('.active').data('code') :
468
+ type === DISTRICT ? this.$city && this.$city.find('.active').data('code') :
469
+ type === COUNTY ? this.$district && this.$district.find('.active').data('code') : code
470
+ );
471
+ //districts = $.isNumeric(code) ? ChineseDistricts[code] : null;
472
+        //判断是否应该去远程加载数据
473
+ districts = $.isNumeric(code) ? this.remoteLoadData(type,code) : null;
474
+ if ($.isPlainObject(districts)) {
475
+ $.each(districts, function (code, address) {
476
+ var provs;
477
+ if (type === PROVINCE) {
478
+ provs = [];
479
+ for (var i = 0; i < address.length; i++) {
480
+ if (address[i].address === value) {
481
+ matched = {
482
+ code: address[i].code,
483
+ address: address[i].address
484
+ };
485
+ }
486
+ provs.push({
487
+ code: address[i].code,
488
+ address: address[i].address,
489
+ selected: address[i].address === value
490
+ });
491
+ }
492
+ data[code] = provs;
493
+ } else {
494
+ if (address === value) {
495
+ matched = {
496
+ code: code,
497
+ address: address
498
+ };
499
+ }
500
+ data.push({
501
+ code: code,
502
+ address: address,
503
+ selected: address === value
504
+ });
505
+ }
506
+ });
507
+ }
508
+
509
+ $select.html(type === PROVINCE ? this.getProvinceList(data) :
510
+ this.getList(data, type));
511
+ $select.data('item', matched);//当前tab添加item(包含选择对象的内容)
512
+ if(! (type === PROVINCE)){//标识:下一个选项没有数据则关闭
513
+ if(data.length==0){
514
+ return true;
515
+ }
516
+ }
517
+ },
518
+ //遍历省份
519
+ getProvinceList: function (data) {
520
+ var list = [],
521
+ $this = this,
522
+ simple = this.options.simple;
523
+
524
+ $.each(data, function (i, n) {
525
+ list.push('<dl class="clearfix">');
526
+ list.push('<dt>' + i + '</dt><dd>');
527
+ $.each(n, function (j, m) {
528
+ list.push(
529
+ '<a' +
530
+ ' title="' + (m.address || '') + '"' +
531
+ ' data-code="' + (m.code || '') + '"' +
532
+ ' class="' +
533
+ (m.selected ? ' active' : '') +
534
+ '">' +
535
+ ( simple ? $this.simplize(m.address, PROVINCE) : m.address) +
536
+ '</a>');
537
+ });
538
+ list.push('</dd></dl>');
539
+ });
540
+
541
+ return list.join('');
542
+ },
543
+ //遍历市或区或县
544
+ getList: function (data, type) {
545
+ var list = [],
546
+ $this = this,
547
+ simple = this.options.simple;
548
+ list.push('<dl class="clearfix"><dd>');
549
+
550
+ $.each(data, function (i, n) {
551
+ list.push(
552
+ '<a' +
553
+ ' title="' + (n.address || '') + '"' +
554
+ ' data-code="' + (n.code || '') + '"' +
555
+ ' class="' +
556
+ (n.selected ? ' active' : '') +
557
+ '">' +
558
+ ( simple ? $this.simplize(n.address, type) : n.address) +
559
+ '</a>');
560
+ });
561
+ list.push('</dd></dl>');
562
+
563
+ return list.join('');
564
+ },
565
+ //简化名字
566
+ simplize: function (address, type) {
567
+ address = address || '';
568
+ if (type === PROVINCE) {
569
+ return address.replace(/[省,市,自治区,壮族,回族,维吾尔]/g, '');
570
+ } else {
571
+ return address;
572
+ }
573
+
574
+ // } else if (type === CITY) {
575
+ // return address.replace(/[回族,蒙古,苗族,白族,傣族,景颇族,藏族,彝族,壮族,傈僳族,布依族,侗族]/g, '')
576
+ // .replace('哈萨克', '').replace('自治州', '').replace(/自治县/, '');
577
+ // } else if (type === DISTRICT) {
578
+ // return address.length > 2 ? address.replace(/[市,区,县,旗]/g, '') : address;
579
+ // } else if (type === COUNTY) {
580
+ // return address;
581
+ // }
582
+ },
583
+ //处理tab显示
584
+ tab: function (type) {
585
+ var $selects = this.$dropdown.find('.city-select');
586
+ var $tabs = this.$dropdown.find('.city-select-tab > a');
587
+ var $select = this['$' + type];
588
+ var $tab = this.$dropdown.find('.city-select-tab > a[data-count="' + type + '"]');
589
+ if ($select) {
590
+ $selects.hide();
591
+ $select.show();
592
+ $tabs.removeClass('active');
593
+ $tab.addClass('active');
594
+ }
595
+ },
596
+
597
+ reset: function () {
598
+ this.$element.val(null).trigger('change');
599
+ },
600
+
601
+ destroy: function () {
602
+ this.unbind();
603
+ this.$element.removeData(NAMESPACE).removeClass('city-picker-input');
604
+ this.$textspan.remove();
605
+ this.$dropdown.remove();
606
+ },
607
+ //远程加载数据
608
+ remoteLoadData: function (cityType,cityId) {
609
+ var resultData = {};
610
+ if(PROVINCE===cityType)
611
+ {
612
+ return ChineseDistricts[cityId];
613
+ }
614
+ $.ajax({
615
+ // url: "/city-data/"+cityType+"/"+cityId,
616
+ url: '/city_picker?code=' + cityId + "&city_type=" + cityType + '&level=' + this.options.level,
617
+ type: "GET",
618
+ dataType: "json",
619
+ async: false,
620
+ contentType: "application/json",
621
+ success: function (result) {
622
+ // console.log(result)
623
+ if(result.code=="0")
624
+ {
625
+ resultData = result.data;
626
+ }else
627
+ {
628
+ console.log(result.desc);
629
+ }
630
+ }
631
+ });
632
+ return resultData;
633
+ }
634
+ };
635
+
636
+ CityPicker.DEFAULTS = {
637
+ simple: false,
638
+ responsive: false,
639
+ placeholder: '请选择省/市/区/镇',
640
+ level: 'county',
641
+ province: '',
642
+ city: '',
643
+ district: '',
644
+ county:'',
645
+ addrElm: '#addrValue',
646
+ delimiter: ','
647
+ };
648
+
649
+ CityPicker.setDefaults = function (options) {
650
+ $.extend(CityPicker.DEFAULTS, options);
651
+ };
652
+
653
+ // Save the other citypicker
654
+ CityPicker.other = $.fn.citypicker;
655
+
656
+ // Register as jQuery plugin
657
+ $.fn.citypicker = function (option) {
658
+ var args = [].slice.call(arguments, 1);
659
+
660
+ return this.each(function () {
661
+ var $this = $(this);
662
+ var data = $this.data(NAMESPACE);
663
+ var options;
664
+ var fn;
665
+
666
+ if (!data) {
667
+ if (/destroy/.test(option)) {
668
+ return;
669
+ }
670
+
671
+ options = $.extend({}, $this.data(), $.isPlainObject(option) && option);
672
+ $this.data(NAMESPACE, (data = new CityPicker(this, options)));
673
+ }
674
+
675
+ if (typeof option === 'string' && $.isFunction(fn = data[option])) {
676
+ fn.apply(data, args);
677
+ }
678
+ });
679
+ };
680
+
681
+ $.fn.citypicker.Constructor = CityPicker;
682
+ $.fn.citypicker.setDefaults = CityPicker.setDefaults;
683
+
684
+ // No conflict
685
+ $.fn.citypicker.noConflict = function () {
686
+ $.fn.citypicker = CityPicker.other;
687
+ return this;
688
+ };
689
+
690
+ $(function () {
691
+ $('[data-toggle="city-picker"]').citypicker();
692
+ });
693
+ });