Blame view

sources/apps/files/js/files.js 23.6 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;
  	},
03e52840d   Kload   Init
68
69
  	isFileNameValid:function (name) {
  		if (name === '.') {
31b7f2792   Kload   Upgrade to ownclo...
70
71
72
  			throw t('files', '\'.\' is an invalid file name.');
  		} else if (name.length === 0) {
  			throw t('files', 'File name cannot be empty.');
03e52840d   Kload   Init
73
74
75
76
77
  		}
  
  		// check for invalid characters
  		var invalid_characters = ['\\', '/', '<', '>', ':', '"', '|', '?', '*'];
  		for (var i = 0; i < invalid_characters.length; i++) {
31b7f2792   Kload   Upgrade to ownclo...
78
79
  			if (name.indexOf(invalid_characters[i]) !== -1) {
  				throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.");
03e52840d   Kload   Init
80
81
  			}
  		}
03e52840d   Kload   Init
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  		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...
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
  	},
  
  	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
209
210
211
  	}
  };
  $(document).ready(function() {
31b7f2792   Kload   Upgrade to ownclo...
212
213
214
215
216
  	// FIXME: workaround for trashbin app
  	if (window.trashBinApp) {
  		return;
  	}
  	Files.displayEncryptionWarning();
03e52840d   Kload   Init
217
  	Files.bindKeyboardShortcuts(document, jQuery);
31b7f2792   Kload   Upgrade to ownclo...
218
219
220
  
  	FileList.postProcessList();
  	Files.setupDragAndDrop();
03e52840d   Kload   Init
221
222
  
  	$('#file_action_panel').attr('activeAction', false);
31b7f2792   Kload   Upgrade to ownclo...
223
224
  	// allow dropping on the "files" app icon
  	$('ul#apps li:first-child').data('dir','').droppable(crumbDropOptions);
03e52840d   Kload   Init
225
226
227
228
229
230
231
232
233
  
  	// 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...
234
235
  		OC.Upload.cancelUploads();
  		procesSelection();
03e52840d   Kload   Init
236
237
238
  	});
  
  	// Show trash bin
31b7f2792   Kload   Upgrade to ownclo...
239
  	$('#trash').on('click', function() {
03e52840d   Kload   Init
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  		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...
257
  						if (index === i) {
03e52840d   Kload   Init
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  							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...
274
275
  				var selectedCount = $('td.filename input:checkbox:checked').length;
  				if (selectedCount === $('td.filename input:checkbox').length) {
03e52840d   Kload   Init
276
277
278
279
280
281
  					$('#select_all').attr('checked', 'checked');
  				}
  			}
  			procesSelection();
  		} else {
  			var filename=$(this).parent().parent().attr('data-file');
31b7f2792   Kload   Upgrade to ownclo...
282
  			var tr=$('tr[data-file="'+filename+'"]');
03e52840d   Kload   Init
283
  			var renaming=tr.data('renaming');
31b7f2792   Kload   Upgrade to ownclo...
284
  			if (!renaming && !FileList.isLoading(filename)) {
03e52840d   Kload   Init
285
286
287
288
289
  				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...
290
  				if (action) {
03e52840d   Kload   Init
291
292
293
294
295
296
297
298
299
300
  					event.preventDefault();
  					action(filename);
  				}
  			}
  		}
  
  	});
  
  	// Sets the select_all checkbox behaviour :
  	$('#select_all').click(function() {
31b7f2792   Kload   Upgrade to ownclo...
301
  		if ($(this).attr('checked')) {
03e52840d   Kload   Init
302
303
304
  			// Check all
  			$('td.filename input:checkbox').attr('checked', true);
  			$('td.filename input:checkbox').parent().parent().addClass('selected');
31b7f2792   Kload   Upgrade to ownclo...
305
  		} else {
03e52840d   Kload   Init
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  			// 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...
322
  					if (index === i) {
03e52840d   Kload   Init
323
324
325
326
327
328
329
330
331
  						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...
332
  		if (!$(this).attr('checked')) {
03e52840d   Kload   Init
333
  			$('#select_all').attr('checked',false);
31b7f2792   Kload   Upgrade to ownclo...
334
335
  		} else {
  			if (selectedCount===$('td.filename input:checkbox').length) {
03e52840d   Kload   Init
336
337
338
339
340
341
342
  				$('#select_all').attr('checked',true);
  			}
  		}
  		procesSelection();
  	});
  
  	$('.download').click('click',function(event) {
31b7f2792   Kload   Upgrade to ownclo...
343
  		var files=getSelectedFilesTrash('name');
03e52840d   Kload   Init
344
345
346
347
  		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...
348
349
350
  		var downloadURL = document.getElementById("downloadURL");
  		if ( downloadURL ) {
  			window.location = downloadURL.value+"&download&files=" + encodeURIComponent(fileslist);
03e52840d   Kload   Init
351
  		} else {
31b7f2792   Kload   Upgrade to ownclo...
352
  			window.location = OC.filePath('files', 'ajax', 'download.php') + '?'+ $.param({ dir: dir, files: fileslist });
03e52840d   Kload   Init
353
354
355
356
357
  		}
  		return false;
  	});
  
  	$('.delete-selected').click(function(event) {
31b7f2792   Kload   Upgrade to ownclo...
358
  		var files=getSelectedFilesTrash('name');
03e52840d   Kload   Init
359
360
361
362
363
364
365
366
367
368
  		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
369
370
  	//do a background scan if needed
  	scanFiles();
31b7f2792   Kload   Upgrade to ownclo...
371
  	Files.initBreadCrumbs();
03e52840d   Kload   Init
372
373
  
  	$(window).resize(function() {
31b7f2792   Kload   Upgrade to ownclo...
374
375
  		var width = $(this).width();
  		Files.resizeBreadcrumbs(width, false);
03e52840d   Kload   Init
376
  	});
31b7f2792   Kload   Upgrade to ownclo...
377
378
  	var width = $(this).width();
  	Files.resizeBreadcrumbs(width, true);
03e52840d   Kload   Init
379
380
381
382
  
  	// display storage warnings
  	setTimeout ( "Files.displayStorageWarnings()", 100 );
  	OC.Notification.setDefault(Files.displayStorageWarnings);
31b7f2792   Kload   Upgrade to ownclo...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  	// 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
403
  	}
31b7f2792   Kload   Upgrade to ownclo...
404
405
406
  	//scroll to and highlight preselected file
  	if (getURLParameter('scrollto')) {
  		FileList.scrollTo(getURLParameter('scrollto'));
03e52840d   Kload   Init
407
408
  	}
  });
31b7f2792   Kload   Upgrade to ownclo...
409
  function scanFiles(force, dir, users) {
03e52840d   Kload   Init
410
411
412
  	if (!OC.currentUser) {
  		return;
  	}
31b7f2792   Kload   Upgrade to ownclo...
413
  	if (!dir) {
03e52840d   Kload   Init
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  		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...
431
432
  	scannerEventSource.listen('count',function(count) {
  		console.log(count + ' files scanned');
03e52840d   Kload   Init
433
  	});
31b7f2792   Kload   Upgrade to ownclo...
434
435
  	scannerEventSource.listen('folder',function(path) {
  		console.log('now scanning ' + path);
03e52840d   Kload   Init
436
  	});
31b7f2792   Kload   Upgrade to ownclo...
437
  	scannerEventSource.listen('done',function(count) {
03e52840d   Kload   Init
438
  		scanFiles.scanning=false;
31b7f2792   Kload   Upgrade to ownclo...
439
440
  		console.log('done after ' + count + ' files');
  		Files.updateStorageStatistics();
03e52840d   Kload   Init
441
  	});
31b7f2792   Kload   Upgrade to ownclo...
442
  	scannerEventSource.listen('user',function(user) {
03e52840d   Kload   Init
443
444
445
446
447
448
449
450
  		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...
451
  	if (result.status === 'success') {
03e52840d   Kload   Init
452
453
454
455
456
  		callback.call();
  	} else {
  		alert(result.data.message);
  	}
  }
31b7f2792   Kload   Upgrade to ownclo...
457
  var createDragShadow = function(event) {
03e52840d   Kload   Init
458
459
460
461
462
463
  	//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...
464
  	var selectedFiles = getSelectedFilesTrash();
03e52840d   Kload   Init
465

31b7f2792   Kload   Upgrade to ownclo...
466
  	if (!isDragSelected && selectedFiles.length === 1) {
03e52840d   Kload   Init
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  		//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...
482
  	$(selectedFiles).each(function(i,elem) {
03e52840d   Kload   Init
483
484
485
486
487
488
489
  		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...
490
491
492
493
  			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
494
495
496
497
  		}
  	});
  
  	return dragshadow;
31b7f2792   Kload   Upgrade to ownclo...
498
  };
03e52840d   Kload   Init
499
500
501
502
  
  //options for file drag/drop
  var dragOptions={
  	revert: 'invalid', revertDuration: 300,
31b7f2792   Kload   Upgrade to ownclo...
503
  	opacity: 0.7, zIndex: 100, appendTo: 'body', cursorAt: { left: 24, top: 18 },
03e52840d   Kload   Init
504
505
506
507
  	helper: createDragShadow, cursor: 'move',
  	stop: function(event, ui) {
  		$('#fileList tr td.filename').addClass('ui-draggable');
  	}
31b7f2792   Kload   Upgrade to ownclo...
508
  };
03e52840d   Kload   Init
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  // 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...
524
  		$(files).each(function(i,row) {
03e52840d   Kload   Init
525
526
527
528
529
530
  			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
31b7f2792   Kload   Upgrade to ownclo...
531
532
533
534
  						var oldSize = $('#fileList tr[data-file="'+target+'"]').data('size');
  						var newSize = oldSize + $('#fileList tr[data-file="'+file+'"]').data('size');
  						$('#fileList tr[data-file="'+target+'"]').data('size', newSize);
  						$('#fileList tr[data-file="'+target+'"]').find('td.filesize').text(humanFileSize(newSize));
03e52840d   Kload   Init
535
536
537
538
539
540
541
542
543
544
  
  						FileList.remove(file);
  						procesSelection();
  						$('#notification').hide();
  					} else {
  						$('#notification').hide();
  						$('#notification').text(result.data.message);
  						$('#notification').fadeIn();
  					}
  				} else {
31b7f2792   Kload   Upgrade to ownclo...
545
  					OC.dialogs.alert(t('files', 'Error moving file'), t('files', 'Error'));
03e52840d   Kload   Init
546
547
548
549
550
  				}
  			});
  		});
  	},
  	tolerance: 'pointer'
31b7f2792   Kload   Upgrade to ownclo...
551
  };
03e52840d   Kload   Init
552
553
554
555
  
  var crumbDropOptions={
  	drop: function( event, ui ) {
  		var target=$(this).data('dir');
31b7f2792   Kload   Upgrade to ownclo...
556
557
  		var dir = $('#dir').val();
  		while(dir.substr(0,1) === '/') {//remove extra leading /'s
03e52840d   Kload   Init
558
559
  				dir=dir.substr(1);
  		}
31b7f2792   Kload   Upgrade to ownclo...
560
561
562
  		dir = '/' + dir;
  		if (dir.substr(-1,1) !== '/') {
  			dir = dir + '/';
03e52840d   Kload   Init
563
  		}
31b7f2792   Kload   Upgrade to ownclo...
564
  		if (target === dir || target+'/' === dir) {
03e52840d   Kload   Init
565
566
567
  			return;
  		}
  		var files = ui.helper.find('tr');
31b7f2792   Kload   Upgrade to ownclo...
568
  		$(files).each(function(i,row) {
03e52840d   Kload   Init
569
570
571
572
573
574
575
576
577
578
579
580
581
582
  			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...
583
  					OC.dialogs.alert(t('files', 'Error moving file'), t('files', 'Error'));
03e52840d   Kload   Init
584
585
586
587
588
  				}
  			});
  		});
  	},
  	tolerance: 'pointer'
31b7f2792   Kload   Upgrade to ownclo...
589
  };
03e52840d   Kload   Init
590

31b7f2792   Kload   Upgrade to ownclo...
591
592
593
594
595
596
597
598
599
  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) {
03e52840d   Kload   Init
600
601
602
603
604
605
606
607
  		$('#headerName>span.name').text(t('files','Name'));
  		$('#headerSize').text(t('files','Size'));
  		$('#modified').text(t('files','Modified'));
  		$('table').removeClass('multiselect');
  		$('.selectedActions').hide();
  	}
  	else {
  		$('.selectedActions').show();
31b7f2792   Kload   Upgrade to ownclo...
608
609
  		var totalSize = 0;
  		for(var i=0; i<selectedFiles.length; i++) {
03e52840d   Kload   Init
610
611
  			totalSize+=selectedFiles[i].size;
  		};
31b7f2792   Kload   Upgrade to ownclo...
612
  		for(var i=0; i<selectedFolders.length; i++) {
03e52840d   Kload   Init
613
614
  			totalSize+=selectedFolders[i].size;
  		};
31b7f2792   Kload   Upgrade to ownclo...
615
616
617
618
619
620
  		$('#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
621
622
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
623
624
  		if (selectedFiles.length>0) {
  			selection += n('files', '%n file', '%n files', selectedFiles.length);
03e52840d   Kload   Init
625
  		}
31b7f2792   Kload   Upgrade to ownclo...
626
  		$('#headerName span.name').text(selection);
03e52840d   Kload   Init
627
628
629
630
631
632
633
  		$('#modified').text('');
  		$('table').addClass('multiselect');
  	}
  }
  
  /**
   * @brief get a list of selected files
31b7f2792   Kload   Upgrade to ownclo...
634
635
   * @param {string} property (option) the property of the file requested
   * @return {array}
03e52840d   Kload   Init
636
637
638
639
640
   *
   * 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...
641
  function getSelectedFilesTrash(property) {
03e52840d   Kload   Init
642
643
  	var elements=$('td.filename input:checkbox:checked').parent().parent();
  	var files=[];
31b7f2792   Kload   Upgrade to ownclo...
644
  	elements.each(function(i,element) {
03e52840d   Kload   Init
645
646
647
648
  		var file={
  			name:$(element).attr('data-file'),
  			mime:$(element).data('mime'),
  			type:$(element).data('type'),
31b7f2792   Kload   Upgrade to ownclo...
649
650
  			size:$(element).data('size'),
  			etag:$(element).data('etag')
03e52840d   Kload   Init
651
  		};
31b7f2792   Kload   Upgrade to ownclo...
652
  		if (property) {
03e52840d   Kload   Init
653
  			files.push(file[property]);
31b7f2792   Kload   Upgrade to ownclo...
654
  		} else {
03e52840d   Kload   Init
655
656
657
658
659
  			files.push(file);
  		}
  	});
  	return files;
  }
31b7f2792   Kload   Upgrade to ownclo...
660
661
662
663
664
665
666
  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
667
668
669
  		});
  	}
  }
31b7f2792   Kload   Upgrade to ownclo...
670
  Files.getMimeIcon.cache={};
03e52840d   Kload   Init
671

31b7f2792   Kload   Upgrade to ownclo...
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
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
724
725
726
727
  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');
  		}
  
  		if ( $('#publicUploadButtonMock').length ) {
  			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) {
  	if ($('tr[data-file="'+name+'"]').exists()) {
03e52840d   Kload   Init
728
729
730
731
732
733
734
735
  		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...
736
  		if (numMatch && numMatch.length>0) {
03e52840d   Kload   Init
737
  			num=parseInt(numMatch[numMatch.length-1])+1;
31b7f2792   Kload   Upgrade to ownclo...
738
  			base=base.split('(');
03e52840d   Kload   Init
739
740
741
742
743
744
745
746
747
748
749
  			base.pop();
  			base=$.trim(base.join('('));
  		}
  		name=base+' ('+num+')';
  		if (extension) {
  			name = name+'.'+extension;
  		}
  		return getUniqueName(name);
  	}
  	return name;
  }
31b7f2792   Kload   Upgrade to ownclo...
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
  
  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));
  	}
  }