import LazyLoad from 'vanilla-lazyload/dist/lazyload'

export default class LoadImagesClass {
	constructor () {
		this.classInit()

		document.addEventListener('swup:animation:out:end', this.classDestroy.bind(this))
		document.addEventListener('swup:content:replace', this.classInit.bind(this))
		$(document).on('clientColumnsAdded', this.classInit.bind(this))
	}

	classInit () {
		this.lazy = new LazyLoad({
			'elements_selector': '.lazy',
			callback_error: (img) => {
				img.setAttribute('src', 'placeholder.jpg');
			}
		})
	}

	classDestroy () {
		this.lazy.destroy()
	}
}
