uni-collapse-item.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "uniCollapseItem",
  5. props: {
  6. // 列表标题
  7. title: {
  8. type: String,
  9. default: ""
  10. },
  11. name: {
  12. type: [Number, String],
  13. default: ""
  14. },
  15. // 是否禁用
  16. disabled: {
  17. type: Boolean,
  18. default: false
  19. },
  20. // 是否显示动画
  21. showAnimation: {
  22. type: Boolean,
  23. default: true
  24. },
  25. // 是否展开
  26. open: {
  27. type: Boolean,
  28. default: false
  29. },
  30. // 缩略图
  31. thumb: {
  32. type: String,
  33. default: ""
  34. },
  35. // 标题分隔线显示类型
  36. titleBorder: {
  37. type: String,
  38. default: "auto"
  39. },
  40. border: {
  41. type: Boolean,
  42. default: true
  43. },
  44. showArrow: {
  45. type: Boolean,
  46. default: true
  47. }
  48. },
  49. data() {
  50. const elId = `Uni_${Math.ceil(Math.random() * 1e6).toString(36)}`;
  51. return {
  52. isOpen: false,
  53. isheight: null,
  54. height: 0,
  55. elId,
  56. nameSync: 0
  57. };
  58. },
  59. watch: {
  60. open(val) {
  61. this.isOpen = val;
  62. this.onClick(val, "init");
  63. }
  64. },
  65. updated(e) {
  66. this.$nextTick(() => {
  67. this.init(true);
  68. });
  69. },
  70. created() {
  71. this.collapse = this.getCollapse();
  72. this.oldHeight = 0;
  73. this.onClick(this.open, "init");
  74. },
  75. // TODO vue3
  76. unmounted() {
  77. this.__isUnmounted = true;
  78. this.uninstall();
  79. },
  80. mounted() {
  81. if (!this.collapse)
  82. return;
  83. if (this.name !== "") {
  84. this.nameSync = this.name;
  85. } else {
  86. this.nameSync = this.collapse.childrens.length + "";
  87. }
  88. if (this.collapse.names.indexOf(this.nameSync) === -1) {
  89. this.collapse.names.push(this.nameSync);
  90. } else {
  91. console.warn(`name 值 ${this.nameSync} 重复`);
  92. }
  93. if (this.collapse.childrens.indexOf(this) === -1) {
  94. this.collapse.childrens.push(this);
  95. }
  96. this.init();
  97. },
  98. methods: {
  99. init(type) {
  100. this.getCollapseHeight(type);
  101. },
  102. uninstall() {
  103. if (this.collapse) {
  104. this.collapse.childrens.forEach((item, index) => {
  105. if (item === this) {
  106. this.collapse.childrens.splice(index, 1);
  107. }
  108. });
  109. this.collapse.names.forEach((item, index) => {
  110. if (item === this.nameSync) {
  111. this.collapse.names.splice(index, 1);
  112. }
  113. });
  114. }
  115. },
  116. onClick(isOpen, type) {
  117. if (this.disabled)
  118. return;
  119. this.isOpen = isOpen;
  120. if (this.isOpen && this.collapse) {
  121. this.collapse.setAccordion(this);
  122. }
  123. if (type !== "init") {
  124. this.collapse.onChange(isOpen, this);
  125. }
  126. },
  127. getCollapseHeight(type, index = 0) {
  128. const views = common_vendor.index.createSelectorQuery().in(this);
  129. views.select(`#${this.elId}`).fields({
  130. size: true
  131. }, (data) => {
  132. if (index >= 10)
  133. return;
  134. if (!data) {
  135. index++;
  136. this.getCollapseHeight(false, index);
  137. return;
  138. }
  139. this.height = data.height;
  140. this.isheight = true;
  141. if (type)
  142. return;
  143. this.onClick(this.isOpen, "init");
  144. }).exec();
  145. },
  146. getNvueHwight(type) {
  147. dom.getComponentRect(this.$refs["collapse--hook"], (option) => {
  148. if (option && option.result && option.size) {
  149. this.height = option.size.height;
  150. this.isheight = true;
  151. if (type)
  152. return;
  153. this.onClick(this.open, "init");
  154. }
  155. });
  156. },
  157. /**
  158. * 获取父元素实例
  159. */
  160. getCollapse(name = "uniCollapse") {
  161. let parent = this.$parent;
  162. let parentName = parent.$options.name;
  163. while (parentName !== name) {
  164. parent = parent.$parent;
  165. if (!parent)
  166. return false;
  167. parentName = parent.$options.name;
  168. }
  169. return parent;
  170. }
  171. }
  172. };
  173. if (!Array) {
  174. const _easycom_uni_icons2 = common_vendor.resolveComponent("uni-icons");
  175. _easycom_uni_icons2();
  176. }
  177. const _easycom_uni_icons = () => "../../../uni-icons/components/uni-icons/uni-icons.js";
  178. if (!Math) {
  179. _easycom_uni_icons();
  180. }
  181. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  182. return common_vendor.e({
  183. a: $props.thumb
  184. }, $props.thumb ? {
  185. b: $props.thumb
  186. } : {}, {
  187. c: common_vendor.t($props.title),
  188. d: $props.disabled ? 1 : "",
  189. e: $props.showArrow
  190. }, $props.showArrow ? {
  191. f: common_vendor.p({
  192. color: $props.disabled ? "#ddd" : "#bbb",
  193. size: "14",
  194. type: "bottom"
  195. }),
  196. g: $data.isOpen ? 1 : "",
  197. h: $props.showAnimation === true ? 1 : ""
  198. } : {}, {
  199. i: common_vendor.o(($event) => $options.onClick(!$data.isOpen)),
  200. j: $data.isOpen && $props.titleBorder === "auto" ? 1 : "",
  201. k: $props.titleBorder !== "none" ? 1 : "",
  202. l: $data.elId,
  203. m: $data.isheight ? 1 : "",
  204. n: $props.border && $data.isOpen ? 1 : "",
  205. o: $props.showAnimation ? 1 : "",
  206. p: ($data.isOpen ? $data.height : 0) + "px"
  207. });
  208. }
  209. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/Users/RuHu.Xu/Desktop/mall-newfeifan-zx-app/uni_modules/uni-collapse/components/uni-collapse-item/uni-collapse-item.vue"]]);
  210. wx.createComponent(Component);