/
image.ts
225 lines (186 loc) · 5.45 KB
/
image.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* ___ ___ __
* \ \/ / / _
* \ / /_| |_
* / \____ _|
* /__/\__\ |_|
*
* @file image.ts
* @author Etienne Cochard
*
* Copyright (c) 2019-2022 R-libre ingenierie
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
import { x4document } from './x4dom'
import { Component, CProps, html } from './component'
interface LazyLoad {
el: Component;
src: string;
}
// ============================================================================
// [IMAGE]
// ============================================================================
export interface ImageProps extends CProps
{
src: string;
alt?: string;
lazy?: boolean; // mark image as lazy loading
alignment?: 'fill' | 'contain' | 'cover' | 'scale-down' | 'none';
overlays?: Component[];
}
const emptyImageSrc = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
function _isStaticImage( src: string ) {
return src.substring(0,5)=='data:';
}
/**
* Standard image class
*/
export class Image extends Component<ImageProps>
{
protected m_created: boolean;
protected m_lazysrc: string; // expected
private m_img: Component;
constructor(props: ImageProps) {
super(props);
this.m_created = false;
this.m_props.lazy = props.lazy===false ? false : true;
this.m_props.alt = props.alt;
if( props.lazy!==false ) {
this.m_lazysrc = props.src;
props.src = emptyImageSrc;
}
this.setDomEvent( 'create', ( ) => {
if( props.lazy ) {
this.setImage( this.m_lazysrc, true );
}
});
}
/** @ignore */
render( ) {
let mp = this.m_props;
this.m_img = new Component( {
tag: 'img',
attrs: {
draggable: false,
alt: mp.alt ?? '',
decoding: mp.lazy ? 'async' : undefined,
},
style: {
objectFit: mp.alignment ? mp.alignment : undefined
}
});
if( mp.overlays ) {
mp.overlays.forEach( x => x.addClass('@fit') );
this.setContent( [this.m_img,...mp.overlays] );
}
else {
this.setContent( this.m_img );
}
}
/**
* change the image
* @param src - image path
*/
public setImage( src: string, force?: boolean ) {
if( !src ) {
src = emptyImageSrc;
this.addClass( 'empty' );
}
else {
this.removeClass( 'empty' );
}
if( !this.m_props.lazy ) {
this.m_props.src = src;
this.m_lazysrc = src;
if( this.m_img.dom ) {
this.m_img.dom.setAttribute( 'src', src );
}
}
else if( force || this.m_lazysrc!=src ) {
if( _isStaticImage(src) ) {
// not to download -> direct display
this.m_props.src = src;
this.m_lazysrc = src;
if( this.m_img.dom ) {
this.m_img.dom.setAttribute( 'src', this.m_props.src );
}
}
else {
// clear current image while waiting
this.m_props.src = emptyImageSrc;
if( this.m_img.dom ) {
this.m_img.dom.setAttribute( 'src', this.m_props.src );
}
this.m_lazysrc = src;
if( this.dom ) {
this._update_image( );
}
}
}
}
private _update_image( ) {
console.assert( !!this.dom );
if( this.m_lazysrc && !_isStaticImage(this.m_lazysrc) ) {
// we do not push Components in a static array...
Image.lazy_images_waiting.push( {
el: this,
src: this.m_lazysrc
} );
if( Image.lazy_image_timer===undefined ) {
Image.lazy_image_timer = setInterval( Image.lazyWatch as TimerHandler, 10 );
}
}
}
private static lazy_images_waiting: LazyLoad[] = [];
private static lazy_image_timer: number = undefined;
private static lazyWatch( ) {
let newList = [];
let done = 0;
Image.lazy_images_waiting.forEach( ( lazy ) => {
let dom = lazy.el.dom,
src = lazy.src;
// skip deleted elements
if( !dom || dom.offsetParent === null ) {
// do not append to newList
return;
}
let rc = dom.getBoundingClientRect();
// if it is visible & inserted inside the document
if( !done && dom.offsetParent!==null &&
rc.bottom >= 0 && rc.right >= 0 &&
rc.top <= (window.innerHeight || x4document.documentElement.clientHeight) &&
rc.left <= (window.innerWidth || x4document.documentElement.clientWidth) ) {
// ok, we load the image
let img = <HTMLElement>dom.firstChild;
img.setAttribute( 'src', src );
lazy.el.removeClass( 'empty' );
done++;
}
else {
// still not visible: may be next time
newList.push( lazy );
}
} );
Image.lazy_images_waiting = newList;
// no more elements to watch...
if( newList.length==0 ) {
clearInterval( Image.lazy_image_timer );
Image.lazy_image_timer = undefined;
}
}
}