-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbox.js
313 lines (254 loc) · 9.44 KB
/
lightbox.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
(function(){
'use strict';
var LightboxBluePrint = function LightboxBluePrint() {
var lightbox = this;
this.active = false;
this.settings = {
mask: 'lightbox__mask',
maskActive: 'lightbox__mask--active',
main: 'lightbox__main',
file: 'lightbox__file',
fileVisible: 'lightbox__file--visible',
center: 'lightbox__center-image',
title: 'lightbox__title',
titlePadding: 'lightbox__title__padding',
exit: 'lightbox__exit',
prev: 'lightbox__prev',
next: 'lightbox__next',
spinner: 'lightbox__spinner',
spinnerBounce1: 'lightbox__spinner__double-bounce1',
spinnerBounce2: 'lightbox__spinner__double-bounce2',
control: 'lightbox__control',
iconClose: 'icon-close',
iconArrowLeft: 'icon-arrow-left',
iconArrowRight: 'icon-arrow-right',
},
this.util = {
findById: function findById(array, idAny, prevOrNextNum) {
prevOrNextNum = prevOrNextNum || 0;
// Loop though entire cache and find object with specified id
for ( var i = 0, len = array.length; i < len; i++ ) {
if ( array[i].id == idAny ) {
// Allow for grabing next or previous object insted.
return array[ i + prevOrNextNum ];
}
}
},
DOM: function DOM(elementStr, attributesObj, contentNodeOrStr) {
var contentNodeOrStr = Array.prototype.slice.call(arguments);
var elementStr = contentNodeOrStr.shift();
var attributesObj = contentNodeOrStr.shift();
var elementNode = document.createElement( elementStr );
for ( var attr in attributesObj ) {
elementNode[attr] = attributesObj[attr];
}
// Append contents
for ( var i = 0, len = contentNodeOrStr.length; i < len; i++ ) {
var content = contentNodeOrStr[i];
content = typeof content === 'string' ? document.createTextNode(content) :
typeof content === 'object' ? content : null;
if ( content ) {
elementNode.appendChild(content);
}
}
return elementNode;
}
},
this.install = function install() {
if ( document.getElementsByTagName(this.settings.mask).length ) {
return;
}
var body = document.getElementsByTagName('body')[0];
var lightbox = this.util.DOM('div', { id: this.settings.mask },
this.util.DOM('div', { id: this.settings.main }),
this.util.DOM('div', { id: this.settings.exit, className: this.settings.control },
this.util.DOM('span', { className: this.settings.iconClose })
),
this.util.DOM('div', { id: this.settings.prev, className: this.settings.control },
this.util.DOM('span', { className: this.settings.iconArrowLeft })
),
this.util.DOM('div', { id: this.settings.next, className: this.settings.control },
this.util.DOM('span', { className: this.settings.iconArrowRight })
)
);
body.appendChild(lightbox);
this.addEventListeners();
},
this.addEventListeners = function addEventListeners() {
// Thumbnail click to open lightbox
document.getElementById(this.settings.thumbnails)
.addEventListener('click', function lightboxClickThumbnails(event) {
if ( event.target && event.target.nodeName == 'IMG' ) {
lightbox.open(event.target.id);
}
});
// Close button
document.getElementById(this.settings.exit)
.addEventListener('click', function lightboxClickExit() {
lightbox.close();
});
// Previous button
document.getElementById(this.settings.prev)
.addEventListener('click', function lightboxClickPrev() {
lightbox.prev();
});
// Next button
document.getElementById(this.settings.next)
.addEventListener('click', function lightboxClickNext() {
lightbox.next();
});
// Keyboard support
document.onkeydown = function lightboxKeydown(event) {
event = event || window.event;
var code = event.keyCode || event.charCode;
switch(code) {
// Esc key.
case 27:
lightbox.close();
break;
// Left and Up arrows.
case 37: case 38:
lightbox.prev();
break;
// Right and Down arrows.
case 39: case 40:
lightbox.next();
break;
}
// Prevent default behaviour only if user hit a shortcut.
if ( code === 27 || (code >= 37 && code <= 40) ) {
event.preventDefault();
}
};
// Window resize
window.addEventListener('resize', function lightboxResize() {
if ( lightbox.active ) {
lightbox.positionNavigation();
}
});
},
this.open = function open(idAny) {
var mask = document.getElementById(this.settings.mask);
var startingPhoto = idAny ? this.util.findById(this.settings.imageCache, idAny) : this.settings.imageCache[0];
this.active = true;
this.displayImage(startingPhoto);
mask.style.display = 'block';
setTimeout(function(){
mask.className = lightbox.settings.maskActive;
}, 100);
},
this.close = function close() {
var mask = document.getElementById( this.settings.mask );
var regexp = new RegExp('\s*' + this.settings.maskActive, 'gi');
this.active = false;
mask.className = mask.className.replace(regexp, '');
setTimeout(function(){
mask.style.display = 'none';
}, 500);
},
this.prev = function prev() {
// Get prev image, if this is the first image, loop back to last.
var imageId = document.getElementsByClassName(this.settings.file)[0].getElementsByTagName('IMG')[0].id;
var image = this.util.findById(this.settings.imageCache, imageId, -1) || this.settings.imageCache[ this.settings.imageCache.length -1 ];
this.displayImage(image);
},
this.next = function next() {
// Get next image, if this is the last image, loop back to first.
var imageId = document.getElementsByClassName(this.settings.file)[0].getElementsByTagName('IMG')[0].id;
var image = this.util.findById(this.settings.imageCache, imageId, 1) || this.settings.imageCache[0];
this.displayImage(image);
},
this.displayImage = function displayImage(imageObj) {
var mainContainer = document.getElementById(this.settings.main);
var image = this.image(imageObj);
var wrapper = this.util.DOM('div', { className: this.settings.file },
this.util.DOM('div', { className: this.settings.center }),
image
);
this.loading.show();
image.onload = function imageLoad() {
lightbox.loading.hide();
// Re-position controlls.
lightbox.positionNavigation();
// Make image visible.
wrapper.className += ' ' + lightbox.settings.fileVisible;
}
if ( imageObj.title ) {
var title = this.util.DOM('div', { className: this.settings.title },
this.util.DOM('div', { className: this.settings.titlePadding }, imageObj.title)
);
wrapper.appendChild( title );
}
mainContainer.innerHTML = '';
mainContainer.appendChild( wrapper );
},
this.image = function image(photoObj, sizeStr) {
return this.util.DOM('img', {
id: photoObj.id,
src: this.settings.imageUrl(photoObj, sizeStr),
alt: photoObj.title
});
},
this.loading = {
show: function loadingShow() {
var mask = document.getElementById(lightbox.settings.mask);
var spinner = lightbox.util.DOM('div', { id: lightbox.settings.spinner, className: lightbox.settings.control },
lightbox.util.DOM('div', { className: lightbox.settings.spinnerBounce1 }),
lightbox.util.DOM('div', { className: lightbox.settings.spinnerBounce2 })
);
mask.appendChild(spinner);
},
hide: function loadingShow() {
var spinner = document.getElementById(lightbox.settings.spinner);
if (spinner) {
spinner.parentNode.removeChild(spinner);
}
}
},
this.positionNavigation = function positionNavigation() {
var prevControl = document.getElementById(lightbox.settings.prev);
var nextControl = document.getElementById(lightbox.settings.next);
if ( lightbox.settings.imageCache.length > 1 ) {
prevControl.style.display = nextControl.style.display = 'block';
}
var controlSize = (document.getElementById(lightbox.settings.next)).offsetWidth;
var maskWidth = window.innerWidth;
var maskHeight = window.innerHeight;
var file = (document.getElementsByClassName(lightbox.settings.file)[0]).getElementsByTagName('IMG')[0];
var fileWidth = file.offsetWidth;
var fileHeight = file.offsetHeight;
var prevPos = ((maskWidth - fileWidth) / 2) - controlSize;
var nextPos = ((maskWidth + fileWidth) / 2) + controlSize;
var title = document.getElementsByClassName(lightbox.settings.title)[0];
// Adjust prev/next icons positions.
prevControl.style.left = (prevPos > controlSize ? prevPos : controlSize) + 'px';
nextControl.style.left = (nextPos < maskWidth ? nextPos : maskWidth - controlSize) + 'px';
// Adjust image title
if ( title ) {
title.style.top = (maskHeight - fileHeight)/2 + (fileHeight - title.clientHeight) + 'px';
title.style.left = title.style.right = file.offsetLeft + 'px';
}
}
}
function Lightbox(optionsObj) {
optionsObj = optionsObj || {};
for ( var opt in optionsObj ) {
if ( optionsObj.hasOwnProperty(opt) ) {
this.settings[opt] = optionsObj[opt];
}
}
if ( typeof this.settings.imageCache === 'object' && this.settings.imageCache.length ) {
this.settings.imageCache = this.settings.imageCache;
}
if ( typeof this.settings.thumbnails === 'string' ) {
var photosContainer = document.getElementById(this.settings.thumbnails);
for ( var i = 0, len = this.settings.imageCache.length; i < len; i++ ) {
var photo = this.image(this.settings.imageCache[i], 'thumbnail');
photosContainer.appendChild(photo);
}
}
this.install();
}
Lightbox.prototype = new LightboxBluePrint();
window.Lightbox = Lightbox;
})();