2018-12-11 06:26:48 +00:00
|
|
|
<div class="lazy-image {fillFixSize ? 'lazy-image-fixed-size': ''}" style={computedStyle} >
|
2018-08-31 23:35:26 +00:00
|
|
|
<img
|
2018-12-08 17:44:12 +00:00
|
|
|
class="{fillFixSize ? 'fixed-size-img': ''}"
|
2018-08-31 23:35:26 +00:00
|
|
|
aria-hidden={ariaHidden}
|
|
|
|
{alt}
|
|
|
|
{title}
|
|
|
|
{width}
|
|
|
|
{height}
|
|
|
|
src={displaySrc}
|
2018-12-08 17:42:38 +00:00
|
|
|
style={focusStyle}
|
2018-08-31 23:35:26 +00:00
|
|
|
ref:node
|
|
|
|
/>
|
2018-03-14 06:07:30 +00:00
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
.lazy-image {
|
2018-12-08 17:42:38 +00:00
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
2018-03-14 06:07:30 +00:00
|
|
|
overflow: hidden;
|
2018-12-08 17:42:38 +00:00
|
|
|
display: flex;
|
2018-03-14 06:07:30 +00:00
|
|
|
}
|
2018-12-08 17:42:38 +00:00
|
|
|
|
2018-12-08 17:44:12 +00:00
|
|
|
.fixed-size-img {
|
2018-12-08 17:42:38 +00:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
2018-12-11 06:26:48 +00:00
|
|
|
.lazy-image-fixed-size {
|
2018-12-08 17:42:38 +00:00
|
|
|
position: absolute;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-14 06:07:30 +00:00
|
|
|
</style>
|
|
|
|
<script>
|
2018-08-23 04:00:53 +00:00
|
|
|
import { decodeImage } from '../_utils/decodeImage'
|
2018-12-08 17:44:12 +00:00
|
|
|
import { coordsToPercent } from '../_utils/coordsToPercent'
|
2018-03-14 14:24:16 +00:00
|
|
|
|
2018-12-08 17:44:12 +00:00
|
|
|
export default {
|
2018-08-23 04:00:53 +00:00
|
|
|
async oncreate () {
|
|
|
|
try {
|
2018-08-31 23:35:26 +00:00
|
|
|
await decodeImage(this.refs.node)
|
2018-08-23 04:00:53 +00:00
|
|
|
} catch (e) {
|
2018-08-31 23:35:26 +00:00
|
|
|
this.set({ error: true })
|
2018-03-14 14:24:16 +00:00
|
|
|
}
|
2018-04-30 05:13:41 +00:00
|
|
|
},
|
|
|
|
data: () => ({
|
2018-08-31 23:35:26 +00:00
|
|
|
error: false,
|
2018-12-08 17:42:38 +00:00
|
|
|
forceSize: false,
|
2018-08-23 04:00:53 +00:00
|
|
|
fallback: void 0,
|
2018-12-08 17:42:38 +00:00
|
|
|
focus: void 0,
|
2018-08-23 04:00:53 +00:00
|
|
|
background: '',
|
|
|
|
width: void 0,
|
|
|
|
height: void 0,
|
2018-12-05 06:31:46 +00:00
|
|
|
ariaHidden: false,
|
2018-08-23 04:00:53 +00:00
|
|
|
alt: '',
|
|
|
|
title: ''
|
|
|
|
}),
|
|
|
|
computed: {
|
2018-12-08 17:42:38 +00:00
|
|
|
computedStyle: ({ background }) => {
|
2018-08-23 04:00:53 +00:00
|
|
|
return [
|
|
|
|
background && `background: ${background};`
|
|
|
|
].filter(Boolean).join('')
|
2018-08-31 23:35:26 +00:00
|
|
|
},
|
2018-12-08 17:42:38 +00:00
|
|
|
focusStyle: ({ focus }) => {
|
|
|
|
// Here we do a pure css version instead of using
|
|
|
|
// https://github.com/jonom/jquery-focuspoint#1-calculate-your-images-focus-point
|
|
|
|
|
|
|
|
if (!focus) return 'background-position: center;'
|
|
|
|
return `object-position: ${coordsToPercent(focus.x)}% ${100 - coordsToPercent(focus.y)}%;`
|
|
|
|
},
|
2018-12-08 17:44:12 +00:00
|
|
|
fillFixSize: ({ forceSize, $largeInlineMedia }) => !$largeInlineMedia && !forceSize,
|
2018-08-31 23:35:26 +00:00
|
|
|
displaySrc: ({ error, src, fallback }) => ((error && fallback) || src)
|
2018-08-23 04:00:53 +00:00
|
|
|
}
|
2018-03-14 06:07:30 +00:00
|
|
|
}
|
2018-12-04 07:08:38 +00:00
|
|
|
</script>
|