-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (89 loc) · 3.18 KB
/
index.js
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
function ImageLoader(grid, imagePath, width, height, swatchColor, onclick, callback) {
var self = this;
function _randomId() {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4());
}
self.img = new Image();
self.grid = grid;
self.callback = callback;
self.id = _randomId();
self.image = {
path: imagePath,
width: width,
height: height,
swatch: swatchColor,
click: onclick
};
self.display = {
width: self.image.width,
height: self.image.height
};
/**
* Update image class when image is loaded
*/
self.onImageLoad = function () {
document.getElementById("img-" + self.id).setAttribute('src', self.image.path);
self.holder.setAttribute('class', 'imageholder imageholder--loaded');
self.imageElement.setAttribute('class', 'image image--loaded');
};
/**
* Calculate sizes to fit in grid
*/
self.resize = function () {
var gridWidth = self.grid.offsetWidth;
var diff = 0;
if (self.image.width > self.image.height) {
diff = gridWidth / self.image.width;
self.display.width = Math.ceil(self.image.width * diff);
self.display.height = Math.ceil(self.image.height * diff);
} else {
diff = gridWidth / self.image.width;
self.display.width = Math.ceil(gridWidth);
self.display.height = Math.ceil(self.image.height * diff);
}
};
/**
* Set everything up
*/
self.start = function () {
self.grid = document.getElementById(self.grid);
self.resize();
window.addEventListener("resize", self.windowResize);
self.holder = document.createElement('div');
self.holder.setAttribute('class', 'imageholder imageholder--notloaded');
self.imageElement = document.createElement('img');
self.imageElement.setAttribute('data-src', self.image.path);
self.imageElement.setAttribute('class', 'image image--notloaded');
self.imageElement.setAttribute('style', 'height:' + self.display.height + 'px;width:' + self.display.width + 'px;background:' + self.image.swatch + ';');
self.imageElement.setAttribute('id', "img-" + self.id);
if (self.image.click) {
self.imageElement.onclick = self.imageClick;
}
self.holder.appendChild(self.imageElement);
self.grid.appendChild(self.holder);
self.img.onload = self.onImageLoad;
self.img.src = self.image.path;
if (self.callback) {
callback(self);
}
};
/**
* Calls image click method with
* ImageObject, ImageID, Event
* @param event
*/
self.imageClick = function (event) {
self.image.click(self.image, 'img-' + self.id, event);
};
/**
* On window resize event
*/
self.windowResize = function () {
self.resize();
self.imageElement.setAttribute('style', 'height:' + self.display.height + 'px;width:' + self.display.width + 'px;background:' + self.image.swatch + ';');
};
self.start();
}