'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
import { getNavigationBarHeight } from '../utils'
 
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
}
 
export default {
  name: 'Native',
  data () {
    return {
      position: {
        top: '0px',
        left: '0px',
        width: '0px',
        height: '0px',
        position: 'static'
      },
      hidden: false
    }
  },
  provide () {
    return {
      parentOnDraw: this._onDraw
    }
  },
  inject: {
    parentOnDraw: { default: null }
  },
  created () {
    this.isNative = true
    this.onCanInsertCallbacks = []
    this.onDrawCallbacks = []
  },
  mounted () {
    this._updatePosition()
    this.onCanInsertCallbacks.forEach(callback => callback())
    this.onCanInsertCallbacks = null
    this.$on('uni-view-update', this._requestPositionUpdate)
  },
  methods: {
    _updatePosition () {
      const rect = (this.$refs.container || this.$el).getBoundingClientRect()
      this.hidden = rect.width === 0 || rect.height === 0
      if (!this.hidden) {
        const position = this.position
        position.position = getFixed(this.$el) ? 'absolute' : 'static'
        const keys = ['top', 'left', 'width', 'height']
        keys.forEach(key => {
          let val = rect[key]
          val = key === 'top' ? val + (position.position === 'static' ? (document.documentElement.scrollTop || document.body.scrollTop || 0) : getNavigationBarHeight()) : val
          position[key] = val + 'px'
        })
      }
    },
    _requestPositionUpdate () {
      if (this._positionUpdateRequest) {
        cancelAnimationFrame(this._positionUpdateRequest)
      }
      this._positionUpdateRequest = requestAnimationFrame(() => {
        delete this._positionUpdateRequest
        this._updatePosition()
      })
    },
    _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)
      }
    }
  }
}