import _ from 'underscore'; import './lazyloadImage'; export const fixCordova = function(url) { if (url && url.indexOf('data:image') === 0) { return url; } if (Meteor.isCordova && (url && url[0] === '/')) { url = Meteor.absoluteUrl().replace(/\/$/, '') + url; const query = `rc_uid=${ Meteor.userId() }&rc_token=${ Meteor._localStorage.getItem( 'Meteor.loginToken' ) }`; if (url.indexOf('?') === -1) { url = `${ url }?${ query }`; } else { url = `${ url }&${ query }`; } } if (Meteor.settings['public'].sandstorm || url.match(/^(https?:)?\/\//i)) { return url; } else if (navigator.userAgent.indexOf('Electron') > -1) { return __meteor_runtime_config__.ROOT_URL_PATH_PREFIX + url; } else { return Meteor.absoluteUrl().replace(/\/$/, '') + url; } }; const loadImage = el => { const img = new Image(); const src = el.getAttribute('data-src'); el.className = el.className.replace('lazy-img', ''); img.onload = function() { el.src = src; el.removeAttribute('data-src'); }; img.src = fixCordova(src); }; const isVisible = el => { requestAnimationFrame(() => { const rect = el.getBoundingClientRect(); if (rect.top >= -100 && rect.left >= 0 && rect.top <= (window.innerHeight || document.documentElement.clientHeight)) { return loadImage(el); } }); }; window.addEventListener('resize', window.lazyloadtick); export const lazyloadtick = _.debounce(() => { [...document.querySelectorAll('.lazy-img[data-src]')] .forEach(isVisible); }, 500); window.lazyloadtick = lazyloadtick; export const addImage = el => isVisible(el);