native.js 1.7 KB
Newer Older
Q
qiang 已提交
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
import {
  listeners
} from 'uni-mixins'

export default {
  name: 'Native',
  mixins: [listeners],
  data () {
    return {
      style: {
        top: '0px',
        left: '0px',
        width: '0px',
        height: '0px',
        position: 'static'
      },
      hidden: false,
      tags: []
    }
  },
  mounted () {
    this._updateStyle()
    this.$nextTick(() => {
      if (this.tags.length) {
        const view = this.view = plus.nativeObj.View('view' + Date.now(), this.style, this.tags)
        plus.webview.currentWebview().append(view)
        if (this.hidden) {
          view.hide()
        }
      }
    })
  },
  beforeDestroy () {
    this.view && this.view.close()
    delete this.view
  },
  listeners: {
    '@view-update': '_requestUpdate'
  },
  methods: {
    insertTextView () {

    },
    updateTextView () {

    },
    removeTextView () {

    },
    insertImageView () {

    },
    updateImageView () {

    },
    removeImageView () {

    },
    _updateStyle () {
      const rect = this.$refs.container.getBoundingClientRect()
      this.hidden = false;
      ['top', 'left', 'width', 'height'].forEach(key => {
        let val = rect[key]
        val = key === 'top' ? val + (document.documentElement.scrollTop || document.body.scrollTop || 0) : val
        if (!val && (key === 'width' || key === 'height')) {
          this.hidden = true
        }
        this.style[key] = val + 'px'
      })
    },
    _requestUpdate () {
      if (this._animationFrame) {
        cancelAnimationFrame(this._animationFrame)
      }
      if (this._isMounted) {
        this._animationFrame = requestAnimationFrame(() => {
          delete this._animationFrame
          this._updateStyle()
        })
      }
    }
  }
}