forked from compact/angular-bootstrap-lightbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbox-service.js
449 lines (401 loc) · 14.2 KB
/
lightbox-service.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* @class Lightbox
* @classdesc Lightbox service.
* @memberOf bootstrapLightbox
*/
angular.module('bootstrapLightbox').provider('Lightbox', function () {
/**
* Template URL passed into `$modal.open()`.
* @type {String}
* @name templateUrl
* @memberOf bootstrapLightbox.Lightbox
*/
this.templateUrl = 'lightbox.html';
/**
* Whether images should be scaled to the maximum possible dimensions.
* @type {Boolean}
* @name fullScreenMode
* @memberOf bootstrapLightbox.Lightbox
*/
this.fullScreenMode = false;
/**
* @param {*} image An element in the array of images.
* @return {String} The URL of the given image.
* @type {Function}
* @name getImageUrl
* @memberOf bootstrapLightbox.Lightbox
*/
this.getImageUrl = function (image) {
return typeof image === 'string' ? image : image.url;
};
/**
* @param {*} image An element in the array of images.
* @return {String} The caption of the given image.
* @type {Function}
* @name getImageCaption
* @memberOf bootstrapLightbox.Lightbox
*/
this.getImageCaption = function (image) {
return image.caption;
};
/**
* Calculate the max and min limits to the width and height of the displayed
* image (all are optional). The max dimensions override the min
* dimensions if they conflict.
* @param {Object} dimensions Contains the properties `windowWidth`,
* `windowHeight`, `imageWidth`, and `imageHeight`.
* @return {Object} May optionally contain the properties `minWidth`,
* `minHeight`, `maxWidth`, and `maxHeight`.
* @type {Function}
* @name calculateImageDimensionLimits
* @memberOf bootstrapLightbox.Lightbox
*/
this.calculateImageDimensionLimits = function (dimensions) {
if (dimensions.windowWidth >= 768) {
return {
// 92px = 2 * (30px margin of .modal-dialog
// + 1px border of .modal-content
// + 15px padding of .modal-body)
// with the goal of 30px side margins; however, the actual side margins
// will be slightly less (at 22.5px) due to the vertical scrollbar
'maxWidth': dimensions.windowWidth - 92,
// 126px = 92px as above
// + 34px outer height of .lightbox-nav
'maxHeight': dimensions.windowHeight - 126
};
} else {
return {
// 52px = 2 * (10px margin of .modal-dialog
// + 1px border of .modal-content
// + 15px padding of .modal-body)
'maxWidth': dimensions.windowWidth - 52,
// 86px = 52px as above
// + 34px outer height of .lightbox-nav
'maxHeight': dimensions.windowHeight - 86
};
}
};
/**
* Calculate the width and height of the modal. This method gets called
* after the width and height of the image, as displayed inside the modal,
* are calculated.
* @param {Object} dimensions Contains the properties `windowWidth`,
* `windowHeight`, `imageDisplayWidth`, and `imageDisplayHeight`.
* @return {Object} Must contain the properties `width` and `height`.
* @type {Function}
* @name calculateModalDimensions
* @memberOf bootstrapLightbox.Lightbox
*/
this.calculateModalDimensions = function (dimensions) {
// 400px = arbitrary min width
// 32px = 2 * (1px border of .modal-content
// + 15px padding of .modal-body)
var width = Math.max(400, dimensions.imageDisplayWidth + 32);
// 200px = arbitrary min height
// 66px = 32px as above
// + 34px outer height of .lightbox-nav
var height = Math.max(200, dimensions.imageDisplayHeight + 66);
// first case: the modal width cannot be larger than the window width
// 20px = arbitrary value larger than the vertical scrollbar
// width in order to avoid having a horizontal scrollbar
// second case: Bootstrap modals are not centered below 768px
if (width >= dimensions.windowWidth - 20 || dimensions.windowWidth < 768) {
width = 'auto';
}
// the modal height cannot be larger than the window height
if (height >= dimensions.windowHeight) {
height = 'auto';
}
return {
'width': width,
'height': height
};
};
/**
* @param {*} image An element in the array of images.
* @return {Boolean} Whether the provided element is a video.
* @type {Function}
* @name isVideo
* @memberOf bootstrapLightbox.Lightbox
*/
this.isVideo = function (image) {
if (typeof image === 'object' && image && image.type) {
return image.type === 'video';
}
return false;
};
/**
* @param {*} image An element in the array of images.
* @return {Boolean} Whether the provided element is a video that is to be
* embedded with an external service like YouTube. By default, this is
* determined by the url not ending in `.mp4`, `.ogg`, or `.webm`.
* @type {Function}
* @name isSharedVideo
* @memberOf bootstrapLightbox.Lightbox
*/
this.isSharedVideo = function (image) {
return this.isVideo(image) &&
!this.getImageUrl(image).match(/\.(mp4|ogg|webm)$/);
};
this.$get = ['$document', '$injector', '$modal', '$timeout', 'ImageLoader',
function ($document, $injector, $modal, $timeout, ImageLoader) {
// optional dependency
var cfpLoadingBar = $injector.has('cfpLoadingBar') ?
$injector.get('cfpLoadingBar'): null;
var Lightbox = {};
/**
* Array of all images to be shown in the lightbox (not `Image` objects).
* @type {Array}
* @name images
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.images = [];
/**
* The index in the `Lightbox.images` aray of the image that is currently
* shown in the lightbox.
* @type {Number}
* @name index
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.index = -1;
// set the configurable properties and methods, the defaults of which are
// defined above
Lightbox.templateUrl = this.templateUrl;
Lightbox.fullScreenMode = this.fullScreenMode;
Lightbox.getImageUrl = this.getImageUrl;
Lightbox.getImageCaption = this.getImageCaption;
Lightbox.calculateImageDimensionLimits = this.calculateImageDimensionLimits;
Lightbox.calculateModalDimensions = this.calculateModalDimensions;
Lightbox.isVideo = this.isVideo;
Lightbox.isSharedVideo = this.isSharedVideo;
/**
* Whether keyboard navigation is currently enabled for navigating through
* images in the lightbox.
* @type {Boolean}
* @name keyboardNavEnabled
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.keyboardNavEnabled = false;
/**
* The image currently shown in the lightbox.
* @type {*}
* @name image
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.image = {};
/**
* The UI Bootstrap modal instance. See {@link
* http://angular-ui.github.io/bootstrap/#/modal}.
* @type {Object}
* @name modalInstance
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.modalInstance = null;
/**
* The URL of the current image. This is a property of the service rather
* than of `Lightbox.image` because `Lightbox.image` need not be an
* object, and besides it would be poor practice to alter the given
* objects.
* @type {String}
* @name imageUrl
* @memberOf bootstrapLightbox.Lightbox
*/
/**
* The optional caption of the current image.
* @type {String}
* @name imageCaption
* @memberOf bootstrapLightbox.Lightbox
*/
/**
* Whether an image is currently being loaded.
* @type {Boolean}
* @name loading
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.loading = false;
/**
* Open the lightbox modal.
* @param {Array} newImages An array of images. Each image may be of
* any type.
* @param {Number} newIndex The index in `newImages` to set as the
* current image.
* @param {Object} modalParams Custom params for the angular UI
* bootstrap modal (in $modal.open()).
* @return {Object} The created UI Bootstrap modal instance.
* @type {Function}
* @name openModal
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.openModal = function (newImages, newIndex, modalParams) {
Lightbox.images = newImages;
Lightbox.setImage(newIndex);
// store the modal instance so we can close it manually if we need to
Lightbox.modalInstance = $modal.open(angular.extend({
'templateUrl': Lightbox.templateUrl,
'controller': ['$scope', function ($scope) {
// $scope is the modal scope, a child of $rootScope
$scope.Lightbox = Lightbox;
Lightbox.keyboardNavEnabled = true;
}],
'windowClass': 'lightbox-modal'
}, modalParams || {}));
// modal close handler
Lightbox.modalInstance.result['finally'](function () {
// prevent the lightbox from flickering from the old image when it gets
// opened again
Lightbox.images = [];
Lightbox.index = 1;
Lightbox.image = {};
Lightbox.imageUrl = null;
Lightbox.imageCaption = null;
Lightbox.keyboardNavEnabled = false;
// complete any lingering loading bar progress
if (cfpLoadingBar) {
cfpLoadingBar.complete();
}
});
return Lightbox.modalInstance;
};
/**
* Close the lightbox modal.
* @param {*} result This argument can be useful if the modal promise
* gets handler(s) attached to it.
* @type {Function}
* @name closeModal
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.closeModal = function (result) {
return Lightbox.modalInstance.close(result);
};
/**
* This method can be used in all methods which navigate/change the
* current image.
* @param {Number} newIndex The index in the array of images to set as
* the new current image.
* @type {Function}
* @name setImage
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.setImage = function (newIndex) {
if (!(newIndex in Lightbox.images)) {
throw 'Invalid image.';
}
// update the loading flag and start the loading bar
Lightbox.loading = true;
if (cfpLoadingBar) {
cfpLoadingBar.start();
}
var image = Lightbox.images[newIndex];
var imageUrl = Lightbox.getImageUrl(image);
var success = function (properties) {
// update service properties for the image
properties = properties || {};
Lightbox.index = properties.index || newIndex;
Lightbox.image = properties.image || image;
Lightbox.imageUrl = properties.imageUrl || imageUrl;
Lightbox.imageCaption = properties.imageCaption ||
Lightbox.getImageCaption(image);
// restore the loading flag and complete the loading bar
Lightbox.loading = false;
if (cfpLoadingBar) {
cfpLoadingBar.complete();
}
};
if (!Lightbox.isVideo(image)) {
// load the image before setting it, so everything in the view is
// updated at the same time; otherwise, the previous image remains while
// the current image is loading
ImageLoader.load(imageUrl).then(function () {
success();
}, function () {
success({
'imageUrl': '//:0', // blank image
// use the caption to show the user an error
'imageCaption': 'Failed to load image'
});
});
} else {
success();
}
};
/**
* Navigate to the first image.
* @type {Function}
* @name firstImage
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.firstImage = function () {
Lightbox.setImage(0);
};
/**
* Navigate to the previous image.
* @type {Function}
* @name prevImage
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.prevImage = function () {
Lightbox.setImage((Lightbox.index - 1 + Lightbox.images.length) %
Lightbox.images.length);
};
/**
* Navigate to the next image.
* @type {Function}
* @name nextImage
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.nextImage = function () {
Lightbox.setImage((Lightbox.index + 1) % Lightbox.images.length);
};
/**
* Navigate to the last image.
* @type {Function}
* @name lastImage
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.lastImage = function () {
Lightbox.setImage(Lightbox.images.length - 1);
};
/**
* Call this method to set both the array of images and the current image
* (based on the current index). A use case is when the image collection
* gets changed dynamically in some way while the lightbox is still
* open.
* @param {Array} newImages The new array of images.
* @type {Function}
* @name setImages
* @memberOf bootstrapLightbox.Lightbox
*/
Lightbox.setImages = function (newImages) {
Lightbox.images = newImages;
Lightbox.setImage(Lightbox.index);
};
// Bind the left and right arrow keys for image navigation. This event
// handler never gets unbinded. Disable this using the `keyboardNavEnabled`
// flag. It is automatically disabled when the target is an input and or a
// textarea. TODO: Move this to a directive.
$document.bind('keydown', function (event) {
if (!Lightbox.keyboardNavEnabled) {
return;
}
// method of Lightbox to call
var method = null;
switch (event.which) {
case 39: // right arrow key
method = 'nextImage';
break;
case 37: // left arrow key
method = 'prevImage';
break;
}
if (method !== null && ['input', 'textarea'].indexOf(
event.target.tagName.toLowerCase()) === -1) {
// the view doesn't update without a manual digest
$timeout(function () {
Lightbox[method]();
});
event.preventDefault();
}
});
return Lightbox;
}];
});