2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import addEventListener from '../_util/Dom/addEventListener';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import shallowequal from 'shallowequal';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import getScroll from '../_util/getScroll';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
2018-03-19 10:16:27 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getTargetRect(target) {
|
|
|
|
return target !== window ? target.getBoundingClientRect() : { top: 0, left: 0, bottom: 0 };
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getOffset(element, target) {
|
|
|
|
const elemRect = element.getBoundingClientRect();
|
|
|
|
const targetRect = getTargetRect(target);
|
2018-03-01 22:47:08 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const scrollTop = getScroll(target, true);
|
|
|
|
const scrollLeft = getScroll(target, false);
|
2018-03-01 22:47:08 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const docElem = window.document.body;
|
|
|
|
const clientTop = docElem.clientTop || 0;
|
|
|
|
const clientLeft = docElem.clientLeft || 0;
|
2018-03-01 22:47:08 +08:00
|
|
|
|
|
|
|
return {
|
2019-01-12 11:33:27 +08:00
|
|
|
top: elemRect.top - targetRect.top + scrollTop - clientTop,
|
|
|
|
left: elemRect.left - targetRect.left + scrollLeft - clientLeft,
|
2018-03-01 22:47:08 +08:00
|
|
|
width: elemRect.width,
|
|
|
|
height: elemRect.height,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getDefaultTarget() {
|
|
|
|
return typeof window !== 'undefined' ? window : null;
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Affix
|
|
|
|
const AffixProps = {
|
|
|
|
/**
|
|
|
|
* 距离窗口顶部达到指定偏移量后触发
|
|
|
|
*/
|
|
|
|
offsetTop: PropTypes.number,
|
|
|
|
offset: PropTypes.number,
|
|
|
|
/** 距离窗口底部达到指定偏移量后触发 */
|
|
|
|
offsetBottom: PropTypes.number,
|
|
|
|
/** 固定状态改变时触发的回调函数 */
|
|
|
|
// onChange?: (affixed?: boolean) => void;
|
|
|
|
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
|
|
|
|
target: PropTypes.func,
|
|
|
|
prefixCls: PropTypes.string,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
const Affix = {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'AAffix',
|
2018-03-01 22:47:08 +08:00
|
|
|
props: AffixProps,
|
2018-03-02 16:54:22 +08:00
|
|
|
mixins: [BaseMixin],
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
this.events = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load'];
|
|
|
|
this.eventHandlers = {};
|
2018-03-01 22:47:08 +08:00
|
|
|
return {
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeMount() {
|
|
|
|
this.updatePosition = throttleByAnimationFrame(this.updatePosition);
|
2018-03-02 16:54:22 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
|
|
|
const target = this.target || getDefaultTarget;
|
2018-03-01 22:47:08 +08:00
|
|
|
// Wait for parent component ref has its value
|
|
|
|
this.timeout = setTimeout(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setTargetEventListeners(target);
|
2019-01-02 20:56:06 +08:00
|
|
|
// Mock Event object.
|
2019-01-12 11:33:27 +08:00
|
|
|
this.updatePosition({});
|
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 11:33:27 +08:00
|
|
|
target(val) {
|
|
|
|
this.clearEventListeners();
|
|
|
|
this.setTargetEventListeners(val);
|
2018-09-05 21:28:54 +08:00
|
|
|
// Mock Event object.
|
2019-01-12 11:33:27 +08:00
|
|
|
this.updatePosition({});
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetTop(val) {
|
|
|
|
this.updatePosition({});
|
2018-09-05 21:28:54 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetBottom(val) {
|
|
|
|
this.updatePosition({});
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
|
|
|
this.clearEventListeners();
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.updatePosition.cancel();
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
setAffixStyle(e, affixStyle) {
|
|
|
|
const { target = getDefaultTarget } = this;
|
|
|
|
const originalAffixStyle = this.affixStyle;
|
|
|
|
const isWindow = target() === window;
|
2018-03-01 22:47:08 +08:00
|
|
|
if (e.type === 'scroll' && originalAffixStyle && affixStyle && isWindow) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
|
|
|
if (shallowequal(affixStyle, originalAffixStyle)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
|
|
|
this.setState({ affixStyle: affixStyle }, () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const affixed = !!this.affixStyle;
|
|
|
|
if ((affixStyle && !originalAffixStyle) || (!affixStyle && originalAffixStyle)) {
|
|
|
|
this.$emit('change', affixed);
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
setPlaceholderStyle(placeholderStyle) {
|
|
|
|
const originalPlaceholderStyle = this.placeholderStyle;
|
2018-03-01 22:47:08 +08:00
|
|
|
if (shallowequal(placeholderStyle, originalPlaceholderStyle)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ placeholderStyle: placeholderStyle });
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
syncPlaceholderStyle(e) {
|
|
|
|
const { affixStyle } = this;
|
2018-04-06 20:56:19 +08:00
|
|
|
if (!affixStyle) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$refs.placeholderNode.style.cssText = '';
|
2018-04-06 20:56:19 +08:00
|
|
|
this.setAffixStyle(e, {
|
|
|
|
...affixStyle,
|
|
|
|
width: this.$refs.placeholderNode.offsetWidth + 'px',
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-06 20:56:19 +08:00
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width: this.$refs.placeholderNode.offsetWidth + 'px',
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-04-06 20:56:19 +08:00
|
|
|
},
|
2018-03-01 22:47:08 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
updatePosition(e) {
|
|
|
|
const { offsetBottom, offset, target = getDefaultTarget } = this;
|
|
|
|
let { offsetTop } = this;
|
|
|
|
const targetNode = target();
|
2018-03-01 22:47:08 +08:00
|
|
|
|
|
|
|
// Backwards support
|
2018-09-05 21:28:54 +08:00
|
|
|
// Fix: if offsetTop === 0, it will get undefined,
|
|
|
|
// if offsetBottom is type of number, offsetMode will be { top: false, ... }
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetTop = typeof offsetTop === 'undefined' ? offset : offsetTop;
|
|
|
|
const scrollTop = getScroll(targetNode, true);
|
|
|
|
const affixNode = this.$el;
|
|
|
|
const elemOffset = getOffset(affixNode, targetNode);
|
2018-03-01 22:47:08 +08:00
|
|
|
const elemSize = {
|
|
|
|
width: this.$refs.fixedNode.offsetWidth,
|
|
|
|
height: this.$refs.fixedNode.offsetHeight,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
|
|
|
|
const offsetMode = {
|
|
|
|
top: false,
|
|
|
|
bottom: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
// Default to `offsetTop=0`.
|
|
|
|
if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') {
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetMode.top = true;
|
|
|
|
offsetTop = 0;
|
2018-03-01 22:47:08 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
offsetMode.top = typeof offsetTop === 'number';
|
|
|
|
offsetMode.bottom = typeof offsetBottom === 'number';
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const targetInnerHeight = targetNode.innerHeight || targetNode.clientHeight;
|
2018-03-01 22:47:08 +08:00
|
|
|
if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
|
2019-01-12 11:33:27 +08:00
|
|
|
// Fixed Top
|
|
|
|
const width = `${elemOffset.width}px`;
|
|
|
|
const top = `${targetRect.top + offsetTop}px`;
|
2018-03-01 22:47:08 +08:00
|
|
|
this.setAffixStyle(e, {
|
|
|
|
position: 'fixed',
|
|
|
|
top,
|
|
|
|
left: `${targetRect.left + elemOffset.left}px`,
|
|
|
|
width,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width,
|
|
|
|
height: `${elemSize.height}px`,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
} else if (
|
|
|
|
scrollTop < elemOffset.top + elemSize.height + offsetBottom - targetInnerHeight &&
|
|
|
|
offsetMode.bottom
|
|
|
|
) {
|
2019-01-12 11:33:27 +08:00
|
|
|
// Fixed Bottom
|
|
|
|
const targetBottomOffet =
|
|
|
|
targetNode === window ? 0 : window.innerHeight - targetRect.bottom;
|
|
|
|
const width = `${elemOffset.width}px`;
|
2018-03-01 22:47:08 +08:00
|
|
|
this.setAffixStyle(e, {
|
|
|
|
position: 'fixed',
|
|
|
|
bottom: targetBottomOffet + offsetBottom + 'px',
|
|
|
|
left: targetRect.left + elemOffset.left + 'px',
|
|
|
|
width,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width,
|
|
|
|
height: elemOffset.height + 'px',
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { affixStyle } = this;
|
|
|
|
if (
|
|
|
|
e.type === 'resize' &&
|
|
|
|
affixStyle &&
|
|
|
|
affixStyle.position === 'fixed' &&
|
|
|
|
affixNode.offsetWidth
|
|
|
|
) {
|
|
|
|
this.setAffixStyle(e, { ...affixStyle, width: affixNode.offsetWidth + 'px' });
|
2018-03-01 22:47:08 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setAffixStyle(e, null);
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setPlaceholderStyle(null);
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
2018-04-06 20:56:19 +08:00
|
|
|
if (e.type === 'resize') {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.syncPlaceholderStyle(e);
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
setTargetEventListeners(getTarget) {
|
|
|
|
const target = getTarget();
|
2018-03-01 22:47:08 +08:00
|
|
|
if (!target) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.clearEventListeners();
|
2018-03-01 22:47:08 +08:00
|
|
|
|
|
|
|
this.events.forEach(eventName => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.eventHandlers[eventName] = addEventListener(target, eventName, this.updatePosition);
|
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
clearEventListeners() {
|
2018-03-01 22:47:08 +08:00
|
|
|
this.events.forEach(eventName => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const handler = this.eventHandlers[eventName];
|
2018-03-01 22:47:08 +08:00
|
|
|
if (handler && handler.remove) {
|
2019-01-12 11:33:27 +08:00
|
|
|
handler.remove();
|
2018-03-01 22:47:08 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
const { prefixCls, affixStyle, placeholderStyle, $slots, $props } = this;
|
2018-03-01 22:47:08 +08:00
|
|
|
const className = classNames({
|
|
|
|
[prefixCls || 'ant-affix']: affixStyle,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-01 22:47:08 +08:00
|
|
|
|
|
|
|
const props = {
|
|
|
|
attrs: omit($props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<div {...props} style={placeholderStyle} ref="placeholderNode">
|
|
|
|
<div class={className} ref="fixedNode" style={affixStyle}>
|
2018-03-01 22:47:08 +08:00
|
|
|
{$slots.default}
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-01 22:47:08 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-01 22:47:08 +08:00
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
/* istanbul ignore next */
|
2019-01-12 11:33:27 +08:00
|
|
|
Affix.install = function(Vue) {
|
|
|
|
Vue.component(Affix.name, Affix);
|
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default Affix;
|