index.js 7.3 KB

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