Skip to content

Commit

Permalink
Detach custom event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed May 20, 2018
1 parent e56cf3e commit 68fd5b5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
29 changes: 25 additions & 4 deletions src/decorators/customEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,27 @@ const setUntargetItems = function (currentTarget, targetArray) {
}
}

let customListener
const customListeners = {
registry: new WeakMap(),
set (target, event, listener) {
if (this.registry.has(target)) {
const map = this.registry.get(target)
map[event] = listener
return this.registry
}

return this.registry.set(target, { [event]: listener })
},

get (target, event) {
const map = this.registry.get(target)
if (map !== undefined) {
return map[event]
}

return void 0
}
}

export default function (target) {
target.prototype.isCustomEvent = function (ele) {
Expand All @@ -48,8 +68,9 @@ export default function (target) {
const dataEventOff = ele.getAttribute('data-event-off') || eventOff

dataEvent.split(' ').forEach(event => {
ele.removeEventListener(event, customListener)
customListener = checkStatus.bind(this, dataEventOff)
ele.removeEventListener(event, customListeners.get(ele, event))
const customListener = checkStatus.bind(this, dataEventOff)
customListeners.set(ele, event, customListener)
ele.addEventListener(event, customListener, false)
})
if (dataEventOff) {
Expand All @@ -66,7 +87,7 @@ export default function (target) {
const dataEvent = event || ele.getAttribute('data-event')
const dataEventOff = eventOff || ele.getAttribute('data-event-off')

ele.removeEventListener(dataEvent, customListener)
ele.removeEventListener(dataEvent, customListeners.get(ele, event))
if (dataEventOff) ele.removeEventListener(dataEventOff, this.hideTooltip)
}
}
34 changes: 28 additions & 6 deletions standalone/react-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,9 @@ exports.default = function (target) {
var dataEventOff = ele.getAttribute('data-event-off') || eventOff;

dataEvent.split(' ').forEach(function (event) {
ele.removeEventListener(event, customListener);
customListener = checkStatus.bind(_this, dataEventOff);
ele.removeEventListener(event, customListeners.get(ele, event));
var customListener = checkStatus.bind(_this, dataEventOff);
customListeners.set(ele, event, customListener);
ele.addEventListener(event, customListener, false);
});
if (dataEventOff) {
Expand All @@ -1261,11 +1262,13 @@ exports.default = function (target) {
var dataEvent = event || ele.getAttribute('data-event');
var dataEventOff = eventOff || ele.getAttribute('data-event-off');

ele.removeEventListener(dataEvent, customListener);
ele.removeEventListener(dataEvent, customListeners.get(ele, event));
if (dataEventOff) ele.removeEventListener(dataEventOff, this.hideTooltip);
};
};

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
* Custom events to control showing and hiding of tooltip
*
Expand Down Expand Up @@ -1302,7 +1305,26 @@ var setUntargetItems = function setUntargetItems(currentTarget, targetArray) {
}
};

var customListener = void 0;
var customListeners = {
registry: new WeakMap(),
set: function set(target, event, listener) {
if (this.registry.has(target)) {
var map = this.registry.get(target);
map[event] = listener;
return this.registry;
}

return this.registry.set(target, _defineProperty({}, event, listener));
},
get: function get(target, event) {
var map = this.registry.get(target);
if (map !== undefined) {
return map[event];
}

return void 0;
}
};

},{}],14:[function(require,module,exports){
'use strict';
Expand Down Expand Up @@ -1806,9 +1828,9 @@ var ReactTooltip = (0, _staticMethods2.default)(_class = (0, _windowListener2.de
var content = void 0;
if (getContent) {
if (Array.isArray(getContent)) {
content = getContent[0] && getContent[0]();
content = getContent[0] && getContent[0](this.state.originTooltip);
} else {
content = getContent();
content = getContent(this.state.originTooltip);
}
}

Expand Down
Loading

0 comments on commit 68fd5b5

Please sign in to comment.