native.js 2.9 KB
Newer Older
1
import { getNavigationBarHeight } from '../utils'
2 3 4 5 6 7 8 9 10 11 12 13 14

function getFixed ($el) {
  let fixed
  while ($el) {
    const style = getComputedStyle($el)
    const transform = style.transform || style.webkitTransform
    fixed = transform && transform !== 'none' ? false : fixed
    fixed = style.position === 'fixed' ? true : fixed
    $el = $el.parentElement
  }
  return fixed
}

Q
qiang 已提交
15 16 17 18
export default {
  name: 'Native',
  data () {
    return {
Q
qiang 已提交
19
      position: {
Q
qiang 已提交
20 21 22 23 24 25
        top: '0px',
        left: '0px',
        width: '0px',
        height: '0px',
        position: 'static'
      },
Q
qiang 已提交
26
      hidden: false
Q
qiang 已提交
27 28
    }
  },
D
DCloud_LXH 已提交
29 30 31 32 33 34 35 36
  provide () {
    return {
      parentOnDraw: this._onDraw
    }
  },
  inject: {
    parentOnDraw: { default: null }
  },
Q
qiang 已提交
37 38 39
  created () {
    this.isNative = true
    this.onCanInsertCallbacks = []
D
DCloud_LXH 已提交
40
    this.onDrawCallbacks = []
Q
qiang 已提交
41
  },
Q
qiang 已提交
42
  mounted () {
Q
qiang 已提交
43
    this._updatePosition()
D
DCloud_LXH 已提交
44 45
    this.onCanInsertCallbacks.forEach(callback => callback())
    this.onCanInsertCallbacks = null
Q
qiang 已提交
46
    this.$on('uni-view-update', this._requestPositionUpdate)
Q
qiang 已提交
47 48
  },
  methods: {
Q
qiang 已提交
49 50 51 52
    _updatePosition () {
      const rect = (this.$refs.container || this.$el).getBoundingClientRect()
      this.hidden = rect.width === 0 || rect.height === 0
      if (!this.hidden) {
53 54 55 56
        const position = this.position
        position.position = getFixed(this.$el) ? 'absolute' : 'static'
        const keys = ['top', 'left', 'width', 'height']
        keys.forEach(key => {
Q
qiang 已提交
57
          let val = rect[key]
58 59
          val = key === 'top' ? val + (position.position === 'static' ? (document.documentElement.scrollTop || document.body.scrollTop || 0) : getNavigationBarHeight()) : val
          position[key] = val + 'px'
Q
qiang 已提交
60 61
        })
      }
Q
qiang 已提交
62 63 64 65 66 67 68 69 70
    },
    _requestPositionUpdate () {
      if (this._positionUpdateRequest) {
        cancelAnimationFrame(this._positionUpdateRequest)
      }
      this._positionUpdateRequest = requestAnimationFrame(() => {
        delete this._positionUpdateRequest
        this._updatePosition()
      })
D
DCloud_LXH 已提交
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
    },
    _onParentReady (parentReadyCallback) {
      const callback = (parentPosition) => {
        parentReadyCallback(parentPosition)
        this.onDrawCallbacks.forEach(callback => callback(this.position))
        this.onDrawCallbacks = null
      }
      this._onSelfReady(() => {
        if (this.parentOnDraw) {
          this.parentOnDraw(callback)
        } else {
          callback({
            top: '0px',
            left: '0px',
            width: Number.MAX_SAFE_INTEGER + 'px',
            height: Number.MAX_SAFE_INTEGER + 'px',
            position: 'static'
          })
        }
      })
    },
    _onSelfReady (callback) {
      if (this.onCanInsertCallbacks) {
        this.onCanInsertCallbacks.push(callback)
      } else {
        callback()
      }
    },
    _onDraw (callback) {
      if (this.onDrawCallbacks) {
        this.onDrawCallbacks.push(callback)
      } else {
        callback(this.position)
      }
Q
qiang 已提交
105 106 107
    }
  }
}