admin
2020-12-08 7f2d1daf6af6ecebad84d80de46a0e5024bbf5da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import fmtUnit from '../_util/fmtUnit';
Component({
  props: {
    className: '',
    // tabbar激活的 tab 样式 class
    activeCls: '',
    // tabbar的自定义样式class
    tabBarCls: '',
    // 选中选项卡下划线颜色 & 胶囊选中背景色
    tabBarUnderlineColor: '#1677FF',
    // 选中选项卡字体颜色
    tabBarActiveTextColor: '#1677FF',
    // 胶囊选中选项卡字体颜色
    capsuleTabBarActiveTextColor: '#ffffff',
    // 未选中选项卡字体颜色
    tabBarInactiveTextColor: '#333333',
    // 未选中描述字体颜色
    tabBarSubTextColor: '#999999',
    // 选中描述字体颜色
    tabBarActiveSubTextColor: '#ffffff',
    // 选项卡背景颜色
    tabBarBackgroundColor: '#ffffff',
    // 胶囊选项卡未选中的背景色
    capsuleTabBarBackgroundColor: '#e5e5e5',
    showPlus: false,
    // tabs 内容区是否可拖动,true 可拖动,内容区固定高度 false 不可拖动,内容区自适应高度
    swipeable: true,
    // 当前激活tab id
    activeTab: 0,
    animation: true,
    duration: 500,
    // 是否为胶囊形式 tab
    capsule: false,
    // 是否有副标题
    hasSubTitle: false,
    elevator: false,
    floorNumber: [],
    elevatorTop: '0px',
    showBadge: false,
    // 选中选项卡下划线宽度
    tabBarUnderlineWidth: '',
    // 选中选项卡下划线高度
    tabBarUnderlineHeight: '',
    // 电梯组件 tab-content 距离顶部高度
    elevatorContentTop: 0,
    // 通过接收外部传值,动态控制 tab-content 在 swiper 下的高度
    tabContentHeight: '',
    // plus icon 类型更多的支持
    plusIcon: 'add',
    plusIconSize: 16,
    plusIconColor: '',
    // plus icon 使用 image 的方式
    plusImg: '',
    plusImgWidth: '',
    plusImgHeight: '',
    // tab-bar 是否滚动定位在顶部的判断
    stickyBar: false
  },
  data: {
    windowWidth: 0,
    tabWidth: 0.25,
    autoplay: false,
    animation: false,
    showLeftShadow: false,
    showRightShadow: true,
    version: my.SDKVersion,
    viewScrollLeft: 0,
    tabViewNum: 0,
    hideRightShadow: false,
    boxWidth: 0,
    elWidth: 0,
    tabFontSize15: fmtUnit('15px'),
    tabFontSize13: fmtUnit('13px'),
    _showPlus: false
  },
  didMount: function didMount() {
    var _this = this;
 
    var _this$props = this.props,
        tabs = _this$props.tabs,
        animation = _this$props.animation,
        hasSubTitle = _this$props.hasSubTitle,
        elevator = _this$props.elevator,
        showPlus = _this$props.showPlus;
 
    if (tabs.length !== 0 || !tabs) {
      this.setData({
        _showPlus: showPlus
      });
      this.setWindowWidth();
 
      if (hasSubTitle) {
        this.setData({
          capsule: true
        });
      }
 
      this.setData({
        tabWidth: tabs.length > 3 ? 0.25 : 1 / tabs.length,
        animation: animation,
        autoplay: true
      });
 
      if (elevator) {
        this.setData({
          swipeable: false
        }); // 记录电梯组件总高度,并写入 data
 
        my.createSelectorQuery().select('#am-tabs-elevator-content').boundingClientRect().exec(function (ret) {
          _this.setData({
            elevatorHeight: ret[0].height
          });
        }); // 获取电梯组件每个 pane 的 top 值
 
        this.getElevatorHeight(tabs);
      } // 初始状态下,如果 activeTab 数值较大,会将后面的 tab 前移
 
 
      var boxWidth = 0;
      var elWidth = 0;
      var elLeft = 0;
      my.createSelectorQuery().select("#tabs-item-" + this.props.tabsName + "-" + this.props.activeTab).boundingClientRect().exec(function (ret) {
        elWidth = ret[0].width;
        elLeft = ret[0].left;
 
        _this.setData({
          elWidth: elWidth,
          elLeft: elLeft
        });
      });
      my.createSelectorQuery().select("#am-tabs-bar-" + this.props.tabsName + "-content").boundingClientRect().exec(function (ret) {
        boxWidth = ret[0].width;
 
        _this.setData({
          boxWidth: boxWidth
        });
 
        setTimeout(function () {
          _this.setData({
            viewScrollLeft: Math.floor(_this.data.elWidth + _this.data.elLeft - _this.data.boxWidth)
          });
        }, 300);
      });
    }
  },
  didUpdate: function didUpdate(prevProps, prevData) {
    var _this2 = this;
 
    var _this$props2 = this.props,
        tabs = _this$props2.tabs,
        elevator = _this$props2.elevator,
        showPlus = _this$props2.showPlus;
    this.setData({
      _showPlus: showPlus
    });
 
    if (prevProps.tabs.length !== tabs.length) {
      this.setData({
        tabWidth: tabs.length > 3 ? 0.25 : 1 / tabs.length
      });
    }
 
    if (elevator) {
      // 当 didUpdate 时判断电梯组件总高度是否发生变化
      my.createSelectorQuery().select('#am-tabs-elevator-content').boundingClientRect().exec(function (ret) {
        if (ret[0].height !== _this2.data.elevatorHeight) {
          // 如高度变化将页面滚动至顶部,重新设置电梯总高度
          my.pageScrollTo({
            scrollTop: 0,
            success: function success() {
              _this2.setData({
                elevatorHeight: ret[0].height
              }); // 总高度变化后,重新获取电梯组件每个 panel 的 top 值
 
 
              _this2.getElevatorHeight(tabs);
            }
          });
        }
      });
      this.$page.data.floorNumber = this.data.floorNumber;
 
      if (this.$page.data.getFloorNumber >= 0) {
        this.setData({
          tabViewNum: this.$page.data.getFloorNumber,
          prevTabViewNum: prevData.tabViewNum
        });
      }
    }
  },
  methods: {
    setWindowWidth: function setWindowWidth() {
      var _this3 = this;
 
      my.getSystemInfo({
        success: function success(res) {
          _this3.setData({
            windowWidth: res.windowWidth
          });
        }
      });
    },
    getElevatorHeight: function getElevatorHeight(tabs) {
      var _this4 = this;
 
      var _loop = function _loop(i) {
        my.createSelectorQuery().select("#am-tabs-elevator-pane-" + i).boundingClientRect().select('.am-tabs-bar-sticky').boundingClientRect().exec(function (ret) {
          var _this4$props = _this4.props,
              elevatorTop = _this4$props.elevatorTop,
              elevatorContentTop = _this4$props.elevatorContentTop;
          var tabContentDistance = 0;
 
          if (elevatorTop.match(/\d+px/)) {
            tabContentDistance = parseInt(elevatorTop, 10);
          } else {
            tabContentDistance = parseInt(elevatorContentTop, 10);
          }
 
          _this4.props.floorNumber[i] = ret[0].top - ret[1].height - tabContentDistance;
 
          _this4.setData({
            floorNumber: _this4.props.floorNumber
          });
        });
      };
 
      for (var i = 0; i < tabs.length; i++) {
        _loop(i);
      }
 
      setTimeout(function () {
        _this4.$page.data.floorNumber = _this4.data.floorNumber;
      }, 100);
    },
    handleSwiperChange: function handleSwiperChange(e) {
      var current = e.detail.current;
      var tabsName = e.target.dataset.tabsName;
      this.setData({
        tabViewNum: current
      });
 
      if (this.props.onChange) {
        this.props.onChange({
          index: current,
          tabsName: tabsName
        });
      }
    },
    handleTabClick: function handleTabClick(e) {
      var _this5 = this;
 
      var _e$target$dataset = e.target.dataset,
          index = _e$target$dataset.index,
          tabsName = _e$target$dataset.tabsName,
          floor = _e$target$dataset.floor;
      var boxWidth = 0;
      var elWidth = 0;
      my.createSelectorQuery().select("#" + e.currentTarget.id).boundingClientRect().exec(function (ret) {
        elWidth = ret[0].width;
 
        _this5.setData({
          elWidth: elWidth
        });
      });
      my.createSelectorQuery().select("#am-tabs-bar-" + tabsName + "-content").boundingClientRect().exec(function (ret) {
        boxWidth = ret[0].width;
 
        _this5.setData({
          boxWidth: boxWidth
        });
 
        if (_this5.data.elWidth > _this5.data.boxWidth / 2) {
          setTimeout(function () {
            _this5.setData({
              viewScrollLeft: e.currentTarget.offsetLeft - 40
            });
          }, 300);
        } else {
          setTimeout(function () {
            _this5.setData({
              viewScrollLeft: e.currentTarget.offsetLeft - Math.floor(_this5.data.boxWidth / 2)
            });
          }, 300);
        }
      });
 
      if (this.props.onTabClick && !this.props.elevator) {
        this.props.onTabClick({
          index: index,
          tabsName: tabsName
        });
      }
 
      if (this.props.onTabClick && this.props.elevator) {
        this.setData({
          tabViewNum: this.data.prevTabViewNum
        });
        setTimeout(function () {
          _this5.props.onTabClick({
            index: index,
            tabsName: tabsName
          });
        }, 300);
        my.pageScrollTo({
          scrollTop: Math.ceil(floor),
          duration: 1
        });
      }
    },
    handlePlusClick: function handlePlusClick() {
      if (this.props.onPlusClick) {
        this.props.onPlusClick();
      }
    },
    showLeftShadow: function showLeftShadow(e) {
      var _e$detail = e.detail,
          scrollLeft = _e$detail.scrollLeft,
          scrollWidth = _e$detail.scrollWidth; // 判断是否隐藏左边的阴影
 
      if (scrollLeft > 0) {
        this.setData({
          showLeftShadow: true
        });
      } else {
        this.setData({
          showLeftShadow: false
        });
      } // 判断是否隐藏右边的阴影
 
 
      if (scrollLeft + this.data.boxWidth >= scrollWidth - 8) {
        this.setData({
          showRightShadow: false
        });
      } else {
        this.setData({
          showRightShadow: true
        });
      }
    },
    onTabFirstShow: function onTabFirstShow(e) {
      // SDKversion 最低要求 1.9.4
      var _e$target$dataset2 = e.target.dataset,
          index = _e$target$dataset2.index,
          tabsName = _e$target$dataset2.tabsName;
 
      if (this.props.onTabFirstShow) {
        this.props.onTabFirstShow({
          index: index,
          tabsName: tabsName
        });
      }
    }
  }
});