2020-11-27 23:02:59 +01:00
|
|
|
// https://forum.vuejs.org/t/how-to-use-helper-functions-for-imported-modules-in-vuejs-vue-template/6266/5
|
|
|
|
export default {
|
|
|
|
directives: {
|
|
|
|
lazy: {
|
2021-07-12 12:50:40 +02:00
|
|
|
bind (figure, binding) {
|
2021-09-21 11:05:29 +02:00
|
|
|
// console.log('directive lazy bind', figure, binding)
|
2020-12-23 19:11:46 +01:00
|
|
|
// show only the first image
|
2021-03-31 18:42:05 +02:00
|
|
|
if (binding.value === 0) {
|
2021-07-12 12:50:40 +02:00
|
|
|
const img = figure.querySelector('img:not(.blank)')
|
|
|
|
figure.classList.add('loading')
|
|
|
|
img.addEventListener('load', function (e) {
|
2021-09-02 15:09:35 +02:00
|
|
|
// console.log('img loaded', e)
|
2021-07-12 12:50:40 +02:00
|
|
|
figure.classList.remove('loading')
|
|
|
|
figure.classList.add('loaded')
|
|
|
|
})
|
|
|
|
img.addEventListener('error', function (e) {
|
|
|
|
console.error('img ERROR', e)
|
|
|
|
e.target.classList.remove('loading')
|
|
|
|
e.target.classList.add('error')
|
|
|
|
})
|
2020-11-27 23:02:59 +01:00
|
|
|
img.setAttribute('src', img.getAttribute('data-src'))
|
2021-07-12 12:50:40 +02:00
|
|
|
// img.removeAttribute('data-src')
|
|
|
|
// img.classList.remove('lazy')
|
2020-11-27 23:02:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
switcher: {
|
2021-03-31 18:42:05 +02:00
|
|
|
inserted (el, binding) {
|
2020-11-27 23:02:59 +01:00
|
|
|
// switch images on mousemove
|
2021-03-31 18:42:05 +02:00
|
|
|
el.addEventListener('mousemove', function (event) {
|
2021-07-12 12:50:40 +02:00
|
|
|
const figs = this.querySelectorAll('figure.loaded')
|
2021-08-05 11:13:22 +02:00
|
|
|
// console.log('mousemove', figs.length, this, event)
|
2020-11-27 23:02:59 +01:00
|
|
|
// let len = figs.length
|
|
|
|
// let w = this.clientWidth;
|
|
|
|
// let g = w / len;
|
|
|
|
// let delta = Math.floor(event.layerX / g)
|
2021-08-05 11:13:22 +02:00
|
|
|
// console.log(event.offsetX, this.clientWidth, figs.length)
|
|
|
|
// console.log(event.offsetX / (this.clientWidth / figs.length))
|
|
|
|
let delta = Math.floor(event.offsetX / (this.clientWidth / figs.length))
|
2021-08-05 10:59:11 +02:00
|
|
|
delta = delta < 0 ? 0 : delta >= figs.length ? figs.length - 1 : delta
|
2021-08-05 11:13:22 +02:00
|
|
|
// console.log('delta', delta)
|
2020-11-27 23:02:59 +01:00
|
|
|
figs.forEach((fig, index) => {
|
2021-08-05 11:13:22 +02:00
|
|
|
// console.log(index)
|
2021-03-31 18:42:05 +02:00
|
|
|
if (index === delta) {
|
2020-12-01 23:02:35 +01:00
|
|
|
// fig.style.display = "block"
|
2021-03-31 18:42:05 +02:00
|
|
|
fig.classList.remove('hide')
|
|
|
|
fig.classList.add('show')
|
|
|
|
} else {
|
2020-12-01 23:02:35 +01:00
|
|
|
// fig.style.display = "none"
|
2021-03-31 18:42:05 +02:00
|
|
|
fig.classList.remove('show')
|
|
|
|
fig.classList.add('hide')
|
2020-11-27 23:02:59 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-08-04 18:26:50 +02:00
|
|
|
mounted () {
|
|
|
|
// lazy load images on mouseover
|
2021-09-02 15:09:35 +02:00
|
|
|
// console.log('card mounted', this.$options.name)
|
2021-08-04 18:26:50 +02:00
|
|
|
// if (this.$options.name ==! 'ModalCard') {
|
|
|
|
this.activateLazyLoad()
|
|
|
|
// }
|
|
|
|
},
|
2021-08-04 13:50:00 +02:00
|
|
|
updated () {
|
2020-11-27 23:02:59 +01:00
|
|
|
// lazy load images on mouseover
|
2021-09-02 15:09:35 +02:00
|
|
|
// console.log('card updated', this.$options.name)
|
2020-12-23 19:11:46 +01:00
|
|
|
// if (this.$options.name ==! 'ModalCard') {
|
2021-08-04 18:26:50 +02:00
|
|
|
this.activateLazyLoad()
|
|
|
|
// }
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
// used by ModalCard
|
|
|
|
activateLazyLoad () {
|
2021-09-02 15:09:35 +02:00
|
|
|
// console.log('card activateLazyLoad', this.$options.name)
|
2021-07-12 12:50:40 +02:00
|
|
|
|
2021-08-04 18:26:50 +02:00
|
|
|
this.$el.addEventListener('mouseover', function (event) {
|
|
|
|
const figures = this.querySelectorAll('.images figure.lazy:not(.loaded):not(.loading)')
|
2021-08-05 00:18:26 +02:00
|
|
|
console.log('mouseover', this, figures)
|
2021-08-04 18:26:50 +02:00
|
|
|
figures.forEach((figure, index) => {
|
|
|
|
const img = figure.querySelector('img:not(.blank)')
|
|
|
|
// console.log('forEach img',img)
|
|
|
|
img.classList.add('loading')
|
|
|
|
img.addEventListener('load', function (e) {
|
|
|
|
// console.log('img loaded', e)
|
|
|
|
figure.classList.remove('loading')
|
|
|
|
figure.classList.add('loaded')
|
|
|
|
})
|
|
|
|
img.addEventListener('error', function (e) {
|
|
|
|
console.error('img ERROR', figure, e)
|
|
|
|
})
|
|
|
|
const src = img.getAttribute('data-src')
|
2021-07-12 12:50:40 +02:00
|
|
|
|
2021-08-04 18:26:50 +02:00
|
|
|
// for debug purpose
|
|
|
|
console.log(figures.length, index)
|
|
|
|
// let src = img.getAttribute('data-src')
|
|
|
|
// if (index > 0 && index < figures.length - 1) {
|
|
|
|
// src = img.getAttribute('data-src').replace('?', '/bad?')
|
|
|
|
// }
|
2021-07-12 12:50:40 +02:00
|
|
|
|
2021-08-04 18:26:50 +02:00
|
|
|
img.setAttribute('src', src)
|
|
|
|
// img.removeAttribute('data-src')
|
|
|
|
// img.classList.remove('lazy')
|
|
|
|
})
|
|
|
|
}, { once: true })
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 23:02:59 +01:00
|
|
|
}
|