index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. 'use strict';
  2. var __assign =
  3. (this && this.__assign) ||
  4. function () {
  5. __assign =
  6. Object.assign ||
  7. function (t) {
  8. for (var s, i = 1, n = arguments.length; i < n; i++) {
  9. s = arguments[i];
  10. for (var p in s)
  11. if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  12. }
  13. return t;
  14. };
  15. return __assign.apply(this, arguments);
  16. };
  17. Object.defineProperty(exports, '__esModule', { value: true });
  18. var component_1 = require('../common/component');
  19. var shared_1 = require('../picker/shared');
  20. var utils_1 = require('../common/utils');
  21. var EMPTY_CODE = '000000';
  22. component_1.VantComponent({
  23. classes: ['active-class', 'toolbar-class', 'column-class'],
  24. props: __assign(__assign({}, shared_1.pickerProps), {
  25. value: {
  26. type: String,
  27. observer: function (value) {
  28. this.code = value;
  29. this.setValues();
  30. },
  31. },
  32. areaList: {
  33. type: Object,
  34. value: {},
  35. observer: 'setValues',
  36. },
  37. columnsNum: {
  38. type: null,
  39. value: 3,
  40. },
  41. columnsPlaceholder: {
  42. type: Array,
  43. observer: function (val) {
  44. this.setData({
  45. typeToColumnsPlaceholder: {
  46. province: val[0] || '',
  47. city: val[1] || '',
  48. county: val[2] || '',
  49. },
  50. });
  51. },
  52. },
  53. }),
  54. data: {
  55. columns: [{ values: [] }, { values: [] }, { values: [] }],
  56. typeToColumnsPlaceholder: {},
  57. },
  58. mounted: function () {
  59. var _this = this;
  60. utils_1.requestAnimationFrame(function () {
  61. _this.setValues();
  62. });
  63. },
  64. methods: {
  65. getPicker: function () {
  66. if (this.picker == null) {
  67. this.picker = this.selectComponent('.van-area__picker');
  68. }
  69. return this.picker;
  70. },
  71. onCancel: function (event) {
  72. this.emit('cancel', event.detail);
  73. },
  74. onConfirm: function (event) {
  75. var index = event.detail.index;
  76. var value = event.detail.value;
  77. value = this.parseValues(value);
  78. this.emit('confirm', { value: value, index: index });
  79. },
  80. emit: function (type, detail) {
  81. detail.values = detail.value;
  82. delete detail.value;
  83. this.$emit(type, detail);
  84. },
  85. parseValues: function (values) {
  86. var columnsPlaceholder = this.data.columnsPlaceholder;
  87. return values.map(function (value, index) {
  88. if (
  89. value &&
  90. (!value.code || value.name === columnsPlaceholder[index])
  91. ) {
  92. return __assign(__assign({}, value), { code: '', name: '' });
  93. }
  94. return value;
  95. });
  96. },
  97. onChange: function (event) {
  98. var _this = this;
  99. var _a;
  100. var _b = event.detail,
  101. index = _b.index,
  102. picker = _b.picker,
  103. value = _b.value;
  104. this.code = value[index].code;
  105. (_a = this.setValues()) === null || _a === void 0
  106. ? void 0
  107. : _a.then(function () {
  108. _this.$emit('change', {
  109. picker: picker,
  110. values: _this.parseValues(picker.getValues()),
  111. index: index,
  112. });
  113. });
  114. },
  115. getConfig: function (type) {
  116. var areaList = this.data.areaList;
  117. return (areaList && areaList[type + '_list']) || {};
  118. },
  119. getList: function (type, code) {
  120. if (type !== 'province' && !code) {
  121. return [];
  122. }
  123. var typeToColumnsPlaceholder = this.data.typeToColumnsPlaceholder;
  124. var list = this.getConfig(type);
  125. var result = Object.keys(list).map(function (code) {
  126. return {
  127. code: code,
  128. name: list[code],
  129. };
  130. });
  131. if (code != null) {
  132. // oversea code
  133. if (code[0] === '9' && type === 'city') {
  134. code = '9';
  135. }
  136. result = result.filter(function (item) {
  137. return item.code.indexOf(code) === 0;
  138. });
  139. }
  140. if (typeToColumnsPlaceholder[type] && result.length) {
  141. // set columns placeholder
  142. var codeFill =
  143. type === 'province'
  144. ? ''
  145. : type === 'city'
  146. ? EMPTY_CODE.slice(2, 4)
  147. : EMPTY_CODE.slice(4, 6);
  148. result.unshift({
  149. code: '' + code + codeFill,
  150. name: typeToColumnsPlaceholder[type],
  151. });
  152. }
  153. return result;
  154. },
  155. getIndex: function (type, code) {
  156. var compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  157. var list = this.getList(type, code.slice(0, compareNum - 2));
  158. // oversea code
  159. if (code[0] === '9' && type === 'province') {
  160. compareNum = 1;
  161. }
  162. code = code.slice(0, compareNum);
  163. for (var i = 0; i < list.length; i++) {
  164. if (list[i].code.slice(0, compareNum) === code) {
  165. return i;
  166. }
  167. }
  168. return 0;
  169. },
  170. setValues: function () {
  171. var picker = this.getPicker();
  172. if (!picker) {
  173. return;
  174. }
  175. var code = this.code || this.getDefaultCode();
  176. var provinceList = this.getList('province');
  177. var cityList = this.getList('city', code.slice(0, 2));
  178. var stack = [];
  179. var indexes = [];
  180. var columnsNum = this.data.columnsNum;
  181. if (columnsNum >= 1) {
  182. stack.push(picker.setColumnValues(0, provinceList, false));
  183. indexes.push(this.getIndex('province', code));
  184. }
  185. if (columnsNum >= 2) {
  186. stack.push(picker.setColumnValues(1, cityList, false));
  187. indexes.push(this.getIndex('city', code));
  188. if (cityList.length && code.slice(2, 4) === '00') {
  189. code = cityList[0].code;
  190. }
  191. }
  192. if (columnsNum === 3) {
  193. stack.push(
  194. picker.setColumnValues(
  195. 2,
  196. this.getList('county', code.slice(0, 4)),
  197. false
  198. )
  199. );
  200. indexes.push(this.getIndex('county', code));
  201. }
  202. return Promise.all(stack)
  203. .catch(function () {})
  204. .then(function () {
  205. return picker.setIndexes(indexes);
  206. })
  207. .catch(function () {});
  208. },
  209. getDefaultCode: function () {
  210. var columnsPlaceholder = this.data.columnsPlaceholder;
  211. if (columnsPlaceholder.length) {
  212. return EMPTY_CODE;
  213. }
  214. var countyCodes = Object.keys(this.getConfig('county'));
  215. if (countyCodes[0]) {
  216. return countyCodes[0];
  217. }
  218. var cityCodes = Object.keys(this.getConfig('city'));
  219. if (cityCodes[0]) {
  220. return cityCodes[0];
  221. }
  222. return '';
  223. },
  224. getValues: function () {
  225. var picker = this.getPicker();
  226. if (!picker) {
  227. return [];
  228. }
  229. return this.parseValues(
  230. picker.getValues().filter(function (value) {
  231. return !!value;
  232. })
  233. );
  234. },
  235. getDetail: function () {
  236. var values = this.getValues();
  237. var area = {
  238. code: '',
  239. country: '',
  240. province: '',
  241. city: '',
  242. county: '',
  243. };
  244. if (!values.length) {
  245. return area;
  246. }
  247. var names = values.map(function (item) {
  248. return item.name;
  249. });
  250. area.code = values[values.length - 1].code;
  251. if (area.code[0] === '9') {
  252. area.country = names[1] || '';
  253. area.province = names[2] || '';
  254. } else {
  255. area.province = names[0] || '';
  256. area.city = names[1] || '';
  257. area.county = names[2] || '';
  258. }
  259. return area;
  260. },
  261. reset: function (code) {
  262. this.code = code || '';
  263. return this.setValues();
  264. },
  265. },
  266. });