index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // components/My_categoryListings/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. height: {
  8. type: String,
  9. value: '500'
  10. },
  11. list: {
  12. type: Array,
  13. value: []
  14. }
  15. },
  16. options: {
  17. addGlobalClass: true
  18. },
  19. lifetimes: {
  20. ready() {}
  21. },
  22. /**
  23. * 组件的初始数据
  24. */
  25. data: {
  26. systemIndex: 0, //系统index
  27. portIndex: 0, //选择端口Index
  28. moduleIndex: 0, //选择模块Index
  29. portID: null, //端口ID
  30. leftIntoViewId: null,
  31. rightIntoViewId: null,
  32. heightList: [],
  33. modulesIndex: 0, //模块列表
  34. scrollTop: 0,
  35. checkList: [], //选中列表
  36. },
  37. observers: {
  38. 'list': function (list) {
  39. console.log(12)
  40. if (!list.length) return;
  41. let data = list[0].clients[0];
  42. let id = data.modules[0] ? "M" + data.modules[0].systemmoduleid : '';
  43. if (this.data.portID == null) this.setData({
  44. portID: 'S' + data.systemclientid,
  45. leftIntoViewId: id,
  46. rightIntoViewId: id
  47. })
  48. setTimeout(() => {
  49. this.getAppsHeight();
  50. }, 200)
  51. }
  52. },
  53. /**
  54. * 组件的方法列表
  55. */
  56. methods: {
  57. /* 模块选择 */
  58. checkBack(e) {
  59. let list = this.data.checkList,
  60. obj = {
  61. id: e.target.id - 0,
  62. arr: JSON.parse(JSON.stringify(e.detail.arr))
  63. };
  64. const index = list.findIndex(v => v.id == obj.id);
  65. if (index == -1) {
  66. list.push(obj)
  67. } else {
  68. list.splice(index, 1, obj)
  69. }
  70. let idList = e.detail.apps.map(v => v.systemappid);
  71. const i = list.findIndex(v => v.id == obj.id);
  72. list[i].arr = list[i].arr.filter(v => idList.includes(v.systemappid));
  73. this.setData({
  74. checkList: list
  75. });
  76. const mIndex = this.data.list[this.data.systemIndex].clients[this.data.portIndex].modules.findIndex(v => v.systemmoduleid == obj.id);
  77. this.setData({
  78. [`list[${this.data.systemIndex}].clients[${this.data.portIndex}].modules[${mIndex}].apps`]: e.detail.apps
  79. })
  80. },
  81. backData() {
  82. let newArr = [];
  83. this.data.checkList.forEach(v => newArr = newArr.concat(v.arr));
  84. return newArr;
  85. },
  86. /* 系统分类 */
  87. changePortID(e) {
  88. const {
  89. dataset
  90. } = e.currentTarget;
  91. this.setData({
  92. portID: e.target.id,
  93. systemIndex: dataset.index,
  94. portIndex: dataset.i,
  95. modulesIndex: 0,
  96. scrollTop: 0
  97. });
  98. setTimeout(() => {
  99. this.getAppsHeight();
  100. }, 300)
  101. },
  102. /* 点击模块分类 */
  103. changeType(e) {
  104. this.setData({
  105. leftIntoViewId: e.target.id,
  106. rightIntoViewId: e.target.id
  107. })
  108. },
  109. getAppsHeight() {
  110. const list = this.data.list[this.data.systemIndex].clients[this.data.portIndex].modules,
  111. that = this;
  112. let heightList = [];
  113. let id = 'M' + list[0].systemmoduleid;
  114. for (let i = 0; i < list.length; i++) {
  115. let query = wx.createSelectorQuery().in(that).select('.' + 'M1' + list[i].systemmoduleid).boundingClientRect();
  116. query.exec(res => {
  117. if (!res[0]) return this.getAppsHeight();
  118. heightList.push(res[0])
  119. if (list.length == heightList.length) {
  120. this.setData({
  121. heightList,
  122. leftIntoViewId: id,
  123. rightIntoViewId: id
  124. })
  125. let MyArr = that.selectAllComponents('.My_group');
  126. for (let k = 0; k < MyArr.length; k++) {
  127. MyArr[k].refactorDom();
  128. }
  129. }
  130. })
  131. };
  132. },
  133. viewScroll({
  134. detail
  135. }) {
  136. let arr = this.data.heightList, //获取元素信息
  137. id = this.data.leftIntoViewId, //获取当前左侧栏选项
  138. i = arr.findIndex(v => v.id == id), //选项当前在数组中的索引号
  139. scrollTop = detail.scrollTop,
  140. //判断当前索引是不是数组最后一条数据,不是的话拿下一个索引的距顶距离
  141. top = (i + 1 == arr.length) ? arr[i].top - 15 - arr[i].height : arr[i + 1].top - 15 - arr[i + 1].height;
  142. if (scrollTop >= top) {
  143. if (!arr[i + 1]) return;
  144. if (id == arr[i + 1].id) return;
  145. this.setData({
  146. leftIntoViewId: arr[i + 1].id
  147. })
  148. } else if (scrollTop < top) {
  149. if (!arr[i - 1] || id == arr[0].id) return;
  150. if (scrollTop < Math.abs(arr[i - 1].top - arr[i - 1].height / 2)) this.setData({
  151. leftIntoViewId: arr[i - 1].id
  152. })
  153. } else {
  154. if (id == arr[1].id) return; //排除第一个
  155. }
  156. },
  157. }
  158. })