2019-01-12 03:33:27 +00: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';
|
2019-03-11 03:54:14 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-08-28 02:50:19 +00:00
|
|
|
import Base from '../base';
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getTargetRect(target) {
|
|
|
|
return target !== window ? target.getBoundingClientRect() : { top: 0, left: 0, bottom: 0 };
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getOffset(element, target) {
|
|
|
|
const elemRect = element.getBoundingClientRect();
|
|
|
|
const targetRect = getTargetRect(target);
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const scrollTop = getScroll(target, true);
|
|
|
|
const scrollLeft = getScroll(target, false);
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const docElem = window.document.body;
|
|
|
|
const clientTop = docElem.clientTop || 0;
|
|
|
|
const clientLeft = docElem.clientLeft || 0;
|
2018-03-01 14:47:08 +00:00
|
|
|
|
|
|
|
return {
|
2019-01-12 03:33:27 +00:00
|
|
|
top: elemRect.top - targetRect.top + scrollTop - clientTop,
|
|
|
|
left: elemRect.left - targetRect.left + scrollLeft - clientLeft,
|
2018-03-01 14:47:08 +00:00
|
|
|
width: elemRect.width,
|
|
|
|
height: elemRect.height,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getDefaultTarget() {
|
|
|
|
return typeof window !== 'undefined' ? window : null;
|
2018-03-01 14:47:08 +00: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 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
const Affix = {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AAffix',
|
2018-03-01 14:47:08 +00:00
|
|
|
props: AffixProps,
|
2018-03-02 08:54:22 +00:00
|
|
|
mixins: [BaseMixin],
|
2019-03-11 03:54:14 +00:00
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-11 03:54:14 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
this.events = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load'];
|
|
|
|
this.eventHandlers = {};
|
2018-03-01 14:47:08 +00:00
|
|
|
return {
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
beforeMount() {
|
|
|
|
this.updatePosition = throttleByAnimationFrame(this.updatePosition);
|
2018-03-02 08:54:22 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
|
|
|
const target = this.target || getDefaultTarget;
|
2018-03-01 14:47:08 +00:00
|
|
|
// Wait for parent component ref has its value
|
|
|
|
this.timeout = setTimeout(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setTargetEventListeners(target);
|
2019-01-02 12:56:06 +00:00
|
|
|
// Mock Event object.
|
2019-01-12 03:33:27 +00:00
|
|
|
this.updatePosition({});
|
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
target(val) {
|
|
|
|
this.clearEventListeners();
|
|
|
|
this.setTargetEventListeners(val);
|
2018-09-05 13:28:54 +00:00
|
|
|
// Mock Event object.
|
2019-01-12 03:33:27 +00:00
|
|
|
this.updatePosition({});
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
offsetTop() {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.updatePosition({});
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
offsetBottom() {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.updatePosition({});
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
beforeDestroy() {
|
|
|
|
this.clearEventListeners();
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.updatePosition.cancel();
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
setAffixStyle(e, affixStyle) {
|
|
|
|
const { target = getDefaultTarget } = this;
|
|
|
|
const originalAffixStyle = this.affixStyle;
|
|
|
|
const isWindow = target() === window;
|
2018-03-01 14:47:08 +00:00
|
|
|
if (e.type === 'scroll' && originalAffixStyle && affixStyle && isWindow) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
if (shallowequal(affixStyle, originalAffixStyle)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
this.setState({ affixStyle: affixStyle }, () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const affixed = !!this.affixStyle;
|
|
|
|
if ((affixStyle && !originalAffixStyle) || (!affixStyle && originalAffixStyle)) {
|
|
|
|
this.$emit('change', affixed);
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
setPlaceholderStyle(placeholderStyle) {
|
|
|
|
const originalPlaceholderStyle = this.placeholderStyle;
|
2018-03-01 14:47:08 +00:00
|
|
|
if (shallowequal(placeholderStyle, originalPlaceholderStyle)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState({ placeholderStyle: placeholderStyle });
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
syncPlaceholderStyle(e) {
|
|
|
|
const { affixStyle } = this;
|
2018-04-06 12:56:19 +00:00
|
|
|
if (!affixStyle) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-06 12:56:19 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$refs.placeholderNode.style.cssText = '';
|
2018-04-06 12:56:19 +00:00
|
|
|
this.setAffixStyle(e, {
|
|
|
|
...affixStyle,
|
|
|
|
width: this.$refs.placeholderNode.offsetWidth + 'px',
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-06 12:56:19 +00:00
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width: this.$refs.placeholderNode.offsetWidth + 'px',
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-04-06 12:56:19 +00:00
|
|
|
},
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
updatePosition(e) {
|
|
|
|
const { offsetBottom, offset, target = getDefaultTarget } = this;
|
|
|
|
let { offsetTop } = this;
|
|
|
|
const targetNode = target();
|
2018-03-01 14:47:08 +00:00
|
|
|
|
|
|
|
// Backwards support
|
2018-09-05 13:28:54 +00:00
|
|
|
// Fix: if offsetTop === 0, it will get undefined,
|
|
|
|
// if offsetBottom is type of number, offsetMode will be { top: false, ... }
|
2019-01-12 03:33:27 +00:00
|
|
|
offsetTop = typeof offsetTop === 'undefined' ? offset : offsetTop;
|
|
|
|
const scrollTop = getScroll(targetNode, true);
|
|
|
|
const affixNode = this.$el;
|
|
|
|
const elemOffset = getOffset(affixNode, targetNode);
|
2018-03-01 14:47:08 +00:00
|
|
|
const elemSize = {
|
|
|
|
width: this.$refs.fixedNode.offsetWidth,
|
|
|
|
height: this.$refs.fixedNode.offsetHeight,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
|
|
|
|
const offsetMode = {
|
|
|
|
top: false,
|
|
|
|
bottom: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
// Default to `offsetTop=0`.
|
|
|
|
if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') {
|
2019-01-12 03:33:27 +00:00
|
|
|
offsetMode.top = true;
|
|
|
|
offsetTop = 0;
|
2018-03-01 14:47:08 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
offsetMode.top = typeof offsetTop === 'number';
|
|
|
|
offsetMode.bottom = typeof offsetBottom === 'number';
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const targetInnerHeight = targetNode.innerHeight || targetNode.clientHeight;
|
2019-03-11 03:54:14 +00:00
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/13662
|
|
|
|
if (scrollTop >= elemOffset.top - offsetTop && offsetMode.top) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// Fixed Top
|
|
|
|
const width = `${elemOffset.width}px`;
|
|
|
|
const top = `${targetRect.top + offsetTop}px`;
|
2018-03-01 14:47:08 +00:00
|
|
|
this.setAffixStyle(e, {
|
|
|
|
position: 'fixed',
|
|
|
|
top,
|
|
|
|
left: `${targetRect.left + elemOffset.left}px`,
|
|
|
|
width,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width,
|
|
|
|
height: `${elemSize.height}px`,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
} else if (
|
2019-03-11 03:54:14 +00:00
|
|
|
scrollTop <= elemOffset.top + elemSize.height + offsetBottom - targetInnerHeight &&
|
2018-03-01 14:47:08 +00:00
|
|
|
offsetMode.bottom
|
|
|
|
) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// Fixed Bottom
|
|
|
|
const targetBottomOffet =
|
|
|
|
targetNode === window ? 0 : window.innerHeight - targetRect.bottom;
|
|
|
|
const width = `${elemOffset.width}px`;
|
2018-03-01 14:47:08 +00:00
|
|
|
this.setAffixStyle(e, {
|
|
|
|
position: 'fixed',
|
|
|
|
bottom: targetBottomOffet + offsetBottom + 'px',
|
|
|
|
left: targetRect.left + elemOffset.left + 'px',
|
|
|
|
width,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width,
|
|
|
|
height: elemOffset.height + 'px',
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00: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 14:47:08 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setAffixStyle(e, null);
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setPlaceholderStyle(null);
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2018-04-06 12:56:19 +00:00
|
|
|
if (e.type === 'resize') {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.syncPlaceholderStyle(e);
|
2018-04-06 12:56:19 +00:00
|
|
|
}
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
setTargetEventListeners(getTarget) {
|
|
|
|
const target = getTarget();
|
2018-03-01 14:47:08 +00:00
|
|
|
if (!target) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.clearEventListeners();
|
2018-03-01 14:47:08 +00:00
|
|
|
|
|
|
|
this.events.forEach(eventName => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.eventHandlers[eventName] = addEventListener(target, eventName, this.updatePosition);
|
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
clearEventListeners() {
|
2018-03-01 14:47:08 +00:00
|
|
|
this.events.forEach(eventName => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handler = this.eventHandlers[eventName];
|
2018-03-01 14:47:08 +00:00
|
|
|
if (handler && handler.remove) {
|
2019-01-12 03:33:27 +00:00
|
|
|
handler.remove();
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { prefixCls, affixStyle, placeholderStyle, $slots, $props } = this;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2018-03-01 14:47:08 +00:00
|
|
|
const className = classNames({
|
2019-03-11 03:54:14 +00:00
|
|
|
[getPrefixCls('affix', prefixCls)]: affixStyle,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
|
|
|
|
const props = {
|
|
|
|
attrs: omit($props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
return (
|
2019-01-12 03:33:27 +00:00
|
|
|
<div {...props} style={placeholderStyle} ref="placeholderNode">
|
|
|
|
<div class={className} ref="fixedNode" style={affixStyle}>
|
2018-03-01 14:47:08 +00:00
|
|
|
{$slots.default}
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2019-01-12 03:33:27 +00:00
|
|
|
Affix.install = function(Vue) {
|
2019-08-28 02:50:19 +00:00
|
|
|
Vue.use(Base);
|
2019-01-12 03:33:27 +00:00
|
|
|
Vue.component(Affix.name, Affix);
|
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default Affix;
|