ant-design-vue/components/affix/index.jsx

266 lines
7.9 KiB
Vue
Raw Normal View History

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';
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
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-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);
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
// Mock Event object.
2019-01-12 03:33:27 +00:00
this.updatePosition({});
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
},
2019-01-12 03:33:27 +00:00
offsetTop(val) {
this.updatePosition({});
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
},
2019-01-12 03:33:27 +00:00
offsetBottom(val) {
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
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
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;
2018-03-01 14:47:08 +00:00
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 (
scrollTop < elemOffset.top + elemSize.height + offsetBottom - targetInnerHeight &&
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;
2018-03-01 14:47:08 +00:00
const className = classNames({
[prefixCls || 'ant-affix']: 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
/* istanbul ignore next */
2019-01-12 03:33:27 +00:00
Affix.install = function(Vue) {
Vue.component(Affix.name, Affix);
};
2019-01-12 03:33:27 +00:00
export default Affix;