forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg.ts
More file actions
154 lines (126 loc) · 3.51 KB
/
img.ts
File metadata and controls
154 lines (126 loc) · 3.51 KB
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
import { Component, Input, HostBinding, ElementRef, ChangeDetectionStrategy, ViewEncapsulation, NgZone } from '@angular/core';
import { nativeRaf } from '../../util/dom';
import { isPresent } from '../../util/util';
import { Platform } from '../../platform/platform';
/**
* @private
*/
@Component({
selector: 'ion-img',
template:
'<div class="img-placeholder" [style.height]="_h" [style.width]="_w"></div>',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class Img {
_src: string = '';
_normalizeSrc: string = '';
_imgs: HTMLImageElement[] = [];
_w: string;
_h: string;
_enabled: boolean = true;
_init: boolean;
constructor(private _elementRef: ElementRef, private _platform: Platform, private _zone: NgZone) {}
@Input()
set src(val: string) {
let tmpImg = new Image();
tmpImg.src = isPresent(val) ? val : '';
this._src = isPresent(val) ? val : '';
this._normalizeSrc = tmpImg.src;
if (this._init) {
this._update();
}
}
ngOnInit() {
this._init = true;
this._update();
}
_update() {
if (this._enabled && this._src !== '') {
// actively update the image
for (var i = this._imgs.length - 1; i >= 0; i--) {
if (this._imgs[i].src === this._normalizeSrc) {
// this is the active image
if (this._imgs[i].complete) {
this._loaded(true);
}
} else {
// no longer the active image
if (this._imgs[i].parentElement) {
this._imgs[i].parentElement.removeChild(this._imgs[i]);
}
this._imgs.splice(i, 1);
}
}
if (!this._imgs.length) {
this._zone.runOutsideAngular(() => {
let img = new Image();
img.style.width = this._width;
img.style.height = this._height;
if (isPresent(this.alt)) {
img.alt = this.alt;
}
if (isPresent(this.title)) {
img.title = this.title;
}
img.addEventListener('load', () => {
if (img.src === this._normalizeSrc) {
this._elementRef.nativeElement.appendChild(img);
nativeRaf(() => {
this._update();
});
}
});
img.src = this._src;
this._imgs.push(img);
this._loaded(false);
});
}
} else {
// do not actively update the image
if (!this._imgs.some(img => img.src === this._normalizeSrc)) {
this._loaded(false);
}
}
}
_loaded(isLoaded: boolean) {
this._elementRef.nativeElement.classList[isLoaded ? 'add' : 'remove']('img-loaded');
}
enable(shouldEnable: boolean) {
this._enabled = shouldEnable;
this._update();
}
@Input()
set width(val: string | number) {
this._w = getUnitValue(val);
}
@Input()
set height(val: string | number) {
this._h = getUnitValue(val);
}
@Input() alt: string;
@Input() title: string;
@HostBinding('style.width')
get _width(): string {
return isPresent(this._w) ? this._w : '';
}
@HostBinding('style.height')
get _height(): string {
return isPresent(this._h) ? this._h : '';
}
}
function getUnitValue(val: any): string {
if (isPresent(val)) {
if (typeof val === 'string') {
if (val.indexOf('%') > -1 || val.indexOf('px') > -1) {
return val;
}
if (val.length) {
return val + 'px';
}
} else if (typeof val === 'number') {
return val + 'px';
}
}
return '';
}