utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. AREA_EMPTY_CODE: () => AREA_EMPTY_CODE,
  21. INHERIT_PROPS: () => INHERIT_PROPS,
  22. INHERIT_SLOTS: () => INHERIT_SLOTS,
  23. formatDataForCascade: () => formatDataForCascade
  24. });
  25. module.exports = __toCommonJS(stdin_exports);
  26. const AREA_EMPTY_CODE = "000000";
  27. const INHERIT_SLOTS = [
  28. "title",
  29. "cancel",
  30. "confirm",
  31. "toolbar",
  32. "columns-top",
  33. "columns-bottom"
  34. ];
  35. const INHERIT_PROPS = [
  36. "title",
  37. "loading",
  38. "readonly",
  39. "optionHeight",
  40. "swipeDuration",
  41. "visibleOptionNum",
  42. "cancelButtonText",
  43. "confirmButtonText"
  44. ];
  45. const makeOption = (text = "", value = AREA_EMPTY_CODE, children = void 0) => ({
  46. text,
  47. value,
  48. children
  49. });
  50. function formatDataForCascade({
  51. areaList,
  52. columnsNum,
  53. columnsPlaceholder: placeholder
  54. }) {
  55. const {
  56. city_list: city = {},
  57. county_list: county = {},
  58. province_list: province = {}
  59. } = areaList;
  60. const showCity = +columnsNum > 1;
  61. const showCounty = +columnsNum > 2;
  62. const getProvinceChildren = () => {
  63. if (showCity) {
  64. return placeholder.length > 1 ? [
  65. makeOption(
  66. placeholder[1],
  67. AREA_EMPTY_CODE,
  68. showCounty ? [] : void 0
  69. )
  70. ] : [];
  71. }
  72. };
  73. const provinceMap = /* @__PURE__ */ new Map();
  74. Object.keys(province).forEach((code) => {
  75. provinceMap.set(
  76. code.slice(0, 2),
  77. makeOption(province[code], code, getProvinceChildren())
  78. );
  79. });
  80. const cityMap = /* @__PURE__ */ new Map();
  81. if (showCity) {
  82. const getCityChildren = () => {
  83. if (showCounty) {
  84. return placeholder.length > 2 ? [makeOption(placeholder[2])] : [];
  85. }
  86. };
  87. Object.keys(city).forEach((code) => {
  88. const option = makeOption(city[code], code, getCityChildren());
  89. cityMap.set(code.slice(0, 4), option);
  90. const province2 = provinceMap.get(code.slice(0, 2));
  91. if (province2) {
  92. province2.children.push(option);
  93. }
  94. });
  95. }
  96. if (showCounty) {
  97. Object.keys(county).forEach((code) => {
  98. const city2 = cityMap.get(code.slice(0, 4));
  99. if (city2) {
  100. city2.children.push(makeOption(county[code], code));
  101. }
  102. });
  103. }
  104. const options = Array.from(provinceMap.values());
  105. if (placeholder.length) {
  106. const county2 = showCounty ? [makeOption(placeholder[2])] : void 0;
  107. const city2 = showCity ? [makeOption(placeholder[1], AREA_EMPTY_CODE, county2)] : void 0;
  108. options.unshift(makeOption(placeholder[0], AREA_EMPTY_CODE, city2));
  109. }
  110. return options;
  111. }