index.js 8.4 KB

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