/*! * 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( '' + (n.address || '') + '' ); }); 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(); }); });