A free, self-hostable news aggregator…
Fix issue #75 : chargement des images en lazyload, reste à le rendre optionnel
Marien Fressinaud1d459af0464e
diff --git a/app/App_FrontController.php b/app/App_FrontController.phpindex 77261280e..5729a115d 100644--- a/app/App_FrontController.php+++ b/app/App_FrontController.php@@ -56,6 +56,7 @@ class App_FrontController extends FrontController {View::appendScript ('https://login.persona.org/include.js');}View::appendScript (Url::display ('/scripts/jquery.js'));+ View::appendScript (Url::display ('/scripts/jquery.lazyload.min.js'));View::appendScript (Url::display ('/scripts/notification.js'));}diff --git a/app/views/index/index.phtml b/app/views/index/index.phtmlindex 9974a9b0b..7037b2405 100644--- a/app/views/index/index.phtml+++ b/app/views/index/index.phtml@@ -1,4 +1,12 @@<?php+function lazyimg($content) {+ return preg_replace(+ '/<img([^<]+)src=([\'"])([^"\']*)([\'"])([^<]*)>/i',+ '<img$1src="' . Url::display('/data/grey.gif') . '" data-original="$3"$5>',+ $content+ );+}+$output = Request::param ('output', 'normal');if ($output == 'rss') {@@ -58,7 +66,7 @@ if ($output == 'rss') {<h1 class="title"><?php echo $item->title (); ?></h1><?php $author = $item->author (); ?><?php echo $author != '' ? '<div class="author">' . Translate::t ('by_author', $author) . '</div>' : ''; ?>- <?php echo $item->content (); ?>+ <?php echo lazyimg($item->content ()); ?></div><ul class="horizontal-list bottom">diff --git a/app/views/javascript/main.phtml b/app/views/javascript/main.phtmlindex e6c882333..443f62c31 100644--- a/app/views/javascript/main.phtml+++ b/app/views/javascript/main.phtml@@ -109,8 +109,8 @@ function mark_favorite (active) {}function init_img () {- $(".flux .content img").each (function () {- if ($(this).width () > ($("#stream .content").width()) / 2) {+ $(".flux_content .content img").each (function () {+ if ($(this).width () > ($(".flux_content .content").width()) / 2) {$(this).addClass("big");}});@@ -125,6 +125,8 @@ function init_posts () {<?php } ?>init_img ();+ // TODO rendre optionnel+ $(".flux .content img").lazyload();if (hide_posts) {$(".flux:not(.active) .flux_content").hide ();diff --git a/public/scripts/jquery.lazyload.min.js b/public/scripts/jquery.lazyload.min.jsnew file mode 100644index 000000000..79663d7bb--- /dev/null+++ b/public/scripts/jquery.lazyload.min.js@@ -0,0 +1,15 @@+/*+ * Lazy Load - jQuery plugin for lazy loading images+ *+ * Copyright (c) 2007-2013 Mika Tuupola+ *+ * Licensed under the MIT license:+ * http://www.opensource.org/licenses/mit-license.php+ *+ * Project home:+ * http://www.appelsiini.net/projects/lazyload+ *+ * Version: 1.8.4+ *+ */+(function(a,b,c,d){var e=a(b);a.fn.lazyload=function(c){function i(){var b=0;f.each(function(){var c=a(this);if(h.skip_invisible&&!c.is(":visible"))return;if(!a.abovethetop(this,h)&&!a.leftofbegin(this,h))if(!a.belowthefold(this,h)&&!a.rightoffold(this,h))c.trigger("appear"),b=0;else if(++b>h.failure_limit)return!1})}var f=this,g,h={threshold:0,failure_limit:0,event:"scroll",effect:"show",container:b,data_attribute:"original",skip_invisible:!0,appear:null,load:null};return c&&(d!==c.failurelimit&&(c.failure_limit=c.failurelimit,delete c.failurelimit),d!==c.effectspeed&&(c.effect_speed=c.effectspeed,delete c.effectspeed),a.extend(h,c)),g=h.container===d||h.container===b?e:a(h.container),0===h.event.indexOf("scroll")&&g.bind(h.event,function(a){return i()}),this.each(function(){var b=this,c=a(b);b.loaded=!1,c.one("appear",function(){if(!this.loaded){if(h.appear){var d=f.length;h.appear.call(b,d,h)}a("<img />").bind("load",function(){c.hide().attr("src",c.data(h.data_attribute))[h.effect](h.effect_speed),b.loaded=!0;var d=a.grep(f,function(a){return!a.loaded});f=a(d);if(h.load){var e=f.length;h.load.call(b,e,h)}}).attr("src",c.data(h.data_attribute))}}),0!==h.event.indexOf("scroll")&&c.bind(h.event,function(a){b.loaded||c.trigger("appear")})}),e.bind("resize",function(a){i()}),/iphone|ipod|ipad.*os 5/gi.test(navigator.appVersion)&&e.bind("pageshow",function(b){b.originalEvent.persisted&&f.each(function(){a(this).trigger("appear")})}),a(b).load(function(){i()}),this},a.belowthefold=function(c,f){var g;return f.container===d||f.container===b?g=e.height()+e.scrollTop():g=a(f.container).offset().top+a(f.container).height(),g<=a(c).offset().top-f.threshold},a.rightoffold=function(c,f){var g;return f.container===d||f.container===b?g=e.width()+e.scrollLeft():g=a(f.container).offset().left+a(f.container).width(),g<=a(c).offset().left-f.threshold},a.abovethetop=function(c,f){var g;return f.container===d||f.container===b?g=e.scrollTop():g=a(f.container).offset().top,g>=a(c).offset().top+f.threshold+a(c).height()},a.leftofbegin=function(c,f){var g;return f.container===d||f.container===b?g=e.scrollLeft():g=a(f.container).offset().left,g>=a(c).offset().left+f.threshold+a(c).width()},a.inviewport=function(b,c){return!a.rightoffold(b,c)&&!a.leftofbegin(b,c)&&!a.belowthefold(b,c)&&!a.abovethetop(b,c)},a.extend(a.expr[":"],{"below-the-fold":function(b){return a.belowthefold(b,{threshold:0})},"above-the-top":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-screen":function(b){return a.rightoffold(b,{threshold:0})},"left-of-screen":function(b){return!a.rightoffold(b,{threshold:0})},"in-viewport":function(b){return a.inviewport(b,{threshold:0})},"above-the-fold":function(b){return!a.belowthefold(b,{threshold:0})},"right-of-fold":function(b){return a.rightoffold(b,{threshold:0})},"left-of-fold":function(b){return!a.rightoffold(b,{threshold:0})}})})(jQuery,window,document)