|
| 1 | +/** |
| 2 | + * Custom events to control showing and hiding of tooltip |
| 3 | + * |
| 4 | + * @attributes |
| 5 | + * - `event` {String} |
| 6 | + * - `eventOff` {String} |
| 7 | + */ |
| 8 | + |
| 9 | +const checkStatus = function (dataEventOff, e) { |
| 10 | + const {show} = this.state |
| 11 | + const dataIsCapture = e.currentTarget.getAttribute('data-iscapture') |
| 12 | + const isCapture = dataIsCapture && dataIsCapture === 'true' || this.props.isCapture |
| 13 | + const currentItem = e.currentTarget.getAttribute('currentItem') |
| 14 | + |
| 15 | + if (!isCapture) e.stopPropagation() |
| 16 | + if (show && currentItem === 'true') { |
| 17 | + if (!dataEventOff) this.hideTooltip(e) |
| 18 | + } else { |
| 19 | + e.currentTarget.setAttribute('currentItem', 'true') |
| 20 | + setUntargetItems(e.currentTarget, this.getTargetArray()) |
| 21 | + this.showTooltip(e) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const setUntargetItems = function (currentTarget, targetArray) { |
| 26 | + for (let i = 0; i < targetArray.length; i++) { |
| 27 | + if (currentTarget !== targetArray[i]) { |
| 28 | + targetArray[i].setAttribute('currentItem', 'false') |
| 29 | + } else { |
| 30 | + targetArray[i].setAttribute('currentItem', 'true') |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export default function (target) { |
| 36 | + target.prototype.isCustomEvent = function (ele) { |
| 37 | + const {event} = this.state |
| 38 | + return event || ele.getAttribute('data-event') |
| 39 | + } |
| 40 | + |
| 41 | + /* Bind listener for custom event */ |
| 42 | + target.prototype.customBindListener = function (ele) { |
| 43 | + const {event, eventOff} = this.state |
| 44 | + const dataEvent = ele.getAttribute('data-event') || event |
| 45 | + const dataEventOff = ele.getAttribute('data-event-off') || eventOff |
| 46 | + |
| 47 | + dataEvent.split(' ').forEach(event => { |
| 48 | + ele.removeEventListener(event, checkStatus) |
| 49 | + ele.addEventListener(event, checkStatus.bind(this, dataEventOff), false) |
| 50 | + }) |
| 51 | + if (dataEventOff) { |
| 52 | + dataEventOff.split(' ').forEach(event => { |
| 53 | + ele.removeEventListener(event, this.hideTooltip) |
| 54 | + ele.addEventListener(event, ::this.hideTooltip, false) |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /* Unbind listener for custom event */ |
| 60 | + target.prototype.customUnbindListener = function (ele) { |
| 61 | + const {event, eventOff} = this.state |
| 62 | + const dataEvent = event || ele.getAttribute('data-event') |
| 63 | + const dataEventOff = eventOff || ele.getAttribute('data-event-off') |
| 64 | + |
| 65 | + ele.removeEventListener(dataEvent, checkStatus) |
| 66 | + if (dataEventOff) ele.removeEventListener(dataEventOff, this.hideTooltip) |
| 67 | + } |
| 68 | +} |
0 commit comments