From e61a67134d8ba2c6363c951031456870be34f3cb Mon Sep 17 00:00:00 2001 From: Ivan Malopinsky Date: Sun, 25 Jan 2015 00:00:00 -0500 Subject: [PATCH] jsdoc for invisible image functions, polyfill for rAF --- src/holder.js | 49 ++++++++++++++++++++++++++++++------------------ src/polyfills.js | 32 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/holder.js b/src/holder.js index 49a8aa3..20d2222 100644 --- a/src/holder.js +++ b/src/holder.js @@ -869,28 +869,36 @@ Holder.js - client side image placeholders } } - //todo jsdoc + /** + * Iterates through all current invisible images, and if they're visible, renders them and removes them from further checks. Runs every animation frame. + * + * @private + */ function visibilityCheck() { - var renderableImages = []; - var keys = Object.keys(App.vars.invisibleImages); - var el; - for (var i = 0, l = keys.length; i < l; i++) { - el = App.vars.invisibleImages[keys[i]]; - if (dimensionCheck(el) && el.nodeName.toLowerCase() == 'img') { - renderableImages.push(el); - delete App.vars.invisibleImages[keys[i]]; - } + var renderableImages = []; + var keys = Object.keys(App.vars.invisibleImages); + var el; + for (var i = 0, l = keys.length; i < l; i++) { + el = App.vars.invisibleImages[keys[i]]; + if (dimensionCheck(el) && el.nodeName.toLowerCase() == 'img') { + renderableImages.push(el); + delete App.vars.invisibleImages[keys[i]]; } + } - if (renderableImages.length) { - Holder.run({ - images: renderableImages - }); - } - global.requestAnimationFrame(visibilityCheck); + if (renderableImages.length) { + Holder.run({ + images: renderableImages + }); + } + global.requestAnimationFrame(visibilityCheck); } - //todo jsdoc + /** + * Starts checking for invisible placeholders if not doing so yet. Does nothing otherwise. + * + * @private + */ function startVisibilityCheck() { if (!App.vars.visibilityCheckStarted) { global.requestAnimationFrame(visibilityCheck); @@ -898,7 +906,12 @@ Holder.js - client side image placeholders } } - //todo jsdoc + /** + * Sets a unique ID for an image detected to be invisible and adds it to the map of invisible images checked by visibilityCheck + * + * @private + * @param el Invisible DOM element + */ function setInvisible(el) { if (!el.holderData.invisibleId) { App.vars.invisibleId += 1; diff --git a/src/polyfills.js b/src/polyfills.js index 1ce1ab2..79c7edb 100644 --- a/src/polyfills.js +++ b/src/polyfills.js @@ -257,3 +257,35 @@ if (!Object.prototype.hasOwnProperty){ } /*jshint +W001, +W103 */ } + +//requestAnimationFrame polyfill for older Firefox/Chrome versions +if (!window.requestAnimationFrame) { + if (window.webkitRequestAnimationFrame) { + //https://github.com/Financial-Times/polyfill-service/blob/master/polyfills/requestAnimationFrame/polyfill-webkit.js + (function (global) { + // window.requestAnimationFrame + global.requestAnimationFrame = function (callback) { + return webkitRequestAnimationFrame(function () { + callback(performance.now()); + }); + }; + + // window.cancelAnimationFrame + global.cancelAnimationFrame = webkitCancelAnimationFrame; + }(this)); + } else if (window.mozRequestAnimationFrame) { + //https://github.com/Financial-Times/polyfill-service/blob/master/polyfills/requestAnimationFrame/polyfill-moz.js + (function (global) { + // window.requestAnimationFrame + global.requestAnimationFrame = function (callback) { + return mozRequestAnimationFrame(function () { + callback(performance.now()); + }); + }; + + // window.cancelAnimationFrame + global.cancelAnimationFrame = mozCancelAnimationFrame; + + }(this)); + } +} \ No newline at end of file