Blame view

sources/apps/gallery/js/slideshow.js 11.1 KB
6d9380f96   Cédric Dupont   Update sources OC...
1
  /* global Gallery, Thumbnail */
d1bafeea1   Kload   [fix] Upgrade to ...
2
3
4
5
6
7
8
9
  jQuery.fn.slideShow = function (container, start, options) {
  	var i, images = [], settings;
  	start = start || 0;
  	settings = jQuery.extend({
  		'interval': 5000,
  		'play'    : false,
  		'maxScale': 2
  	}, options);
6d9380f96   Cédric Dupont   Update sources OC...
10
  	var slideShow = $('#slideshow');
d1bafeea1   Kload   [fix] Upgrade to ...
11
  	if (settings.play){
6d9380f96   Cédric Dupont   Update sources OC...
12
13
  		slideShow.children('.play').hide();
  		slideShow.children('.pause').show();
d1bafeea1   Kload   [fix] Upgrade to ...
14
15
  	}
  	else{
6d9380f96   Cédric Dupont   Update sources OC...
16
17
  		slideShow.children('.play').show();
  		slideShow.children('.pause').hide();
d1bafeea1   Kload   [fix] Upgrade to ...
18
19
20
21
  	}
  	jQuery.fn.slideShow.container = container;
  	jQuery.fn.slideShow.settings = settings;
  	jQuery.fn.slideShow.current = start;
6d9380f96   Cédric Dupont   Update sources OC...
22
23
24
25
26
27
28
  	if (settings.images) {
  		images = settings.images;
  	} else {
  		for (i = 0; i < this.length; i++) {
  			var imageLink = this[i];
  			images.push(imageLink.imageUrl || imageLink.href);
  		}
d1bafeea1   Kload   [fix] Upgrade to ...
29
30
31
32
33
34
35
36
37
  	}
  	container.children('img').remove();
  	container.show();
  	jQuery.fn.slideShow.images = images;
  	jQuery.fn.slideShow.cache = [];
  	jQuery.fn.slideShow.showImage(images[start], images[start + 1]);
  	jQuery.fn.slideShow.progressBar = container.find('.progress');
  
  	// hide arrows and play/pause when only one pic
6d9380f96   Cédric Dupont   Update sources OC...
38
  	slideShow.find('.next, .previous').toggle(images.length > 1);
d1bafeea1   Kload   [fix] Upgrade to ...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  	if (images.length === 1) {
  		// note: only handling hide case here as we don't want to
  		// re-show the buttons that might have been hidden by
  		// the settings.play condition above
  		$('#slideshow').find('.play, .pause').hide();
  	}
  
  	jQuery(window).resize(function () {
  		jQuery.fn.slideShow.loadImage(jQuery.fn.slideShow.images[jQuery.fn.slideShow.current]).then(function (image) {
  			jQuery.fn.slideShow.fitImage(container, image);
  		});
  	});
  	return jQuery.fn.slideShow;
  };
  
  jQuery.fn.slideShow.progressBar = null;
  
  jQuery.fn.slideShow.loadImage = function (url) {
  	if (!jQuery.fn.slideShow.cache[url]) {
  		jQuery.fn.slideShow.cache[url] = new jQuery.Deferred();
  		var image = new Image();
6d9380f96   Cédric Dupont   Update sources OC...
60
  		jQuery.fn.slideShow.cache[url].fail(function () {
d1bafeea1   Kload   [fix] Upgrade to ...
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
  			image = false;
  			jQuery.fn.slideShow.cache[url] = false;
  		});
  		image.onload = function () {
  			if (image) {
  				image.natWidth = image.width;
  				image.natHeight = image.height;
  			}
  			if (jQuery.fn.slideShow.cache[url]) {
  				jQuery.fn.slideShow.cache[url].resolve(image);
  			}
  		};
  		image.onerror = function () {
  			if (jQuery.fn.slideShow.cache[url]) {
  				jQuery.fn.slideShow.cache[url].reject(url);
  			}
  		};
  		image.src = url;
  	}
  	return jQuery.fn.slideShow.cache[url];
  };
  
  jQuery.fn.slideShow.fitImage = function (container, image) {
  	var ratio = image.natWidth / image.natHeight,
  		screenRatio = container.width() / container.height(),
  		width = null, height = null, top = null;
  	if (ratio > screenRatio) {
  		if (container.width() > image.natWidth * jQuery.fn.slideShow.settings.maxScale) {
  			top = ((container.height() - image.natHeight) / 2) + 'px';
  			height = image.natHeight + 'px';
  			width = image.natWidth + 'px';
  		} else {
  			width = container.width() + 'px';
  			height = (container.width() / ratio) + 'px';
  			top = ((container.height() - (container.width() / ratio)) / 2) + 'px';
  		}
  	} else {
  		if (container.height() > image.natHeight * jQuery.fn.slideShow.settings.maxScale) {
  			top = ((container.height() - image.natHeight) / 2) + 'px';
  			height = image.natHeight + 'px';
  			width = image.natWidth + 'px';
  		} else {
  			top = 0;
  			height = container.height() + 'px';
  			width = (container.height() * ratio) + "px";
  		}
  	}
  	jQuery(image).css({
  		top   : top,
  		width : width,
  		height: height
  	});
  };
  
  jQuery.fn.slideShow.showImage = function (url, preloadUrl) {
  	var container = jQuery.fn.slideShow.container;
  
  	container.css('background-position', 'center');
  	jQuery.fn.slideShow.loadImage(url).then(function (image) {
  		container.css('background-position', '-10000px 0');
  		if (url === jQuery.fn.slideShow.images[jQuery.fn.slideShow.current]) {
  			container.children('img').remove();
  			container.append(image);
  			jQuery.fn.slideShow.fitImage(container, image);
  			if (jQuery.fn.slideShow.settings.play) {
  				jQuery.fn.slideShow.setTimeout();
  			}
  			if (preloadUrl) {
  				jQuery.fn.slideShow.loadImage(preloadUrl);
  			}
  		}
  	});
  };
  
  jQuery.fn.slideShow.play = function () {
  	if (jQuery.fn.slideShow.settings) {
  		jQuery.fn.slideShow.settings.play = true;
  		jQuery.fn.slideShow.setTimeout();
  	}
  };
  
  jQuery.fn.slideShow.pause = function () {
  	if (jQuery.fn.slideShow.settings) {
  		jQuery.fn.slideShow.settings.play = false;
  		jQuery.fn.slideShow.clearTimeout();
  	}
  };
  
  jQuery.fn.slideShow.setTimeout = function () {
  	jQuery.fn.slideShow.clearTimeout();
  	jQuery.fn.slideShow.timeout = setTimeout(jQuery.fn.slideShow.next, jQuery.fn.slideShow.settings.interval);
  	jQuery.fn.slideShow.progressBar.stop();
  	jQuery.fn.slideShow.progressBar.css('height', '6px');
  	jQuery.fn.slideShow.progressBar.animate({'height': '26px'}, jQuery.fn.slideShow.settings.interval, 'linear');
  };
  
  jQuery.fn.slideShow.clearTimeout = function () {
  	if (jQuery.fn.slideShow.timeout) {
  		clearTimeout(jQuery.fn.slideShow.timeout);
  	}
  	jQuery.fn.slideShow.progressBar.stop();
  	jQuery.fn.slideShow.progressBar.css('height', '6px');
  	jQuery.fn.slideShow.timeout = 0;
  };
  
  jQuery.fn.slideShow.next = function () {
  	if (jQuery.fn.slideShow.container) {
  		jQuery.fn.slideShow.current++;
  		if (jQuery.fn.slideShow.current >= jQuery.fn.slideShow.images.length) {
  			jQuery.fn.slideShow.current = 0;
  		}
  		var image = jQuery.fn.slideShow.images[jQuery.fn.slideShow.current],
  			nextImage = jQuery.fn.slideShow.images[(jQuery.fn.slideShow.current + 1) % jQuery.fn.slideShow.images.length];
  		jQuery.fn.slideShow.showImage(image, nextImage);
  	}
  };
  
  jQuery.fn.slideShow.previous = function () {
  	if (jQuery.fn.slideShow.container) {
  		jQuery.fn.slideShow.current--;
  		if (jQuery.fn.slideShow.current < 0) {
  			jQuery.fn.slideShow.current = jQuery.fn.slideShow.images.length - 1;
  		}
  		var image = jQuery.fn.slideShow.images[jQuery.fn.slideShow.current],
  			previousImage = jQuery.fn.slideShow.images[(jQuery.fn.slideShow.current - 1 + jQuery.fn.slideShow.images.length) % jQuery.fn.slideShow.images.length];
  		jQuery.fn.slideShow.showImage(image, previousImage);
  	}
  };
  
  jQuery.fn.slideShow.stop = function () {
  	if (jQuery.fn.slideShow.container) {
  		jQuery.fn.slideShow.clearTimeout();
  		jQuery.fn.slideShow.container.hide();
  		jQuery.fn.slideShow.container = null;
  		if (jQuery.fn.slideShow.onstop) {
  			jQuery.fn.slideShow.onstop();
  		}
  	}
  };
  
  jQuery.fn.slideShow.hideImage = function () {
  	var container = jQuery.fn.slideShow.container;
  	if (container) {
  		container.children('img').remove();
  	}
  };
  
  jQuery.fn.slideShow.onstop = null;
  
  
  Slideshow = {};
  Slideshow.start = function (images, start, options) {
6d9380f96   Cédric Dupont   Update sources OC...
213
  	options = options || {};
d1bafeea1   Kload   [fix] Upgrade to ...
214
215
216
  	var content = $('#content');
  	start = start || 0;
  	Thumbnail.concurrent = 1; //make sure we can load the image and doesn't get blocked by loading thumbnail
6d9380f96   Cédric Dupont   Update sources OC...
217
218
219
220
221
  	if (images.slideShow) {
  		images.slideShow($('#slideshow'), start, options);
  	} else {
  		options.images = images;
  		jQuery.fn.slideShow($('#slideshow'), start, options);
d1bafeea1   Kload   [fix] Upgrade to ...
222
  	}
d1bafeea1   Kload   [fix] Upgrade to ...
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
  };
  
  Slideshow.end = function () {
  	jQuery.fn.slideShow.stop();
  };
  
  Slideshow.next = function (event) {
  	if (event) {
  		event.stopPropagation();
  	}
  	jQuery.fn.slideShow.hideImage();
  	jQuery.fn.slideShow.next();
  };
  
  Slideshow.previous = function (event) {
  	if (event) {
  		event.stopPropagation();
  	}
  	jQuery.fn.slideShow.hideImage();
  	jQuery.fn.slideShow.previous();
  };
  
  Slideshow.pause = function (event) {
  	if (event) {
  		event.stopPropagation();
  	}
  	$('#slideshow').children('.play').show();
  	$('#slideshow').children('.pause').hide();
  	Slideshow.playPause.playing = false;
  	jQuery.fn.slideShow.pause();
  };
  
  Slideshow.play = function (event) {
  	if (event) {
  		event.stopPropagation();
  	}
  	$('#slideshow').children('.play').hide();
  	$('#slideshow').children('.pause').show();
  	Slideshow.playPause.playing = true;
  	jQuery.fn.slideShow.play();
  };
  Slideshow.playPause = function () {
  	if (Slideshow.playPause.playing) {
  		Slideshow.pause();
  	} else {
  		Slideshow.play();
  	}
  };
  Slideshow.playPause.playing = false;
  Slideshow._getSlideshowTemplate = function () {
  	var defer = $.Deferred();
  	if (!this.$slideshowTemplate) {
  		var self = this;
  		$.get(OC.filePath('gallery', 'templates', 'slideshow.html'), function (tmpl) {
  			self.$slideshowTemplate = $(tmpl);
  			defer.resolve(self.$slideshowTemplate);
  		})
  			.fail(function () {
  				defer.reject();
  			});
  	} else {
  		defer.resolve(this.$slideshowTemplate);
  	}
  	return defer.promise();
  };
  
  $(document).ready(function () {
  	if ($('#body-login').length > 0) {
  		return true; //deactivate slideshow on login page
  	}
  
  	//close slideshow on esc
  	$(document).keyup(function (e) {
  		if (e.keyCode === 27) { // esc
  			Slideshow.end();
  		} else if (e.keyCode === 37) { // left
  			Slideshow.previous();
  		} else if (e.keyCode === 39) { // right
  			Slideshow.next();
  		} else if (e.keyCode === 32) { // space
  			Slideshow.playPause();
  		}
  	});
  
  	$.when(Slideshow._getSlideshowTemplate()).then(function ($tmpl) {
  		$('body').append($tmpl); //move the slideshow outside the content so we can hide the content
  
  		if (!SVGSupport()) { //replace all svg images with png images for browser that dont support svg
  			replaceSVG();
  		}
  
  		var slideshow = $('#slideshow');
  		slideshow.children('.next').click(Slideshow.next);
  		slideshow.children('.previous').click(Slideshow.previous);
  		slideshow.children('.exit').click(jQuery.fn.slideShow.stop);
  		slideshow.children('.pause').click(Slideshow.pause);
  		slideshow.children('.play').click(Slideshow.play);
  		slideshow.click(Slideshow.next);
  
  		if ($.fn.mousewheel) {
  			slideshow.bind('mousewheel.fb', function (e, delta) {
  				e.preventDefault();
  				if ($(e.target).get(0).clientHeight === 0 || $(e.target).get(0).scrollHeight === $(e.target).get(0).clientHeight) {
  					if (delta > 0) {
  						Slideshow.previous();
  					} else {
  						Slideshow.next();
  					}
  				}
  			});
  		}
  	})
  		.fail(function () {
6d9380f96   Cédric Dupont   Update sources OC...
336
  			OC.Notification.show(t('core', 'Error loading slideshow template'));
d1bafeea1   Kload   [fix] Upgrade to ...
337
  		});
6d9380f96   Cédric Dupont   Update sources OC...
338
339
340
341
342
343
  	if (OCA.Files && typeof Slideshow !== 'undefined') {
  		OCA.Files.fileActions.register('image', 'View', OC.PERMISSION_READ, '', function (filename, context) {
  			var files = context.fileList.files;
  			var start = 0;
  			var images = [];
  			var dir = context.dir + '/';
d1bafeea1   Kload   [fix] Upgrade to ...
344
  			var user = OC.currentUser;
6d9380f96   Cédric Dupont   Update sources OC...
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
  			var width = $(document).width() * window.devicePixelRatio;
  			for (var i = 0; i < files.length; i++) {
  				var file = files[i];
  				if (file.mimetype && file.mimetype.indexOf('image') >= 0) {
  					if (file.mimetype === 'image/svg+xml') {
  						imageUrl = OCA.Files.Files.getDownloadUrl(file.name, dir);
  					} else {
  						var imageUrl = OC.generateUrl('/core/preview.png?file={file}&x={x}&a=true&scalingup=0', {
  							x: width,
  							file: encodeURIComponent(dir + file.name)
  						});
  						if (!user) {
  							imageUrl = OC.generateUrl(
  								'/apps/files_sharing/publicpreview?file={file}&x={x}&a=true&t={t}&scalingup=0', {
  									file: encodeURIComponent(dir + file.name),
  									x: width,
  									t: $('#sharingToken').val()
  								});
  						}
  					}
  
  					images.push({
  						name: file.name,
  						// use gallery URL instead of download URL
  						imageUrl: imageUrl
  					});
  				}
d1bafeea1   Kload   [fix] Upgrade to ...
372
  			}
6d9380f96   Cédric Dupont   Update sources OC...
373
374
  			for (i = 0; i < images.length; i++) {
  				if (images[i].name === filename) {
d1bafeea1   Kload   [fix] Upgrade to ...
375
376
  					start = i;
  				}
6d9380f96   Cédric Dupont   Update sources OC...
377
378
  			}
  			jQuery.fn.slideShow.call(images, $('#slideshow'), start);
d1bafeea1   Kload   [fix] Upgrade to ...
379
  		});
6d9380f96   Cédric Dupont   Update sources OC...
380
  		OCA.Files.fileActions.setDefault('image', 'View');
d1bafeea1   Kload   [fix] Upgrade to ...
381
382
  	}
  });