Blame view

sources/apps/files/js/files.js 24.2 KB
03e52840d   Kload   Init
1
  Files={
31b7f2792   Kload   Upgrade to ownclo...
2
3
4
5
6
7
8
9
  	// file space size sync
  	_updateStorageStatistics: function() {
  		Files._updateStorageStatisticsTimeout = null;
  		var currentDir = FileList.getCurrentDirectory(),
  			state = Files.updateStorageStatistics;
  		if (state.dir){
  			if (state.dir === currentDir) {
  				return;
03e52840d   Kload   Init
10
  			}
31b7f2792   Kload   Upgrade to ownclo...
11
12
13
14
15
16
17
18
  			// cancel previous call, as it was for another dir
  			state.call.abort();
  		}
  		state.dir = currentDir;
  		state.call = $.getJSON(OC.filePath('files','ajax','getstoragestats.php') + '?dir=' + encodeURIComponent(currentDir),function(response) {
  			state.dir = null;
  			state.call = null;
  			Files.updateMaxUploadFilesize(response);
03e52840d   Kload   Init
19
  		});
03e52840d   Kload   Init
20
  	},
31b7f2792   Kload   Upgrade to ownclo...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  	updateStorageStatistics: function(force) {
  		if (!OC.currentUser) {
  			return;
  		}
  
  		// debounce to prevent calling too often
  		if (Files._updateStorageStatisticsTimeout) {
  			clearTimeout(Files._updateStorageStatisticsTimeout);
  		}
  		if (force) {
  			Files._updateStorageStatistics();
  		}
  		else {
  			Files._updateStorageStatisticsTimeout = setTimeout(Files._updateStorageStatistics, 250);
  		}
  	},
03e52840d   Kload   Init
37
  	updateMaxUploadFilesize:function(response) {
31b7f2792   Kload   Upgrade to ownclo...
38
  		if (response === undefined) {
03e52840d   Kload   Init
39
40
  			return;
  		}
31b7f2792   Kload   Upgrade to ownclo...
41
  		if (response.data !== undefined && response.data.uploadMaxFilesize !== undefined) {
03e52840d   Kload   Init
42
43
44
45
46
  			$('#max_upload').val(response.data.uploadMaxFilesize);
  			$('#upload.button').attr('original-title', response.data.maxHumanFilesize);
  			$('#usedSpacePercent').val(response.data.usedSpacePercent);
  			Files.displayStorageWarnings();
  		}
31b7f2792   Kload   Upgrade to ownclo...
47
  		if (response[0] === undefined) {
03e52840d   Kload   Init
48
49
  			return;
  		}
31b7f2792   Kload   Upgrade to ownclo...
50
  		if (response[0].uploadMaxFilesize !== undefined) {
03e52840d   Kload   Init
51
52
53
54
55
56
57
  			$('#max_upload').val(response[0].uploadMaxFilesize);
  			$('#upload.button').attr('original-title', response[0].maxHumanFilesize);
  			$('#usedSpacePercent').val(response[0].usedSpacePercent);
  			Files.displayStorageWarnings();
  		}
  
  	},
31b7f2792   Kload   Upgrade to ownclo...
58
59
60
61
62
63
64
65
66
67
  
  	/**
  	 * Fix path name by removing double slash at the beginning, if any
  	 */
  	fixPath: function(fileName) {
  		if (fileName.substr(0, 2) == '//') {
  			return fileName.substr(1);
  		}
  		return fileName;
  	},
837968727   Kload   [enh] Upgrade to ...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  	/**
  	 * Checks whether the given file name is valid.
  	 * @param name file name to check
  	 * @return true if the file name is valid.
  	 * Throws a string exception with an error message if
  	 * the file name is not valid
  	 */
  	isFileNameValid: function (name, root) {
  		var trimmedName = name.trim();
  		if (trimmedName === '.'
  				|| trimmedName === '..'
  				|| (root === '/' &&  trimmedName.toLowerCase() === 'shared'))
  		{
  			throw t('files', '"{name}" is an invalid file name.', {name: name});
  		} else if (trimmedName.length === 0) {
31b7f2792   Kload   Upgrade to ownclo...
83
  			throw t('files', 'File name cannot be empty.');
03e52840d   Kload   Init
84
85
86
87
88
  		}
  
  		// check for invalid characters
  		var invalid_characters = ['\\', '/', '<', '>', ':', '"', '|', '?', '*'];
  		for (var i = 0; i < invalid_characters.length; i++) {
31b7f2792   Kload   Upgrade to ownclo...
89
90
  			if (name.indexOf(invalid_characters[i]) !== -1) {
  				throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.");
03e52840d   Kload   Init
91
92
  			}
  		}
03e52840d   Kload   Init
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  		return true;
  	},
  	displayStorageWarnings: function() {
  		if (!OC.Notification.isHidden()) {
  			return;
  		}
  
  		var usedSpacePercent = $('#usedSpacePercent').val();
  		if (usedSpacePercent > 98) {
  			OC.Notification.show(t('files', 'Your storage is full, files can not be updated or synced anymore!'));
  			return;
  		}
  		if (usedSpacePercent > 90) {
  			OC.Notification.show(t('files', 'Your storage is almost full ({usedSpacePercent}%)', {usedSpacePercent: usedSpacePercent}));
  		}
31b7f2792   Kload   Upgrade to ownclo...
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
  	},
  
  	displayEncryptionWarning: function() {
  
  		if (!OC.Notification.isHidden()) {
  			return;
  		}
  
  		var encryptedFiles = $('#encryptedFiles').val();
  		var initStatus = $('#encryptionInitStatus').val();
  		if (initStatus === '0') { // enc not initialized, but should be
  			OC.Notification.show(t('files_encryption', 'Encryption App is enabled but your keys are not initialized, please log-out and log-in again'));
  			return;
  		}
  		if (initStatus === '1') { // encryption tried to init but failed
  			OC.Notification.showHtml(t('files_encryption', 'Invalid private key for Encryption App. Please update your private key password in your personal settings to recover access to your encrypted files.'));
  			return;
  		}
  		if (encryptedFiles === '1') {
  			OC.Notification.show(t('files_encryption', 'Encryption was disabled but your files are still encrypted. Please go to your personal settings to decrypt your files.'));
  			return;
  		}
  	},
  
  	setupDragAndDrop: function() {
  		var $fileList = $('#fileList');
  
  		//drag/drop of files
  		$fileList.find('tr td.filename').each(function(i,e) {
  			if ($(e).parent().data('permissions') & OC.PERMISSION_DELETE) {
  				$(e).draggable(dragOptions);
  			}
  		});
  
  		$fileList.find('tr[data-type="dir"] td.filename').each(function(i,e) {
  			if ($(e).parent().data('permissions') & OC.PERMISSION_CREATE) {
  				$(e).droppable(folderDropOptions);
  			}
  		});
  	},
  
  	lastWidth: 0,
  
  	initBreadCrumbs: function () {
  		var $controls = $('#controls');
  
  		Files.lastWidth = 0;
  		Files.breadcrumbs = [];
  
  		// initialize with some extra space
  		Files.breadcrumbsWidth = 64;
  		if ( document.getElementById("navigation") ) {
  			Files.breadcrumbsWidth += $('#navigation').get(0).offsetWidth;
  		}
  		Files.hiddenBreadcrumbs = 0;
  
  		$.each($('.crumb'), function(index, breadcrumb) {
  			Files.breadcrumbs[index] = breadcrumb;
  			Files.breadcrumbsWidth += $(breadcrumb).get(0).offsetWidth;
  		});
  
  		$.each($('#controls .actions>div'), function(index, action) {
  			Files.breadcrumbsWidth += $(action).get(0).offsetWidth;
  		});
  
  		// event handlers for breadcrumb items
  		$controls.find('.crumb a').on('click', onClickBreadcrumb);
  
  		// setup drag and drop
  		$controls.find('.crumb:not(.last)').droppable(crumbDropOptions);
  	},
  
  	resizeBreadcrumbs: function (width, firstRun) {
  		if (width !== Files.lastWidth) {
  			if ((width < Files.lastWidth || firstRun) && width < Files.breadcrumbsWidth) {
  				if (Files.hiddenBreadcrumbs === 0) {
  					Files.breadcrumbsWidth -= $(Files.breadcrumbs[1]).get(0).offsetWidth;
  					$(Files.breadcrumbs[1]).find('a').hide();
  					$(Files.breadcrumbs[1]).append('<span>...</span>');
  					Files.breadcrumbsWidth += $(Files.breadcrumbs[1]).get(0).offsetWidth;
  					Files.hiddenBreadcrumbs = 2;
  				}
  				var i = Files.hiddenBreadcrumbs;
  				while (width < Files.breadcrumbsWidth && i > 1 && i < Files.breadcrumbs.length - 1) {
  					Files.breadcrumbsWidth -= $(Files.breadcrumbs[i]).get(0).offsetWidth;
  					$(Files.breadcrumbs[i]).hide();
  					Files.hiddenBreadcrumbs = i;
  					i++;
  				}
  			} else if (width > Files.lastWidth && Files.hiddenBreadcrumbs > 0) {
  				var i = Files.hiddenBreadcrumbs;
  				while (width > Files.breadcrumbsWidth && i > 0) {
  					if (Files.hiddenBreadcrumbs === 1) {
  						Files.breadcrumbsWidth -= $(Files.breadcrumbs[1]).get(0).offsetWidth;
  						$(Files.breadcrumbs[1]).find('span').remove();
  						$(Files.breadcrumbs[1]).find('a').show();
  						Files.breadcrumbsWidth += $(Files.breadcrumbs[1]).get(0).offsetWidth;
  					} else {
  						$(Files.breadcrumbs[i]).show();
  						Files.breadcrumbsWidth += $(Files.breadcrumbs[i]).get(0).offsetWidth;
  						if (Files.breadcrumbsWidth > width) {
  							Files.breadcrumbsWidth -= $(Files.breadcrumbs[i]).get(0).offsetWidth;
  							$(Files.breadcrumbs[i]).hide();
  							break;
  						}
  					}
  					i--;
  					Files.hiddenBreadcrumbs = i;
  				}
  			}
  			Files.lastWidth = width;
  		}
03e52840d   Kload   Init
220
221
222
  	}
  };
  $(document).ready(function() {
31b7f2792   Kload   Upgrade to ownclo...
223
224
225
226
227
  	// FIXME: workaround for trashbin app
  	if (window.trashBinApp) {
  		return;
  	}
  	Files.displayEncryptionWarning();
03e52840d   Kload   Init
228
  	Files.bindKeyboardShortcuts(document, jQuery);
31b7f2792   Kload   Upgrade to ownclo...
229
230
231
  
  	FileList.postProcessList();
  	Files.setupDragAndDrop();
03e52840d   Kload   Init
232
233
  
  	$('#file_action_panel').attr('activeAction', false);
31b7f2792   Kload   Upgrade to ownclo...
234
235
  	// allow dropping on the "files" app icon
  	$('ul#apps li:first-child').data('dir','').droppable(crumbDropOptions);
03e52840d   Kload   Init
236
237
238
239
240
241
242
243
244
  
  	// Triggers invisible file input
  	$('#upload a').on('click', function() {
  		$(this).parent().children('#file_upload_start').trigger('click');
  		return false;
  	});
  
  	// Trigger cancelling of file upload
  	$('#uploadprogresswrapper .stop').on('click', function() {
31b7f2792   Kload   Upgrade to ownclo...
245
246
  		OC.Upload.cancelUploads();
  		procesSelection();
03e52840d   Kload   Init
247
248
249
  	});
  
  	// Show trash bin
31b7f2792   Kload   Upgrade to ownclo...
250
  	$('#trash').on('click', function() {
03e52840d   Kload   Init
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  		window.location=OC.filePath('files_trashbin', '', 'index.php');
  	});
  
  	var lastChecked;
  
  	// Sets the file link behaviour :
  	$('#fileList').on('click','td.filename a',function(event) {
  		if (event.ctrlKey || event.shiftKey) {
  			event.preventDefault();
  			if (event.shiftKey) {
  				var last = $(lastChecked).parent().parent().prevAll().length;
  				var first = $(this).parent().parent().prevAll().length;
  				var start = Math.min(first, last);
  				var end = Math.max(first, last);
  				var rows = $(this).parent().parent().parent().children('tr');
  				for (var i = start; i < end; i++) {
  					$(rows).each(function(index) {
31b7f2792   Kload   Upgrade to ownclo...
268
  						if (index === i) {
03e52840d   Kload   Init
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  							var checkbox = $(this).children().children('input:checkbox');
  							$(checkbox).attr('checked', 'checked');
  							$(checkbox).parent().parent().addClass('selected');
  						}
  					});
  				}
  			}
  			var checkbox = $(this).parent().children('input:checkbox');
  			lastChecked = checkbox;
  			if ($(checkbox).attr('checked')) {
  				$(checkbox).removeAttr('checked');
  				$(checkbox).parent().parent().removeClass('selected');
  				$('#select_all').removeAttr('checked');
  			} else {
  				$(checkbox).attr('checked', 'checked');
  				$(checkbox).parent().parent().toggleClass('selected');
31b7f2792   Kload   Upgrade to ownclo...
285
286
  				var selectedCount = $('td.filename input:checkbox:checked').length;
  				if (selectedCount === $('td.filename input:checkbox').length) {
03e52840d   Kload   Init
287
288
289
290
291
292
  					$('#select_all').attr('checked', 'checked');
  				}
  			}
  			procesSelection();
  		} else {
  			var filename=$(this).parent().parent().attr('data-file');
a293d369c   Kload   Update sources to...
293
  			var tr = FileList.findFileEl(filename);
03e52840d   Kload   Init
294
  			var renaming=tr.data('renaming');
31b7f2792   Kload   Upgrade to ownclo...
295
  			if (!renaming && !FileList.isLoading(filename)) {
03e52840d   Kload   Init
296
297
298
299
300
  				FileActions.currentFile = $(this).parent();
  				var mime=FileActions.getCurrentMimeType();
  				var type=FileActions.getCurrentType();
  				var permissions = FileActions.getCurrentPermissions();
  				var action=FileActions.getDefault(mime,type, permissions);
31b7f2792   Kload   Upgrade to ownclo...
301
  				if (action) {
03e52840d   Kload   Init
302
303
304
305
306
307
308
309
310
311
  					event.preventDefault();
  					action(filename);
  				}
  			}
  		}
  
  	});
  
  	// Sets the select_all checkbox behaviour :
  	$('#select_all').click(function() {
31b7f2792   Kload   Upgrade to ownclo...
312
  		if ($(this).attr('checked')) {
03e52840d   Kload   Init
313
314
315
  			// Check all
  			$('td.filename input:checkbox').attr('checked', true);
  			$('td.filename input:checkbox').parent().parent().addClass('selected');
31b7f2792   Kload   Upgrade to ownclo...
316
  		} else {
03e52840d   Kload   Init
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  			// Uncheck all
  			$('td.filename input:checkbox').attr('checked', false);
  			$('td.filename input:checkbox').parent().parent().removeClass('selected');
  		}
  		procesSelection();
  	});
  
  	$('#fileList').on('change', 'td.filename input:checkbox',function(event) {
  		if (event.shiftKey) {
  			var last = $(lastChecked).parent().parent().prevAll().length;
  			var first = $(this).parent().parent().prevAll().length;
  			var start = Math.min(first, last);
  			var end = Math.max(first, last);
  			var rows = $(this).parent().parent().parent().children('tr');
  			for (var i = start; i < end; i++) {
  				$(rows).each(function(index) {
31b7f2792   Kload   Upgrade to ownclo...
333
  					if (index === i) {
03e52840d   Kload   Init
334
335
336
337
338
339
340
341
342
  						var checkbox = $(this).children().children('input:checkbox');
  						$(checkbox).attr('checked', 'checked');
  						$(checkbox).parent().parent().addClass('selected');
  					}
  				});
  			}
  		}
  		var selectedCount=$('td.filename input:checkbox:checked').length;
  		$(this).parent().parent().toggleClass('selected');
31b7f2792   Kload   Upgrade to ownclo...
343
  		if (!$(this).attr('checked')) {
03e52840d   Kload   Init
344
  			$('#select_all').attr('checked',false);
31b7f2792   Kload   Upgrade to ownclo...
345
346
  		} else {
  			if (selectedCount===$('td.filename input:checkbox').length) {
03e52840d   Kload   Init
347
348
349
350
351
352
353
  				$('#select_all').attr('checked',true);
  			}
  		}
  		procesSelection();
  	});
  
  	$('.download').click('click',function(event) {
31b7f2792   Kload   Upgrade to ownclo...
354
  		var files=getSelectedFilesTrash('name');
03e52840d   Kload   Init
355
356
357
358
  		var fileslist = JSON.stringify(files);
  		var dir=$('#dir').val()||'/';
  		OC.Notification.show(t('files','Your download is being prepared. This might take some time if the files are big.'));
  		// use special download URL if provided, e.g. for public shared files
31b7f2792   Kload   Upgrade to ownclo...
359
360
  		var downloadURL = document.getElementById("downloadURL");
  		if ( downloadURL ) {
837968727   Kload   [enh] Upgrade to ...
361
362
363
364
365
366
  			// downloading all in root of public share ? (replacement for old "Download" button)
  			if ($('#isPublic').val() && dir === '/' && $('#select_all').is(':checked')) {
  				window.location = downloadURL.value;
  			} else {
  				window.location = downloadURL.value+"&download&files=" + encodeURIComponent(fileslist);
  			}
03e52840d   Kload   Init
367
  		} else {
31b7f2792   Kload   Upgrade to ownclo...
368
  			window.location = OC.filePath('files', 'ajax', 'download.php') + '?'+ $.param({ dir: dir, files: fileslist });
03e52840d   Kload   Init
369
370
371
372
373
  		}
  		return false;
  	});
  
  	$('.delete-selected').click(function(event) {
31b7f2792   Kload   Upgrade to ownclo...
374
  		var files=getSelectedFilesTrash('name');
03e52840d   Kload   Init
375
376
377
378
379
380
381
382
383
384
  		event.preventDefault();
  		FileList.do_delete(files);
  		return false;
  	});
  
  	// drag&drop support using jquery.fileupload
  	// TODO use OC.dialogs
  	$(document).bind('drop dragover', function (e) {
  			e.preventDefault(); // prevent browser from doing anything, if file isn't dropped in dropZone
  	});
03e52840d   Kload   Init
385
386
  	//do a background scan if needed
  	scanFiles();
31b7f2792   Kload   Upgrade to ownclo...
387
  	Files.initBreadCrumbs();
03e52840d   Kload   Init
388
389
  
  	$(window).resize(function() {
31b7f2792   Kload   Upgrade to ownclo...
390
391
  		var width = $(this).width();
  		Files.resizeBreadcrumbs(width, false);
03e52840d   Kload   Init
392
  	});
31b7f2792   Kload   Upgrade to ownclo...
393
394
  	var width = $(this).width();
  	Files.resizeBreadcrumbs(width, true);
03e52840d   Kload   Init
395
396
397
398
  
  	// display storage warnings
  	setTimeout ( "Files.displayStorageWarnings()", 100 );
  	OC.Notification.setDefault(Files.displayStorageWarnings);
31b7f2792   Kload   Upgrade to ownclo...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  	// only possible at the moment if user is logged in
  	if (OC.currentUser) {
  		// start on load - we ask the server every 5 minutes
  		var updateStorageStatisticsInterval = 5*60*1000;
  		var updateStorageStatisticsIntervalId = setInterval(Files.updateStorageStatistics, updateStorageStatisticsInterval);
  
  		// Use jquery-visibility to de-/re-activate file stats sync
  		if ($.support.pageVisibility) {
  			$(document).on({
  				'show.visibility': function() {
  					if (!updateStorageStatisticsIntervalId) {
  						updateStorageStatisticsIntervalId = setInterval(Files.updateStorageStatistics, updateStorageStatisticsInterval);
  					}
  				},
  				'hide.visibility': function() {
  					clearInterval(updateStorageStatisticsIntervalId);
  					updateStorageStatisticsIntervalId = 0;
  				}
  			});
  		}
03e52840d   Kload   Init
419
  	}
31b7f2792   Kload   Upgrade to ownclo...
420
421
422
  	//scroll to and highlight preselected file
  	if (getURLParameter('scrollto')) {
  		FileList.scrollTo(getURLParameter('scrollto'));
03e52840d   Kload   Init
423
424
  	}
  });
31b7f2792   Kload   Upgrade to ownclo...
425
  function scanFiles(force, dir, users) {
03e52840d   Kload   Init
426
427
428
  	if (!OC.currentUser) {
  		return;
  	}
31b7f2792   Kload   Upgrade to ownclo...
429
  	if (!dir) {
03e52840d   Kload   Init
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  		dir = '';
  	}
  	force = !!force; //cast to bool
  	scanFiles.scanning = true;
  	var scannerEventSource;
  	if (users) {
  		var usersString;
  		if (users === 'all') {
  			usersString = users;
  		} else {
  			usersString = JSON.stringify(users);
  		}
  		scannerEventSource = new OC.EventSource(OC.filePath('files','ajax','scan.php'),{force: force,dir: dir, users: usersString});
  	} else {
  		scannerEventSource = new OC.EventSource(OC.filePath('files','ajax','scan.php'),{force: force,dir: dir});
  	}
  	scanFiles.cancel = scannerEventSource.close.bind(scannerEventSource);
31b7f2792   Kload   Upgrade to ownclo...
447
448
  	scannerEventSource.listen('count',function(count) {
  		console.log(count + ' files scanned');
03e52840d   Kload   Init
449
  	});
31b7f2792   Kload   Upgrade to ownclo...
450
451
  	scannerEventSource.listen('folder',function(path) {
  		console.log('now scanning ' + path);
03e52840d   Kload   Init
452
  	});
31b7f2792   Kload   Upgrade to ownclo...
453
  	scannerEventSource.listen('done',function(count) {
03e52840d   Kload   Init
454
  		scanFiles.scanning=false;
31b7f2792   Kload   Upgrade to ownclo...
455
456
  		console.log('done after ' + count + ' files');
  		Files.updateStorageStatistics();
03e52840d   Kload   Init
457
  	});
31b7f2792   Kload   Upgrade to ownclo...
458
  	scannerEventSource.listen('user',function(user) {
03e52840d   Kload   Init
459
460
461
462
463
464
465
466
  		console.log('scanning files for ' + user);
  	});
  }
  scanFiles.scanning=false;
  
  function boolOperationFinished(data, callback) {
  	result = jQuery.parseJSON(data.responseText);
  	Files.updateMaxUploadFilesize(result);
31b7f2792   Kload   Upgrade to ownclo...
467
  	if (result.status === 'success') {
03e52840d   Kload   Init
468
469
470
471
472
  		callback.call();
  	} else {
  		alert(result.data.message);
  	}
  }
31b7f2792   Kload   Upgrade to ownclo...
473
  var createDragShadow = function(event) {
03e52840d   Kload   Init
474
475
476
477
478
479
  	//select dragged file
  	var isDragSelected = $(event.target).parents('tr').find('td input:first').prop('checked');
  	if (!isDragSelected) {
  		//select dragged file
  		$(event.target).parents('tr').find('td input:first').prop('checked',true);
  	}
31b7f2792   Kload   Upgrade to ownclo...
480
  	var selectedFiles = getSelectedFilesTrash();
03e52840d   Kload   Init
481

31b7f2792   Kload   Upgrade to ownclo...
482
  	if (!isDragSelected && selectedFiles.length === 1) {
03e52840d   Kload   Init
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
  		//revert the selection
  		$(event.target).parents('tr').find('td input:first').prop('checked',false);
  	}
  
  	//also update class when we dragged more than one file
  	if (selectedFiles.length > 1) {
  		$(event.target).parents('tr').addClass('selected');
  	}
  
  	// build dragshadow
  	var dragshadow = $('<table class="dragshadow"></table>');
  	var tbody = $('<tbody></tbody>');
  	dragshadow.append(tbody);
  
  	var dir=$('#dir').val();
31b7f2792   Kload   Upgrade to ownclo...
498
  	$(selectedFiles).each(function(i,elem) {
03e52840d   Kload   Init
499
500
501
502
503
504
505
  		var newtr = $('<tr/>').attr('data-dir', dir).attr('data-filename', elem.name);
  		newtr.append($('<td/>').addClass('filename').text(elem.name));
  		newtr.append($('<td/>').addClass('size').text(humanFileSize(elem.size)));
  		tbody.append(newtr);
  		if (elem.type === 'dir') {
  			newtr.find('td.filename').attr('style','background-image:url('+OC.imagePath('core', 'filetypes/folder.png')+')');
  		} else {
31b7f2792   Kload   Upgrade to ownclo...
506
507
508
509
  			var path = getPathForPreview(elem.name);
  			Files.lazyLoadPreview(path, elem.mime, function(previewpath) {
  				newtr.find('td.filename').attr('style','background-image:url('+previewpath+')');
  			}, null, null, elem.etag);
03e52840d   Kload   Init
510
511
512
513
  		}
  	});
  
  	return dragshadow;
31b7f2792   Kload   Upgrade to ownclo...
514
  };
03e52840d   Kload   Init
515
516
517
518
  
  //options for file drag/drop
  var dragOptions={
  	revert: 'invalid', revertDuration: 300,
31b7f2792   Kload   Upgrade to ownclo...
519
  	opacity: 0.7, zIndex: 100, appendTo: 'body', cursorAt: { left: 24, top: 18 },
03e52840d   Kload   Init
520
521
522
523
  	helper: createDragShadow, cursor: 'move',
  	stop: function(event, ui) {
  		$('#fileList tr td.filename').addClass('ui-draggable');
  	}
31b7f2792   Kload   Upgrade to ownclo...
524
  };
03e52840d   Kload   Init
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
  // sane browsers support using the distance option
  if ( $('html.ie').length === 0) {
  	dragOptions['distance'] = 20;
  }
  
  var folderDropOptions={
  	drop: function( event, ui ) {
  		//don't allow moving a file into a selected folder
  		if ($(event.target).parents('tr').find('td input:first').prop('checked') === true) {
  			return false;
  		}
  
  		var target = $(this).closest('tr').data('file');
  
  		var files = ui.helper.find('tr');
31b7f2792   Kload   Upgrade to ownclo...
540
  		$(files).each(function(i,row) {
03e52840d   Kload   Init
541
542
543
544
545
546
  			var dir = $(row).data('dir');
  			var file = $(row).data('filename');
  			$.post(OC.filePath('files', 'ajax', 'move.php'), { dir: dir, file: file, target: dir+'/'+target }, function(result) {
  				if (result) {
  					if (result.status === 'success') {
  						//recalculate folder size
a293d369c   Kload   Update sources to...
547
548
549
550
551
552
  						var oldFile = FileList.findFileEl(target);
  						var newFile = FileList.findFileEl(file);
  						var oldSize = oldFile.data('size');
  						var newSize = oldSize + newFile.data('size');
  						oldFile.data('size', newSize);
  						oldFile.find('td.filesize').text(humanFileSize(newSize));
03e52840d   Kload   Init
553
554
555
556
557
558
559
560
561
562
  
  						FileList.remove(file);
  						procesSelection();
  						$('#notification').hide();
  					} else {
  						$('#notification').hide();
  						$('#notification').text(result.data.message);
  						$('#notification').fadeIn();
  					}
  				} else {
31b7f2792   Kload   Upgrade to ownclo...
563
  					OC.dialogs.alert(t('files', 'Error moving file'), t('files', 'Error'));
03e52840d   Kload   Init
564
565
566
567
568
  				}
  			});
  		});
  	},
  	tolerance: 'pointer'
31b7f2792   Kload   Upgrade to ownclo...
569
  };
03e52840d   Kload   Init
570
571
572
573
  
  var crumbDropOptions={
  	drop: function( event, ui ) {
  		var target=$(this).data('dir');
31b7f2792   Kload   Upgrade to ownclo...
574
575
  		var dir = $('#dir').val();
  		while(dir.substr(0,1) === '/') {//remove extra leading /'s
03e52840d   Kload   Init
576
577
  				dir=dir.substr(1);
  		}
31b7f2792   Kload   Upgrade to ownclo...
578
579
580
  		dir = '/' + dir;
  		if (dir.substr(-1,1) !== '/') {
  			dir = dir + '/';
03e52840d   Kload   Init
581
  		}
31b7f2792   Kload   Upgrade to ownclo...
582
  		if (target === dir || target+'/' === dir) {
03e52840d   Kload   Init
583
584
585
  			return;
  		}
  		var files = ui.helper.find('tr');
31b7f2792   Kload   Upgrade to ownclo...
586
  		$(files).each(function(i,row) {
03e52840d   Kload   Init
587
588
589
590
591
592
593
594
595
596
597
598
599
600
  			var dir = $(row).data('dir');
  			var file = $(row).data('filename');
  			$.post(OC.filePath('files', 'ajax', 'move.php'), { dir: dir, file: file, target: target }, function(result) {
  				if (result) {
  					if (result.status === 'success') {
  						FileList.remove(file);
  						procesSelection();
  						$('#notification').hide();
  					} else {
  						$('#notification').hide();
  						$('#notification').text(result.data.message);
  						$('#notification').fadeIn();
  					}
  				} else {
31b7f2792   Kload   Upgrade to ownclo...
601
  					OC.dialogs.alert(t('files', 'Error moving file'), t('files', 'Error'));
03e52840d   Kload   Init
602
603
604
605
606
  				}
  			});
  		});
  	},
  	tolerance: 'pointer'
31b7f2792   Kload   Upgrade to ownclo...
607
  };
03e52840d   Kload   Init
608

31b7f2792   Kload   Upgrade to ownclo...
609
610
611
612
613
614
615
616
617
  function procesSelection() {
  	var selected = getSelectedFilesTrash();
  	var selectedFiles = selected.filter(function(el) {
  		return el.type==='file';
  	});
  	var selectedFolders = selected.filter(function(el) {
  		return el.type==='dir';
  	});
  	if (selectedFiles.length === 0 && selectedFolders.length === 0) {
a293d369c   Kload   Update sources to...
618
  		$('#headerName span.name').text(t('files','Name'));
03e52840d   Kload   Init
619
620
621
622
  		$('#headerSize').text(t('files','Size'));
  		$('#modified').text(t('files','Modified'));
  		$('table').removeClass('multiselect');
  		$('.selectedActions').hide();
a293d369c   Kload   Update sources to...
623
  		$('#select_all').removeAttr('checked');
03e52840d   Kload   Init
624
625
626
  	}
  	else {
  		$('.selectedActions').show();
31b7f2792   Kload   Upgrade to ownclo...
627
628
  		var totalSize = 0;
  		for(var i=0; i<selectedFiles.length; i++) {
03e52840d   Kload   Init
629
630
  			totalSize+=selectedFiles[i].size;
  		};
31b7f2792   Kload   Upgrade to ownclo...
631
  		for(var i=0; i<selectedFolders.length; i++) {
03e52840d   Kload   Init
632
633
  			totalSize+=selectedFolders[i].size;
  		};
31b7f2792   Kload   Upgrade to ownclo...
634
635
636
637
638
639
  		$('#headerSize').text(humanFileSize(totalSize));
  		var selection = '';
  		if (selectedFolders.length > 0) {
  			selection += n('files', '%n folder', '%n folders', selectedFolders.length);
  			if (selectedFiles.length > 0) {
  				selection += ' & ';
03e52840d   Kload   Init
640
641
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
642
643
  		if (selectedFiles.length>0) {
  			selection += n('files', '%n file', '%n files', selectedFiles.length);
03e52840d   Kload   Init
644
  		}
31b7f2792   Kload   Upgrade to ownclo...
645
  		$('#headerName span.name').text(selection);
03e52840d   Kload   Init
646
647
648
649
650
651
652
  		$('#modified').text('');
  		$('table').addClass('multiselect');
  	}
  }
  
  /**
   * @brief get a list of selected files
31b7f2792   Kload   Upgrade to ownclo...
653
654
   * @param {string} property (option) the property of the file requested
   * @return {array}
03e52840d   Kload   Init
655
656
657
658
659
   *
   * possible values for property: name, mime, size and type
   * if property is set, an array with that property for each file is returnd
   * if it's ommited an array of objects with all properties is returned
   */
31b7f2792   Kload   Upgrade to ownclo...
660
  function getSelectedFilesTrash(property) {
03e52840d   Kload   Init
661
662
  	var elements=$('td.filename input:checkbox:checked').parent().parent();
  	var files=[];
31b7f2792   Kload   Upgrade to ownclo...
663
  	elements.each(function(i,element) {
03e52840d   Kload   Init
664
665
666
667
  		var file={
  			name:$(element).attr('data-file'),
  			mime:$(element).data('mime'),
  			type:$(element).data('type'),
31b7f2792   Kload   Upgrade to ownclo...
668
669
  			size:$(element).data('size'),
  			etag:$(element).data('etag')
03e52840d   Kload   Init
670
  		};
31b7f2792   Kload   Upgrade to ownclo...
671
  		if (property) {
03e52840d   Kload   Init
672
  			files.push(file[property]);
31b7f2792   Kload   Upgrade to ownclo...
673
  		} else {
03e52840d   Kload   Init
674
675
676
677
678
  			files.push(file);
  		}
  	});
  	return files;
  }
31b7f2792   Kload   Upgrade to ownclo...
679
680
681
682
683
684
685
  Files.getMimeIcon = function(mime, ready) {
  	if (Files.getMimeIcon.cache[mime]) {
  		ready(Files.getMimeIcon.cache[mime]);
  	} else {
  		$.get( OC.filePath('files','ajax','mimeicon.php'), {mime: mime}, function(path) {
  			Files.getMimeIcon.cache[mime]=path;
  			ready(Files.getMimeIcon.cache[mime]);
03e52840d   Kload   Init
686
687
688
  		});
  	}
  }
31b7f2792   Kload   Upgrade to ownclo...
689
  Files.getMimeIcon.cache={};
03e52840d   Kload   Init
690

31b7f2792   Kload   Upgrade to ownclo...
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
  function getPathForPreview(name) {
  	var path = $('#dir').val() + '/' + name;
  	return path;
  }
  
  Files.lazyLoadPreview = function(path, mime, ready, width, height, etag) {
  	// get mime icon url
  	Files.getMimeIcon(mime, function(iconURL) {
  		var urlSpec = {};
  		var previewURL;
  		ready(iconURL); // set mimeicon URL
  
  		// now try getting a preview thumbnail URL
  		if ( ! width ) {
  			width = $('#filestable').data('preview-x');
  		}
  		if ( ! height ) {
  			height = $('#filestable').data('preview-y');
  		}
  		// note: the order of arguments must match the one
  		// from the server's template so that the browser
  		// knows it's the same file for caching
  		urlSpec.x = width;
  		urlSpec.y = height;
  		urlSpec.file = Files.fixPath(path);
  
  		if (etag){
  			// use etag as cache buster
  			urlSpec.c = etag;
  		}
  		else {
  			console.warn('Files.lazyLoadPreview(): missing etag argument');
  		}
a293d369c   Kload   Update sources to...
724
  		if ( $('#isPublic').length ) {
31b7f2792   Kload   Upgrade to ownclo...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  			urlSpec.t = $('#dirToken').val();
  			previewURL = OC.Router.generate('core_ajax_public_preview', urlSpec);
  		} else {
  			previewURL = OC.Router.generate('core_ajax_preview', urlSpec);
  		}
  		previewURL = previewURL.replace('(', '%28');
  		previewURL = previewURL.replace(')', '%29');
  
  		// preload image to prevent delay
  		// this will make the browser cache the image
  		var img = new Image();
  		img.onload = function(){
  			//set preview thumbnail URL
  			ready(previewURL);
  		}
  		img.src = previewURL;
  	});
  }
  
  function getUniqueName(name) {
a293d369c   Kload   Update sources to...
745
  	if (FileList.findFileEl(name).exists()) {
03e52840d   Kload   Init
746
747
748
749
750
751
752
753
  		var parts=name.split('.');
  		var extension = "";
  		if (parts.length > 1) {
  			extension=parts.pop();
  		}
  		var base=parts.join('.');
  		numMatch=base.match(/\((\d+)\)/);
  		var num=2;
31b7f2792   Kload   Upgrade to ownclo...
754
  		if (numMatch && numMatch.length>0) {
03e52840d   Kload   Init
755
  			num=parseInt(numMatch[numMatch.length-1])+1;
31b7f2792   Kload   Upgrade to ownclo...
756
  			base=base.split('(');
03e52840d   Kload   Init
757
758
759
760
761
762
763
764
765
766
767
  			base.pop();
  			base=$.trim(base.join('('));
  		}
  		name=base+' ('+num+')';
  		if (extension) {
  			name = name+'.'+extension;
  		}
  		return getUniqueName(name);
  	}
  	return name;
  }
31b7f2792   Kload   Upgrade to ownclo...
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
  
  function checkTrashStatus() {
  	$.post(OC.filePath('files_trashbin', 'ajax', 'isEmpty.php'), function(result) {
  		if (result.data.isEmpty === false) {
  			$("input[type=button][id=trash]").removeAttr("disabled");
  		}
  	});
  }
  
  function onClickBreadcrumb(e) {
  	var $el = $(e.target).closest('.crumb'),
  		$targetDir = $el.data('dir');
  		isPublic = !!$('#isPublic').val();
  
  	if ($targetDir !== undefined && !isPublic) {
  		e.preventDefault();
  		FileList.changeDirectory(decodeURIComponent($targetDir));
  	}
  }