ant-design-vue/components/affix/index.jsx
tangjinzhou ff25efe185
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 21:28:54 +08:00

267 lines
7.6 KiB
Vue

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