china-location
Version:
Chinese Administrative Division information, 中国行政区划信息
3 lines (2 loc) • 2.2 kB
JavaScript
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t=t||self).chinaLocation=i()}(this,function(){return class{constructor(t){this.locationData=t,this.currentProvinces=this.getProvinces();var i=this.currentProvinces[0];this.currentCities=this.getCities(i);var e=this.currentCities[0];this.currentDistricts=this.getDistricts(e,i),this.activeProvince=i.code,this.activeCity=e.code,this.activeDistrict=this.currentDistricts[0].code}getProvinces(){var t=Object.keys(this.locationData),i=[];return t.forEach(t=>{var e=this.locationData[t];i.push({code:e.code,name:e.name})}),i}getCities(t){var i=this.locationData[t.code].cities,e=Object.keys(i),r=[];return e.forEach(t=>{var e=i[t];r.push({code:e.code,name:e.name})}),r}getDistricts(t,i){var e=this.locationData[i.code].cities[t.code].districts,r=Object.keys(e),c=[];return r.forEach(t=>{c.push({code:t,name:e[t]})}),c}getProvinceByCode(t){return this.locationData[t]}getCityByCode(t,i){return this.getProvinceByCode(i).cities[t]}getDistrictByCode(t,i,e){return this.getCityByCode(i,e).districts[t]}getCity(t,i){return i[t]}getDistrict(t,i){return i[t]}changeLocation(t,i,e){return this.changeProvince(t,i,e),this}changeProvince(t,i,e){var r=this.getCities({code:t}),c=r[0];if(i)for(var s=0;s<r.length;s++){var n=r[s];if(n.code==i){c=n;break}}this.currentCities=r,this.activeProvince=t,this.changeCity(c?c.code:"",e)}changeCity(t,i){var e=this.getDistricts({code:t},{code:this.activeProvince});this.currentDistricts=e,this.activeCity=t;var r=e[0];if(i)for(var c=0;c<e.length;c++){var s=e[c];if(s.code==i){r=s;break}}this.changeDistrict(r?r.code:"")}changeDistrict(t){this.activeDistrict=t}getCurrentAddress(){var t=this.getProvinceByCode(this.activeProvince),i=this.getCity(this.activeCity,t.cities),e=this.getDistrict(this.activeDistrict,i.districts);return{province:{code:t.code,name:t.name},city:{code:i.code,name:i.name},district:{code:this.activeDistrict,name:e||""}}}getCurrentProvinces(){return this.currentProvinces}getCurrentCities(){return this.currentCities}getCurrentDistricts(){return this.currentDistricts}}});
//# sourceMappingURL=ChinaLocation.umd.js.map