From 09382b3e5019be18a129bc54936b8d3216d50dd4 Mon Sep 17 00:00:00 2001 From: wanlei Date: Fri, 3 Nov 2017 18:06:00 +0800 Subject: [PATCH 1/4] fix: [tooltip] mount every time --- components/tooltip/demo/basic.vue | 66 ++++++++++++++++++ components/tooltip/index.js | 1 + components/tooltip/tooltip.vue | 109 ++++++++++++++++-------------- examples/tooltip.vue | 30 -------- 4 files changed, 124 insertions(+), 82 deletions(-) create mode 100644 components/tooltip/demo/basic.vue delete mode 100644 examples/tooltip.vue diff --git a/components/tooltip/demo/basic.vue b/components/tooltip/demo/basic.vue new file mode 100644 index 000000000..c9169f442 --- /dev/null +++ b/components/tooltip/demo/basic.vue @@ -0,0 +1,66 @@ + + + + diff --git a/components/tooltip/index.js b/components/tooltip/index.js index 4b5f7d8bc..b93dd566a 100644 --- a/components/tooltip/index.js +++ b/components/tooltip/index.js @@ -1,3 +1,4 @@ import ToolTip from './tooltip.vue' +import './style' export default ToolTip diff --git a/components/tooltip/tooltip.vue b/components/tooltip/tooltip.vue index 33912bae6..e7dfb2353 100644 --- a/components/tooltip/tooltip.vue +++ b/components/tooltip/tooltip.vue @@ -33,7 +33,6 @@ export default { visible: false, left: 0, top: 0, - domNode: null, } }, computed: { @@ -44,42 +43,46 @@ export default { } }, }, - created() { - const div = document.createElement('div') - document.body.appendChild(div) - const that = this - const vnode = new Vue({ - data() { - return { - left: 0, - top: 0, - } - }, - render(h) { - return ( - -
-
-
-
- {that.title} + methods: { + mountNode(callback) { + if (this.vnode) { + callback() + return + } + const div = document.createElement('div') + document.body.appendChild(div) + const that = this + const vnode = new Vue({ + data() { + return { + left: 0, + top: 0, + } + }, + render(h) { + return ( + +
+
+
+
+ {that.title} +
-
-
- ) - } - }).$mount(div) - this.$nextTick(() => { - this.vnode = vnode - this.domNode = div - }) - }, - methods: { + + ) + } + }).$mount(div) + this.$nextTick(() => { + this.vnode = vnode + callback() + }) + }, onPopupAlign: (placement, domNode, target, align) => { if (!placement) { return; @@ -118,10 +121,10 @@ export default { left += window.scrollX const ret = { left, top } - if (/top/.test(placement)) ret.top -= popup.height - if (/bottom/.test(placement)) ret.top += height - if (/left/.test(placement)) ret.left -= popup.width - if (/right/.test(placement)) ret.left += width + if (/top/.test(placement)) ret.top -= popup.height + 5 + if (/bottom/.test(placement)) ret.top += height + 5 + if (/left/.test(placement)) ret.left -= popup.width + 10 + if (/right/.test(placement)) ret.left += width + 5 if (/Left/.test(placement)) { } else if(/Right/.test(placement)) { @@ -138,41 +141,43 @@ export default { return ret }, showNode() { - this.visible = true - this.$nextTick(() => { - const popup = this.vnode.$el.getBoundingClientRect() - const content = this.$el.getBoundingClientRect() - const { left, top } = this.computeOffset(popup, content, this.placement) - this.vnode.left = left - this.vnode.top = top + this.mountNode(() => { + this.visible = true + this.$nextTick(() => { + const popup = this.vnode.$el.getBoundingClientRect() + const content = this.$el.getBoundingClientRect() + const { left, top } = this.computeOffset(popup, content, this.placement) + this.vnode.left = left + this.vnode.top = top + }) + this.onPopupAlign(this.placement, this.$el, this.vnode.$el, { offset: [0,0] }) }) - this.onPopupAlign(this.placement, this.$el, this.vnode.$el, { offset: [0,0] }) }, hideNode() { this.visible = false } }, render(h) { - let node = this.vnode const inner = this.$slots.default[0] inner.data = inner.data || {} inner.data.on = inner.data.on || {} inner.data.on.mouseenter = this.addEventHandle(inner.data.on.mouseenter, this.showNode) inner.data.on.mouseleave = this.addEventHandle(inner.data.on.mouseleave, this.hideNode) -// console.info(inner) + return this.$slots.default[0] }, updated() { + if (!this.vnode) return const popup = this.vnode.$el.getBoundingClientRect() const content = this.$el.getBoundingClientRect() const { left, top } = this.computeOffset(popup, content, this.placement) this.vnode.left = left this.vnode.top = top }, - beforeDestory() { - console.info('没有成功清除实例 ,看vue panel') + beforeDestroy() { + if (!this.vnode) return + this.vnode.$el.remove() this.vnode.$destroy(); - this.domNode && this.domNode.remove() } } diff --git a/examples/tooltip.vue b/examples/tooltip.vue deleted file mode 100644 index 651957dbd..000000000 --- a/examples/tooltip.vue +++ /dev/null @@ -1,30 +0,0 @@ - - - From c0c246ed112a5536c4c11637de7981ab0b03877b Mon Sep 17 00:00:00 2001 From: wanlei Date: Fri, 3 Nov 2017 20:53:01 +0800 Subject: [PATCH 2/4] fix: [tooltip] different placement --- components/tooltip/demo/basic.vue | 17 +++++++++-------- components/tooltip/tooltip.vue | 24 ++++++++++++++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/components/tooltip/demo/basic.vue b/components/tooltip/demo/basic.vue index c9169f442..a1e0451d3 100644 --- a/components/tooltip/demo/basic.vue +++ b/components/tooltip/demo/basic.vue @@ -1,11 +1,11 @@ diff --git a/components/tooltip/tooltip.vue b/components/tooltip/tooltip.vue index e7dfb2353..80068597c 100644 --- a/components/tooltip/tooltip.vue +++ b/components/tooltip/tooltip.vue @@ -33,6 +33,7 @@ export default { visible: false, left: 0, top: 0, + realPlacement: this.placement, } }, computed: { @@ -44,6 +45,19 @@ export default { }, }, methods: { + checkPosition(popup, text, placement) { + let { top, left, bottom, right } = text + const reg = /(top|bottom|left|right)(.*)/ + const [, abstractPos, suffix] = placement.match(reg) + console.info(right - left, popup.width) + let ret = placement + // we can change the position many times + if (abstractPos === 'left' && left < popup.width) ret = 'right' + suffix + if (abstractPos === 'right' && document.documentElement.clientWidth - right < popup.width) ret = 'left' + suffix + if (abstractPos === 'top' && top < popup.height) ret = 'bottom' + suffix + if (abstractPos === 'bottom' && document.documentElement.clientHeight - bottom < popup.height) ret = 'left' + suffix + return ret + }, mountNode(callback) { if (this.vnode) { callback() @@ -64,7 +78,7 @@ export default {
@@ -146,11 +160,13 @@ export default { this.$nextTick(() => { const popup = this.vnode.$el.getBoundingClientRect() const content = this.$el.getBoundingClientRect() - const { left, top } = this.computeOffset(popup, content, this.placement) + const place = this.checkPosition(popup, content, this.placement) + this.realPlacement = place + const { left, top } = this.computeOffset(popup, content, place) this.vnode.left = left this.vnode.top = top }) - this.onPopupAlign(this.placement, this.$el, this.vnode.$el, { offset: [0,0] }) + this.onPopupAlign(this.realPlacement, this.$el, this.vnode.$el, { offset: [0,0] }) }) }, hideNode() { @@ -170,7 +186,7 @@ export default { if (!this.vnode) return const popup = this.vnode.$el.getBoundingClientRect() const content = this.$el.getBoundingClientRect() - const { left, top } = this.computeOffset(popup, content, this.placement) + const { left, top } = this.computeOffset(popup, content, this.realPlacement) this.vnode.left = left this.vnode.top = top }, From 7427f8099f1569460e2805921a69533fda805534 Mon Sep 17 00:00:00 2001 From: wanlei Date: Fri, 3 Nov 2017 20:55:41 +0800 Subject: [PATCH 3/4] fix: [tooltip] new formula --- components/tooltip/demo/{basic.vue => index.vue} | 0 components/tooltip/tooltip.vue | 1 - 2 files changed, 1 deletion(-) rename components/tooltip/demo/{basic.vue => index.vue} (100%) diff --git a/components/tooltip/demo/basic.vue b/components/tooltip/demo/index.vue similarity index 100% rename from components/tooltip/demo/basic.vue rename to components/tooltip/demo/index.vue diff --git a/components/tooltip/tooltip.vue b/components/tooltip/tooltip.vue index 80068597c..e26091eb6 100644 --- a/components/tooltip/tooltip.vue +++ b/components/tooltip/tooltip.vue @@ -49,7 +49,6 @@ export default { let { top, left, bottom, right } = text const reg = /(top|bottom|left|right)(.*)/ const [, abstractPos, suffix] = placement.match(reg) - console.info(right - left, popup.width) let ret = placement // we can change the position many times if (abstractPos === 'left' && left < popup.width) ret = 'right' + suffix From 59f1d5b4db267e699a1e2dad7d0bcd2a9ab34e6a Mon Sep 17 00:00:00 2001 From: wanlei Date: Mon, 6 Nov 2017 12:13:36 +0800 Subject: [PATCH 4/4] update: [tooltip] more API supported --- components/tooltip/demo/index.vue | 40 ++++++++++-- components/tooltip/tooltip.vue | 102 +++++++++++++++++++++++------- 2 files changed, 113 insertions(+), 29 deletions(-) diff --git a/components/tooltip/demo/index.vue b/components/tooltip/demo/index.vue index a1e0451d3..178db3bff 100644 --- a/components/tooltip/demo/index.vue +++ b/components/tooltip/demo/index.vue @@ -2,19 +2,37 @@
-

This is just a test, put your cursor here

+ :title="showText" + :autoAdjustOverflow="autoAdjustOverflow" + > +

撞到边缘翻转位置 & 点击更新

- {{showText}} + {{autoAdjustOverflow ? '启用' : '关闭'}}自动调整中
+

切换arrowPointAtCenter模式

+ {{arrowPointAtCenter}}
- {{td}} + + {{td}} +
+
+

+ + arrowPointAtCenter arrowPointAtCenter arrowPointAtCenter + +

+
@@ -33,7 +51,9 @@ ['left', '', '', '', 'right'], ['leftBottom', '', '', '', 'rightBottom'], ['', 'bottomLeft', 'bottom', 'bottomRight', ''], - ] + ], + arrowPointAtCenter: false, + autoAdjustOverflow: true, } }, methods: { @@ -43,6 +63,12 @@ } else { this.showText += ' ' } + }, + change() { + this.arrowPointAtCenter = !this.arrowPointAtCenter + }, + reverse() { + this.autoAdjustOverflow = !this.autoAdjustOverflow } }, components: { @@ -52,6 +78,10 @@ }