index.js 8.4 KB

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