11 lines
13 KiB
JavaScript
Raw Normal View History

2025-02-28 09:29:43 +08:00
/*!
* CityPicker v1.3.2
* https://github.com/tshi0912/citypicker
*
* Copyright (c) 2015-2019 Tao Shi
* Released under the MIT license
*
* Date: 2019-10-19T17:21:49.592Z
*/
(function(factory){if(typeof define==="function"&&define.amd){define(["jquery","ChineseDistricts"],factory)}else if(typeof exports==="object"){factory(require("jquery"),require("ChineseDistricts"))}else{factory(jQuery,ChineseDistricts)}})(function($,ChineseDistricts){"use strict";if(typeof ChineseDistricts==="undefined"){throw new Error('The file "city-picker.data.js" must be included first!')}var NAMESPACE="citypicker";var EVENT_CHANGE="change."+NAMESPACE;var PROVINCE="province";var CITY="city";var DISTRICT="district";function CityPicker(element,options){this.$element=$(element);this.$dropdown=null;this.options=$.extend({},CityPicker.DEFAULTS,$.isPlainObject(options)&&options);this.active=false;this.dems=[];this.needBlur=false;this.init()}CityPicker.prototype={constructor:CityPicker,init:function(){this.codeRender();this.defineDems();this.render();this.bind();this.active=true},codeRender:function(){var code=this.$element.attr("code");var value=this.$element.val();if(code!==undefined&&code!==""&&!isNaN(Number(code)))this.$element.val($.fn.citypicker.getAddressbyCodeId(code));if(value!==undefined&&value!==""&&!isNaN(Number(value)))this.$element.val($.fn.citypicker.getAddressbyCodeId(value))},render:function(){var p=this.getPosition(),placeholder=this.$element.attr("placeholder")||this.options.placeholder,textspan='<span class="city-picker-span" style="'+this.getWidthStyle(p.width)+"height:"+p.height+"px;line-height:"+(p.height-1)+'px;">'+(placeholder?'<span class="placeholder">'+placeholder+"</span>":"")+'<span class="title"></span><div class="arrow"></div>'+"</span>",dropdown='<div class="city-picker-dropdown" style="left:0px;top:100%;'+this.getWidthStyle(p.width,true)+'">'+'<div class="city-select-wrap">'+'<div class="city-select-tab">'+'<a class="active" data-count="province">省份</a>'+(this.includeDem("city")?'<a data-count="city">城市</a>':"")+(this.includeDem("district")?'<a data-count="district">区县</a>':"")+"</div>"+'<div class="city-select-content">'+'<div class="city-select province" data-count="province"></div>'+(this.includeDem("city")?'<div class="city-select city" data-count="city"></div>':"")+(this.includeDem("district")?'<div class="city-select district" data-count="district"></div>':"")+"</div></div>";this.$element.addClass("city-picker-input");this.$textspan=$(textspan).insertAfter(this.$element);this.$dropdown=$(dropdown).insertAfter(this.$textspan);var $select=this.$dropdown.find(".city-select");$.each(this.dems,$.proxy(function(i,type){this["$"+type]=$select.filter("."+type+"")},this));this.refresh()},refresh:function(force){var $select=this.$dropdown.find(".city-select");$select.data("item",null);var val=this.$element.val()||"";val=val.split("/");$.each(this.dems,$.proxy(function(i,type){if(val[i]&&i<val.length){this.options[type]=val[i]}else if(force){this.options[type]=""}this.output(type)},this));this.tab(PROVINCE);this.feedText();this.feedVal()},defineDems:function(){var stop=false;$.each([PROVINCE,CITY,DISTRICT],$.proxy(function(i,type){if(!stop){this.dems.push(type)}if(type===this.options.level){stop=true}},this))},includeDem:function(type){return $.inArray(type,this.dems)!==-1},getPosition:function(){var p,h,w,s,pw;p=this.$element.position();s=this.getSize(this.$element);h=s.height;w=s.width;if(this.options.responsive){pw=this.$element.offsetParent().width();if(pw){w=w/pw;if(w>.99){w=1}w=w*100+"%"}}return{top:p.top||0,left:p.left||0,height:h,width:w}},getSize:function($dom){var $wrap,$clone,sizes;if(!$dom.is(":visible")){$wrap=$("<div />").appendTo($("body"));$wrap.css({position:"absolute !important",visibility:"hidden !important",display:"block !important"});$clone=$dom.clone().appendTo($wrap);sizes={width:$clone.outerWidth(),height:$clone.outerHeight()};$wrap.remove()}else{sizes={width:$dom.outerWidth(),height:$dom.outerHeight()}}return sizes},getWidthStyle:function(w,dropdown){if(this.options.responsive&&!$.isNumeric(w)){return"width:"+w+";"}else{return"width:"+(dropdown?Math.max(320,w):w)+"px;"}},bind:function(){var $this=this;$(document).on("click",this._mouteclick=function(e){var