var Gallery = {}; Gallery.albums = {}; Gallery.images = []; Gallery.currentAlbum = ''; Gallery.subAlbums = {}; Gallery.users = []; Gallery.displayNames = []; Gallery.sortFunction = function (a, b) { return a.toLowerCase().localeCompare(b.toLowerCase()); }; // fill the albums from Gallery.images Gallery.fillAlbums = function () { var def = new $.Deferred(); $.getJSON(OC.filePath('gallery', 'ajax', 'getimages.php')).then(function (data) { var albumPath, i, imagePath, parent, path; Gallery.users = data.users; Gallery.displayNames = data.displayNames; for (i = 0; i < data.images.length; i++) { Gallery.images.push(data.images[i].path); } Gallery.fillAlbums.fill(Gallery.albums, Gallery.images); Gallery.fillAlbums.fillSubAlbums(Gallery.subAlbums, Gallery.albums); Gallery.fillAlbums.sortAlbums(Gallery.subAlbums); def.resolve(); }); return def; }; Gallery.fillAlbums.fill = function (albums, images) { var imagePath, albumPath, parent; images.sort(); for (i = 0; i < images.length; i++) { imagePath = images[i]; albumPath = OC.dirname(imagePath); if (!albums[albumPath]) { albums[albumPath] = []; } parent = OC.dirname(albumPath); while (parent && !albums[parent] && parent !== albumPath) { albums[parent] = []; parent = OC.dirname(parent); } albums[albumPath].push(imagePath); } }; Gallery.fillAlbums.fillSubAlbums = function (subAlbums, albums) { var albumPath, parent; for (albumPath in albums) { if (albums.hasOwnProperty(albumPath)) { if (albumPath !== '') { parent = OC.dirname(albumPath); if (albumPath !== parent) { if (!subAlbums[parent]) { subAlbums[parent] = []; } subAlbums[parent].push(albumPath); } } } } }; Gallery.fillAlbums.sortAlbums = function (albums) { var path; for (path in albums) { if (albums.hasOwnProperty(path)) { albums[path].sort(Gallery.sortFunction); } } }; Gallery.getAlbumInfo = function (album) { 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); }; Gallery.getAlbumThumbnailPaths = function (album) { var paths = []; if (Gallery.albums[album].length) { paths = Gallery.albums[album].slice(0, 10); } if (Gallery.subAlbums[album]) { for (var i = 0; i < Gallery.subAlbums[album].length; i++) { if (paths.length < 10) { paths = paths.concat(Gallery.getAlbumThumbnailPaths(Gallery.subAlbums[album][i])); } } } return paths; }; Gallery.share = function (event) { if (!OC.Share.droppedDown) { event.preventDefault(); event.stopPropagation(); Gallery.getAlbumInfo(Gallery.currentAlbum).then(function (info) { $('a.share').data('item', info.fileid) .data('possible-permissions', info.permissions). click(); }); } }; Gallery.view = {}; Gallery.view.element = null; Gallery.view.clear = function () { Gallery.view.element.empty(); }; Gallery.view.cache = {}; Gallery.view.addImage = function (image) { var link , thumb; if (Gallery.view.cache[image]) { Gallery.view.element.append(Gallery.view.cache[image]); thumb = Thumbnail.get(image); thumb.queue(); } else { link = $(''); link.addClass('image'); link.attr('data-path', image); link.attr('href', Gallery.getImage(image)).attr('rel', 'album').attr('alt', OC.basename(image)).attr('title', OC.basename(image)); thumb = Thumbnail.get(image); thumb.queue().then(function (thumb) { link.append(thumb); }); Gallery.view.element.append(link); Gallery.view.cache[image] = link; } }; Gallery.view.addAlbum = function (path, name) { var link, image, label, thumbs, thumb; name = name || OC.basename(path); if (Gallery.view.cache[path]) { thumbs = Gallery.view.addAlbum.thumbs[path]; Gallery.view.element.append(Gallery.view.cache[path]); //event handlers are removed when using clear() Gallery.view.cache[path].click(function () { Gallery.view.viewAlbum(path); }); Gallery.view.cache[path].mousemove(function (event) { Gallery.view.addAlbum.mouseEvent.call(Gallery.view.cache[path], thumbs, event); }); thumb = Thumbnail.get(thumbs[0], true); thumb.queue(); } else { thumbs = Gallery.getAlbumThumbnailPaths(path); Gallery.view.addAlbum.thumbs[path] = thumbs; link = $(''); label = $('