Skip to content

Commit

Permalink
[mms] Implement lazy loading for images in HTML messages in dynamic v…
Browse files Browse the repository at this point in the history
…iew.
  • Loading branch information
slusarz committed Aug 25, 2014
1 parent ee59c74 commit fa6a439
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions imp/docs/CHANGES
Expand Up @@ -2,6 +2,7 @@
v6.3.0-git
----------

[mms] Implement lazy loading for images in HTML messages in dynamic view.
[mms] Add 'dynamic_prefs' hook to allow for changing of default values for
browser-based preferences in dynamic view.
[mms] Removed compose 'use_vfs' configuration option.
Expand Down
47 changes: 43 additions & 4 deletions imp/js/imp.js
Expand Up @@ -47,15 +47,15 @@ var IMP_JS = {
}

callback = this.iframeResize.bindAsEventListener(this, iframe);
doc = iframe.contentDocument || iframe.contentWindow.document;
doc = this.iframeDoc(iframe);

Prototype.Selector.select('[htmlimgblocked]', doc).each(function(img) {
var src = img.getAttribute('htmlimgblocked');
img.removeAttribute('htmlimgblocked');

if (img.getAttribute('src')) {
img.onload = callback;
img.setAttribute('src', src);
img.setAttribute('data-src', src);
} else {
if (img.getAttribute('background')) {
img.setAttribute('background', src);
Expand Down Expand Up @@ -94,7 +94,7 @@ var IMP_JS = {
return;
}

var d = id.contentDocument || id.contentWindow.document, ev;
var d = this.iframeDoc(id), ev;

id.onload = function(e) {
this.iframeResize(e, id);
Expand Down Expand Up @@ -132,7 +132,7 @@ var IMP_JS = {

id = $(id);
if (id) {
body = (id.contentDocument || id.contentWindow.document).body;
body = this.iframeDoc(id).body;
html = body.parentNode;

Element.setStyle(body, { height: null });
Expand All @@ -150,9 +150,35 @@ var IMP_JS = {
}

id.setStyle({ height: h + 'px' });

this.iframeImgLazyLoad(id);
}
},

iframeImgLazyLoad: function(iframe)
{
var range_top, range_bottom,
mb = $('messageBody'),
doc = this.iframeDoc(iframe);

range_top = mb.scrollTop + mb.cumulativeOffset().top - iframe.cumulativeOffset().top;
range_bottom = range_top + mb.getHeight();

Prototype.Selector.select('IMG[data-src]', doc).each(function(img) {
var co = Element.cumulativeOffset(img);
if (co.top > range_top && co.top < range_bottom) {
src = Element.readAttribute(img, 'data-src');
Element.writeAttribute(img, 'src', Element.readAttribute(img, 'data-src'));
Element.writeAttribute(img, 'data-src', null);
}
});
},

iframeDoc: function(i)
{
return i.contentDocument || i.contentWindow.document;
},

printWindow: function(win)
{
win.print();
Expand Down Expand Up @@ -191,6 +217,19 @@ var IMP_JS = {
}

return hash >>> 0;
},

onDomLoad: function()
{
var mb = $('messageBody');

if (mb) {
mb.observe('scroll', function() {
$('messageBody').select('IFRAME.htmlMsgData').each(this.iframeImgLazyLoad.bind(this));
}.bind(this));
}
}

};

document.observe('dom:loaded', IMP_JS.onDomLoad.bind(IMP_JS));
4 changes: 3 additions & 1 deletion imp/lib/Mime/Viewer/Html.php
Expand Up @@ -371,7 +371,6 @@ protected function _node($doc, $node)
'id' => $id,
'imp_img_view' => 'data'
)));
$node->setAttribute('src', $val);
}

/* Block images.*/
Expand All @@ -385,6 +384,9 @@ protected function _node($doc, $node)
$node->setAttribute(self::IMGBLOCK, $url);
$node->setAttribute('src', $this->_imgBlockImg());
$this->_imptmp['imgblock'] = true;
} else {
$node->removeAttribute('src');
$node->setAttribute('data-src', $val);
}
}

Expand Down
1 change: 1 addition & 0 deletions imp/package.xml
Expand Up @@ -33,6 +33,7 @@
</stability>
<license uri="http://www.horde.org/licenses/gpl">GPL-2.0</license>
<notes>
* [mms] Implement lazy loading for images in HTML messages in dynamic view.
* [mms] Add &apos;dynamic_prefs&apos; hook to allow for changing of default values for browser-based preferences in dynamic view.
* [mms] Removed compose &apos;use_vfs&apos; configuration option.
* [mjr] Add IMP_Api::addFlags().
Expand Down
2 changes: 1 addition & 1 deletion imp/templates/dynamic/message.html.php
Expand Up @@ -112,7 +112,7 @@
</div>
</div>
</div>
<div class="messageBody allowTextSelection">
<div id="messageBody" class="messageBody allowTextSelection">
<?php echo $this->msgtext ?>
</div>
</div>
Expand Down

0 comments on commit fa6a439

Please sign in to comment.