js/galleryimage.js   A
last analyzed

Complexity

Total Complexity 19
Complexity/F 1.46

Size

Lines of Code 187
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 79
dl 0
loc 187
rs 10
c 0
b 0
f 0
wmc 19
mnd 6
bc 6
fnc 13
bpm 0.4615
cpm 1.4615
noi 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) {
14
	"use strict";
15
16
	/**
17
	 * Creates a new image object to store information about a media file
18
	 *
19
	 * @param {string} src
20
	 * @param {string} path
21
	 * @param {number} fileId
22
	 * @param {string} mimeType
23
	 * @param {number} mTime modification time
24
	 * @param {string} etag
25
	 * @param {number} size
26
	 * @param {boolean} sharedWithUser
27
	 * @param {string} owner
28
	 * @param {number} permissions
29
 * @constructor
30
	 */
31
	var GalleryImage = function (src, path, fileId, mimeType, mTime, etag, size, sharedWithUser,
32
								 owner, permissions) {
33
		this.src = src;
34
		this.path = path;
35
		this.fileId = fileId;
36
		this.mimeType = mimeType;
37
		this.mTime = mTime;
38
		this.etag = etag;
39
		this.size = size;
40
		this.sharedWithUser = sharedWithUser;
41
		this.owner = owner;
42
		this.permissions = permissions;
43
		this.thumbnail = null;
44
		this.domDef = null;
45
		this.spinner = null;
46
	};
47
48
	GalleryImage.prototype = {
49
		/**
50
		 * Returns the Thumbnail ID
51
		 *
52
		 * @returns {[number]}
53
		 */
54
		getThumbnailIds: function () {
55
			return [this.fileId];
56
		},
57
58
		/**
59
		 * Returns a reference to a loading Thumbnail.image
60
		 *
61
		 * @param {boolean} square
62
		 *
63
		 * @returns {jQuery.Deferred<Thumbnail.image>}
64
		 */
65
		getThumbnail: function (square) {
66
			if (this.thumbnail === null) {
67
				this.thumbnail = Thumbnails.get(this.fileId, square);
68
			}
69
			return this.thumbnail.loadingDeferred;
70
		},
71
72
		/**
73
		 * Returns the width of a thumbnail
74
		 *
75
		 * Used to calculate the width of the row as we add more images to it
76
		 *
77
		 * @returns {number}
78
		 */
79
		getThumbnailWidth: function (targetHeight) {
80
			var image = this;
81
			// img is a Thumbnail.image
82
			return this.getThumbnail(false).then(function (img) {
83
				var width = 0;
84
				if (img) {
85
					// In Firefox, you don't get the size of a SVG before it's added to the DOM
86
					image.domDef.children('.image').append(img);
87
					if (image.mimeType === 'image/svg+xml') {
88
						image.thumbnail.ratio = img.width / img.height;
89
					}
90
					width = Math.round(targetHeight * image.thumbnail.ratio);
91
				}
92
93
				return width;
94
			});
95
		},
96
97
		/**
98
		 * Creates the container, the a and img elements in the DOM
99
		 *
100
		 * Each image is also a link to start the full screen slideshow
101
		 *
102
		 * @param {number} targetHeight
103
		 *
104
		 * @return {a}
105
		 */
106
		getDom: function (targetHeight) {
107
			if (this.domDef === null) {
108
				var imageElement = Gallery.Templates.galleryimage({
109
					targetHeight: targetHeight,
110
					targetWidth: targetHeight,
111
					label: OC.basename(this.path),
112
					path: this.path
113
				});
114
				this.domDef = $(imageElement);
115
				this._addLabel();
116
				this.spinner = this.domDef.children('.image-loader');
117
			}
118
			return this.domDef;
119
		},
120
121
		/**
122
		 * Resizes the image once it has been loaded
123
		 *
124
		 * @param {Number} targetHeight
125
		 * @param {Number} newWidth
126
		 */
127
		resize: function (targetHeight, newWidth) {
128
			if (this.spinner !== null) {
129
				var img = this.thumbnail.image;
130
				this.spinner.remove();
131
				this.spinner = null;
132
				this.domDef.attr('data-width', newWidth)
133
					.attr('data-height', targetHeight);
134
135
				var url = this._getLink();
136
				this.domDef.attr('href', url);
137
138
				// This will stretch wide images to make them reach targetHeight
139
				$(img).css({
140
					'width': newWidth,
141
					'height': targetHeight
142
				});
143
				img.alt = encodeURI(this.path);
144
145
				this.domDef.click(this._openImage.bind(this));
146
			}
147
		},
148
149
		/**
150
		 * Adds a label to the album
151
		 *
152
		 * @private
153
		 */
154
		_addLabel: function () {
155
			var imageLabel = this.domDef.children('.image-label');
156
			this.domDef.hover(function () {
157
				imageLabel.slideToggle(OC.menuSpeed);
158
			}, function () {
159
				imageLabel.slideToggle(OC.menuSpeed);
160
			});
161
		},
162
163
		/**
164
		 * Generates the link for the click action of the image
165
		 *
166
		 * @returns {string}
167
		 * @private
168
		 */
169
		_getLink: function () {
170
			var url = '#' + encodeURIComponent(this.path);
171
			if (!this.thumbnail.valid) {
172
				var params = {
173
					c: this.etag,
174
					requesttoken: oc_requesttoken
175
				};
176
				url = Gallery.utility.buildGalleryUrl(
177
					'files',
178
					'/download/' + this.fileId,
179
					params
180
				);
181
			}
182
183
			return url;
184
		},
185
186
		/**
187
		 * Call when the image is clicked on.
188
		 *
189
		 * @param event
190
		 * @private
191
		 */
192
		_openImage: function (event) {
193
			event.stopPropagation();
194
			// click function for future use.
195
		}
196
	};
197
198
	window.GalleryImage = GalleryImage;
199
})(jQuery, Gallery, oc_requesttoken);
200