1
0
Fork 0
SnoozeSquad/SnoozeSquad.js

81 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-05-28 15:51:39 +00:00
"use strict";
class SnoozeSquad
{
constructor(options)
{
this.range = 1;
this.attributeName = "data-src";
this.updateInterval = 250;
for (let optionKey in options)
this[optionKey] = options[optionKey];
this.querySelector = `[${this.attributeName}]`;
this.lastUpdateTime = 0;
this.start();
2016-11-03 17:40:27 +00:00
this.update();
2016-05-28 15:51:39 +00:00
}
start()
{
2016-06-09 08:06:10 +00:00
// Passive event listener check from
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
var supportsPassiveEvents = false;
try {
var opts = Object.defineProperty({}, 'passive', { get: function() { supportsPassiveEvents = true; } });
window.addEventListener("test", null, opts);
} catch (e) {}
2016-05-28 15:51:39 +00:00
window.addEventListener("scroll", (function(event) {
if((+new Date()) - this.lastUpdateTime > this.updateInterval)
this.update();
2016-06-09 08:06:10 +00:00
}).bind(this), supportsPassiveEvents ? { passive: true } : false);
2016-05-28 15:51:39 +00:00
}
update()
{
this.lastUpdateTime = +new Date();
//console.log("Updating");
var snoozingElements = this.getSnoozingElements();
for (let element of snoozingElements)
{
if(this.isElementInRange(element))
this.wakeUpElement(element);
}
}
getSnoozingElements()
{
return document.querySelectorAll(this.querySelector);
}
isElementInRange(element)
{
var bounds = element.getBoundingClientRect(),
range = this.range * window.innerHeight;
if(bounds.bottom > -range && bounds.top < window.innerHeight + range &&
bounds.right > -range && bounds.left < window.innerWidth + range)
{
return true;
}
return false;
}
wakeUpElement(element)
{
var attr = this.attributeName.replace("data-", "");
if(element.dataset.hasOwnProperty(attr))
{
//console.info("Woke up element", element);
element.src = element.dataset[attr];
}
else
{
//console.warn(`Failed to wake up element (no ${attr} attribute present)`);
}
}
}