refactor umbraAboveBelowOrOnScreen into umbraBehavior object

This commit is contained in:
Noah Levitt 2015-09-24 12:34:55 -07:00
parent f2ead0570e
commit a17b0f3b8d

View File

@ -1,17 +1,4 @@
var umbraEndConditionTarget;
var umbraAboveBelowOrOnScreen = function(e) {
var eTop = e.getBoundingClientRect().top;
if (eTop < window.scrollY) {
return -1; // above
} else if (eTop > window.scrollY + window.innerHeight) {
return 1; // below
} else {
return 0; // on screen
}
}
var umbraSimpleScrollsAndClicksBehavior = {
var umbraBehavior = {
IDLE_TIMEOUT_SEC : 10,
idleSince : null,
alreadyClicked : {},
@ -39,7 +26,7 @@ var umbraSimpleScrollsAndClicksBehavior = {
continue;
}
var where = umbraAboveBelowOrOnScreen(clickTargets[i]);
var where = this.aboveBelowOrOnScreen(clickTargets[i]);
if (where == 0) {
console.log("clicking on " + clickTargets[i].outerHTML);
@ -66,17 +53,17 @@ var umbraSimpleScrollsAndClicksBehavior = {
if (!clickedSomething) {
if (somethingLeftAbove) {
console.log("scrolling UP because everything on this screen has been clicked but we missed something above");
// console.log("scrolling UP because everything on this screen has been clicked but we missed something above");
window.scrollBy(0, -500);
this.idleSince = null;
} else if (somethingLeftBelow) {
console.log("scrolling because everything on this screen has been clicked but there's more below document.body.clientHeight="
+ document.body.clientHeight);
// console.log("scrolling because everything on this screen has been clicked but there's more below document.body.clientHeight="
// + document.body.clientHeight);
window.scrollBy(0, 200);
this.idleSince = null;
} else if (window.scrollY + window.innerHeight < document.documentElement.scrollHeight) {
console.log("scrolling because we're not to the bottom yet document.body.clientHeight="
+ document.body.clientHeight);
// console.log("scrolling because we're not to the bottom yet document.body.clientHeight="
// + document.body.clientHeight);
window.scrollBy(0, 200);
this.idleSince = null;
} else if (this.idleSince == null) {
@ -105,11 +92,22 @@ var umbraSimpleScrollsAndClicksBehavior = {
}
return false;
},
aboveBelowOrOnScreen : function(e) {
var eTop = e.getBoundingClientRect().top;
if (eTop < window.scrollY) {
return -1; // above
} else if (eTop > window.scrollY + window.innerHeight) {
return 1; // below
} else {
return 0; // on screen
}
},
};
// Called from outside of this script.
var umbraBehaviorFinished = function() {
return umbraSimpleScrollsAndClicksBehavior.isFinished()
return umbraBehavior.isFinished()
};
umbraSimpleScrollsAndClicksBehavior.start();
umbraBehavior.start();