index.js 5.16 KB
Newer Older
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
// static/nativeComponents/CubeNav/index.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgList: [],
    getColumnWidth: '',
    getImgSize: 0,
    navShow: false,
    wrapW: 0,
    w: 0,
    swiperList: [],
    swiperItemW: 0,
    navLeft: 0,
    cube2W: 0,
    cube3H: 0,
  },
  ready() {
    this.init()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {

      let _this = this;

      let columnWidthArr = ['100%', '50%', '33.3%', '25%', '20%', '16.6%']
程智春 committed
42 43

      this.data.datas.componentData.imgList.forEach(item => {
李嘉林 committed
44
        console.log(item,'---------------44')
程智春 committed
45 46
        item.imageUrl = item.imageUrl ? app.DFSImg(item.imageUrl,400,400) : ''
      })
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      this.setData({
        imgList: this.data.datas.componentData.imgList.filter(item => item.visible == undefined || item.visible == 1),
        getColumnWidth: columnWidthArr[this.data.datas.componentData.columnNum - 1],
        getImgSize: (100 - this.data.datas.componentData.imgSize) / 2
      })

      let arr = []
      let count = this.data.datas.componentData.columnNum * this.data.datas.componentData.rowNum;
      let num = Math.ceil(this.data.imgList.length / count);
      for (let i = 0; i < num; i++) {
        arr.push([]);
      }
      this.data.imgList.forEach((item, index) => {
        let i = Math.floor(index / count);
        arr[i].push(item);
      });

      this.setData({
        swiperList: arr
      })

      this.createSelectorQuery().select('.cube-item').boundingClientRect(function(res) {
        console.log('swiper-wrap', res)
        if (res) {
程智春 committed
71 72
          if (_this.data.datas.componentData.rowNum == 1){
            _this.setData({
程智春 committed
73
              cube3H: res.height * _this.data.datas.componentData.rowNum
程智春 committed
74 75 76 77
            })
          } else if (_this.data.datas.componentData.rowNum == 2){
            if(_this.data.imgList.length / _this.data.datas.componentData.columnNum > 1){
              _this.setData({
程智春 committed
78
                cube3H: res.height * _this.data.datas.componentData.rowNum
程智春 committed
79 80 81
              })
            }else{
              _this.setData({
程智春 committed
82
                cube3H: res.height * 1
程智春 committed
83 84 85 86 87
              })
            }
          } else if (_this.data.datas.componentData.rowNum == 3){
            if (_this.data.datas.componentData.columnNum == 1){
              _this.setData({
程智春 committed
88
                cube3H: res.height * (_this.data.imgList / _this.data.datas.componentData.rowNum)
程智春 committed
89 90 91
              })
            }else{
              _this.setData({
程智春 committed
92
                cube3H: res.height * (Math.ceil(_this.data.imgList.length / _this.data.datas.componentData.columnNum))
程智春 committed
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
        }

      }).exec()

      console.log('轮播图swiperList', this.data.swiperList)

      this.createSelectorQuery().select('.cube2').boundingClientRect(function(res) {
        console.log('来这里了', res)
        if (res) {
          let cube2W = res.width

          _this.setData({
            cube2W: cube2W
          })
          let wArr = [cube2W / 1, cube2W / 2, cube2W / 3, cube2W / 4, cube2W / 5, cube2W / 6]


          if (
            Math.floor(
              _this.data.imgList.length /
              (_this.data.datas.componentData.columnNum * _this.data.datas.componentData.rowNum)
            ) == 0
          ) {
            _this.setData({
              wrapW: cube2W
            })
          } else {
            _this.setData({
              wrapW: Math.floor(
                  _this.data.imgList.length /
                  (_this.data.datas.componentData.columnNum * _this.data.datas.componentData.rowNum)
                ) *
                cube2W + (_this.data.imgList.length %
                  (_this.data.datas.componentData.columnNum *
                    _this.data.datas.componentData.rowNum)) *
                wArr[_this.data.datas.componentData.columnNum - 1]
            })
          }
          _this.setData({
            w: wArr[_this.data.datas.componentData.columnNum - 1]
          })
程智春 committed
137
          console.log(_this.data.w)
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
          if (_this.data.wrapW > cube2W) {
            _this.setData({
              navShow: true
            })
          } else {
            _this.setData({
              navShow: false
            })
          }
        }
      }).exec()
    },
    touchmove(e) {
      let _this = this
      this.debounce(function() {
        let moveX = _this.data.wrapW - _this.data.cube2W;
        _this.createSelectorQuery().select('#wrapW').boundingClientRect(function(res) {
          if (res) {
            let x = Math.abs(res.left) / moveX;
            _this.setData({
              navLeft: 30 * x
            })
          }
        }).exec()
      }, 100)()

    },
    debounce(func, wait) {
      let timeout = "";
      return v => {
        if (timeout) {
          clearTimeout(timeout);
        }
        timeout = setTimeout(() => {
          func(v);
        }, wait);
      };
    },
    onclickHandle(e){
      let item = e.currentTarget.dataset.item
      console.log(item)
侯体倬 committed
179
      app.trackCpn(e, this.data.datas.componentName, item)
李嘉林 committed
180
      console.log(item,'---------------------180')
程智春 committed
181
      app.$themeToLink(item.link)
182 183
    }
  }
侯体倬 committed
184
})