index.js 7.1 KB

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