index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. component_1.VantComponent({
  17. classes: ['active-class', 'toolbar-class', 'column-class'],
  18. props: __assign({}, shared_1.pickerProps, { value: String, areaList: {
  19. type: Object,
  20. value: {}
  21. }, columnsNum: {
  22. type: [String, Number],
  23. value: 3
  24. } }),
  25. data: {
  26. columns: [{ values: [] }, { values: [] }, { values: [] }],
  27. displayColumns: [{ values: [] }, { values: [] }, { values: [] }]
  28. },
  29. watch: {
  30. value: function (value) {
  31. this.code = value;
  32. this.setValues();
  33. },
  34. areaList: 'setValues',
  35. columnsNum: function (value) {
  36. this.set({
  37. displayColumns: this.data.columns.slice(0, +value)
  38. });
  39. }
  40. },
  41. mounted: function () {
  42. this.setValues();
  43. },
  44. methods: {
  45. getPicker: function () {
  46. if (this.picker == null) {
  47. this.picker = this.selectComponent('.van-area__picker');
  48. }
  49. return this.picker;
  50. },
  51. onCancel: function (event) {
  52. this.emit('cancel', event.detail);
  53. },
  54. onConfirm: function (event) {
  55. this.emit('confirm', event.detail);
  56. },
  57. emit: function (type, detail) {
  58. detail.values = detail.value;
  59. delete detail.value;
  60. this.$emit(type, detail);
  61. },
  62. onChange: function (event) {
  63. var _this = this;
  64. var _a = event.detail, index = _a.index, picker = _a.picker, value = _a.value;
  65. this.code = value[index].code;
  66. this.setValues().then(function () {
  67. _this.$emit('change', {
  68. picker: picker,
  69. values: picker.getValues(),
  70. index: index
  71. });
  72. });
  73. },
  74. getConfig: function (type) {
  75. var areaList = this.data.areaList;
  76. return (areaList && areaList[type + "_list"]) || {};
  77. },
  78. getList: function (type, code) {
  79. var result = [];
  80. if (type !== 'province' && !code) {
  81. return result;
  82. }
  83. var list = this.getConfig(type);
  84. result = Object.keys(list).map(function (code) { return ({
  85. code: code,
  86. name: list[code]
  87. }); });
  88. if (code) {
  89. // oversea code
  90. if (code[0] === '9' && type === 'city') {
  91. code = '9';
  92. }
  93. result = result.filter(function (item) { return item.code.indexOf(code) === 0; });
  94. }
  95. return result;
  96. },
  97. getIndex: function (type, code) {
  98. var compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  99. var list = this.getList(type, code.slice(0, compareNum - 2));
  100. // oversea code
  101. if (code[0] === '9' && type === 'province') {
  102. compareNum = 1;
  103. }
  104. code = code.slice(0, compareNum);
  105. for (var i = 0; i < list.length; i++) {
  106. if (list[i].code.slice(0, compareNum) === code) {
  107. return i;
  108. }
  109. }
  110. return 0;
  111. },
  112. setValues: function () {
  113. var _this = this;
  114. var county = this.getConfig('county');
  115. var code = this.code || Object.keys(county)[0] || '';
  116. var province = this.getList('province');
  117. var city = this.getList('city', code.slice(0, 2));
  118. var picker = this.getPicker();
  119. if (!picker) {
  120. return;
  121. }
  122. var stack = [];
  123. stack.push(picker.setColumnValues(0, province, false));
  124. stack.push(picker.setColumnValues(1, city, false));
  125. if (city.length && code.slice(2, 4) === '00') {
  126. code = city[0].code;
  127. }
  128. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  129. return Promise.all(stack)
  130. .catch(function () { })
  131. .then(function () {
  132. return picker.setIndexes([
  133. _this.getIndex('province', code),
  134. _this.getIndex('city', code),
  135. _this.getIndex('county', code)
  136. ]);
  137. })
  138. .catch(function () { });
  139. },
  140. getValues: function () {
  141. var picker = this.getPicker();
  142. return picker ? picker.getValues().filter(function (value) { return !!value; }) : [];
  143. },
  144. getDetail: function () {
  145. var values = this.getValues();
  146. var area = {
  147. code: '',
  148. country: '',
  149. province: '',
  150. city: '',
  151. county: ''
  152. };
  153. if (!values.length) {
  154. return area;
  155. }
  156. var names = values.map(function (item) { return item.name; });
  157. area.code = values[values.length - 1].code;
  158. if (area.code[0] === '9') {
  159. area.country = names[1] || '';
  160. area.province = names[2] || '';
  161. }
  162. else {
  163. area.province = names[0] || '';
  164. area.city = names[1] || '';
  165. area.county = names[2] || '';
  166. }
  167. return area;
  168. },
  169. reset: function () {
  170. this.code = '';
  171. return this.setValues();
  172. }
  173. }
  174. });