~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to usr/share/owncloud/apps/gallery/js/gallery.js

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
var Gallery = {};
2
 
Gallery.albums = {};
3
 
Gallery.images = [];
4
 
Gallery.currentAlbum = '';
5
 
Gallery.subAlbums = {};
6
 
Gallery.users = [];
7
 
Gallery.displayNames = [];
8
 
 
9
 
Gallery.sortFunction = function (a, b) {
10
 
        return a.toLowerCase().localeCompare(b.toLowerCase());
11
 
};
12
 
 
13
 
// fill the albums from Gallery.images
14
 
Gallery.fillAlbums = function () {
15
 
        var def = new $.Deferred();
16
 
        var token = $('#gallery').data('token');
17
 
        $.getJSON(OC.filePath('gallery', 'ajax', 'getimages.php'), {token: token}).then(function (data) {
18
 
                var albumPath, i, imagePath, parent, path;
19
 
                Gallery.users = data.users;
20
 
                Gallery.displayNames = data.displayNames;
21
 
                for (i = 0; i < data.images.length; i++) {
22
 
                        Gallery.images.push(data.images[i].path);
23
 
                }
24
 
                Gallery.fillAlbums.fill(Gallery.albums, Gallery.images);
25
 
                Gallery.fillAlbums.fillSubAlbums(Gallery.subAlbums, Gallery.albums);
26
 
 
27
 
                Gallery.fillAlbums.sortAlbums(Gallery.subAlbums);
28
 
                def.resolve();
29
 
        });
30
 
        return def;
31
 
};
32
 
Gallery.fillAlbums.fill = function (albums, images) {
33
 
        var imagePath, albumPath, parent;
34
 
        images.sort();
35
 
        for (i = 0; i < images.length; i++) {
36
 
                imagePath = images[i];
37
 
                albumPath = OC.dirname(imagePath);
38
 
                if (!albums[albumPath]) {
39
 
                        albums[albumPath] = [];
40
 
                }
41
 
                parent = OC.dirname(albumPath);
42
 
                while (parent && !albums[parent] && parent !== albumPath) {
43
 
                        albums[parent] = [];
44
 
                        parent = OC.dirname(parent);
45
 
                }
46
 
                albums[albumPath].push(imagePath);
47
 
        }
48
 
};
49
 
Gallery.fillAlbums.fillSubAlbums = function (subAlbums, albums) {
50
 
        var albumPath, parent;
51
 
        for (albumPath in albums) {
52
 
                if (albums.hasOwnProperty(albumPath)) {
53
 
                        if (albumPath !== '') {
54
 
                                parent = OC.dirname(albumPath);
55
 
                                if (albumPath !== parent) {
56
 
                                        if (!subAlbums[parent]) {
57
 
                                                subAlbums[parent] = [];
58
 
                                        }
59
 
                                        subAlbums[parent].push(albumPath);
60
 
                                }
61
 
                        }
62
 
                }
63
 
        }
64
 
};
65
 
Gallery.fillAlbums.sortAlbums = function (albums) {
66
 
        var path;
67
 
        for (path in albums) {
68
 
                if (albums.hasOwnProperty(path)) {
69
 
                        albums[path].sort(Gallery.sortFunction);
70
 
                }
71
 
        }
72
 
};
73
 
 
74
 
Gallery.getAlbumInfo = function (album) {
75
 
        if (album === $('#gallery').data('token')) {
76
 
                return [];
77
 
        }
78
 
        if (!Gallery.getAlbumInfo.cache[album]) {
79
 
                var def = new $.Deferred();
80
 
                Gallery.getAlbumInfo.cache[album] = def;
81
 
                $.getJSON(OC.filePath('gallery', 'ajax', 'gallery.php'), {gallery: album}, function (data) {
82
 
                        def.resolve(data);
83
 
                });
84
 
        }
85
 
        return Gallery.getAlbumInfo.cache[album];
86
 
};
87
 
Gallery.getAlbumInfo.cache = {};
88
 
Gallery.getImage = function (image) {
89
 
        return OC.filePath('gallery', 'ajax', 'image.php') + '?file=' + encodeURIComponent(image);
90
 
};
91
 
Gallery.getAlbumThumbnailPaths = function (album) {
92
 
        var paths = [];
93
 
        if (Gallery.albums[album].length) {
94
 
                paths = Gallery.albums[album].slice(0, 10);
95
 
        }
96
 
        if (Gallery.subAlbums[album]) {
97
 
                for (var i = 0; i < Gallery.subAlbums[album].length; i++) {
98
 
                        if (paths.length < 10) {
99
 
                                paths = paths.concat(Gallery.getAlbumThumbnailPaths(Gallery.subAlbums[album][i]));
100
 
                        }
101
 
                }
102
 
        }
103
 
        return paths;
104
 
};
105
 
Gallery.share = function (event) {
106
 
        if (!OC.Share.droppedDown) {
107
 
                event.preventDefault();
108
 
                event.stopPropagation();
109
 
 
110
 
                (function () {
111
 
                        var target = OC.Share.showLink;
112
 
                        OC.Share.showLink = function () {
113
 
                                var r = target.apply(this, arguments);
114
 
                                $('#linkText').val($('#linkText').val().replace('service=files', 'service=gallery'));
115
 
                                return r;
116
 
                        };
117
 
                })();
118
 
 
119
 
                Gallery.getAlbumInfo(Gallery.currentAlbum).then(function (info) {
120
 
                        $('a.share').data('item', info.fileid).data('link', true)
121
 
                                .data('possible-permissions', info.permissions).
122
 
                                click();
123
 
                        if (!$('#linkCheckbox').is(':checked')) {
124
 
                                $('#linkText').hide();
125
 
                        }
126
 
                });
127
 
        }
128
 
};
129
 
Gallery.view = {};
130
 
Gallery.view.element = null;
131
 
Gallery.view.clear = function () {
132
 
        Gallery.view.element.empty();
133
 
};
134
 
Gallery.view.cache = {};
135
 
 
136
 
Gallery.view.addImage = function (image) {
137
 
        var link , thumb;
138
 
        if (Gallery.view.cache[image]) {
139
 
                Gallery.view.element.append(Gallery.view.cache[image]);
140
 
                thumb = Thumbnail.get(image);
141
 
                thumb.queue();
142
 
        } else {
143
 
                link = $('<a/>');
144
 
                link.addClass('image loading');
145
 
                link.attr('data-path', image);
146
 
                link.attr('href', Gallery.getImage(image)).attr('rel', 'album').attr('alt', OC.basename(image)).attr('title', OC.basename(image));
147
 
 
148
 
                thumb = Thumbnail.get(image);
149
 
                thumb.queue().then(function (thumb) {
150
 
                        link.removeClass('loading');
151
 
                        link.append(thumb);
152
 
                });
153
 
 
154
 
                Gallery.view.element.append(link);
155
 
                Gallery.view.cache[image] = link;
156
 
        }
157
 
};
158
 
 
159
 
Gallery.view.addAlbum = function (path, name) {
160
 
        var link, image, label, thumbs, thumb;
161
 
        name = name || OC.basename(path);
162
 
        if (Gallery.view.cache[path]) {
163
 
                thumbs = Gallery.view.addAlbum.thumbs[path];
164
 
                Gallery.view.element.append(Gallery.view.cache[path]);
165
 
                //event handlers are removed when using clear()
166
 
                Gallery.view.cache[path].click(function () {
167
 
                        Gallery.view.viewAlbum(path);
168
 
                });
169
 
                Gallery.view.cache[path].mousemove(function (event) {
170
 
                        Gallery.view.addAlbum.mouseEvent.call(Gallery.view.cache[path], thumbs, event);
171
 
                });
172
 
                thumb = Thumbnail.get(thumbs[0], true);
173
 
                thumb.queue();
174
 
        } else {
175
 
                thumbs = Gallery.getAlbumThumbnailPaths(path);
176
 
                Gallery.view.addAlbum.thumbs[path] = thumbs;
177
 
                link = $('<a/>');
178
 
                label = $('<label/>');
179
 
                link.attr('href', '#' + path);
180
 
                link.addClass('album loading');
181
 
                link.click(function () {
182
 
                        Gallery.view.viewAlbum(path);
183
 
                });
184
 
                link.data('path', path);
185
 
                link.data('offset', 0);
186
 
                link.attr('title', OC.basename(path));
187
 
                label.text(name);
188
 
                link.append(label);
189
 
                thumb = Thumbnail.get(thumbs[0], true);
190
 
                thumb.queue().then(function (image) {
191
 
                        link.removeClass('loading');
192
 
                        link.append(image);
193
 
                });
194
 
 
195
 
                link.mousemove(function (event) {
196
 
                        Gallery.view.addAlbum.mouseEvent.call(link, thumbs, event);
197
 
                });
198
 
 
199
 
                Gallery.view.element.append(link);
200
 
                Gallery.view.cache[path] = link;
201
 
        }
202
 
};
203
 
Gallery.view.addAlbum.thumbs = {};
204
 
 
205
 
Gallery.view.addAlbum.mouseEvent = function (thumbs, event) {
206
 
        var mousePos = event.pageX - $(this).offset().left,
207
 
                offset = ((Math.floor((mousePos / 200) * thumbs.length - 1) % thumbs.length) + thumbs.length) % thumbs.length, //workaround js modulo "feature" with negative numbers
208
 
                link = this,
209
 
                oldOffset = $(this).data('offset');
210
 
        if (offset !== oldOffset && !link.data('loading')) {
211
 
                if (!thumbs[offset]) {
212
 
                        console.log(offset);
213
 
                }
214
 
                var thumb = Thumbnail.get(thumbs[offset], true);
215
 
                link.data('loading', true);
216
 
                thumb.load().then(function (image) {
217
 
                        link.data('loading', false);
218
 
                        $('img', link).remove();
219
 
                        link.append(image);
220
 
                });
221
 
                $(this).data('offset', offset);
222
 
        }
223
 
};
224
 
Gallery.view.addAlbum.thumbs = {};
225
 
 
226
 
Gallery.view.viewAlbum = function (albumPath) {
227
 
        if (!albumPath) {
228
 
                albumPath = $('#gallery').data('token');
229
 
        }
230
 
        Thumbnail.queue = [];
231
 
        Gallery.view.clear();
232
 
        Gallery.currentAlbum = albumPath;
233
 
 
234
 
        var i, album, subAlbums, crumbs, path;
235
 
        subAlbums = Gallery.subAlbums[albumPath];
236
 
        if (subAlbums) {
237
 
                for (i = 0; i < subAlbums.length; i++) {
238
 
                        Gallery.view.addAlbum(subAlbums[i]);
239
 
                        Gallery.view.element.append(' '); //add a space for justify
240
 
                }
241
 
        }
242
 
 
243
 
        album = Gallery.albums[albumPath];
244
 
        if (album) {
245
 
                for (i = 0; i < album.length; i++) {
246
 
                        Gallery.view.addImage(album[i]);
247
 
                        Gallery.view.element.append(' '); //add a space for justify
248
 
                }
249
 
        }
250
 
 
251
 
        if (albumPath === OC.currentUser) {
252
 
                $('button.share').hide();
253
 
        } else {
254
 
                $('button.share').show();
255
 
        }
256
 
 
257
 
        OC.Breadcrumb.clear();
258
 
        var albumName = $('#content').data('albumname');
259
 
        if (!albumName) {
260
 
                albumName = t('gallery', 'Pictures');
261
 
        }
262
 
        OC.Breadcrumb.push(albumName, '#').click(function () {
263
 
                Gallery.view.viewAlbum(OC.currentUser);
264
 
        });
265
 
        crumbs = albumPath.split('/');
266
 
        //first entry is username
267
 
        path = crumbs.splice(0, 1);
268
 
        //remove shareid
269
 
        if (path[0] !== OC.currentUser && path[0] !== $('#gallery').data('token')) {
270
 
                path += '/' + crumbs.splice(0, 1);
271
 
        }
272
 
        for (i = 0; i < crumbs.length; i++) {
273
 
                if (crumbs[i]) {
274
 
                        path += '/' + crumbs[i];
275
 
                        Gallery.view.pushBreadCrumb(crumbs[i], path);
276
 
                }
277
 
        }
278
 
 
279
 
        if (albumPath === OC.currentUser) {
280
 
                Gallery.view.showUsers();
281
 
        }
282
 
 
283
 
        Gallery.getAlbumInfo(Gallery.currentAlbum); //preload album info
284
 
};
285
 
 
286
 
Gallery.view.pushBreadCrumb = function (text, path) {
287
 
        OC.Breadcrumb.push(text, '#' + path).click(function () {
288
 
                Gallery.view.viewAlbum(path);
289
 
        });
290
 
};
291
 
 
292
 
Gallery.view.showUsers = function () {
293
 
        var i, j, user, head, subAlbums, album;
294
 
        for (i = 0; i < Gallery.users.length; i++) {
295
 
                user = Gallery.users[i];
296
 
                subAlbums = Gallery.subAlbums[user];
297
 
                if (subAlbums) {
298
 
                        if (subAlbums.length > 0) {
299
 
                                head = $('<h2/>');
300
 
                                head.text(t('gallery', 'Shared by') + ' ' + Gallery.displayNames[user]);
301
 
                                $('#gallery').append(head);
302
 
                                for (j = 0; j < subAlbums.length; j++) {
303
 
                                        album = subAlbums[j];
304
 
                                        album = Gallery.subAlbums[album][0];//first level sub albums is share source id
305
 
                                        Gallery.view.addAlbum(album);
306
 
                                        Gallery.view.element.append(' '); //add a space for justify
307
 
                                }
308
 
                        }
309
 
                }
310
 
        }
311
 
};
312
 
 
313
 
$(document).ready(function () {
314
 
        Gallery.fillAlbums().then(function () {
315
 
                Gallery.view.element = $('#gallery');
316
 
                OC.Breadcrumb.container = $('#breadcrumbs');
317
 
                window.onhashchange();
318
 
                $('button.share').click(Gallery.share);
319
 
        });
320
 
 
321
 
        $('#gallery').on('click', 'a.image', function (event) {
322
 
                var images = $('#gallery').children('a.image');
323
 
                var i = images.index(this),
324
 
                        image = $(this).data('path');
325
 
                event.preventDefault();
326
 
                if (location.hash !== image) {
327
 
                        location.hash = image;
328
 
                        Thumbnail.paused = true;
329
 
                        Slideshow.start(images, i);
330
 
                }
331
 
        });
332
 
 
333
 
        $('#openAsFileListButton').click(function (event) {
334
 
                window.location.href = window.location.href.replace('service=gallery', 'service=files');
335
 
        });
336
 
 
337
 
        jQuery.fn.slideShow.onstop = function () {
338
 
                $('#content').show();
339
 
                Thumbnail.paused = false;
340
 
                $(window).scrollTop(Gallery.scrollLocation);
341
 
                location.hash = Gallery.currentAlbum;
342
 
                Thumbnail.concurrent = 3;
343
 
        };
344
 
});
345
 
 
346
 
window.onhashchange = function () {
347
 
        var album = location.hash.substr(1);
348
 
        if (!album) {
349
 
                album = OC.currentUser;
350
 
        }
351
 
        if (!album) {
352
 
                album = $('#gallery').data('token');
353
 
        }
354
 
        if (Gallery.images.indexOf(album) === -1) {
355
 
                Slideshow.end();
356
 
                Gallery.view.viewAlbum(decodeURIComponent(album));
357
 
        } else {
358
 
                Gallery.view.viewAlbum(OC.dirname(album));
359
 
                $('#gallery').find('a.image[data-path="' + album + '"]').click();
360
 
        }
361
 
};