Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
adding a generic utils.addEvtListener helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Koo committed Apr 15, 2013
1 parent 6f00b11 commit a231d78
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 66 deletions.
55 changes: 21 additions & 34 deletions js/hopscotch-0.0.5.js
Expand Up @@ -52,13 +52,6 @@
}
};

if (window.addEventListener) {
window.addEventListener('load', winLoadHandler, false);
}
else if (window.attachEvent) {
window.attachEvent('onload', winLoadHandler);
}

/**
* utils
* =====
Expand Down Expand Up @@ -284,15 +277,15 @@
/**
* @private
*/
addClickListener: function(el, fn) {
return el.addEventListener ? el.addEventListener('click', fn, false) : el.attachEvent('onclick', fn);
addEvtListener: function(el, evtName, fn) {
return el.addEventListener ? el.addEventListener(evtName, fn, false) : el.attachEvent('on' + evtName, fn);
},

/**
* @private
*/
removeClickListener: function(el, fn) {
return el.removeEventListener ? el.removeEventListener('click', fn, false) : el.detachEvent('click', fn);
removeEvtListener: function(el, evtName, fn) {
return el.removeEventListener ? el.removeEventListener(evtName, fn, false) : el.detachEvent('on' + evtName, fn);
},

documentIsReady: function() {
Expand Down Expand Up @@ -477,6 +470,8 @@
}
};

utils.addEvtListener(window, 'load', winLoadHandler);

callbacks = {
next: [],
prev: [],
Expand Down Expand Up @@ -643,14 +638,14 @@
buttonsEl.appendChild(this.ctaBtnEl);

// Attach click listeners
utils.addClickListener(this.prevBtnEl, function(evt) {
utils.addEvtListener(this.prevBtnEl, 'click', function(evt) {
winHopscotch.prevStep(true);
});

utils.addClickListener(this.nextBtnEl, function(evt) {
utils.addEvtListener(this.nextBtnEl, 'click', function(evt) {
winHopscotch.nextStep(true);
});
utils.addClickListener(this.doneBtnEl, winHopscotch.endTour);
utils.addEvtListener(this.doneBtnEl, 'click', winHopscotch.endTour);

buttonsEl.className = 'hopscotch-actions';
this.buttonsEl = buttonsEl;
Expand Down Expand Up @@ -703,7 +698,7 @@
closeBtnEl.innerHTML = HopscotchI18N.closeTooltip;

if (this.opt.isTourBubble) {
utils.addClickListener(closeBtnEl, function(evt) {
utils.addEvtListener(closeBtnEl, 'click', function(evt) {
var currStepNum = winHopscotch.getCurrStepNum(),
currTour = winHopscotch.getCurrTour(),
doEndCallback = (currStepNum === currTour.steps.length-1);
Expand All @@ -721,7 +716,7 @@
});
}
else {
utils.addClickListener(closeBtnEl, this._getCloseFn());
utils.addEvtListener(closeBtnEl, 'click', this._getCloseFn());
}

this.closeBtnEl = closeBtnEl;
Expand Down Expand Up @@ -803,16 +798,16 @@

if (step.onCTA) {
if (this.onCTA) {
utils.removeClickListener(this.ctaBtnEl, this.onCTA);
utils.removeEvtListener(this.ctaBtnEl, 'click', this.onCTA);
}

utils.addClickListener(this.ctaBtnEl, step.onCTA);
utils.addEvtListener(this.ctaBtnEl, 'click', step.onCTA);
this.onCTA = step.onCTA; // cache for removing later
}
}
else if (this.onCTA) {
// Remove previous CTA callback.
utils.removeClickListener(this.ctaBtnEl, this.onCTA);
utils.removeEvtListener(this.ctaBtnEl, 'click', this.onCTA);
this.onCTA = null;
}

Expand Down Expand Up @@ -1044,10 +1039,10 @@
el.parentNode.removeChild(el);
}
if (this.closeBtnEl) {
utils.removeClickListener(this.closeBtnEl, this._getCloseFn());
utils.removeEvtListener(this.closeBtnEl, 'click', this._getCloseFn());
}
if (this.ctaBtnEl && this.onCTA) {
utils.removeClickListener(this.ctaBtnEl, this.onCTA);
utils.removeEvtListener(this.ctaBtnEl, 'click', this.onCTA);
}
},

Expand All @@ -1058,7 +1053,6 @@
self = this,
resizeCooldown = false, // for updating after window resize
onWinResize,
winResizeTimeout,
appendToBody,
opt;

Expand Down Expand Up @@ -1119,19 +1113,13 @@
}

resizeCooldown = true;
winResizeTimeout = setTimeout(function() {
setTimeout(function() {
self.setPosition(self.currStep);
resizeCooldown = false;
}, 100);
};

if (window.addEventListener) {
window.addEventListener('resize', onWinResize, false);
}

else if (window.attachEvent) {
window.attachEvent('onresize', onWinResize, false);
}
utils.addEvtListener(window, 'resize', onWinResize);

this.hide();

Expand All @@ -1154,7 +1142,6 @@
};

document.addEventListener('DOMContentLoaded', appendToBody, false);
window.addEventListener('load', appendToBody, false);
}
// IE
else {
Expand All @@ -1168,8 +1155,8 @@
};

document.attachEvent('onreadystatechange', appendToBody);
window.attachEvent('onload', appendToBody);
}
utils.addEvtListener(window, 'load', appendToBody);
}
}
};
Expand Down Expand Up @@ -1814,7 +1801,7 @@

// If we want to advance to next step when user clicks on target.
if (step.nextOnTargetClick) {
utils.addClickListener(targetEl, targetClickNextFn);
utils.addEvtListener(targetEl, 'click', targetClickNextFn);
}
});

Expand Down Expand Up @@ -1854,7 +1841,7 @@

if (step.nextOnTargetClick) {
// Detach the listener after we've clicked on the target OR the next button.
utils.removeClickListener(targetEl, targetClickNextFn);
utils.removeEvtListener(targetEl, 'click', targetClickNextFn);
}
changeStep.call(this, doCallbacks, 1);
return this;
Expand Down

0 comments on commit a231d78

Please sign in to comment.