js/app.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 2

Size

Lines of Code 107
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 18
mnd 9
bc 9
fnc 9
bpm 1
cpm 2
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 OC, $, _, Gallery, SlideShow */
13
$(document).ready(function () {
14
	"use strict";
15
	Gallery.utility = new Gallery.Utility();
16
	Gallery.view = new Gallery.View();
17
	Gallery.token = Gallery.utility.getPublicToken();
18
	Gallery.ieVersion = Gallery.utility.getIeVersion();
19
	Gallery.filesClient = new OC.Files.Client({
20
		host: Gallery.utility.getWebdavHost(),
21
		port: OC.getPort(),
22
		root: Gallery.utility.getWebdavRoot(),
23
		useHTTPS: OC.getProtocol() === 'https'
24
	});
25
26
	// The first thing to do is to detect if we're on IE
27
	if (Gallery.ieVersion === 'unsupportedIe') {
28
		Gallery.utility.showIeWarning(Gallery.ieVersion);
29
		Gallery.view.showEmptyFolder('', null);
30
	} else {
31
		if (Gallery.ieVersion === 'oldIe') {
32
			Gallery.utility.showIeWarning(Gallery.ieVersion);
33
		}
34
35
		// Get the config, the files and initialise the slideshow
36
		Gallery.view.showLoading();
37
		$.getJSON(Gallery.utility.buildGalleryUrl('config', '', {}))
38
			.then(function (config) {
39
				Gallery.config = new Gallery.Config(config);
40
				var currentLocation = window.location.href.split('#')[1] || '';
41
				Gallery.activeSlideShow = new SlideShow();
42
				$.when(
43
					Gallery.activeSlideShow.init(
44
						false,
45
						null,
46
						Gallery.config.galleryFeatures
47
					))
48
					.then(function () {
49
						Gallery.getFiles(currentLocation).then(function () {
50
							window.onhashchange();
51
						});
52
					});
53
			});
54
55
		$(document).click(function () {
56
			$('.album-info-container').slideUp();
57
		});
58
59
		// This block loads new rows
60
		$(window).scroll(function () {
61
			Gallery.view.loadVisibleRows(Gallery.albumMap[Gallery.currentAlbum]);
62
		});
63
64
65
		var windowWidth = $(window).width();
66
		var windowHeight = $(window).height();
67
		$(window).resize(_.throttle(function () {
68
			var infoContentContainer = $('.album-info-container');
69
			// This section redraws the photowall and limits the width of dropdowns
70
			if (windowWidth !== $(window).width()) {
71
				if ($('#emptycontent').is(':hidden')) {
72
					Gallery.view.viewAlbum(Gallery.currentAlbum);
73
					infoContentContainer.css('max-width', $(window).width());
74
				}
75
				if (Gallery.currentAlbum) {
76
					Gallery.view.breadcrumb.setMaxWidth($(window).width() - Gallery.buttonsWidth);
77
				}
78
79
				windowWidth = $(window).width();
80
			}
81
			// This makes sure dropdowns will not be hidden after a window resize
82
			if (windowHeight !== $(window).height()) {
83
				infoContentContainer.css('max-height',
84
					$(window).height() - Gallery.browserToolbarHeight);
85
86
				windowHeight = $(window).height();
87
			}
88
		}, 250)); // A shorter delay avoids redrawing the view in the middle of a previous request,
89
				  // but it may kill baby CPUs
90
	}
91
});
92
93
/**
94
 * Responsible to refresh the view when we detect a change of location via the browser URL
95
 */
96
window.onhashchange = function () {
97
	"use strict";
98
	Gallery.view.dimControls();
99
	var currentLocation = window.location.href.split('#')[1] || '';
100
	// The hash location is ALWAYS encoded, despite what the browser shows
101
	var path = decodeURIComponent(currentLocation);
102
103
	// This section tries to determine if the hash location points to a file or a folder
104
	var albumPath = OC.dirname(path);
105
	if (Gallery.albumMap[path]) {
106
		albumPath = path;
107
	} else if (!Gallery.albumMap[albumPath]) {
108
		albumPath = '';
109
	}
110
	// We need to get new files if we've assessed that we've changed folder
111
	if (Gallery.currentAlbum !== null && Gallery.currentAlbum !== albumPath) {
112
		Gallery.getFiles(currentLocation).done(function () {
113
			Gallery.refresh(path, albumPath);
114
		});
115
	} else {
116
		// When the gallery is first loaded, the files have already been fetched
117
		Gallery.refresh(path, albumPath);
118
	}
119
};
120