-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample.imageloader.ts
80 lines (64 loc) · 1.82 KB
/
example.imageloader.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
import {
SimpleEventDispatcher,
ISimpleEvent,
ISimpleEventHandler
} from "strongly-typed-events";
declare var window: any;
declare var alert: any;
declare var Image: any;
window.onload = () => {
let downloader = new ImageDownloader();
downloader.ondownload.subscribe((arg: ImageDownloadArg) => {
var x = `Url: "${arg.url}", height: ${arg.height}, width: ${arg.width}`;
alert(x);
});
downloader.download(
"https://keestalkstech.com/wp-content/uploads/2016/05/pexels-photo-688991.jpeg",
(arg: ImageDownloadArg) => alert(`"${arg.url}"' has been downloaded!`)
);
downloader.download(
"https://keestalkstech.com/wp-content/uploads/2016/05/hashing2-590x332.jpg"
);
downloader.download(
"https://keestalkstech.com/wp-content/uploads/2016/05/pexels-photo-688991-590x393.jpeg"
);
};
class ImageDownloader {
private _ondownload = new SimpleEventDispatcher<ImageDownloadArg>();
public get ondownload(): ISimpleEvent<ImageDownloadArg> {
return this._ondownload.asEvent();
}
public download(
url: string,
callback?: ISimpleEventHandler<ImageDownloadArg>
) {
let img = new Image();
img.onload = () => {
let result = new ImageDownloadArg(url, img.height, img.width);
if (callback) {
callback(result, null);
}
this._ondownload.dispatch(result);
};
img.src = url;
}
}
class ImageDownloadArg {
private _url: string;
private _height: number;
private _width: number;
constructor(url: string, height: number, width: number) {
this._url = url;
this._height = height;
this._width = width;
}
public get url() {
return this._url;
}
public get height() {
return this._height;
}
public get width() {
return this._width;
}
}