index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <uni-shadow-root class="weapp-lib-area-index"><van-picker class="van-area__picker" active-class="active-class" toolbar-class="toolbar-class" column-class="column-class" :show-toolbar="showToolbar" value-key="name" :title="title" :loading="loading" :columns="computed.displayColumns(columns, columnsNum)" :item-height="itemHeight" :visible-item-count="visibleItemCount" :cancel-button-text="cancelButtonText" :confirm-button-text="confirmButtonText" @change="onChange" @confirm="onConfirm" @cancel="onCancel"></van-picker></uni-shadow-root>
  3. </template>
  4. <wxs src="./index.wxs" module="computed"></wxs>
  5. <script>
  6. import VanPicker from '../picker/index.vue'
  7. global['__wxVueOptions'] = {components:{'van-picker': VanPicker}}
  8. global['__wxRoute'] = 'weapp/lib/area/index'
  9. "use strict";
  10. var __assign = (this && this.__assign) || function () {
  11. __assign = Object.assign || function(t) {
  12. for (var s, i = 1, n = arguments.length; i < n; i++) {
  13. s = arguments[i];
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  15. t[p] = s[p];
  16. }
  17. return t;
  18. };
  19. return __assign.apply(this, arguments);
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. var component_1 = require("../common/component");
  23. var shared_1 = require("../picker/shared");
  24. var utils_1 = require("../common/utils");
  25. var EMPTY_CODE = '000000';
  26. (0, component_1.VantComponent)({
  27. classes: ['active-class', 'toolbar-class', 'column-class'],
  28. props: __assign(__assign({}, shared_1.pickerProps), { showToolbar: {
  29. type: Boolean,
  30. value: true,
  31. }, value: {
  32. type: String,
  33. observer: function (value) {
  34. this.code = value;
  35. this.setValues();
  36. },
  37. }, areaList: {
  38. type: Object,
  39. value: {},
  40. observer: 'setValues',
  41. }, columnsNum: {
  42. type: null,
  43. value: 3,
  44. }, columnsPlaceholder: {
  45. type: Array,
  46. observer: function (val) {
  47. this.setData({
  48. typeToColumnsPlaceholder: {
  49. province: val[0] || '',
  50. city: val[1] || '',
  51. county: val[2] || '',
  52. },
  53. });
  54. },
  55. } }),
  56. data: {
  57. columns: [{ values: [] }, { values: [] }, { values: [] }],
  58. typeToColumnsPlaceholder: {},
  59. },
  60. mounted: function () {
  61. var _this = this;
  62. (0, utils_1.requestAnimationFrame)(function () {
  63. _this.setValues();
  64. });
  65. },
  66. methods: {
  67. getPicker: function () {
  68. if (this.picker == null) {
  69. this.picker = this.selectComponent('.van-area__picker');
  70. }
  71. return this.picker;
  72. },
  73. onCancel: function (event) {
  74. this.emit('cancel', event.detail);
  75. },
  76. onConfirm: function (event) {
  77. var index = event.detail.index;
  78. var value = event.detail.value;
  79. value = this.parseValues(value);
  80. this.emit('confirm', { value: value, index: index });
  81. },
  82. emit: function (type, detail) {
  83. detail.values = detail.value;
  84. delete detail.value;
  85. this.$emit(type, detail);
  86. },
  87. parseValues: function (values) {
  88. var columnsPlaceholder = this.data.columnsPlaceholder;
  89. return values.map(function (value, index) {
  90. if (value &&
  91. (!value.code || value.name === columnsPlaceholder[index])) {
  92. return __assign(__assign({}, value), { code: '', name: '' });
  93. }
  94. return value;
  95. });
  96. },
  97. onChange: function (event) {
  98. var _this = this;
  99. var _a;
  100. var _b = event.detail, index = _b.index, picker = _b.picker, value = _b.value;
  101. this.code = value[index].code;
  102. (_a = this.setValues()) === null || _a === void 0 ? void 0 : _a.then(function () {
  103. _this.$emit('change', {
  104. picker: picker,
  105. values: _this.parseValues(picker.getValues()),
  106. index: index,
  107. });
  108. });
  109. },
  110. getConfig: function (type) {
  111. var areaList = this.data.areaList;
  112. return (areaList && areaList["".concat(type, "_list")]) || {};
  113. },
  114. getList: function (type, code) {
  115. if (type !== 'province' && !code) {
  116. return [];
  117. }
  118. var typeToColumnsPlaceholder = this.data.typeToColumnsPlaceholder;
  119. var list = this.getConfig(type);
  120. var result = Object.keys(list).map(function (code) { return ({
  121. code: code,
  122. name: list[code],
  123. }); });
  124. if (code != null) {
  125. // oversea code
  126. if (code[0] === '9' && type === 'city') {
  127. code = '9';
  128. }
  129. result = result.filter(function (item) { return item.code.indexOf(code) === 0; });
  130. }
  131. if (typeToColumnsPlaceholder[type] && result.length) {
  132. // set columns placeholder
  133. var codeFill = type === 'province'
  134. ? ''
  135. : type === 'city'
  136. ? EMPTY_CODE.slice(2, 4)
  137. : EMPTY_CODE.slice(4, 6);
  138. result.unshift({
  139. code: "".concat(code).concat(codeFill),
  140. name: typeToColumnsPlaceholder[type],
  141. });
  142. }
  143. return result;
  144. },
  145. getIndex: function (type, code) {
  146. var compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  147. var list = this.getList(type, code.slice(0, compareNum - 2));
  148. // oversea code
  149. if (code[0] === '9' && type === 'province') {
  150. compareNum = 1;
  151. }
  152. code = code.slice(0, compareNum);
  153. for (var i = 0; i < list.length; i++) {
  154. if (list[i].code.slice(0, compareNum) === code) {
  155. return i;
  156. }
  157. }
  158. return 0;
  159. },
  160. setValues: function () {
  161. var picker = this.getPicker();
  162. if (!picker) {
  163. return;
  164. }
  165. var code = this.code || this.getDefaultCode();
  166. var provinceList = this.getList('province');
  167. var cityList = this.getList('city', code.slice(0, 2));
  168. var stack = [];
  169. var indexes = [];
  170. var columnsNum = this.data.columnsNum;
  171. if (columnsNum >= 1) {
  172. stack.push(picker.setColumnValues(0, provinceList, false));
  173. indexes.push(this.getIndex('province', code));
  174. }
  175. if (columnsNum >= 2) {
  176. stack.push(picker.setColumnValues(1, cityList, false));
  177. indexes.push(this.getIndex('city', code));
  178. if (cityList.length && code.slice(2, 4) === '00') {
  179. code = cityList[0].code;
  180. }
  181. }
  182. if (columnsNum === 3) {
  183. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  184. indexes.push(this.getIndex('county', code));
  185. }
  186. return Promise.all(stack)
  187. .catch(function () { })
  188. .then(function () { return picker.setIndexes(indexes); })
  189. .catch(function () { });
  190. },
  191. getDefaultCode: function () {
  192. var columnsPlaceholder = this.data.columnsPlaceholder;
  193. if (columnsPlaceholder.length) {
  194. return EMPTY_CODE;
  195. }
  196. var countyCodes = Object.keys(this.getConfig('county'));
  197. if (countyCodes[0]) {
  198. return countyCodes[0];
  199. }
  200. var cityCodes = Object.keys(this.getConfig('city'));
  201. if (cityCodes[0]) {
  202. return cityCodes[0];
  203. }
  204. return '';
  205. },
  206. getValues: function () {
  207. var picker = this.getPicker();
  208. if (!picker) {
  209. return [];
  210. }
  211. return this.parseValues(picker.getValues().filter(function (value) { return !!value; }));
  212. },
  213. getDetail: function () {
  214. var values = this.getValues();
  215. var area = {
  216. code: '',
  217. country: '',
  218. province: '',
  219. city: '',
  220. county: '',
  221. };
  222. if (!values.length) {
  223. return area;
  224. }
  225. var names = values.map(function (item) { return item.name; });
  226. area.code = values[values.length - 1].code;
  227. if (area.code[0] === '9') {
  228. area.country = names[1] || '';
  229. area.province = names[2] || '';
  230. }
  231. else {
  232. area.province = names[0] || '';
  233. area.city = names[1] || '';
  234. area.county = names[2] || '';
  235. }
  236. return area;
  237. },
  238. reset: function (code) {
  239. this.code = code || '';
  240. return this.setValues();
  241. },
  242. },
  243. });
  244. export default global['__wxComponents']['weapp/lib/area/index']
  245. </script>
  246. <style platform="mp-weixin">
  247. @import '../common/index.css';
  248. </style>