Completed
Pull Request — master (#328)
by
unknown
02:21
created

GalleryImage._resetSelectionUI   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
/**
2
 * Nextcloud - Gallery
3
 *
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2017
11
 */
12
/* global Handlebars, oc_requesttoken, Gallery, Thumbnails */
13
(function ($, Gallery, oc_requesttoken) {
0 ignored issues
show
Coding Style introduced by
Identifier 'oc_requesttoken' is not in camel case.
Loading history...
14
	"use strict";
15
16
	var TEMPLATE =
17
		'<a class="{{cssClass}}" style="width: {{targetWidth}}px; height: {{targetHeight}}px;" ' +
18
		'href="" data-path="{{path}}" data-id={{fileId}}>' +
19
		'	<div class="image-loader loading"></div>' +
20
		'	<div class="image container"></div>' +
21
		'	<div class="image-label">' +
22
		'		<input id="select-files-{{fileId}}" type="checkbox" class="selectCheckBox checkbox"/>' +
23
		'		<label for="select-files-{{fileId}}"></label>' +
24
		'		<span class="title">{{label}}</span>' +
25
		'	</div>' +
26
		'</a>';
27
28
	/**
29
	 * Creates a new image object to store information about a media file
30
	 *
31
	 * @param {string} src
32
	 * @param {string} path
33
	 * @param {number} fileId
34
	 * @param {string} mimeType
35
	 * @param {number} mTime modification time
36
	 * @param {string} etag
37
	 * @param {number} size
38
	 * @param {boolean} sharedWithUser
39
	 * @param {string} owner
40
	 * @param {number} permissions
41
 * @constructor
42
	 */
43
	var GalleryImage = function (src, path, fileId, mimeType, mTime, etag, size, sharedWithUser,
0 ignored issues
show
introduced by
This function has too many parameters. (10)
Loading history...
44
								 owner, permissions) {
45
		this.src = src;
46
		this.path = path;
47
		this.fileId = fileId;
48
		this.mimeType = mimeType;
49
		this.mTime = mTime;
50
		this.etag = etag;
51
		this.size = size;
52
		this.sharedWithUser = sharedWithUser;
53
		this.owner = owner;
54
		this.permissions = permissions;
55
		this.thumbnail = null;
56
		this.domDef = null;
57
		this.spinner = null;
58
	};
59
60
	GalleryImage.cssClass = 'row-element';
61
	GalleryImage.prototype = {
62
		/**
63
		 * Returns the Thumbnail ID
64
		 *
65
		 * @returns {[number]}
66
		 */
67
		getThumbnailIds: function () {
68
			return [this.fileId];
69
		},
70
71
		/**
72
		 * Returns a reference to a loading Thumbnail.image
73
		 *
74
		 * @param {boolean} square
75
		 *
76
		 * @returns {jQuery.Deferred<Thumbnail.image>}
77
		 */
78
		getThumbnail: function (square) {
79
			if (this.thumbnail === null) {
80
				this.thumbnail = Thumbnails.get(this.fileId, square);
81
			}
82
			return this.thumbnail.loadingDeferred;
83
		},
84
85
		/**
86
		 * Returns the width of a thumbnail
87
		 *
88
		 * Used to calculate the width of the row as we add more images to it
89
		 *
90
		 * @returns {number}
91
		 */
92
		getThumbnailWidth: function (targetHeight) {
93
			var image = this;
94
			// img is a Thumbnail.image
95
			return this.getThumbnail(false).then(function (img) {
96
				var width = 0;
97
				if (img) {
98
					// In Firefox, you don't get the size of a SVG before it's added to the DOM
99
					image.domDef.children('.image').append(img);
100
					if (image.mimeType === 'image/svg+xml') {
101
						image.thumbnail.ratio = img.width / img.height;
102
					}
103
					width = Math.round(targetHeight * image.thumbnail.ratio);
104
				}
105
106
				return width;
107
			});
108
		},
109
110
		/**
111
		 * Creates the container, the a and img elements in the DOM
112
		 *
113
		 * Each image is also a link to start the full screen slideshow
114
		 *
115
		 * @param {number} targetHeight
116
		 *
117
		 * @return {a}
118
		 */
119
		getDom: function (targetHeight) {
120
			if (this.domDef === null) {
121
				var template = Handlebars.compile(TEMPLATE);
122
				var imageElement = template({
123
					cssClass: GalleryImage.cssClass,
124
					fileId: this.fileId,
125
					targetHeight: targetHeight,
126
					targetWidth: targetHeight,
127
					label: OC.basename(this.path),
128
					path: this.path
129
				});
130
				this.domDef = $(imageElement);
131
				this._addLabel();
132
				this.spinner = this.domDef.children('.image-loader');
133
			}
134
			this._resetSelectionUI();
135
			return this.domDef;
136
		},
137
138
		/**
139
		 * Resizes the image once it has been loaded
140
		 *
141
		 * @param {Number} targetHeight
142
		 * @param {Number} newWidth
143
		 */
144
		resize: function (targetHeight, newWidth) {
145
			if (this.spinner !== null) {
146
				var img = this.thumbnail.image;
147
				this.spinner.remove();
148
				this.spinner = null;
149
				this.domDef.attr('data-width', newWidth)
150
					.attr('data-height', targetHeight);
151
152
				var url = this._getLink();
153
				this.domDef.attr('href', url);
154
155
				// This will stretch wide images to make them reach targetHeight
156
				$(img).css({
157
					'width': newWidth,
158
					'height': targetHeight
159
				});
160
				img.alt = encodeURI(this.path);
161
162
				this.domDef.click(this._openImage.bind(this));
163
			}
164
		},
165
166
		/**
167
		 * Adds a label to the album
168
		 *
169
		 * @private
170
		 */
171
		_addLabel: function () {
172
			var imageLabel = this.domDef.children('.image-label');
173
			var showLabel = function() {
174
				imageLabel.slideDown(OC.menuSpeed);
175
			};
176
			var hideLabel = function() {
177
				if ($(this).hasClass('selected')) {
178
					imageLabel.show();
179
					return;
180
				}
181
				imageLabel.slideUp(OC.menuSpeed);
182
			};
183
			this.domDef.hover(showLabel, hideLabel);
184
		},
185
186
		/**
187
		 * Resets selection UI
188
		 * Make sure checkbox is not checked, in case of user navigating between
189
		 * albums back and forth, thus reusing domDef in its last state
190
		 *
191
		 * @private
192
		 */
193
		_resetSelectionUI: function() {
194
			if (this.domDef.hasClass('selected')) {
195
				this.domDef.removeClass('selected');
196
				var selectCheckbox = this.domDef.find('.selectCheckBox').get(0);
197
				if (selectCheckbox && selectCheckbox.checked) {
198
					selectCheckbox.checked = false;
199
				  this.domDef.children('.image-label').hide();
200
				}
201
			}
202
		},
203
204
		/**
205
		 * Generates the link for the click action of the image
206
		 *
207
		 * @returns {string}
208
		 * @private
209
		 */
210
		_getLink: function () {
211
			var url = '#' + encodeURIComponent(this.path);
212
			if (!this.thumbnail.valid) {
213
				var params = {
214
					c: this.etag,
215
					requesttoken: oc_requesttoken
0 ignored issues
show
Coding Style introduced by
Identifier 'oc_requesttoken' is not in camel case.
Loading history...
216
				};
217
				url = Gallery.utility.buildGalleryUrl(
218
					'files',
219
					'/download/' + this.fileId,
220
					params
221
				);
222
			}
223
224
			return url;
225
		},
226
227
		/**
228
		 * Call when the image is clicked on.
229
		 *
230
		 * @param event
231
		 * @private
232
		 */
233
		_openImage: function (event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
event does not seem to be used.
Loading history...
234
			// click function for future use.
235
		}
236
	};
237
238
	window.GalleryImage = GalleryImage;
239
})(jQuery, Gallery, oc_requesttoken);
0 ignored issues
show
Coding Style introduced by
Identifier 'oc_requesttoken' is not in camel case.
Loading history...
240