From 66dcfd7792d65b62f126248222e4643a1d0d6c71 Mon Sep 17 00:00:00 2001 From: Raphael Eidus Date: Sat, 10 May 2014 22:02:41 -0400 Subject: [PATCH] reduce file size --- dist/echo.js | 69 +++++++++++++----------------------------------- dist/echo.min.js | 2 +- src/echo.js | 69 +++++++++++++----------------------------------- 3 files changed, 39 insertions(+), 101 deletions(-) diff --git a/dist/echo.js b/dist/echo.js index a979be7..3479677 100644 --- a/dist/echo.js +++ b/dist/echo.js @@ -3,18 +3,6 @@ window.Echo = (function (global, document, undefined) { 'use strict'; - /** - * toBeLoaded - * @type {Array} - */ - var toBeLoaded = []; - - /** - * toBeUnloaded - * @type {Array} - */ - var toBeUnloaded = []; - /** * callback - initialized to a no-op so that no validations on it's presence need to be made * @type {Function} @@ -43,48 +31,36 @@ window.Echo = (function (global, document, undefined) { * @private */ var _pollImages = function () { - var loadingLength = toBeLoaded.length, - unloadingLength, + var src, + length, i, - self, - view; + elem, + view, + nodes = [].slice.call(document.querySelectorAll('img[data-echo]')); view = { l: 0 - offset.l, t: 0 - offset.t, b: (window.innerHeight || document.documentElement.clientHeight) + offset.b, r: (window.innerWidth || document.documentElement.clientWidth) + offset.r }; - if (loadingLength > 0) { - for (i = 0; i < loadingLength; i++) { - self = toBeLoaded[i]; - if (self && _inView(self, view)) { - if(unload) { - self.setAttribute('data-echo-placeholder', self.src); - toBeUnloaded.push(self); - } - self.src = self.getAttribute('data-echo'); - callback(self, 'load'); - toBeLoaded.splice(i, 1); - loadingLength = toBeLoaded.length; - i--; + for(i=0; i 0) { - for(i = 0; i < unloadingLength; i++) { - self = toBeUnloaded[i]; - if (self && !_inView(self, view)) { - self.src = self.getAttribute('data-echo-placeholder'); - callback(self, 'unload'); - toBeUnloaded.splice(i, 1); - unloadingLength = toBeUnloaded.length; - i--; - toBeLoaded.push(self); + elem.src = elem.getAttribute('data-echo'); + if(!unload) { + elem.removeAttribute('data-echo'); } + callback(elem, 'load'); + } else if(unload && !!(src = elem.getAttribute('data-echo-placeholder'))) { + elem.src = src; + elem.removeAttribute('data-echo-placeholder'); + callback(elem, 'unload'); } } - if(unloadingLength === 0 && loadingLength === 0) { + if(!length) { detach(); } }; @@ -112,7 +88,6 @@ window.Echo = (function (global, document, undefined) { */ var init = function (opts) { - var nodes = document.querySelectorAll('img[data-echo]'); opts = opts || {}; var offsetAll = opts.offset || 0; var offsetVertical = opts.offsetVertical || offsetAll; @@ -132,12 +107,6 @@ window.Echo = (function (global, document, undefined) { unload = !!opts.unload; callback = opts.callback || callback; - toBeLoaded = []; - toBeUnloaded = []; - - for (var i = 0; i < nodes.length; i++) { - toBeLoaded.push(nodes[i]); - } _pollImages(); diff --git a/dist/echo.min.js b/dist/echo.min.js index d007381..725edf0 100644 --- a/dist/echo.min.js +++ b/dist/echo.min.js @@ -1,2 +1,2 @@ /*! Echo v1.5.0 | (c) 2014 @toddmotto | MIT license | github.com/toddmotto/echo */ -window.Echo=function(a,b){"use strict";var c,d,e,f,g=[],h=[],i=function(){},j=function(a,b){var c=a.getBoundingClientRect();return c.right>=b.l&&c.bottom>=b.t&&c.left<=b.r&&c.top<=b.b},k=function(){var a,d,e,k,l=g.length;if(k={l:0-c.l,t:0-c.t,b:(window.innerHeight||b.documentElement.clientHeight)+c.b,r:(window.innerWidth||b.documentElement.clientWidth)+c.r},l>0)for(d=0;l>d;d++)e=g[d],e&&j(e,k)&&(f&&(e.setAttribute("data-echo-placeholder",e.src),h.push(e)),e.src=e.getAttribute("data-echo"),i(e,"load"),g.splice(d,1),l=g.length,d--);if(a=h.length,a>0)for(d=0;a>d;d++)e=h[d],e&&!j(e,k)&&(e.src=e.getAttribute("data-echo-placeholder"),i(e,"unload"),h.splice(d,1),a=h.length,d--,g.push(e));0===a&&0===l&&n()},l=function(){clearTimeout(e),e=setTimeout(k,d)},m=function(e){function j(a,b){return parseInt(a||b,10)}var m=b.querySelectorAll("img[data-echo]");e=e||{};var n=e.offset||0,o=e.offsetVertical||n,p=e.offsetHorizontal||n;c={t:j(e.offsetTop,o),b:j(e.offsetBottom,o),l:j(e.offsetLeft,p),r:j(e.offsetRight,p)},d=j(e.throttle,250),f=!!e.unload,i=e.callback||i,g=[],h=[];for(var q=0;q=b.l&&c.bottom>=b.t&&c.left<=b.r&&c.top<=b.b},i=function(){var a,d,e,i,j,k=[].slice.call(b.querySelectorAll("img[data-echo]"));for(j={l:0-c.l,t:0-c.t,b:(window.innerHeight||b.documentElement.clientHeight)+c.b,r:(window.innerWidth||b.documentElement.clientWidth)+c.r},e=0;d>e;e++)i=k[e],h(i,j)?(f&&i.setAttribute("data-echo-placeholder",i.src),i.src=i.getAttribute("data-echo"),f||i.removeAttribute("data-echo"),g(i,"load")):f&&(a=i.getAttribute("data-echo-placeholder"))&&(i.src=a,i.removeAttribute("data-echo-placeholder"),g(i,"unload"));d||l()},j=function(){clearTimeout(e),e=setTimeout(i,d)},k=function(e){function h(a,b){return parseInt(a||b,10)}e=e||{};var k=e.offset||0,l=e.offsetVertical||k,m=e.offsetHorizontal||k;c={t:h(e.offsetTop,l),b:h(e.offsetBottom,l),l:h(e.offsetLeft,m),r:h(e.offsetRight,m)},d=h(e.throttle,250),f=!!e.unload,g=e.callback||g,i(),b.addEventListener?(a.addEventListener("scroll",j,!1),a.addEventListener("load",j,!1)):(a.attachEvent("onscroll",j),a.attachEvent("onload",j))},l=function(){b.removeEventListener?a.removeEventListener("scroll",j):a.detachEvent("onscroll",j),clearTimeout(e)};return{init:k,detach:l,render:i}}(this,document); \ No newline at end of file diff --git a/src/echo.js b/src/echo.js index e69d2c4..2fdda37 100644 --- a/src/echo.js +++ b/src/echo.js @@ -2,18 +2,6 @@ window.Echo = (function (global, document, undefined) { 'use strict'; - /** - * toBeLoaded - * @type {Array} - */ - var toBeLoaded = []; - - /** - * toBeUnloaded - * @type {Array} - */ - var toBeUnloaded = []; - /** * callback - initialized to a no-op so that no validations on it's presence need to be made * @type {Function} @@ -42,48 +30,36 @@ window.Echo = (function (global, document, undefined) { * @private */ var _pollImages = function () { - var loadingLength = toBeLoaded.length, - unloadingLength, + var src, + length, i, - self, - view; + elem, + view, + nodes = [].slice.call(document.querySelectorAll('img[data-echo]')); view = { l: 0 - offset.l, t: 0 - offset.t, b: (window.innerHeight || document.documentElement.clientHeight) + offset.b, r: (window.innerWidth || document.documentElement.clientWidth) + offset.r }; - if (loadingLength > 0) { - for (i = 0; i < loadingLength; i++) { - self = toBeLoaded[i]; - if (self && _inView(self, view)) { - if(unload) { - self.setAttribute('data-echo-placeholder', self.src); - toBeUnloaded.push(self); - } - self.src = self.getAttribute('data-echo'); - callback(self, 'load'); - toBeLoaded.splice(i, 1); - loadingLength = toBeLoaded.length; - i--; + for(i=0; i 0) { - for(i = 0; i < unloadingLength; i++) { - self = toBeUnloaded[i]; - if (self && !_inView(self, view)) { - self.src = self.getAttribute('data-echo-placeholder'); - callback(self, 'unload'); - toBeUnloaded.splice(i, 1); - unloadingLength = toBeUnloaded.length; - i--; - toBeLoaded.push(self); + elem.src = elem.getAttribute('data-echo'); + if(!unload) { + elem.removeAttribute('data-echo'); } + callback(elem, 'load'); + } else if(unload && !!(src = elem.getAttribute('data-echo-placeholder'))) { + elem.src = src; + elem.removeAttribute('data-echo-placeholder'); + callback(elem, 'unload'); } } - if(unloadingLength === 0 && loadingLength === 0) { + if(!length) { detach(); } }; @@ -111,7 +87,6 @@ window.Echo = (function (global, document, undefined) { */ var init = function (opts) { - var nodes = document.querySelectorAll('img[data-echo]'); opts = opts || {}; var offsetAll = opts.offset || 0; var offsetVertical = opts.offsetVertical || offsetAll; @@ -131,12 +106,6 @@ window.Echo = (function (global, document, undefined) { unload = !!opts.unload; callback = opts.callback || callback; - toBeLoaded = []; - toBeUnloaded = []; - - for (var i = 0; i < nodes.length; i++) { - toBeLoaded.push(nodes[i]); - } _pollImages();