Skip to content

Move lazyIconScript to standard JavaScript #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions public/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,20 @@ ul.flaggedList {
border-radius: 0 3px 3px 0;
text-shadow: 0 1px #045481;
}


.translucent-icon {
opacity: 0;
}

.hide-icon {
display: none;
}

.opaque-icon {
opacity: 1;
}

.fade-icon {
transition: opacity 0.33s ease-in-out;
}
79 changes: 58 additions & 21 deletions views/includes/scripts/lazyIconScript.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
<script type="text/javascript">
(function () {
'use strict';

$(window).load(function () {
window.setTimeout(function () {
$('[data-icon-src]').each(function () {
var container = $(this);
var size = container.data('icon-size');
var img = new Image();

if (size) {
img.width = size;
img.height = size;
}

$(img).load(function () {
var that = $(this).hide();

container.empty().append(that);
that.fadeIn();
}).attr('src', container.data('icon-src'));
});
}, 13);
});
function onShow(aImg) {
aImg.classList.remove('translucent-icon');
aImg.classList.add('opaque-icon');
}

function onError(aEv) {
var img = aEv.target;
var container = img.parentNode;

container.removeChild(img);
}

function onLoad(aEv) {
var img = aEv.target;
var container = img.parentNode;

var node = container.querySelector('i');

container.removeChild(node);
img.classList.remove('hide-icon');

setTimeout(onShow, 250, img);
}

function onTimeout() {
var nodes = document.querySelectorAll('[data-icon-src]');
var i = null;
var container = null;
var dataIconSrc = null;
var node = null;
var img = null;

for (i = 0; container = nodes[i]; i++) {
dataIconSrc = container.getAttribute('data-icon-src');

node = container.querySelector('i');

img = new Image();

img.addEventListener('load', onLoad);
img.addEventListener('error', onError);

img.classList.add('hide-icon');
img.classList.add('fade-icon');
img.classList.add('translucent-icon');

img.src = dataIconSrc;

container.appendChild(img);
}
}

function onDOMContentLoaded(aEv) {
window.setTimeout(onTimeout, 13);
}

document.addEventListener('DOMContentLoaded', onDOMContentLoaded);

})();
</script>