Welcome to mirror list, hosted at ThFree Co, Russian Federation.

galleryview.js « js - github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f829a8ed9a1eaab549ce8e0e46ae8d0922cc40ac (plain)
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
/* global OC, $, _, t, Gallery, Album, GalleryImage, SlideShow */
Gallery.view = {};
Gallery.view.element = null;
Gallery.view.requestId = -1;

/**
 * Removes all thumbnails from the view
 */
Gallery.view.clear = function () {
	// We want to keep all the events
	Gallery.view.element.children().detach();
	Gallery.showLoading();
};

/**
 * Populates the view if there are images or albums to show
 *
 * @param {string} albumPath
 */
Gallery.view.init = function (albumPath) {
	if (Gallery.images.length === 0) {
		Gallery.showEmpty();
	} else {
		// Only do it when the app is initialised
		if (Gallery.view.requestId === -1) {
			$('#download').click(Gallery.download);
			$('button.share').click(Gallery.share);
			$('#album-info-button').click(Gallery.showInfo);
		}
		OC.Breadcrumb.container = $('#breadcrumbs');
		Gallery.view.viewAlbum(albumPath);
	}
};

/**
 * Starts the slideshow
 *
 * @param {string} path
 * @param {string} albumPath
 */
Gallery.view.startSlideshow = function (path, albumPath) {
	var album = Gallery.albumMap[albumPath];
	var images = album.images;
	var startImage = Gallery.imageMap[path];
	Gallery.slideShow(images, startImage);
};

/**
 * Sets up the controls and starts loading the gallery rows
 *
 * @param {string} albumPath
 */
Gallery.view.viewAlbum = function (albumPath) {
	albumPath = albumPath || '';
	if (!Gallery.albumMap[albumPath]) {
		return;
	}

	Gallery.view.clear();
	if (albumPath !== Gallery.currentAlbum) {
		Gallery.view.loadVisibleRows.loading = false;
		Gallery.currentAlbum = albumPath;
		Gallery.view.shareButtonSetup(albumPath);
		Gallery.view.infoButtonSetup();
		Gallery.view.buildBreadCrumb(albumPath);
	}

	Gallery.albumMap[albumPath].viewedItems = 0;
	Gallery.albumMap[albumPath].preloadOffset = 0;

	// Each request has a unique ID, so that we can track which request a row belongs to
	Gallery.view.requestId = Math.random();
	Gallery.albumMap[Gallery.currentAlbum].requestId = Gallery.view.requestId;

	// Loading rows without blocking the execution of the rest of the script
	setTimeout(function () {
		Gallery.view.loadVisibleRows.activeIndex = 0;
		Gallery.view.loadVisibleRows(Gallery.albumMap[Gallery.currentAlbum], Gallery.currentAlbum);
	}, 0);
};

/**
 * Shows or hides the share button depending on if we're in a public gallery or not
 *
 * @param {string} albumPath
 */
Gallery.view.shareButtonSetup = function (albumPath) {
	var shareButton = $('button.share');
	if (albumPath === '' || Gallery.token) {
		shareButton.hide();
	} else {
		shareButton.show();
	}
};

/**
 * Shows or hides the info button based on the information we've received from the server
 */
Gallery.view.infoButtonSetup = function () {
	var infoButton = $('#album-info-button');
	var infoContentElement = $('.album-info-content');
	infoContentElement.slideUp();
	infoContentElement.css('max-height', $(window).height() - 150);
	var albumInfo = Gallery.albumsInfo[Gallery.currentAlbum];
	if ($.isEmptyObject(albumInfo.description) &&
		$.isEmptyObject(albumInfo.copyright) &&
		$.isEmptyObject(albumInfo.copyrightLink)) {
		infoButton.hide();
	} else {
		infoButton.show();
	}
};

/**
 * Loads and displays gallery rows on screen
 *
 * @param {Album} album
 * @param {string} path
 *
 * @returns {boolean|null|*}
 */
Gallery.view.loadVisibleRows = function (album, path) {
	// If the row is still loading (state() = 'pending'), let it load
	if (Gallery.view.loadVisibleRows.loading &&
		Gallery.view.loadVisibleRows.loading.state() !== 'resolved') {
		return Gallery.view.loadVisibleRows.loading;
	}

	/**
	 * At this stage, there is no loading taking place (loading = false|null), so we can look for
	 * new rows
	 */

	var scroll = $('#content-wrapper').scrollTop() + $(window).scrollTop();
	// 2 windows worth of rows is the limit from which we need to start loading new rows. As we
	// scroll down, it grows
	var targetHeight = ($(window).height() * 2) + scroll;
	var showRows = function (album) {

		// If we've reached the end of the album, we kill the loader
		if (!(album.viewedItems < album.subAlbums.length + album.images.length)) {
			Gallery.view.loadVisibleRows.loading = null;
			return;
		}

		// Everything is still in sync, since no deferred calls have been placed yet

		return album.getNextRow($(window).width()).then(function (row) {

			/**
			 * At this stage, the row has a width and contains references to images based on
			 * information available when making the request, but this information may have changed
			 * while we were receiving thumbnails for the row
			 */

			if (Gallery.view.requestId === row.requestId) {
				return row.getDom().then(function (dom) {

					// defer removal of loading class to trigger CSS3 animation
					_.defer(function () {
						dom.removeClass('loading');
					});
					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);
					}

					// No more rows to load at the moment
					Gallery.view.loadVisibleRows.loading = null;
				}, function () {
					// Something went wrong, so kill the loader
					Gallery.view.loadVisibleRows.loading = null;
				});
			} else {
				// This is the safest way to do things
				Gallery.view.viewAlbum(Gallery.currentAlbum);
			}
		});
	};
	if (Gallery.view.element.height() < targetHeight) {
		Gallery.view.loadVisibleRows.loading = true;
		Gallery.view.loadVisibleRows.loading = showRows(album);
		return Gallery.view.loadVisibleRows.loading;
	}
};
Gallery.view.loadVisibleRows.loading = false;

/**
 * Builds the breadcrumb
 *
 * @param {string} albumPath
 */
Gallery.view.buildBreadCrumb = function (albumPath) {
	var i, crumbs, path;
	OC.Breadcrumb.clear();
	var albumName = $('#content').data('albumname');
	if (!albumName) {
		albumName = t('gallery', 'Pictures');
	}
	Gallery.view.pushBreadCrumb(albumName, '');

	path = '';
	crumbs = albumPath.split('/');
	for (i = 0; i < crumbs.length; i++) {
		if (crumbs[i]) {
			if (path) {
				path += '/' + crumbs[i];
			} else {
				path += crumbs[i];
			}
			Gallery.view.pushBreadCrumb(crumbs[i], path);
		}
	}
};

/**
 * Adds a path to the breadcrumb
 *
 * @fixme Needs to shorten long paths like on the Files app
 *
 * @param {string} text
 * @param {string} path
 */
Gallery.view.pushBreadCrumb = function (text, path) {
	OC.Breadcrumb.push(text, '#' + encodeURIComponent(path));
};