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