2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { alignElement, alignPoint } from 'dom-align';
|
|
|
|
import addEventListener from '../_util/Dom/addEventListener';
|
2019-09-03 14:59:41 +00:00
|
|
|
import { isWindow, buffer, isSamePoint, isSimilarValue, restoreFocus } from './util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { cloneElement } from '../_util/vnode.js';
|
|
|
|
import clonedeep from 'lodash/cloneDeep';
|
2019-02-13 14:11:27 +00:00
|
|
|
import { getSlot } from '../_util/props-util';
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getElement(func) {
|
|
|
|
if (typeof func !== 'function' || !func) return null;
|
|
|
|
return func();
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2017-12-12 07:05:45 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getPoint(point) {
|
|
|
|
if (typeof point !== 'object' || !point) return null;
|
|
|
|
return point;
|
2017-12-12 07:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
childrenProps: PropTypes.object,
|
|
|
|
align: PropTypes.object.isRequired,
|
2019-01-12 03:33:27 +00:00
|
|
|
target: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).def(() => window),
|
2017-12-12 07:05:45 +00:00
|
|
|
monitorBufferTime: PropTypes.number.def(50),
|
|
|
|
monitorWindowResize: PropTypes.bool.def(false),
|
|
|
|
disabled: PropTypes.bool.def(false),
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
this.aligned = false;
|
|
|
|
return {};
|
2017-12-13 02:06:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2018-02-27 04:17:53 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.prevProps = { ...this.$props };
|
|
|
|
const props = this.$props;
|
2018-02-27 04:17:53 +00:00
|
|
|
// if parent ref not attached .... use document.getElementById
|
2019-01-12 03:33:27 +00:00
|
|
|
!this.aligned && this.forceAlign();
|
2018-02-27 04:17:53 +00:00
|
|
|
if (!props.disabled && props.monitorWindowResize) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.startMonitorWindowResize();
|
2018-02-27 04:17:53 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 07:05:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
2018-02-27 04:17:53 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const prevProps = this.prevProps;
|
|
|
|
const props = this.$props;
|
|
|
|
let reAlign = false;
|
2018-09-13 04:19:03 +00:00
|
|
|
if (!props.disabled) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const source = this.$el;
|
|
|
|
const sourceRect = source ? source.getBoundingClientRect() : null;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
if (prevProps.disabled) {
|
2019-01-12 03:33:27 +00:00
|
|
|
reAlign = true;
|
2018-02-27 04:17:53 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
const lastElement = getElement(prevProps.target);
|
|
|
|
const currentElement = getElement(props.target);
|
|
|
|
const lastPoint = getPoint(prevProps.target);
|
|
|
|
const currentPoint = getPoint(props.target);
|
2018-09-05 13:28:54 +00:00
|
|
|
if (isWindow(lastElement) && isWindow(currentElement)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// Skip if is window
|
|
|
|
reAlign = false;
|
2018-09-05 13:28:54 +00:00
|
|
|
} else if (
|
|
|
|
lastElement !== currentElement || // Element change
|
2019-01-12 03:33:27 +00:00
|
|
|
(lastElement && !currentElement && currentPoint) || // Change from element to point
|
|
|
|
(lastPoint && currentPoint && currentElement) || // Change from point to element
|
|
|
|
(currentPoint && !isSamePoint(lastPoint, currentPoint))
|
2018-09-05 13:28:54 +00:00
|
|
|
) {
|
2019-01-12 03:33:27 +00:00
|
|
|
reAlign = true;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If source element size changed
|
2019-01-12 03:33:27 +00:00
|
|
|
const preRect = this.sourceRect || {};
|
|
|
|
if (
|
|
|
|
!reAlign &&
|
|
|
|
source &&
|
2019-09-11 14:35:25 +00:00
|
|
|
(!isSimilarValue(preRect.width, sourceRect.width) ||
|
|
|
|
!isSimilarValue(preRect.height, sourceRect.height))
|
2019-01-12 03:33:27 +00:00
|
|
|
) {
|
|
|
|
reAlign = true;
|
2018-02-27 04:17:53 +00:00
|
|
|
}
|
2018-01-15 09:33:34 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.sourceRect = sourceRect;
|
2018-01-15 09:33:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 04:17:53 +00:00
|
|
|
if (reAlign) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.forceAlign();
|
2018-02-27 04:17:53 +00:00
|
|
|
}
|
2018-01-15 09:33:34 +00:00
|
|
|
|
2018-02-27 04:17:53 +00:00
|
|
|
if (props.monitorWindowResize && !props.disabled) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.startMonitorWindowResize();
|
2018-02-27 04:17:53 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.stopMonitorWindowResize();
|
2018-02-27 04:17:53 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.prevProps = { ...this.$props, align: clonedeep(this.$props.align) };
|
|
|
|
});
|
2018-01-15 09:33:34 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
beforeDestroy() {
|
|
|
|
this.stopMonitorWindowResize();
|
2017-12-12 07:05:45 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
startMonitorWindowResize() {
|
2017-12-12 07:05:45 +00:00
|
|
|
if (!this.resizeHandler) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.bufferMonitor = buffer(this.forceAlign, this.$props.monitorBufferTime);
|
|
|
|
this.resizeHandler = addEventListener(window, 'resize', this.bufferMonitor);
|
2017-12-12 07:05:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
stopMonitorWindowResize() {
|
2017-12-12 07:05:45 +00:00
|
|
|
if (this.resizeHandler) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.bufferMonitor.clear();
|
|
|
|
this.resizeHandler.remove();
|
|
|
|
this.resizeHandler = null;
|
2017-12-12 07:05:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
forceAlign() {
|
|
|
|
const { disabled, target, align } = this.$props;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!disabled && target) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const source = this.$el;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let result;
|
|
|
|
const element = getElement(target);
|
|
|
|
const point = getPoint(target);
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-09-03 14:59:41 +00:00
|
|
|
// IE lose focus after element realign
|
|
|
|
// We should record activeElement and restore later
|
|
|
|
const activeElement = document.activeElement;
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
if (element) {
|
2019-01-12 03:33:27 +00:00
|
|
|
result = alignElement(source, element, align);
|
2018-09-05 13:28:54 +00:00
|
|
|
} else if (point) {
|
2019-01-12 03:33:27 +00:00
|
|
|
result = alignPoint(source, point, align);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-09-03 14:59:41 +00:00
|
|
|
restoreFocus(activeElement, source);
|
2019-01-12 03:33:27 +00:00
|
|
|
this.aligned = true;
|
|
|
|
this.$listeners.align && this.$listeners.align(source, result);
|
2017-12-12 07:05:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { childrenProps } = this.$props;
|
2019-02-13 14:11:27 +00:00
|
|
|
const child = getSlot(this)[0];
|
|
|
|
if (child && childrenProps) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return cloneElement(child, { props: childrenProps });
|
2017-12-12 07:05:45 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return child;
|
2017-12-12 07:05:45 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|