Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

apps/files_sharing/js/files_drop.js (13 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
/*
2
 * Copyright (c) 2016 Lukas Reschke <[email protected]>
3
 *
4
 * This file is licensed under the Affero General Public License version 3
5
 * or later.
6
 *
7
 * See the COPYING-README file.
8
 *
9
 */
10
11
(function ($) {
12
	var TEMPLATE =
13
		'<li data-toggle="tooltip" title="{{name}}" data-name="{{name}}">' +
14
		'{{#if isUploading}}' +
15
		'<span class="icon-loading-small"></span> {{name}}' +
16
		'{{else}}' +
17
		'<img src="' + OC.imagePath('core', 'actions/error.svg') + '"/> {{name}}' +
18
		'{{/if}}' +
19
		'</li>';
20
	var Drop = {
21
		/** @type {Function} **/
22
		_template: undefined,
23
24
		initialize: function () {
25
			$(document).bind('drop dragover', function (e) {
26
				// Prevent the default browser drop action:
27
				e.preventDefault();
28
			});
29
			var output = this.template();
30
			$('#public-upload').fileupload({
31
				url: OC.linkTo('files', 'ajax/upload.php'),
32
				dataType: 'json',
33
				dropZone: $('#public-upload'),
34
				formData: {
35
					dirToken: $('#sharingToken').val()
36
				},
37
				add: function(e, data) {
38
					var errors = [];
39
					if(data.files[0]['size'] && data.files[0]['size'] > $('#maxFilesizeUpload').val()) {
40
						errors.push('File is too big');
41
					}
42
43
					$('#drop-upload-done-indicator').addClass('hidden');
44
					$('#drop-upload-progress-indicator').removeClass('hidden');
45
					_.each(data['files'], function(file) {
0 ignored issues
show
['files'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
46
						if(errors.length === 0) {
47
							$('#public-upload ul').append(output({isUploading: true, name: escapeHTML(file.name)}));
0 ignored issues
show
escapeHTML does not seem to be defined.
Loading history...
48
							$('[data-toggle="tooltip"]').tooltip();
49
							data.submit();
50
						} else {
51
							OC.Notification.showTemporary(OC.L10N.translate('files_sharing', 'Could not upload "{filename}"', {filename: file.name}));
52
							$('#public-upload ul').append(output({isUploading: false, name: escapeHTML(file.name)}));
53
							$('[data-toggle="tooltip"]').tooltip();
54
						}
55
					});
56
				},
57
				success: function (response) {
58
					if(response.status !== 'error') {
59
						var mimeTypeUrl = OC.MimeType.getIconUrl(response['mimetype']);
0 ignored issues
show
['mimetype'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
60
						$('#public-upload ul li[data-name="' + escapeHTML(response['filename']) + '"]').html('<img src="' + escapeHTML(mimeTypeUrl) + '"/> ' + escapeHTML(response['filename']));
0 ignored issues
show
Line is too long.
Loading history...
['filename'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
escapeHTML does not seem to be defined.
Loading history...
61
						$('[data-toggle="tooltip"]').tooltip();
62
					} else {
63
						var name = response[0]['data']['filename'];
0 ignored issues
show
['data'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
['filename'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
64
						OC.Notification.showTemporary(OC.L10N.translate('files_sharing', 'Could not upload "{filename}"', {filename: name}));
0 ignored issues
show
Line is too long.
Loading history...
65
						$('#public-upload ul li[data-name="' + escapeHTML(name) + '"]').html(output({isUploading: false, name: escapeHTML(name)}));
0 ignored issues
show
Line is too long.
Loading history...
escapeHTML does not seem to be defined.
Loading history...
66
						$('[data-toggle="tooltip"]').tooltip();
67
					}
68
69
				},
70
				progressall: function (e, data) {
71
					var progress = parseInt(data.loaded / data.total * 100, 10);
72
					if(progress === 100) {
73
						$('#drop-upload-done-indicator').removeClass('hidden');
74
						$('#drop-upload-progress-indicator').addClass('hidden');
75
					} else {
76
						$('#drop-upload-done-indicator').addClass('hidden');
77
						$('#drop-upload-progress-indicator').removeClass('hidden');
78
					}
79
				}
80
			});
81
			$('#public-upload .button.icon-upload').click(function(e) {
82
				e.preventDefault();
83
				$('#public-upload #emptycontent input').focus().trigger('click');
84
			});
85
		},
86
87
		/**
88
		 * @returns {Function} from Handlebars
89
		 * @private
90
		 */
91
		template: function () {
92
			if (!this._template) {
93
				this._template = Handlebars.compile(TEMPLATE);
94
			}
95
			return this._template;
96
		}
97
	};
98
99
	$(document).ready(function() {
100
		if($('#upload-only-interface').val() === "1") {
101
			$('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
102
		}
103
104
		OCA.Files_Sharing_Drop = Drop;
0 ignored issues
show
Identifier 'Files_Sharing_Drop' is not in camel case.
Loading history...
105
		OCA.Files_Sharing_Drop.initialize();
0 ignored issues
show
Identifier 'Files_Sharing_Drop' is not in camel case.
Loading history...
106
	});
107
108
109
})(jQuery);
110