update: [tooltip] more API supported

This commit is contained in:
wanlei 2017-11-06 12:13:36 +08:00
parent 3f09f3f6dc
commit d79ac1e85d
2 changed files with 113 additions and 29 deletions

View File

@ -2,19 +2,37 @@
<div> <div>
<tool-tip <tool-tip
placement="top" placement="top"
:title="showText"> :title="showText"
<h1 @click="boom" style="display: inline-block">This is just a test, put your cursor here</h1> :autoAdjustOverflow="autoAdjustOverflow"
>
<h1 @click="boom" class="test">撞到边缘翻转位置 & 点击更新</h1>
</tool-tip> </tool-tip>
<ant-button>{{showText}}</ant-button> <ant-button @click="reverse" type="primary">{{autoAdjustOverflow ? '启用' : '关闭'}}自动调整中</ant-button>
<div class="box"> <div class="box">
<h2>切换arrowPointAtCenter模式</h2>
<ant-button @click="change">{{arrowPointAtCenter}}</ant-button>
<table> <table>
<tr v-for="(tr, index) in table" :key="index"> <tr v-for="(tr, index) in table" :key="index">
<td v-for="(td, i) in tr" :key="i"> <td v-for="(td, i) in tr" :key="i">
<tool-tip v-if="td" :placement="td" :title="td"><AntButton type="primary">{{td}}</AntButton></tool-tip> <tool-tip
v-if="td"
:placement="td"
:title="td"
:arrowPointAtCenter="arrowPointAtCenter"
>
<AntButton type="primary">{{td}}</AntButton>
</tool-tip>
</td> </td>
</tr> </tr>
</table> </table>
</div> </div>
<div>
<p>
<tool-tip :arrowPointAtCenter="true" title="Consider using the NamedModulesPlugin for module names." placement="topLeft">
<ant-button>arrowPointAtCenter arrowPointAtCenter arrowPointAtCenter</ant-button>
</tool-tip>
</p>
</div>
</div> </div>
</template> </template>
@ -33,7 +51,9 @@
['left', '', '', '', 'right'], ['left', '', '', '', 'right'],
['leftBottom', '', '', '', 'rightBottom'], ['leftBottom', '', '', '', 'rightBottom'],
['', 'bottomLeft', 'bottom', 'bottomRight', ''], ['', 'bottomLeft', 'bottom', 'bottomRight', ''],
] ],
arrowPointAtCenter: false,
autoAdjustOverflow: true,
} }
}, },
methods: { methods: {
@ -43,6 +63,12 @@
} else { } else {
this.showText += ' ' this.showText += ' '
} }
},
change() {
this.arrowPointAtCenter = !this.arrowPointAtCenter
},
reverse() {
this.autoAdjustOverflow = !this.autoAdjustOverflow
} }
}, },
components: { components: {
@ -52,6 +78,10 @@
} }
</script> </script>
<style scoped lang="less"> <style scoped lang="less">
.test {
margin: 20px;
display: inline-block;
}
.box { .box {
margin: 100px; margin: 100px;
} }

View File

@ -34,6 +34,8 @@ export default {
left: 0, left: 0,
top: 0, top: 0,
realPlacement: this.placement, realPlacement: this.placement,
t1: null,
t2: null,
} }
}, },
computed: { computed: {
@ -72,13 +74,30 @@ export default {
top: 0, top: 0,
} }
}, },
methods: {
hideSelf(e) {
if (that.t1) {
clearTimeout(that.t1)
that.t1 = null
}
if (that.mouseLeaveDelay) {
that.t2 = window.setTimeout(() => {
if (e.relatedTarget === that.$el) {
return
}
that.visible = false
}, +that.mouseLeaveDelay * 1e3)
}
}
},
render(h) { render(h) {
return ( return (
<transition name="zoom-big"> <transition name={that.transitionName}>
<div <div
v-show={that.visible} v-show={that.visible}
class={`ant-tooltip ant-tooltip-placement-${that.realPlacement}`} class={`ant-tooltip ant-tooltip-placement-${that.realPlacement}`}
style={{ left: this.left + 'px', top: this.top + 'px' }} style={{ left: this.left + 'px', top: this.top + 'px' }}
onMouseleave={this.hideSelf}
> >
<div class="ant-tooltip-content"> <div class="ant-tooltip-content">
<div class="ant-tooltip-arrow"/> <div class="ant-tooltip-arrow"/>
@ -98,25 +117,25 @@ export default {
}, },
onPopupAlign: (placement, domNode, target, align) => { onPopupAlign: (placement, domNode, target, align) => {
if (!placement) { if (!placement) {
return; return
} }
// //
const rect = domNode.getBoundingClientRect() const rect = domNode.getBoundingClientRect()
const transformOrigin = { const transformOrigin = {
top: '50%', top: '50%',
left: '50%', left: '50%',
}; }
if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) { if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
transformOrigin.top = `${rect.height - align.offset[1]}px`; transformOrigin.top = `${rect.height - align.offset[1]}px`
} else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) { } else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
transformOrigin.top = `${-align.offset[1]}px`; transformOrigin.top = `${-align.offset[1]}px`
} }
if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) { if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
transformOrigin.left = `${rect.width - align.offset[0]}px`; transformOrigin.left = `${rect.width - align.offset[0]}px`
} else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) { } else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
transformOrigin.left = `${-align.offset[0]}px`; transformOrigin.left = `${-align.offset[0]}px`
} }
target.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`; target.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`
}, },
addEventHandle(old, fn) { addEventHandle(old, fn) {
if (!old) { if (!old) {
@ -127,29 +146,36 @@ export default {
return old === fn ? old : [old, fn] return old === fn ? old : [old, fn]
} }
}, },
computeOffset(popup, text, placement) { computeOffset(popup, text, placement, scale) {
let { width, height, top, left } = text let { width, height, top, left } = text
// you cant change the properties of DOMRect // you cant change the properties of DOMRect
top += window.scrollY top += window.scrollY
left += window.scrollX left += window.scrollX
// FIXME: we can get the numbers from scale, but that's not what we really want
const p = { width: popup.width / scale, height: popup.height / scale }
const ret = { left, top } const ret = { left, top }
if (/top/.test(placement)) ret.top -= popup.height + 5 if (/top/.test(placement)) ret.top -= p.height
if (/bottom/.test(placement)) ret.top += height + 5 if (/bottom/.test(placement)) ret.top += height
if (/left/.test(placement)) ret.left -= popup.width + 10 if (/left/.test(placement)) ret.left -= p.width
if (/right/.test(placement)) ret.left += width + 5 if (/right/.test(placement)) ret.left += width
// FIXME: magic number 20 & 14 comes from the offset of triangle
if (/Left/.test(placement)) { if (/Left/.test(placement)) {
if (this.arrowPointAtCenter) ret.left += width / 2 - 20
} else if(/Right/.test(placement)) { } else if(/Right/.test(placement)) {
ret.left += (width - popup.width) ret.left += (width - p.width)
if (this.arrowPointAtCenter) ret.left -= width / 2 - 20
} else if(/(top)|(bottom)/.test(placement)) { } else if(/(top)|(bottom)/.test(placement)) {
ret.left += (width - popup.width) / 2 ret.left += (width - p.width) / 2
} }
if (/Top/.test(placement)) { if (/Top/.test(placement)) {
if (this.arrowPointAtCenter) ret.top += height / 2 - 14
} else if(/Bottom/.test(placement)) { } else if(/Bottom/.test(placement)) {
ret.top += (height - popup.height) ret.top += (height - p.height)
if (this.arrowPointAtCenter) ret.top -= height / 2 - 14
} else if(/(left)|(right)/.test(placement)) { } else if(/(left)|(right)/.test(placement)) {
ret.top += (height - popup.height) / 2 ret.top += (height - p.height) / 2
} }
return ret return ret
}, },
@ -158,41 +184,69 @@ export default {
this.visible = true this.visible = true
this.$nextTick(() => { this.$nextTick(() => {
const popup = this.vnode.$el.getBoundingClientRect() const popup = this.vnode.$el.getBoundingClientRect()
const [, scale = 1] = window.getComputedStyle(this.vnode.$el).transform.match(/matrix\((.*?),/) || []
const content = this.$el.getBoundingClientRect() const content = this.$el.getBoundingClientRect()
const place = this.checkPosition(popup, content, this.placement) const place = this.autoAdjustOverflow ? this.checkPosition(popup, content, this.placement, scale) : this.placement
this.realPlacement = place this.realPlacement = place
const { left, top } = this.computeOffset(popup, content, place) const { left, top } = this.computeOffset(popup, content, place, scale)
this.vnode.left = left this.vnode.left = left
this.vnode.top = top this.vnode.top = top
}) })
this.onPopupAlign(this.realPlacement, this.$el, this.vnode.$el, { offset: [0,0] }) this.onPopupAlign(this.realPlacement, this.$el, this.vnode.$el, { offset: [0,0] })
}) })
}, },
hideNode() { hideNode(e) {
if (!this.vnode) return
if (e.relatedTarget === this.vnode.$el) {
return
}
this.visible = false this.visible = false
},
checkShow(e) {
if (this.t2) {
clearTimeout(this.t2)
this.t2 = null
}
if (this.mouseEnterDelay) {
this.t1 = window.setTimeout(() => {
this.showNode(e)
}, +this.mouseEnterDelay * 1e3)
}
},
checkHide(e) {
if (this.t1) {
clearTimeout(this.t1)
this.t1 = null
}
if (this.mouseLeaveDelay) {
this.t2 = window.setTimeout(() => {
this.hideNode(e)
}, +this.mouseLeaveDelay * 1e3)
}
} }
}, },
render(h) { render(h) {
const inner = this.$slots.default[0] const inner = this.$slots.default[0]
inner.data = inner.data || {} inner.data = inner.data || {}
inner.data.on = inner.data.on || {} inner.data.on = inner.data.on || {}
inner.data.on.mouseenter = this.addEventHandle(inner.data.on.mouseenter, this.showNode) inner.data.on.mouseenter = this.addEventHandle(inner.data.on.mouseenter, this.checkShow)
inner.data.on.mouseleave = this.addEventHandle(inner.data.on.mouseleave, this.hideNode) inner.data.on.mouseleave = this.addEventHandle(inner.data.on.mouseleave, this.checkHide)
return this.$slots.default[0] return this.$slots.default[0]
}, },
updated() { updated() {
if (!this.vnode) return if (!this.vnode) return
const popup = this.vnode.$el.getBoundingClientRect() const popup = this.vnode.$el.getBoundingClientRect()
const [, scale = 1] = window.getComputedStyle(this.vnode.$el).transform.match(/matrix\((.*?),/) || []
const content = this.$el.getBoundingClientRect() const content = this.$el.getBoundingClientRect()
const { left, top } = this.computeOffset(popup, content, this.realPlacement) const { left, top } = this.computeOffset(popup, content, this.realPlacement, scale)
this.vnode.left = left this.vnode.left = left
this.vnode.top = top this.vnode.top = top
}, },
beforeDestroy() { beforeDestroy() {
if (!this.vnode) return if (!this.vnode) return
this.vnode.$el.remove() this.vnode.$el.remove()
this.vnode.$destroy(); this.vnode.$destroy()
} }
} }
</script> </script>