js/galleryconfig.js   A
last analyzed

Complexity

Total Complexity 27
Complexity/F 2.25

Size

Lines of Code 279
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 279
rs 10
c 0
b 0
f 0
wmc 27
mnd 15
bc 15
fnc 12
bpm 1.25
cpm 2.25
noi 2
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 Gallery */
13
(function ($, Gallery) {
14
	"use strict";
15
	/**
16
	 * Stores the gallery configuration
17
	 *
18
	 * @param {{features: string[], mediatypes: string[]}} config
19
	 * @constructor
20
	 */
21
	var Config = function (config) {
22
		this.galleryFeatures = this._setGalleryFeatures(config.features);
23
		this.mediaTypes = this._setMediaTypes(config.mediatypes);
24
	};
25
26
	Config.prototype = {
27
		galleryFeatures: [],
28
		cachedFeaturesString: '',
29
		mediaTypes: [],
30
		cachedMediaTypesString: '',
31
		albumInfo: null,
32
		albumSorting: null,
33
		albumDesign: null,
34
		albumError: false,
35
		infoLoaded: false,
36
37
		/**
38
		 * Returns the list of supported features in a string
39
		 *
40
		 * @returns {string}
41
		 */
42
		getFeatures: function () {
43
			return this.cachedFeaturesString;
44
		},
45
46
		/**
47
		 * Returns the list of supported media types in a string
48
		 *
49
		 * @returns {string}
50
		 */
51
		getMediaTypes: function () {
52
			return this.cachedMediaTypesString;
53
		},
54
55
		/**
56
		 * Stores the configuration about the current album
57
		 *
58
		 * @param {{
59
		 * 	design,
60
		 * 	information,
61
		 * 	sorting,
62
		 * 	error: string
63
		 * }} albumConfig
64
		 * @param albumPath
65
		 */
66
		setAlbumConfig: function (albumConfig, albumPath) {
67
			this.albumInfo = this._setAlbumInfo(albumConfig, albumPath);
68
			this.albumSorting = this._setAlbumSorting(albumConfig);
69
			this.albumDesign = this._setAlbumDesign(albumConfig);
70
			this.albumError = albumConfig.error;
71
		},
72
73
		/**
74
		 * Updates the sorting order
75
		 */
76
		updateAlbumSorting: function (sortConfig) {
77
			this.albumSorting = {
78
				type: sortConfig.type,
79
				order: sortConfig.order,
80
				albumOrder: sortConfig.albumOrder
81
			};
82
		},
83
84
		/**
85
		 * Saves the list of features which have been enabled in the app
86
		 *
87
		 * @param {string[]} configFeatures
88
		 *
89
		 * @returns {Array}
90
		 * @private
91
		 */
92
		_setGalleryFeatures: function (configFeatures) {
93
			var features = [];
94
			var feature = null;
0 ignored issues
show
Unused Code introduced by
The assignment to feature seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
95
			var i, configFeaturesLength = configFeatures.length;
96
			if (configFeaturesLength) {
97
				for (i = 0; i < configFeaturesLength; i++) {
98
					feature = configFeatures[i];
99
					if (this._worksInCurrentBrowser(feature, 'native_svg')) {
100
						features.push(feature);
101
						Gallery.utility.addDomPurifyHooks();
102
					}
103
				}
104
			}
105
			this.cachedFeaturesString = features.join(';');
106
107
			return features;
108
		},
109
110
		/**
111
		 * Saves the list of supported media types
112
		 *
113
		 * @param {string[]} mediaTypes
114
		 *
115
		 * @returns {Array}
116
		 * @private
117
		 */
118
		_setMediaTypes: function (mediaTypes) {
119
			var supportedMediaTypes = [];
120
			var mediaType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to mediaType seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
121
			var i, mediaTypesLength = mediaTypes.length;
122
			if (mediaTypesLength) {
123
				for (i = 0; i < mediaTypesLength; i++) {
124
					mediaType = mediaTypes[i];
125
					if (this._worksInCurrentBrowser(mediaType, 'image/svg+xml')) {
126
						supportedMediaTypes.push(mediaType);
127
					}
128
				}
129
			}
130
			this.cachedMediaTypesString = supportedMediaTypes.join(';');
131
132
			return supportedMediaTypes;
133
		},
134
135
		/**
136
		 * Determines if we can accept a specific config element in Internet Explorer
137
		 *
138
		 * @param {string} feature
139
		 * @param {string} validationRule
140
		 *
141
		 * @returns {boolean}
142
		 * @private
143
		 */
144
		_worksInCurrentBrowser: function (feature, validationRule) {
145
			var isAcceptable = true;
146
			if (feature === validationRule &&
147
				(Gallery.ieVersion !== false && Gallery.ieVersion !== 'edge')) {
148
				isAcceptable = false;
149
			}
150
151
			return isAcceptable;
152
		},
153
154
		/**
155
		 * Saves the description and copyright information for the current album
156
		 *
157
		 * @param {{
158
		 * 	design,
159
		 * 	information,
160
		 * 	sorting,
161
		 * 	error: string
162
		 * }} albumConfig
163
		 * @param albumPath
164
		 *
165
		 * @returns {null||{
166
		 * 	description: string,
167
		 * 	descriptionLink: string,
168
		 * 	copyright: string,
169
		 * 	copyrightLink: string,
170
		 * 	filePath: string,
171
		 * 	inherit: bool,
172
		 * 	level: number
173
		 * }}
174
		 * @private
175
		 */
176
		_setAlbumInfo: function (albumConfig, albumPath) {
177
			/**@type {{
178
			 * 	description: string,
179
			 * 	description_link: string,
180
			 * 	copyright: string,
181
			 * 	copyright_link: string,
182
			 * 	inherit: bool,
183
			 * 	level: number
184
			 * }}
185
			 */
186
			var albumInfo = albumConfig.information;
187
			var params = null;
188
			if (!$.isEmptyObject(albumInfo)) {
189
				var docPath = albumPath;
190
				var level = albumInfo.level;
191
				if (level > 0) {
192
					if (docPath.indexOf('/') !== -1) {
193
						var folders = docPath.split('/');
194
						folders = folders.slice(-0, -level);
195
						docPath = folders.join('/') + '/';
196
					} else {
197
						docPath = '';
198
					}
199
				}
200
201
				/* jshint camelcase: false */
202
				params = {
203
					description: albumInfo.description,
204
					descriptionLink: albumInfo.description_link,
205
					copyright: albumInfo.copyright,
206
					copyrightLink: albumInfo.copyright_link,
207
					filePath: docPath,
208
					inherit: albumInfo.inherit,
209
					level: level
210
				};
211
			}
212
213
			return params;
214
		},
215
216
		/**
217
		 * Saves the description and copyright information for the current album
218
		 *
219
		 * @param {{
220
		 * 	design,
221
		 * 	information,
222
		 * 	sorting,
223
		 * 	error: string
224
		 * }} albumConfig
225
		 *
226
		 * @returns {null||{
227
		 * 	background: string,
228
		 * 	inherit: bool,
229
		 * 	level: number
230
		 * }}
231
		 * @private
232
		 */
233
		_setAlbumDesign: function (albumConfig) {
234
			/**@type {{
235
			 * 	background: string,
236
			 * 	inherit: bool,
237
			 * 	level: number
238
			 * }}
239
			 */
240
			var albumDesign = albumConfig.design;
241
			var params = null;
242
			if (!$.isEmptyObject(albumDesign)) {
243
				params = {
244
					background: albumDesign.background,
245
					inherit: albumDesign.inherit,
246
					level: albumDesign.level
247
				};
248
			}
249
250
			return params;
251
		},
252
253
		/**
254
		 * Saves the sorting configuration for the current album
255
		 *
256
		 * @param {{
257
		 * 	design,
258
		 * 	information,
259
		 * 	sorting,
260
		 * 	error: string
261
		 * }} albumConfig
262
		 *
263
		 * @returns {{type: string, order: string, albumOrder: string}}
264
		 * @private
265
		 */
266
		_setAlbumSorting: function (albumConfig) {
267
			var sortType = 'name';
268
			var sortOrder = 'asc';
269
			var albumSortOrder = 'asc';
270
			if (!$.isEmptyObject(albumConfig.sorting)) {
271
				if (!$.isEmptyObject(albumConfig.sorting.type)) {
272
					sortType = albumConfig.sorting.type;
273
				}
274
				if (!$.isEmptyObject(albumConfig.sorting.order)) {
275
					sortOrder = albumConfig.sorting.order;
276
					if (sortType === 'name') {
277
						albumSortOrder = sortOrder;
278
					}
279
				}
280
			}
281
282
			return {
283
				type: sortType,
284
				order: sortOrder,
285
				albumOrder: albumSortOrder
286
			};
287
		}
288
	};
289
290
	Gallery.Config = Config;
291
})(jQuery, Gallery);
292