checking state: first support only 'simple' behaviors

This commit is contained in:
Barbara Miller 2017-09-01 18:52:26 -07:00
parent 0a52f7fe84
commit 03e14e3db8

View file

@ -22,15 +22,15 @@ var umbraBehavior = {
alreadyDone : [], alreadyDone : [],
idleSince : null, idleSince : null,
intervalId : null, intervalId : null,
state : null,
actions : {{actions|json}}, actions : {{actions|json}},
intervalFunc: function() { intervalFunc: function() {
var actionsLength = this.actions.length; if (!this.state) {
for (var k = 0; k < actionsLength; k++) { this.state = this.actions.length === 1 ? "simple" : "fancy";
var didSomething = false; }
var somethingLeftBelow = false; for (var k = 0; k < this.actions.length; k++) {
var somethingLeftAbove = false;
var selector = this.actions[k].selector; var selector = this.actions[k].selector;
var action = this.actions[k].do ? this.actions[k].do : 'click'; var action = this.actions[k].do ? this.actions[k].do : 'click';
var limit = this.actions[k].limit ? this.actions[k].limit : 0; var limit = this.actions[k].limit ? this.actions[k].limit : 0;
@ -41,94 +41,80 @@ var umbraBehavior = {
this.actions[k].alreadyDone = []; this.actions[k].alreadyDone = [];
} }
var iframes = document.querySelectorAll("iframe"); if (this.state === "simple") {
var documents = Array(iframes.length + 1); var didSomething = false;
documents[0] = document; var somethingLeftBelow = false;
var somethingLeftAbove = false;
iframesLength = iframes.length; var iframes = document.querySelectorAll("iframe");
for (var i = 0; i < iframesLength; i++) { var documents = Array(iframes.length + 1);
documents[i+1] = iframes[i].contentWindow.document; documents[0] = document;
}
documentsLength = documents.length; iframesLength = iframes.length;
for (var j = 0; j < documentsLength; j++) { for (var i = 0; i < iframesLength; i++) {
documents[i+1] = iframes[i].contentWindow.document;
var doTargets = documents[j].querySelectorAll(selector);
if (doTargets == []) {
continue;
} }
doTargetsLength = doTargets.length; documentsLength = documents.length;
for ( var i = 0; i < doTargetsLength; i++) { for (var j = 0; j < documentsLength; j++) {
if (limit && this.actions[k].alreadyDone && this.actions[k].alreadyDone.length >= limit) {
break; var doTargets = documents[j].querySelectorAll(selector);
} if (doTargets == []) {
if (this.alreadyDone.indexOf(doTargets[i]) > -1) {
continue;
}
if (!this.isVisible(doTargets[i])) {
continue; continue;
} }
var where = this.aboveBelowOrOnScreen(doTargets[i]); doTargetsLength = doTargets.length;
if (where == 0) { for ( var i = 0; i < doTargetsLength; i++) {
this.doTarget(doTargets[i], action); if (limit && this.actions[k].alreadyDone && this.actions[k].alreadyDone.length >= limit) {
didSomething = true; break;
if (this.actions[k].alreadyDone) { }
this.actions[k].alreadyDone.push(doTargets[i]); if (this.alreadyDone.indexOf(doTargets[i]) > -1) {
continue;
}
if (!this.isVisible(doTargets[i])) {
continue;
}
var where = this.aboveBelowOrOnScreen(doTargets[i]);
if (where == 0) {
this.doTarget(doTargets[i], action);
if (this.actions[k].alreadyDone) {
this.actions[k].alreadyDone.push(doTargets[i]);
}
didSomething = true;
break; // break from doTargets loop, but not from documents loop
} else if (where > 0) {
somethingLeftBelow = true;
} else if (where < 0) {
somethingLeftAbove = true;
} }
} else if (where > 0) {
somethingLeftBelow = true;
} else if (where < 0) {
somethingLeftAbove = true;
} }
}
if (didSomething) { if (!didSomething) {
break; // break from doTargets loop, but not from documents loop
}
if (somethingLeftAbove) { if (somethingLeftAbove) {
// console.log("scrolling UP because everything on this screen has been done 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); window.scrollBy(0, -500);
this.idleSince = null; this.idleSince = null;
} else if (somethingLeftBelow) { } else if (somethingLeftBelow) {
// console.log("scrolling because everything on this screen has been done but there's more below"); // 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); window.scrollBy(0, 200);
this.idleSince = null; this.idleSince = null;
} else if (window.scrollY + window.innerHeight < document.documentElement.scrollHeight) { } else if (window.scrollY + window.innerHeight < document.documentElement.scrollHeight) {
// console.log("scrolling because we're not to the bottom yet"); // console.log("scrolling because we're not to the bottom yet document.body.clientHeight="
// + document.body.clientHeight);
window.scrollBy(0, 200); window.scrollBy(0, 200);
this.idleSince = null; this.idleSince = null;
} else if (this.idleSince == null) { } else if (this.idleSince == null) {
this.idleSince = Date.now(); this.idleSince = Date.now();
} }
} }
} }
if (!this.idleSince) { if (!this.idleSince) {
this.idleSince = Date.now(); this.idleSince = Date.now();
} }
} }
}, },
start : function() {
var that = this;
this.intervalId = setInterval(function() {
that.intervalFunc()
}, 1000);
},
isFinished : function() {
if (this.idleSince != null) {
var idleTimeMs = Date.now() - this.idleSince;
if (idleTimeMs / 1000 > this.IDLE_TIMEOUT_SEC) {
clearInterval(this.intervalId);
return true;
}
}
return false;
},
aboveBelowOrOnScreen : function(elem) { aboveBelowOrOnScreen : function(elem) {
var eTop = elem.getBoundingClientRect().top; var eTop = elem.getBoundingClientRect().top;
if (eTop < window.scrollY) { if (eTop < window.scrollY) {
@ -162,6 +148,23 @@ var umbraBehavior = {
this.idleSince = null; this.idleSince = null;
}, },
start : function() {
var that = this;
this.intervalId = setInterval(function() {
that.intervalFunc()
}, 500);
},
isFinished : function() {
if (this.idleSince != null) {
var idleTimeMs = Date.now() - this.idleSince;
if (idleTimeMs / 1000 > this.IDLE_TIMEOUT_SEC) {
clearInterval(this.intervalId);
return true;
}
}
return false;
},
// end umbraBehavior definition // end umbraBehavior definition
}; };