liusuyi
2023-04-24 4737f1e038743ced243c9e52423404d9034d6107
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
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
137
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
179
<script>
  import Vue from 'vue'
  import { watchSize, setupResizeAndScrollEventListeners, find } from '../utils'
  import Menu from './Menu'
 
  const PortalTarget = {
    name: 'vue-treeselect--portal-target',
    inject: [ 'instance' ],
 
    watch: {
      'instance.menu.isOpen'(newValue) {
        if (newValue) {
          this.setupHandlers()
        } else {
          this.removeHandlers()
        }
      },
 
      'instance.menu.placement'() {
        this.updateMenuContainerOffset()
      },
    },
 
    created() {
      this.controlResizeAndScrollEventListeners = null
      this.controlSizeWatcher = null
    },
 
    mounted() {
      const { instance } = this
 
      if (instance.menu.isOpen) this.setupHandlers()
    },
 
    methods: {
      setupHandlers() {
        this.updateWidth()
        this.updateMenuContainerOffset()
        this.setupControlResizeAndScrollEventListeners()
        this.setupControlSizeWatcher()
      },
 
      removeHandlers() {
        this.removeControlResizeAndScrollEventListeners()
        this.removeControlSizeWatcher()
      },
 
      setupControlResizeAndScrollEventListeners() {
        const { instance } = this
        const $control = instance.getControl()
 
        // istanbul ignore next
        if (this.controlResizeAndScrollEventListeners) return
 
        this.controlResizeAndScrollEventListeners = {
          remove: setupResizeAndScrollEventListeners($control, this.updateMenuContainerOffset),
        }
      },
 
      setupControlSizeWatcher() {
        const { instance } = this
        const $control = instance.getControl()
 
        // istanbul ignore next
        if (this.controlSizeWatcher) return
 
        this.controlSizeWatcher = {
          remove: watchSize($control, () => {
            this.updateWidth()
            this.updateMenuContainerOffset()
          }),
        }
      },
 
      removeControlResizeAndScrollEventListeners() {
        if (!this.controlResizeAndScrollEventListeners) return
 
        this.controlResizeAndScrollEventListeners.remove()
        this.controlResizeAndScrollEventListeners = null
      },
 
      removeControlSizeWatcher() {
        if (!this.controlSizeWatcher) return
 
        this.controlSizeWatcher.remove()
        this.controlSizeWatcher = null
      },
 
      updateWidth() {
        const { instance } = this
        const $portalTarget = this.$el
        const $control = instance.getControl()
        const controlRect = $control.getBoundingClientRect()
 
        $portalTarget.style.width = controlRect.width + 'px'
      },
 
      updateMenuContainerOffset() {
        const { instance } = this
        const $control = instance.getControl()
        const $portalTarget = this.$el
        const controlRect = $control.getBoundingClientRect()
        const portalTargetRect = $portalTarget.getBoundingClientRect()
        const offsetY = instance.menu.placement === 'bottom' ? controlRect.height : 0
        const left = Math.round(controlRect.left - portalTargetRect.left) + 'px'
        const top = Math.round(controlRect.top - portalTargetRect.top + offsetY) + 'px'
        const menuContainerStyle = this.$refs.menu.$refs['menu-container'].style
        const transformVariations = [ 'transform', 'webkitTransform', 'MozTransform', 'msTransform' ]
        const transform = find(transformVariations, t => t in document.body.style)
 
        // IE9 doesn't support `translate3d()`.
        menuContainerStyle[transform] = `translate(${left}, ${top})`
      },
    },
 
    render() {
      const { instance } = this
      const portalTargetClass = [ 'vue-treeselect__portal-target', instance.wrapperClass ]
      const portalTargetStyle = { zIndex: instance.zIndex }
 
      return (
        <div class={portalTargetClass} style={portalTargetStyle} data-instance-id={instance.getInstanceId()}>
          <Menu ref="menu" />
        </div>
      )
    },
 
    destroyed() {
      this.removeHandlers()
    },
  }
 
  let placeholder
 
  export default {
    name: 'vue-treeselect--menu-portal',
 
    created() {
      this.portalTarget = null
    },
 
    mounted() {
      this.setup()
    },
 
    destroyed() {
      this.teardown()
    },
 
    methods: {
      setup() {
        const el = document.createElement('div')
        document.body.appendChild(el)
 
        this.portalTarget = new Vue({
          el,
          parent: this,
          ...PortalTarget,
        })
      },
 
      teardown() {
        document.body.removeChild(this.portalTarget.$el)
        this.portalTarget.$el.innerHTML = ''
 
        this.portalTarget.$destroy()
        this.portalTarget = null
      },
    },
 
    render() {
      if (!placeholder) placeholder = (
        <div class="vue-treeselect__menu-placeholder" />
      )
 
      return placeholder
    },
  }
</script>