Skip to content

Commit

Permalink
Fix #67, improve customer event
Browse files Browse the repository at this point in the history
  • Loading branch information
wwayne committed Jan 11, 2016
1 parent 61aec1f commit b4c38df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
21 changes: 16 additions & 5 deletions dist/react-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var ReactTooltip = (function (_Component) {
_classCallCheck(this, ReactTooltip);

_Component.call(this, props);
this._bind('showTooltip', 'updateTooltip', 'hideTooltip', 'checkStatus', 'onWindowResize');
this._bind('showTooltip', 'updateTooltip', 'hideTooltip', 'checkStatus', 'onWindowResize', 'bindClickListener');
this.mount = true;
this.state = {
show: false,
Expand Down Expand Up @@ -156,12 +156,16 @@ var ReactTooltip = (function (_Component) {
window.removeEventListener('resize', this.onWindowResize);
};

/* TODO: optimize, bind has been trigger too maany times */

ReactTooltip.prototype.bindListener = function bindListener() {
var targetArray = this.getTargetArray();

var dataEvent = undefined;
for (var i = 0; i < targetArray.length; i++) {
targetArray[i].setAttribute('currentItem', 'false');
if (targetArray[i].getAttribute('currentItem') === null) {
targetArray[i].setAttribute('currentItem', 'false');
}
dataEvent = this.state.event || targetArray[i].getAttribute('data-event');
if (dataEvent) {
targetArray[i].removeEventListener(dataEvent, this.checkStatus);
Expand Down Expand Up @@ -247,18 +251,22 @@ var ReactTooltip = (function (_Component) {
*/

ReactTooltip.prototype.checkStatus = function checkStatus(e) {
e.stopPropagation();
if (this.state.show && e.currentTarget.getAttribute('currentItem') === 'true') {
this.hideTooltip(e);
} else {
e.currentTarget.setAttribute('currentItem', 'true');
/* when click other place, the tooltip should be removed */
window.removeEventListener('click', this.bindClickListener);
window.addEventListener('click', this.bindClickListener, false);

this.showTooltip(e);
this.setUntargetItems(e.currentTarget);
}
};

ReactTooltip.prototype.setUntargetItems = function setUntargetItems(currentTarget) {
var targetArray = this.getTargetArray();

for (var i = 0; i < targetArray.length; i++) {
if (currentTarget !== targetArray[i]) {
targetArray[i].setAttribute('currentItem', 'false');
Expand All @@ -268,6 +276,11 @@ var ReactTooltip = (function (_Component) {
}
};

ReactTooltip.prototype.bindClickListener = function bindClickListener() {
this.globalHide();
window.removeEventListener('click', this.bindClickListener);
};

/**
* When mouse enter, show update
*/
Expand Down Expand Up @@ -379,8 +392,6 @@ var ReactTooltip = (function (_Component) {
window.addEventListener('scroll', this.hideTooltip);
};

/* Remove listener when tooltip hide */

ReactTooltip.prototype.removeScrollListener = function removeScrollListener() {
window.removeEventListener('scroll', this.hideTooltip);
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-tooltip",
"version": "1.0.2",
"version": "1.0.3",
"description": "react tooltip component",
"main": "dist/react-tooltip.js",
"scripts": {
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class ReactTooltip extends Component {

constructor (props) {
super(props)
this._bind('showTooltip', 'updateTooltip', 'hideTooltip', 'checkStatus', 'onWindowResize')
this._bind('showTooltip', 'updateTooltip', 'hideTooltip', 'checkStatus', 'onWindowResize', 'bindClickListener')
this.mount = true
this.state = {
show: false,
Expand Down Expand Up @@ -115,12 +115,15 @@ export default class ReactTooltip extends Component {
window.removeEventListener('resize', this.onWindowResize)
}

/* TODO: optimize, bind has been trigger too maany times */
bindListener () {
let targetArray = this.getTargetArray()

let dataEvent
for (let i = 0; i < targetArray.length; i++) {
targetArray[i].setAttribute('currentItem', 'false')
if (targetArray[i].getAttribute('currentItem') === null) {
targetArray[i].setAttribute('currentItem', 'false')
}
dataEvent = this.state.event || targetArray[i].getAttribute('data-event')
if (dataEvent) {
targetArray[i].removeEventListener(dataEvent, this.checkStatus)
Expand Down Expand Up @@ -197,18 +200,22 @@ export default class ReactTooltip extends Component {
* Used in customer event
*/
checkStatus (e) {
e.stopPropagation()
if (this.state.show && e.currentTarget.getAttribute('currentItem') === 'true') {
this.hideTooltip(e)
} else {
e.currentTarget.setAttribute('currentItem', 'true')
/* when click other place, the tooltip should be removed */
window.removeEventListener('click', this.bindClickListener)
window.addEventListener('click', this.bindClickListener, false)

this.showTooltip(e)
this.setUntargetItems(e.currentTarget)
}
}

setUntargetItems (currentTarget) {
let targetArray = this.getTargetArray()

for (let i = 0; i < targetArray.length; i++) {
if (currentTarget !== targetArray[i]) {
targetArray[i].setAttribute('currentItem', 'false')
Expand All @@ -218,6 +225,11 @@ export default class ReactTooltip extends Component {
}
}

bindClickListener () {
this.globalHide()
window.removeEventListener('click', this.bindClickListener)
}

/**
* When mouse enter, show update
*/
Expand Down Expand Up @@ -311,7 +323,6 @@ export default class ReactTooltip extends Component {
window.addEventListener('scroll', this.hideTooltip)
}

/* Remove listener when tooltip hide */
removeScrollListener () {
window.removeEventListener('scroll', this.hideTooltip)
}
Expand Down

0 comments on commit b4c38df

Please sign in to comment.