Merge branch 'behavior-refactor' into qa

This commit is contained in:
Barbara Miller 2017-09-07 10:50:50 -07:00
commit bd83f079a7
2 changed files with 83 additions and 80 deletions

View File

@ -42,9 +42,7 @@
behavior_js_template: umbraBehavior.js.j2
default_parameters:
actions:
- selector: li.pager__item a
limit: 4
- selector: div.teaser
- selector: div.teaser, li.pager__item a
-
url_regex: '^https?://(?:www\.)?huffingtonpost\.com/.*$'
behavior_js_template: huffpostslides.js
@ -122,7 +120,10 @@
actions:
- selector: .menu-item a
do: mouseover
<<<<<<< HEAD
do_until_hard_timeout: False
=======
>>>>>>> behavior-refactor
- url_regex: '^https?://(?:www\.)?news\.com\.au/.*$'
behavior_js_template: mouseovers.js.j2
default_parameters:

View File

@ -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,95 +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 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);
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);
window.scrollBy(0, 200);
this.idleSince = null;
} else if (this.idleSince == null) {
this.idleSince = Date.now();
}
}
}
if (!didSomething) {
if (somethingLeftAbove) {
// console.log("scrolling UP because everything on this screen has been done 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");
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");
window.scrollBy(0, 200);
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()
}, 250);
},
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) {
@ -163,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
};