diff --git a/brozzler/js-templates/umbraBehavior.js.j2 b/brozzler/js-templates/umbraBehavior.js.j2 index c3716ae..ffe9d22 100644 --- a/brozzler/js-templates/umbraBehavior.js.j2 +++ b/brozzler/js-templates/umbraBehavior.js.j2 @@ -22,15 +22,15 @@ var umbraBehavior = { alreadyDone : [], idleSince : null, intervalId : null, + state : null, + actions : {{actions|json}}, intervalFunc: function() { - var actionsLength = this.actions.length; - for (var k = 0; k < actionsLength; k++) { - var didSomething = false; - var somethingLeftBelow = false; - var somethingLeftAbove = false; - + if (!this.state) { + this.state = this.actions.length === 1 ? "simple" : "fancy"; + } + for (var k = 0; k < this.actions.length; k++) { var selector = this.actions[k].selector; var action = this.actions[k].do ? this.actions[k].do : 'click'; var limit = this.actions[k].limit ? this.actions[k].limit : 0; @@ -41,94 +41,80 @@ var umbraBehavior = { this.actions[k].alreadyDone = []; } - var iframes = document.querySelectorAll("iframe"); - var documents = Array(iframes.length + 1); - documents[0] = document; + if (this.state === "simple") { + var didSomething = false; + var somethingLeftBelow = false; + var somethingLeftAbove = false; - iframesLength = iframes.length; - for (var i = 0; i < iframesLength; i++) { - documents[i+1] = iframes[i].contentWindow.document; - } + var iframes = document.querySelectorAll("iframe"); + var documents = Array(iframes.length + 1); + documents[0] = document; - documentsLength = documents.length; - for (var j = 0; j < documentsLength; j++) { - - var doTargets = documents[j].querySelectorAll(selector); - if (doTargets == []) { - continue; + iframesLength = iframes.length; + for (var i = 0; i < iframesLength; i++) { + documents[i+1] = iframes[i].contentWindow.document; } - doTargetsLength = doTargets.length; - for ( var i = 0; i < doTargetsLength; i++) { - if (limit && this.actions[k].alreadyDone && this.actions[k].alreadyDone.length >= limit) { - break; - } - if (this.alreadyDone.indexOf(doTargets[i]) > -1) { - continue; - } - if (!this.isVisible(doTargets[i])) { + documentsLength = documents.length; + for (var j = 0; j < documentsLength; j++) { + + var doTargets = documents[j].querySelectorAll(selector); + if (doTargets == []) { continue; } - var where = this.aboveBelowOrOnScreen(doTargets[i]); - if (where == 0) { - this.doTarget(doTargets[i], action); - didSomething = true; - if (this.actions[k].alreadyDone) { - this.actions[k].alreadyDone.push(doTargets[i]); + doTargetsLength = doTargets.length; + for ( var i = 0; i < doTargetsLength; i++) { + if (limit && this.actions[k].alreadyDone && this.actions[k].alreadyDone.length >= limit) { + break; + } + 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) { - break; // break from doTargets loop, but not from documents loop - } - + } + if (!didSomething) { 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); this.idleSince = null; } 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); this.idleSince = null; } 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); this.idleSince = null; - } else if (this.idleSince == null) { + } else if (this.idleSince == null) { this.idleSince = Date.now(); } } } - if (!this.idleSince) { 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) { var eTop = elem.getBoundingClientRect().top; if (eTop < window.scrollY) { @@ -162,6 +148,23 @@ var umbraBehavior = { 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 };