index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. "use strict";
  2. var _component = require("../common/component");
  3. var _shared = require("../picker/shared");
  4. var _utils = require("../common/utils");
  5. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  6. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  7. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  8. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  9. function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  10. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  11. var COLUMNSPLACEHOLDERCODE = "000000";
  12. (0, _component.VantComponent)({
  13. classes: ["active-class", "toolbar-class", "column-class"],
  14. props: Object.assign(Object.assign({}, _shared.pickerProps), {
  15. value: {
  16. type: String,
  17. observer: function observer(value) {
  18. this.code = value;
  19. this.setValues();
  20. }
  21. },
  22. areaList: {
  23. type: Object,
  24. value: {},
  25. observer: "setValues"
  26. },
  27. columnsNum: {
  28. type: null,
  29. value: 3,
  30. observer: function observer(value) {
  31. this.setData({
  32. displayColumns: this.data.columns.slice(0, +value)
  33. });
  34. }
  35. },
  36. columnsPlaceholder: {
  37. type: Array,
  38. value: [],
  39. observer: function observer(val) {
  40. this.setData({
  41. typeToColumnsPlaceholder: {
  42. province: val[0] || "",
  43. city: val[1] || "",
  44. county: val[2] || ""
  45. }
  46. });
  47. }
  48. }
  49. }),
  50. data: {
  51. columns: [{
  52. values: []
  53. }, {
  54. values: []
  55. }, {
  56. values: []
  57. }],
  58. displayColumns: [{
  59. values: []
  60. }, {
  61. values: []
  62. }, {
  63. values: []
  64. }],
  65. typeToColumnsPlaceholder: {}
  66. },
  67. mounted: function mounted() {
  68. var _this = this;
  69. (0, _utils.requestAnimationFrame)(function () {
  70. _this.setValues();
  71. });
  72. },
  73. methods: {
  74. getPicker: function getPicker() {
  75. if (this.picker == null) {
  76. this.picker = this.selectComponent(".van-area__picker");
  77. }
  78. return this.picker;
  79. },
  80. onCancel: function onCancel(event) {
  81. this.emit("cancel", event.detail);
  82. },
  83. onConfirm: function onConfirm(event) {
  84. var index = event.detail.index;
  85. var value = event.detail.value;
  86. value = this.parseOutputValues(value);
  87. this.emit("confirm", {
  88. value: value,
  89. index: index
  90. });
  91. },
  92. emit: function emit(type, detail) {
  93. detail.values = detail.value;
  94. delete detail.value;
  95. this.$emit(type, detail);
  96. },
  97. // parse output columns data
  98. parseOutputValues: function parseOutputValues(values) {
  99. var columnsPlaceholder = this.data.columnsPlaceholder;
  100. return values.map(function (value, index) {
  101. // save undefined value
  102. if (!value) return value;
  103. value = JSON.parse(JSON.stringify(value));
  104. if (!value.code || value.name === columnsPlaceholder[index]) {
  105. value.code = "";
  106. value.name = "";
  107. }
  108. return value;
  109. });
  110. },
  111. onChange: function onChange(event) {
  112. var _this2 = this;
  113. var _event$detail = event.detail,
  114. index = _event$detail.index,
  115. picker = _event$detail.picker,
  116. value = _event$detail.value;
  117. this.code = value[index].code;
  118. this.setValues().then(function () {
  119. _this2.$emit("change", {
  120. picker: picker,
  121. values: _this2.parseOutputValues(picker.getValues()),
  122. index: index
  123. });
  124. });
  125. },
  126. getConfig: function getConfig(type) {
  127. var areaList = this.data.areaList;
  128. return areaList && areaList["".concat(type, "_list")] || {};
  129. },
  130. getList: function getList(type, code) {
  131. var typeToColumnsPlaceholder = this.data.typeToColumnsPlaceholder;
  132. var result = [];
  133. if (type !== "province" && !code) {
  134. return result;
  135. }
  136. var list = this.getConfig(type);
  137. result = Object.keys(list).map(function (code) {
  138. return {
  139. code: code,
  140. name: list[code]
  141. };
  142. });
  143. if (code != null) {
  144. // oversea code
  145. if (code[0] === "9" && type === "city") {
  146. code = "9";
  147. }
  148. result = result.filter(function (item) {
  149. return item.code.indexOf(code) === 0;
  150. });
  151. }
  152. if (typeToColumnsPlaceholder[type] && result.length) {
  153. // set columns placeholder
  154. var codeFill = type === "province" ? "" : type === "city" ? COLUMNSPLACEHOLDERCODE.slice(2, 4) : COLUMNSPLACEHOLDERCODE.slice(4, 6);
  155. result.unshift({
  156. code: "".concat(code).concat(codeFill),
  157. name: typeToColumnsPlaceholder[type]
  158. });
  159. }
  160. return result;
  161. },
  162. getIndex: function getIndex(type, code) {
  163. var compareNum = type === "province" ? 2 : type === "city" ? 4 : 6;
  164. var list = this.getList(type, code.slice(0, compareNum - 2)); // oversea code
  165. if (code[0] === "9" && type === "province") {
  166. compareNum = 1;
  167. }
  168. code = code.slice(0, compareNum);
  169. for (var i = 0; i < list.length; i++) {
  170. if (list[i].code.slice(0, compareNum) === code) {
  171. return i;
  172. }
  173. }
  174. return 0;
  175. },
  176. setValues: function setValues() {
  177. var county = this.getConfig("county");
  178. var code = this.code;
  179. if (!code) {
  180. if (this.data.columnsPlaceholder.length) {
  181. code = COLUMNSPLACEHOLDERCODE;
  182. } else if (Object.keys(county)[0]) {
  183. code = Object.keys(county)[0];
  184. } else {
  185. code = "";
  186. }
  187. }
  188. code = code.toString();
  189. var province = this.getList("province");
  190. var city = this.getList("city", code.slice(0, 2));
  191. var picker = this.getPicker();
  192. if (!picker) {
  193. return;
  194. }
  195. var stack = [];
  196. var indexes = [];
  197. var columnsNum = this.data.columnsNum;
  198. if (columnsNum >= 1) {
  199. stack.push(picker.setColumnValues(0, province, false));
  200. indexes.push(this.getIndex("province", code));
  201. }
  202. if (columnsNum >= 2) {
  203. stack.push(picker.setColumnValues(1, city, false));
  204. indexes.push(this.getIndex("city", code));
  205. if (city.length && code.slice(2, 4) === "00") {
  206. var _city = _slicedToArray(city, 1);
  207. code = _city[0].code;
  208. }
  209. }
  210. if (columnsNum === 3) {
  211. stack.push(picker.setColumnValues(2, this.getList("county", code.slice(0, 4)), false));
  212. indexes.push(this.getIndex("county", code));
  213. }
  214. return Promise.all(stack)["catch"](function () {}).then(function () {
  215. return picker.setIndexes(indexes);
  216. })["catch"](function () {});
  217. },
  218. getValues: function getValues() {
  219. var picker = this.getPicker();
  220. return picker ? picker.getValues().filter(function (value) {
  221. return !!value;
  222. }) : [];
  223. },
  224. getDetail: function getDetail() {
  225. var values = this.getValues();
  226. var area = {
  227. code: "",
  228. country: "",
  229. province: "",
  230. city: "",
  231. county: ""
  232. };
  233. if (!values.length) {
  234. return area;
  235. }
  236. var names = values.map(function (item) {
  237. return item.name;
  238. });
  239. area.code = values[values.length - 1].code;
  240. if (area.code[0] === "9") {
  241. area.country = names[1] || "";
  242. area.province = names[2] || "";
  243. } else {
  244. area.province = names[0] || "";
  245. area.city = names[1] || "";
  246. area.county = names[2] || "";
  247. }
  248. return area;
  249. },
  250. reset: function reset(code) {
  251. this.code = code || "";
  252. return this.setValues();
  253. }
  254. }
  255. });