|
| 1 | + |
| 2 | +import TransitionEvents from './css-animation/Event' |
| 3 | + |
| 4 | +export default { |
| 5 | + name: 'Wave', |
| 6 | + props: ['insertExtraNode'], |
| 7 | + mounted () { |
| 8 | + this.$nextTick(() => { |
| 9 | + this.instance = this.bindAnimationEvent(this.$el) |
| 10 | + }) |
| 11 | + }, |
| 12 | + |
| 13 | + beforeDestroy () { |
| 14 | + if (this.instance) { |
| 15 | + this.instance.cancel() |
| 16 | + } |
| 17 | + }, |
| 18 | + methods: { |
| 19 | + isNotGrey (color) { |
| 20 | + const match = (color || '').match(/rgba?\((\d*), (\d*), (\d*)(, [\.\d]*)?\)/) |
| 21 | + if (match && match[1] && match[2] && match[3]) { |
| 22 | + return !(match[1] === match[2] && match[2] === match[3]) |
| 23 | + } |
| 24 | + return true |
| 25 | + }, |
| 26 | + |
| 27 | + onClick (node) { |
| 28 | + if (node.className.indexOf('-leave') >= 0) { |
| 29 | + return |
| 30 | + } |
| 31 | + this.removeExtraStyleNode() |
| 32 | + const { insertExtraNode } = this.$props |
| 33 | + const extraNode = document.createElement('div') |
| 34 | + extraNode.className = 'ant-click-animating-node' |
| 35 | + const attributeName = insertExtraNode ? 'ant-click-animating' : 'ant-click-animating-without-extra-node' |
| 36 | + node.removeAttribute(attributeName) |
| 37 | + node.setAttribute(attributeName, 'true') |
| 38 | + // Get wave color from target |
| 39 | + const waveColor = |
| 40 | + getComputedStyle(node).getPropertyValue('border-top-color') || // Firefox Compatible |
| 41 | + getComputedStyle(node).getPropertyValue('border-color') || |
| 42 | + getComputedStyle(node).getPropertyValue('background-color') |
| 43 | + // Not white or transparnt or grey |
| 44 | + if (waveColor && |
| 45 | + waveColor !== '#ffffff' && |
| 46 | + waveColor !== 'rgb(255, 255, 255)' && |
| 47 | + this.isNotGrey(waveColor) && |
| 48 | + !/rgba\(\d*, \d*, \d*, 0\)/.test(waveColor) && // any transparent rgba color |
| 49 | + waveColor !== 'transparent') { |
| 50 | + extraNode.style.borderColor = waveColor |
| 51 | + this.styleForPesudo = document.createElement('style') |
| 52 | + this.styleForPesudo.innerHTML = |
| 53 | + `[ant-click-animating-without-extra-node]:after { border-color: ${waveColor}; }` |
| 54 | + document.body.appendChild(this.styleForPesudo) |
| 55 | + } |
| 56 | + if (insertExtraNode) { |
| 57 | + node.appendChild(extraNode) |
| 58 | + } |
| 59 | + const transitionEnd = () => { |
| 60 | + node.removeAttribute(attributeName) |
| 61 | + this.removeExtraStyleNode() |
| 62 | + if (insertExtraNode) { |
| 63 | + node.removeChild(extraNode) |
| 64 | + } |
| 65 | + TransitionEvents.removeEndEventListener(node, transitionEnd) |
| 66 | + } |
| 67 | + TransitionEvents.addEndEventListener(node, transitionEnd) |
| 68 | + }, |
| 69 | + |
| 70 | + bindAnimationEvent (node) { |
| 71 | + if (node.getAttribute('disabled') || |
| 72 | + node.className.indexOf('disabled') >= 0) { |
| 73 | + return |
| 74 | + } |
| 75 | + const onClick = (e) => { |
| 76 | + // Fix radio button click twice |
| 77 | + if (e.target.tagName === 'INPUT') { |
| 78 | + return |
| 79 | + } |
| 80 | + setTimeout(() => this.onClick(node), 0) |
| 81 | + } |
| 82 | + node.addEventListener('click', onClick, true) |
| 83 | + return { |
| 84 | + cancel: () => { |
| 85 | + node.removeEventListener('click', onClick, true) |
| 86 | + }, |
| 87 | + } |
| 88 | + }, |
| 89 | + |
| 90 | + removeExtraStyleNode () { |
| 91 | + if (this.styleForPesudo && document.body.contains(this.styleForPesudo)) { |
| 92 | + document.body.removeChild(this.styleForPesudo) |
| 93 | + this.styleForPesudo = null |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + |
| 98 | + render () { |
| 99 | + return this.$slots.default && this.$slots.default[0] |
| 100 | + }, |
| 101 | +} |
0 commit comments