aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/js/navigation.js
blob: cf4de7d61e53f8c2bb96ff1eb40af9638fe0a3fb (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (c) 2014
 *
 * @author Vincent Petry
 * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
 *
 * Edited by: Felix Nüsse <felix.nuesse@t-online.de> 2018
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

(function() {

	/**
	 * @class OCA.Files.Navigation
	 * @classdesc Navigation control for the files app sidebar.
	 *
	 * @param $el element containing the navigation
	 */
	var Navigation = function($el) {
		this.initialize($el);
	};

	/**
	 * @memberof OCA.Files
	 */
	Navigation.prototype = {

		/**
		 * Currently selected item in the list
		 */
		_activeItem: null,

		/**
		 * Currently selected container
		 */
		$currentContent: null,

		/**
		 * Strategy by which the quickaccesslist is sorted
		 */
		$sortingStrategy: 'alphabet',
		/**
		 * Initializes the navigation from the given container
		 *
		 * @private
		 * @param $el element containing the navigation
		 */
		initialize: function($el) {
			this.$el = $el;
			this._activeItem = null;
			this.$currentContent = null;
			this._setupEvents();
			this.initialSort();
		},

		/**
		 * Setup UI events
		 */
		_setupEvents: function() {
			this.$el.on('click', 'li a', _.bind(this._onClickItem, this))
			this.$el.on('click', 'li button', _.bind(this._onClickMenuButton, this));
			this.$el.on('click', 'li input', _.bind(this._onClickMenuItem, this));
		},

		/**
		 * Returns the container of the currently active app.
		 *
		 * @return app container
		 */
		getActiveContainer: function() {
			return this.$currentContent;
		},

		/**
		 * Returns the currently active item
		 * 
		 * @return item ID
		 */
		getActiveItem: function() {
			return this._activeItem;
		},

		/**
		 * Switch the currently selected item, mark it as selected and
		 * make the content container visible, if any.
		 *
		 * @param string itemId id of the navigation item to select
		 * @param array options "silent" to not trigger event
		 */
		setActiveItem: function(itemId, options) {
			var oldItemId = this._activeItem;
			if (itemId === this._activeItem) {
				if (!options || !options.silent) {
					this.$el.trigger(
						new $.Event('itemChanged', {itemId: itemId, previousItemId: oldItemId})
					);
				}
				return;
			}
			this.$el.find('li').removeClass('active');
			if (this.$currentContent) {
				this.$currentContent.addClass('hidden');
				this.$currentContent.trigger(jQuery.Event('hide'));
			}
			this._activeItem = itemId;
			this.$el.find('li[data-id=' + itemId + ']').addClass('active');
			this.$currentContent = $('#app-content-' + itemId);
			this.$currentContent.removeClass('hidden');
			if (!options || !options.silent) {
				this.$currentContent.trigger(jQuery.Event('show'));
				this.$el.trigger(
					new $.Event('itemChanged', {itemId: itemId, previousItemId: oldItemId})
				);
			}
		},

		/**
		 * Returns whether a given item exists
		 */
		itemExists: function(itemId) {
			return this.$el.find('li[data-id=' + itemId + ']').length;
		},

		/**
		 * Event handler for when clicking on an item.
		 */
		_onClickItem: function(ev) {
			var $target = $(ev.target);
			var itemId = $target.closest('li').attr('data-id');
			if (!_.isUndefined(itemId)) {
				this.setActiveItem(itemId);
			}
			ev.preventDefault();
		},
		/**
		 * Event handler for when clicking on an three-dot-menu.
		 */
		_onClickMenuButton: function(ev) {
			var $target = $(ev.target);
			var itemId = $target.closest('button').attr('id');
			if(itemId==='button-favorites'){
				document.getElementById('menu-favorites').classList.toggle('open');
			}
			ev.preventDefault();
		},

		/**
		 * Event handler for when clicking on a menuitem.
		 */
		_onClickMenuItem: function(ev) {

			var qaSelector= '#quickaccess-list';
			var qaKey= 'quickaccess-list';

			var itemId = $(ev.target).closest('input').attr('id');
			var list = document.getElementById(qaKey).getElementsByTagName('li');

			if(itemId==='enableQuickAccess'){
				$.get(OC.generateUrl("/apps/files/api/v1/showquickaccess"),  {show: document.getElementById('enableQuickAccess').checked}, function(data, status){
				});
				$(qaSelector ).toggle();
				document.getElementById('menu-favorites').classList.toggle('open');
			}

			if(itemId==='sortByAlphabet'){
				//Prevents deselecting Group-Item
				if(!document.getElementById('sortByAlphabet').checked){
					ev.preventDefault();
					return;
				}
				this.sortingStrategy='alphabet';
				document.getElementById('sortByDate').checked=false;
				$.get(OC.generateUrl("/apps/files/api/v1/setsortingstrategy"), {strategy: this.sortingStrategy}, function(data, status){});
				this.QuickSort(list, 0, list.length - 1);
				document.getElementById('menu-favorites').classList.toggle('open');
			}

			if(itemId==='sortByDate'){
				//Prevents deselecting Group-Item
				if(!document.getElementById('sortByDate').checked){
					ev.preventDefault();
					return;
				}
				this.sortingStrategy='date';
				document.getElementById('sortByAlphabet').checked=false;
				$.get(OC.generateUrl("/apps/files/api/v1/setsortingstrategy"), {strategy: this.sortingStrategy}, function(data, status){});
				this.QuickSort(list, 0, list.length - 1);
				document.getElementById('menu-favorites').classList.toggle('open');
			}

			if(itemId==='enableReverse'){
				this.reverse(list);
				var state = document.getElementById('enableReverse').checked;
				$.get(OC.generateUrl("/apps/files/api/v1/setreversequickaccess"), {reverse: state}, function(data, status){});
				document.getElementById('menu-favorites').classList.toggle('open');
			}
			//ev.preventDefault();
		},

		/**
		 * Sort initially as setup of sidebar for QuickAccess
		 */
		initialSort: function() {

			var domRevState=document.getElementById('enableReverse').checked;
			var domSortAlphabetState=document.getElementById('sortByAlphabet').checked;
			var domSortDateState=document.getElementById('sortByDate').checked;

			var qaKey= 'quickaccess-list';
			var list = document.getElementById(qaKey).getElementsByTagName('li');

			if(domSortAlphabetState){
				this.sortingStrategy='alphabet';
			}
			if(domSortDateState){
				this.sortingStrategy='date';
			}

			this.QuickSort(list, 0, list.length - 1);

			if(domRevState){
				this.reverse(list);
			}

			/*This creates flashes the UI, which is bad userexperience. It is the cleaner way to do it, that is why i haven't deleted it yet.
			var scope=this;
			$.get(OC.generateUrl("/apps/files/api/v1/getsortingstrategy"), function(data, status){
				scope.sortingStrategy=data;
				scope.QuickSort(list, 0, list.length - 1);

			});

			$.get(OC.generateUrl("/apps/files/api/v1/getreversequickaccess"), function(data, status){
				if(data){
					scope.reverse(list);
				}
			});
			*/
		},

		/**
		 * Sorting-Algorithm for QuickAccess
		 */
		QuickSort: function(list, start, end) {
			var lastmatch;
			if(list.length > 1){
				lastmatch = this.quicksort_helper(list, start, end);
				if(start < lastmatch - 1){
					this.QuickSort(list, start, lastmatch - 1);
				}
				if(lastmatch < end){
					this.QuickSort(list, lastmatch, end);
				}
			}
		},

		/**
		 * Sorting-Algorithm-Helper for QuickAccess
		 */
		quicksort_helper: function(list, start, end) {
			var pivot = Math.floor((end + start) / 2);
			var pivotelem = this.getCompareValue(list,pivot);
			var	i = start;
			var	j = end;

			while(i <= j){
				while(this.getCompareValue(list,i) < pivotelem){
					i++;
				}
				while(this.getCompareValue(list,j) > pivotelem){
					j--;
				}
				if(i <= j){
					this.swap(list, i, j);
					i++;
					j--;
				}
			}
			return i;
		},

		/**
		 * Sorting-Algorithm-Helper for QuickAccess
		 * This method allows easy access to the element which is sorted by.
		 */
		getCompareValue: function(nodes, int){
			if(this.sortingStrategy==='alphabet'){
			;	return nodes[int].getElementsByTagName('a')[0].innerHTML.toLowerCase()
			}else if(this.sortingStrategy==='date'){
				return nodes[int].getAttribute('folderPos').toLowerCase();
			}
			return nodes[int].getElementsByTagName('a')[0].innerHTML.toLowerCase();
		},

		/**
		 * Sorting-Algorithm-Helper for QuickAccess
		 * This method allows easy swapping of elements.
		 */
		swap: function(list, j, i){
			list[i].before(list[j]);
			list[j].before(list[i]);
		},

		/**
		 * Reverse QuickAccess-List
		 */
		reverse: function(list){
			var len=list.length-1;
			for(var i = 0; i < len/2; i++) {
				this.swap(list, i, len-i);
			}
		}

	};

	OCA.Files.Navigation = Navigation;

})();