index.js 8.2 KB

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