From 2365becc148fa8a1c519507b6ec7a3b21d530dc6 Mon Sep 17 00:00:00 2001 From: Tim Hobbs Date: Tue, 8 Apr 2014 11:22:03 -0700 Subject: [PATCH] Enabled lazy load of poster images This should make initial load of the movies page a little more responsive for larger libraries. This can be used on any page loading images. --- dashboard-ui/css/librarybrowser.css | 5 ++ dashboard-ui/scripts/librarybrowser.js | 13 +++- dashboard-ui/scripts/movies.js | 11 +++- .../thirdparty/jquery.unveil-custom.js | 62 +++++++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 dashboard-ui/thirdparty/jquery.unveil-custom.js diff --git a/dashboard-ui/css/librarybrowser.css b/dashboard-ui/css/librarybrowser.css index a65d8a37dc..e97f9d215b 100644 --- a/dashboard-ui/css/librarybrowser.css +++ b/dashboard-ui/css/librarybrowser.css @@ -1144,6 +1144,11 @@ a.itemTag:hover { margin-left: .5em; } +.lazy { + opacity: 0; + transition: opacity .3s ease-in; +} + @media all and (min-height: 500px) { .alphabetPicker { diff --git a/dashboard-ui/scripts/librarybrowser.js b/dashboard-ui/scripts/librarybrowser.js index 6c18da00f3..c94937596f 100644 --- a/dashboard-ui/scripts/librarybrowser.js +++ b/dashboard-ui/scripts/librarybrowser.js @@ -433,6 +433,8 @@ options.shape = options.shape || "portrait"; + options.lazy = options.lazy || false; + var html = ""; var primaryImageAspectRatio = options.shape == 'auto' ? LibraryBrowser.getAveragePrimaryImageAspectRatio(items) : null; @@ -668,7 +670,7 @@ var style = ""; - if (imgUrl) { + if (imgUrl && !options.lazy) { style += 'background-image:url(\'' + imgUrl + '\');'; } @@ -681,9 +683,16 @@ imageCssClass += " coveredPosterItemImage"; } + var dataSrc = ""; + + if (options.lazy) { + imageCssClass += " lazy"; + dataSrc = ' data-src="' + imgUrl + '"'; + } + var progressHtml = options.showProgress === false ? '' : LibraryBrowser.getItemProgressBarHtml(item); - html += '
'; + html += '
'; html += '
'; diff --git a/dashboard-ui/scripts/movies.js b/dashboard-ui/scripts/movies.js index 4f44bbe16b..4e0a2dc72c 100644 --- a/dashboard-ui/scripts/movies.js +++ b/dashboard-ui/scripts/movies.js @@ -56,7 +56,8 @@ context: 'movies', showTitle: true, centerText: true, - selectionPanel: true + selectionPanel: true, + lazy: true }); $('.itemsContainer', page).removeClass('timelineItemsContainer'); } @@ -383,4 +384,12 @@ updateFilterControls(this); }); + $(function () { + $("body").on("create", function () { + $(".lazy").unveil(200, function () { + this.style.opacity = 1; + }); + }); + }); + })(jQuery, document); \ No newline at end of file diff --git a/dashboard-ui/thirdparty/jquery.unveil-custom.js b/dashboard-ui/thirdparty/jquery.unveil-custom.js new file mode 100644 index 0000000000..79f275a309 --- /dev/null +++ b/dashboard-ui/thirdparty/jquery.unveil-custom.js @@ -0,0 +1,62 @@ +/** + * jQuery Unveil + * A very lightweight jQuery plugin to lazy load images + * http://luis-almeida.github.com/unveil + * + * Licensed under the MIT license. + * Copyright 2013 Luís Almeida + * https://github.com/luis-almeida + */ + +; (function ($) { + + $.fn.unveil = function (threshold, callback) { + + var $w = $(window), + th = threshold || 0, + retina = window.devicePixelRatio > 1, + attrib = retina ? "data-src-retina" : "data-src", + images = this, + loaded; + + this.one("unveil", function () { + var elemType = $(this).get(0).tagName; + var source = this.getAttribute(attrib); + source = source || this.getAttribute("data-src"); + if (source) { + if (elemType === "DIV") { + this.style.backgroundImage = "url('" + source + "')"; + } else { + this.setAttribute("src", source); + } + if (typeof callback === "function") callback.call(this); + } + }); + + function unveil() { + var inview = images.filter(function () { + var $e = $(this); + if ($e.is(":hidden")) return; + + var wt = $w.scrollTop(), + wb = wt + $w.height(), + et = $e.offset().top, + eb = et + $e.height(); + + return eb >= wt - th && et <= wb + th; + }); + + loaded = inview.trigger("unveil"); + images = images.not(loaded); + } + + $w.scroll(unveil); + $w.resize(unveil); + + unveil(); + + return this; + + }; + +})(window.jQuery || window.Zepto); \ No newline at end of file