From 78d3332bb3b4e34d41ae26c30c1b5c7508973442 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Fri, 22 Dec 2017 18:43:28 +0800 Subject: [PATCH] add trigger --- components/_util/Dom/contains.js | 11 + components/_util/getContainerRenderMixin.js | 67 +++ components/trigger/LazyRenderBox.vue | 28 + components/trigger/Popup.vue | 194 +++++++ components/trigger/PopupInner.vue | 39 ++ components/trigger/index.js | 3 + components/trigger/index.vue | 533 ++++++++++++++++++++ components/trigger/utils.js | 27 + 8 files changed, 902 insertions(+) create mode 100644 components/_util/Dom/contains.js create mode 100644 components/_util/getContainerRenderMixin.js create mode 100644 components/trigger/LazyRenderBox.vue create mode 100644 components/trigger/Popup.vue create mode 100644 components/trigger/PopupInner.vue create mode 100644 components/trigger/index.js create mode 100644 components/trigger/index.vue create mode 100644 components/trigger/utils.js diff --git a/components/_util/Dom/contains.js b/components/_util/Dom/contains.js new file mode 100644 index 000000000..f034a2d2e --- /dev/null +++ b/components/_util/Dom/contains.js @@ -0,0 +1,11 @@ +export default function contains (root, n) { + let node = n + while (node) { + if (node === root) { + return true + } + node = node.parentNode + } + + return false +} diff --git a/components/_util/getContainerRenderMixin.js b/components/_util/getContainerRenderMixin.js new file mode 100644 index 000000000..0a8f8b54c --- /dev/null +++ b/components/_util/getContainerRenderMixin.js @@ -0,0 +1,67 @@ +function defaultGetContainer () { + const container = document.createElement('div') + document.body.appendChild(container) + return container +} + +export default function getContainerRenderMixin (config) { + const { + autoMount = true, + autoDestroy = true, + isVisible, + isForceRender, + getComponent, + getContainer = defaultGetContainer, + } = config + + let mixin + if (autoMount) { + mixin = { + ...mixin, + mounted () { + this.renderComponent() + }, + } + } + + if (autoDestroy) { + mixin = { + ...mixin, + beforeDestroy () { + this.removeContainer() + }, + } + } + mixin = { + ...mixin, + methods: { + removeContainer () { + if (this._container) { + const container = this._container + container.parentNode.removeChild(container) + this._container = null + } + }, + renderComponent (componentArg) { + if ( + !isVisible || isVisible(this) || + (isForceRender && isForceRender(this)) + ) { + if (!this._container) { + this._container = getContainer(this) + } + this._container.appendChild(this.$el) + } + let component + if (this.getComponent) { + component = this.getComponent(componentArg) + } else { + component = getComponent(this, componentArg) + } + this._component = component + }, + }, + } + + return mixin +} diff --git a/components/trigger/LazyRenderBox.vue b/components/trigger/LazyRenderBox.vue new file mode 100644 index 000000000..5b3054570 --- /dev/null +++ b/components/trigger/LazyRenderBox.vue @@ -0,0 +1,28 @@ + diff --git a/components/trigger/Popup.vue b/components/trigger/Popup.vue new file mode 100644 index 000000000..c61714fad --- /dev/null +++ b/components/trigger/Popup.vue @@ -0,0 +1,194 @@ + diff --git a/components/trigger/PopupInner.vue b/components/trigger/PopupInner.vue new file mode 100644 index 000000000..b7554e5eb --- /dev/null +++ b/components/trigger/PopupInner.vue @@ -0,0 +1,39 @@ + diff --git a/components/trigger/index.js b/components/trigger/index.js new file mode 100644 index 000000000..0f504fc7d --- /dev/null +++ b/components/trigger/index.js @@ -0,0 +1,3 @@ +// export this package's api +import Trigger from './src/' +export default Trigger diff --git a/components/trigger/index.vue b/components/trigger/index.vue new file mode 100644 index 000000000..6fd73affd --- /dev/null +++ b/components/trigger/index.vue @@ -0,0 +1,533 @@ + diff --git a/components/trigger/utils.js b/components/trigger/utils.js new file mode 100644 index 000000000..e2af70378 --- /dev/null +++ b/components/trigger/utils.js @@ -0,0 +1,27 @@ +function isPointsEq (a1, a2) { + return a1[0] === a2[0] && a1[1] === a2[1] +} + +export function getAlignFromPlacement (builtinPlacements, placementStr, align) { + const baseAlign = builtinPlacements[placementStr] || {} + return { + ...baseAlign, + ...align, + } +} + +export function getPopupClassNameFromAlign (builtinPlacements, prefixCls, align) { + const points = align.points + for (const placement in builtinPlacements) { + if (builtinPlacements.hasOwnProperty(placement)) { + if (isPointsEq(builtinPlacements[placement].points, points)) { + return `${prefixCls}-placement-${placement}` + } + } + } + return '' +} + +export function saveRef (name, component) { + this[name] = component +}