index.js 3.42 KB
import { IMG_URL } from '~/utils/constantData.js'

Component({
  externalClasses: ['custom-class'],
  properties: {
    items: {
      type: Array,
      value: []
    },
    threshold: {
      type: Number,
      value: 24
    },
    clampLines: {
      type: Number,
      value: 1
    }
  },
  data: {
    IMG_URL,
    displayItems: [],
    showPopup: false,
    popupTitle: '',
    popupText: ''
  },
  observers: {
    'items, threshold, clampLines': function (items, threshold, clampLines) {
      console.log(items)
      const display = (items || []).map(it => {
        const val = (it && it.value !== undefined && it.value !== null) ? String(it.value) : ''
        // 初始不按字符数判断,渲染后根据容器宽度再计算
        return { ...it, isLong: false, clampLines: it.clampLines || clampLines }
      })
      this.setData({ displayItems: display }, () => {
        wx.nextTick(() => this._calcOverflowByWidth())
      })
    }
  },
  methods: {
    rpx2px(rpx) {
      const sw = this._screenWidth || (this._screenWidth = wx.getSystemInfoSync().windowWidth)
      return (rpx * sw) / 750
    },
    _calcTextWidth(text, fontPx) {
      if (!text) return 0
      let width = 0
      for (let i = 0; i < text.length; i++) {
        const ch = text[i]
        if (ch === '/' || ch === '-' || ch === '·') {
          width += fontPx * 0.35
        } else if (ch === '_' || ch === ':' || ch === ';' || ch === '、' || ch === ',' || ch === '。') {
          width += fontPx * 0.5
        } else if (/\s/.test(ch)) {
          width += fontPx * 0.32
        } else if (/\d/.test(ch)) {
          width += fontPx * 0.62
        } else if (/[a-z]/.test(ch)) {
          width += fontPx * 0.62
        } else if (/[A-Z]/.test(ch)) {
          width += fontPx * 0.74
        } else if (/[\u4e00-\u9fa5]/.test(ch)) {
          width += fontPx * 0.98
        } else {
          width += fontPx * 0.55
        }
      }
      return width
    },
    _calcOverflowByWidth() {
      const query = wx.createSelectorQuery().in(this)
      query.selectAll('.value-text').boundingClientRect()
      query.exec(res => {
        const rects = (res && res[0]) || []
        const { displayItems, clampLines, threshold } = this.data
        if (!displayItems || !displayItems.length) return
        const updated = displayItems.map((it, i) => {
          const rect = rects[i] || {}
          const widthPx = rect.width || 0
          const fontRpx = it.highlight ? 36 : 24
          const fontPx = this.rpx2px(fontRpx)
          const lines = it.clampLines || clampLines || 1
          const fallback = it.threshold || threshold
          const val = (it.value !== undefined && it.value !== null) ? String(it.value) : ''
          if (!widthPx || !fontPx) {
            return { ...it, isLong: val.length > fallback }
          }
          const textWidth = this._calcTextWidth(val, fontPx)
          const isLong = textWidth > (widthPx * lines)
          return { ...it, isLong }
        })
        this.setData({ displayItems: updated })
      })
    },
    onMoreTap(e) {
      let { index, type } = e.currentTarget.dataset
      if (!type) return
      const item = this.data.displayItems[index] || {}
      this.setData({
        showPopup: true,
        popupTitle: item.label || '',
        popupText: item.value || ''
      })
    },
    onPopupConfirm() {
      this.setData({ showPopup: false })
    },
    onPopupClose() {
      this.setData({ showPopup: false })
    }
  }
})