Blame view

sources/apps/gallery/js/gallery.js 8.02 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
  var Gallery = {};
d1bafeea1   Kload   [fix] Upgrade to ...
2
3
  Gallery.images = [];
  Gallery.currentAlbum = '';
d1bafeea1   Kload   [fix] Upgrade to ...
4
  Gallery.users = [];
6d9380f96   Cédric Dupont   Update sources OC...
5
6
  Gallery.albumMap = {};
  Gallery.imageMap = {};
d1bafeea1   Kload   [fix] Upgrade to ...
7

6d9380f96   Cédric Dupont   Update sources OC...
8
9
10
11
12
13
14
15
16
17
18
19
  Gallery.getAlbum = function (path) {
  	if (!Gallery.albumMap[path]) {
  		Gallery.albumMap[path] = new Album(path, [], [], OC.basename(path));
  		if (path !== '') {
  			var parent = OC.dirname(path);
  			if (parent === path) {
  				parent = '';
  			}
  			Gallery.getAlbum(parent).subAlbums.push(Gallery.albumMap[path]);
  		}
  	}
  	return Gallery.albumMap[path];
d1bafeea1   Kload   [fix] Upgrade to ...
20
21
22
23
  };
  
  // fill the albums from Gallery.images
  Gallery.fillAlbums = function () {
6d9380f96   Cédric Dupont   Update sources OC...
24
25
26
  	var sortFunction = function (a, b) {
  		return a.path.toLowerCase().localeCompare(b.path.toLowerCase());
  	};
d1bafeea1   Kload   [fix] Upgrade to ...
27
  	var token = $('#gallery').data('token');
6d9380f96   Cédric Dupont   Update sources OC...
28
29
  	var album, image;
  	return $.getJSON(OC.filePath('gallery', 'ajax', 'getimages.php'), {token: token}).then(function (data) {
d1bafeea1   Kload   [fix] Upgrade to ...
30
31
  		Gallery.users = data.users;
  		Gallery.displayNames = data.displayNames;
6d9380f96   Cédric Dupont   Update sources OC...
32
  		Gallery.images = data.images;
d1bafeea1   Kload   [fix] Upgrade to ...
33

6d9380f96   Cédric Dupont   Update sources OC...
34
35
36
37
38
39
40
41
42
43
44
45
  		for (var i = 0; i < Gallery.images.length; i++) {
  			var parts = Gallery.images[i].split('/');
  			parts.shift();
  			var path = parts.join('/');
  			image = new GalleryImage(Gallery.images[i], path);
  			var dir = OC.dirname(Gallery.images[i]);
  			parts = dir.split('/');
  			parts.shift();
  			dir = parts.join('/');
  			album = Gallery.getAlbum(dir);
  			album.images.push(image);
  			Gallery.imageMap[image.path] = image;
d1bafeea1   Kload   [fix] Upgrade to ...
46
  		}
6d9380f96   Cédric Dupont   Update sources OC...
47
48
49
50
  
  		for (path in Gallery.albumMap) {
  			Gallery.albumMap[path].images.sort(sortFunction);
  			Gallery.albumMap[path].subAlbums.sort(sortFunction);
d1bafeea1   Kload   [fix] Upgrade to ...
51
  		}
6d9380f96   Cédric Dupont   Update sources OC...
52
  	});
d1bafeea1   Kload   [fix] Upgrade to ...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  };
  
  Gallery.getAlbumInfo = function (album) {
  	if (album === $('#gallery').data('token')) {
  		return [];
  	}
  	if (!Gallery.getAlbumInfo.cache[album]) {
  		var def = new $.Deferred();
  		Gallery.getAlbumInfo.cache[album] = def;
  		$.getJSON(OC.filePath('gallery', 'ajax', 'gallery.php'), {gallery: album}, function (data) {
  			def.resolve(data);
  		});
  	}
  	return Gallery.getAlbumInfo.cache[album];
  };
  Gallery.getAlbumInfo.cache = {};
  Gallery.getImage = function (image) {
  	return OC.filePath('gallery', 'ajax', 'image.php') + '?file=' + encodeURIComponent(image);
  };
d1bafeea1   Kload   [fix] Upgrade to ...
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
  Gallery.share = function (event) {
  	if (!OC.Share.droppedDown) {
  		event.preventDefault();
  		event.stopPropagation();
  
  		(function () {
  			var target = OC.Share.showLink;
  			OC.Share.showLink = function () {
  				var r = target.apply(this, arguments);
  				$('#linkText').val($('#linkText').val().replace('service=files', 'service=gallery'));
  				return r;
  			};
  		})();
  
  		Gallery.getAlbumInfo(Gallery.currentAlbum).then(function (info) {
  			$('a.share').data('item', info.fileid).data('link', true)
  				.data('possible-permissions', info.permissions).
  				click();
  			if (!$('#linkCheckbox').is(':checked')) {
  				$('#linkText').hide();
  			}
  		});
  	}
  };
  Gallery.view = {};
  Gallery.view.element = null;
  Gallery.view.clear = function () {
  	Gallery.view.element.empty();
6d9380f96   Cédric Dupont   Update sources OC...
100
  	Gallery.showLoading();
d1bafeea1   Kload   [fix] Upgrade to ...
101
102
  };
  Gallery.view.cache = {};
d1bafeea1   Kload   [fix] Upgrade to ...
103
104
  
  Gallery.view.viewAlbum = function (albumPath) {
6d9380f96   Cédric Dupont   Update sources OC...
105
106
107
108
  	var i, crumbs, path;
  	albumPath = albumPath || '';
  	if (!Gallery.albumMap[albumPath]) {
  		return;
d1bafeea1   Kload   [fix] Upgrade to ...
109
  	}
d1bafeea1   Kload   [fix] Upgrade to ...
110

6d9380f96   Cédric Dupont   Update sources OC...
111
112
113
  	Gallery.view.clear();
  	if (albumPath !== Gallery.currentAlbum) {
  		Gallery.view.loadVisibleRows.loading = false;
d1bafeea1   Kload   [fix] Upgrade to ...
114
  	}
6d9380f96   Cédric Dupont   Update sources OC...
115
  	Gallery.currentAlbum = albumPath;
d1bafeea1   Kload   [fix] Upgrade to ...
116

6d9380f96   Cédric Dupont   Update sources OC...
117
  	if (albumPath === '' || $('#gallery').data('token')) {
d1bafeea1   Kload   [fix] Upgrade to ...
118
119
120
121
122
123
124
125
126
127
128
  		$('button.share').hide();
  	} else {
  		$('button.share').show();
  	}
  
  	OC.Breadcrumb.clear();
  	var albumName = $('#content').data('albumname');
  	if (!albumName) {
  		albumName = t('gallery', 'Pictures');
  	}
  	OC.Breadcrumb.push(albumName, '#').click(function () {
6d9380f96   Cédric Dupont   Update sources OC...
129
  		Gallery.view.viewAlbum('');
d1bafeea1   Kload   [fix] Upgrade to ...
130
  	});
6d9380f96   Cédric Dupont   Update sources OC...
131
  	path = '';
d1bafeea1   Kload   [fix] Upgrade to ...
132
  	crumbs = albumPath.split('/');
d1bafeea1   Kload   [fix] Upgrade to ...
133
134
  	for (i = 0; i < crumbs.length; i++) {
  		if (crumbs[i]) {
6d9380f96   Cédric Dupont   Update sources OC...
135
136
137
138
139
  			if (path) {
  				path += '/' + crumbs[i];
  			} else {
  				path += crumbs[i];
  			}
d1bafeea1   Kload   [fix] Upgrade to ...
140
141
142
  			Gallery.view.pushBreadCrumb(crumbs[i], path);
  		}
  	}
d1bafeea1   Kload   [fix] Upgrade to ...
143
  	Gallery.getAlbumInfo(Gallery.currentAlbum); //preload album info
6d9380f96   Cédric Dupont   Update sources OC...
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
  
  	Gallery.albumMap[albumPath].viewedItems = 0;
  	setTimeout(function () {
  		Gallery.view.loadVisibleRows.activeIndex = 0;
  		Gallery.view.loadVisibleRows(Gallery.albumMap[Gallery.currentAlbum], Gallery.currentAlbum);
  	}, 0);
  };
  
  Gallery.view.loadVisibleRows = function (album, path) {
  	if (Gallery.view.loadVisibleRows.loading && Gallery.view.loadVisibleRows.loading.state() !== 'resolved') {
  		return Gallery.view.loadVisibleRows.loading;
  	}
  	// load 2 windows worth of rows
  	var scroll = $('#content-wrapper').scrollTop() + $(window).scrollTop();
  	var targetHeight = ($(window).height() * 2) + scroll;
  	var showRows = function (album) {
  		if (!(album.viewedItems < album.subAlbums.length + album.images.length)) {
  			Gallery.view.loadVisibleRows.loading = null;
  			return;
  		}
  		return album.getNextRow(Gallery.view.element.width()).then(function (row) {
  			return row.getDom().then(function (dom) {
  				if (Gallery.currentAlbum !== path) {
  					Gallery.view.loadVisibleRows.loading = null;
  					return; //throw away the row if the user has navigated away in the meantime
  				}
  				if (Gallery.view.element.length == 1) {
  					Gallery.showNormal();
  				}
  				Gallery.view.element.append(dom);
  				if (album.viewedItems < album.subAlbums.length + album.images.length && Gallery.view.element.height() < targetHeight) {
  					return showRows(album);
  				} else {
  					Gallery.view.loadVisibleRows.loading = null;
  				}
  			}, function () {
  				Gallery.view.loadVisibleRows.loading = null;
  			});
  		});
  	};
  	if (Gallery.view.element.height() < targetHeight) {
  		Gallery.view.loadVisibleRows.loading = true;
  		Gallery.view.loadVisibleRows.loading = showRows(album);
  		return Gallery.view.loadVisibleRows.loading;
  	}
d1bafeea1   Kload   [fix] Upgrade to ...
189
  };
6d9380f96   Cédric Dupont   Update sources OC...
190
  Gallery.view.loadVisibleRows.loading = false;
d1bafeea1   Kload   [fix] Upgrade to ...
191
192
193
194
195
196
  
  Gallery.view.pushBreadCrumb = function (text, path) {
  	OC.Breadcrumb.push(text, '#' + path).click(function () {
  		Gallery.view.viewAlbum(path);
  	});
  };
6d9380f96   Cédric Dupont   Update sources OC...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  Gallery.showEmpty = function () {
  	$('#controls').addClass('hidden');
  	$('#emptycontent').removeClass('hidden');
  	$('#loading').addClass('hidden');
  };
  
  Gallery.showLoading = function () {
  	$('#emptycontent').addClass('hidden');
  	$('#controls').removeClass('hidden');
  	$('#loading').removeClass('hidden');
  };
  
  Gallery.showNormal = function () {
  	$('#emptycontent').addClass('hidden');
  	$('#controls').removeClass('hidden');
  	$('#loading').addClass('hidden');
d1bafeea1   Kload   [fix] Upgrade to ...
213
214
215
  };
  
  $(document).ready(function () {
6d9380f96   Cédric Dupont   Update sources OC...
216
217
218
  	Gallery.showLoading();
  
  	Gallery.view.element = $('#gallery');
d1bafeea1   Kload   [fix] Upgrade to ...
219
  	Gallery.fillAlbums().then(function () {
6d9380f96   Cédric Dupont   Update sources OC...
220
221
222
  		if(Gallery.images.length === 0) {
  			Gallery.showEmpty();
  		}
d1bafeea1   Kload   [fix] Upgrade to ...
223
224
225
226
  		OC.Breadcrumb.container = $('#breadcrumbs');
  		window.onhashchange();
  		$('button.share').click(Gallery.share);
  	});
6d9380f96   Cédric Dupont   Update sources OC...
227
  	Gallery.view.element.on('click', 'a.image', function (event) {
d1bafeea1   Kload   [fix] Upgrade to ...
228
  		event.preventDefault();
6d9380f96   Cédric Dupont   Update sources OC...
229
230
231
232
  		var path = $(this).data('path');
  		var album = Gallery.albumMap[Gallery.currentAlbum];
  		if (location.hash !== encodeURI(path)) {
  			location.hash = encodeURI(path);
d1bafeea1   Kload   [fix] Upgrade to ...
233
  			Thumbnail.paused = true;
6d9380f96   Cédric Dupont   Update sources OC...
234
235
236
237
238
  			var images = album.images.map(function (image) {
  				return Gallery.getImage(image.src);
  			});
  			var clickedImage = Gallery.imageMap[path];
  			var i = images.indexOf(Gallery.getImage(clickedImage.src));
d1bafeea1   Kload   [fix] Upgrade to ...
239
240
241
242
243
244
245
246
247
248
249
  			Slideshow.start(images, i);
  		}
  	});
  
  	$('#openAsFileListButton').click(function (event) {
  		window.location.href = window.location.href.replace('service=gallery', 'service=files');
  	});
  
  	jQuery.fn.slideShow.onstop = function () {
  		$('#content').show();
  		Thumbnail.paused = false;
6d9380f96   Cédric Dupont   Update sources OC...
250
  		location.hash = encodeURI(Gallery.currentAlbum);
d1bafeea1   Kload   [fix] Upgrade to ...
251
252
  		Thumbnail.concurrent = 3;
  	};
6d9380f96   Cédric Dupont   Update sources OC...
253
254
255
256
257
258
259
260
261
262
263
  
  	$(window).scroll(function () {
  		Gallery.view.loadVisibleRows(Gallery.albumMap[Gallery.currentAlbum], Gallery.currentAlbum);
  	});
  	$('#content-wrapper').scroll(function () {
  		Gallery.view.loadVisibleRows(Gallery.albumMap[Gallery.currentAlbum], Gallery.currentAlbum);
  	});
  
  	$(window).resize(_.throttle(function () {
  		Gallery.view.viewAlbum(Gallery.currentAlbum);
  	}, 500));
d1bafeea1   Kload   [fix] Upgrade to ...
264
265
266
  });
  
  window.onhashchange = function () {
6d9380f96   Cédric Dupont   Update sources OC...
267
268
  	var album = decodeURI(location.hash).substr(1);
  	if (!Gallery.imageMap[album]) {
d1bafeea1   Kload   [fix] Upgrade to ...
269
  		Slideshow.end();
6d9380f96   Cédric Dupont   Update sources OC...
270
271
272
273
  		album = decodeURIComponent(album);
  		if (Gallery.currentAlbum !== album || album == '') {
  			Gallery.view.viewAlbum(album);
  		}
d1bafeea1   Kload   [fix] Upgrade to ...
274
275
276
277
278
  	} else {
  		Gallery.view.viewAlbum(OC.dirname(album));
  		$('#gallery').find('a.image[data-path="' + album + '"]').click();
  	}
  };