2016-06-09 13:05:02 +03:00
/ *
* Copyright ( c ) 2016 Lukas Reschke < lukas @ statuscode . ch >
*
* This file is licensed under the Affero General Public License version 3
* or later .
*
* See the COPYING - README file .
*
* /
( function ( $ ) {
2016-06-11 16:15:37 +03:00
var TEMPLATE =
'<li data-toggle="tooltip" title="{{name}}" data-name="{{name}}">' +
'{{#if isUploading}}' +
'<span class="icon-loading-small"></span> {{name}}' +
'{{else}}' +
'<img src="' + OC . imagePath ( 'core' , 'actions/error.svg' ) + '"/> {{name}}' +
'{{/if}}' +
'</li>' ;
2016-06-09 13:05:02 +03:00
var Drop = {
2016-06-11 16:15:37 +03:00
/** @type {Function} **/
_template : undefined ,
2016-06-09 13:05:02 +03:00
initialize : function ( ) {
$ ( document ) . bind ( 'drop dragover' , function ( e ) {
// Prevent the default browser drop action:
e . preventDefault ( ) ;
} ) ;
2016-06-11 16:15:37 +03:00
var output = this . template ( ) ;
2016-06-09 18:45:16 +03:00
$ ( '#public-upload' ) . fileupload ( {
2016-06-09 13:05:02 +03:00
url : OC . linkTo ( 'files' , 'ajax/upload.php' ) ,
dataType : 'json' ,
2016-06-09 18:45:16 +03:00
dropZone : $ ( '#public-upload' ) ,
2016-06-09 13:05:02 +03:00
formData : {
dirToken : $ ( '#sharingToken' ) . val ( )
2016-06-09 18:45:16 +03:00
} ,
add : function ( e , data ) {
2016-06-09 22:44:54 +03:00
var errors = [ ] ;
if ( data . files [ 0 ] [ 'size' ] && data . files [ 0 ] [ 'size' ] > $ ( '#maxFilesizeUpload' ) . val ( ) ) {
errors . push ( 'File is too big' ) ;
}
2016-06-10 15:47:40 +03:00
$ ( '#drop-upload-done-indicator' ) . addClass ( 'hidden' ) ;
$ ( '#drop-upload-progress-indicator' ) . removeClass ( 'hidden' ) ;
2016-06-09 18:45:16 +03:00
_ . each ( data [ 'files' ] , function ( file ) {
2016-06-09 22:44:54 +03:00
if ( errors . length === 0 ) {
2016-06-11 16:15:37 +03:00
$ ( '#public-upload ul' ) . append ( output ( { isUploading : true , name : escapeHTML ( file . name ) } ) ) ;
2016-06-09 22:44:54 +03:00
$ ( '[data-toggle="tooltip"]' ) . tooltip ( ) ;
data . submit ( ) ;
} else {
OC . Notification . showTemporary ( OC . L10N . translate ( 'files_sharing' , 'Could not upload "{filename}"' , { filename : file . name } ) ) ;
2016-06-11 16:15:37 +03:00
$ ( '#public-upload ul' ) . append ( output ( { isUploading : false , name : escapeHTML ( file . name ) } ) ) ;
2016-06-09 22:44:54 +03:00
$ ( '[data-toggle="tooltip"]' ) . tooltip ( ) ;
}
2016-06-09 18:45:16 +03:00
} ) ;
} ,
success : function ( response ) {
2016-06-09 22:44:54 +03:00
if ( response . status !== 'error' ) {
var mimeTypeUrl = OC . MimeType . getIconUrl ( response [ 'mimetype' ] ) ;
$ ( '#public-upload ul li[data-name="' + escapeHTML ( response [ 'filename' ] ) + '"]' ) . html ( '<img src="' + escapeHTML ( mimeTypeUrl ) + '"/> ' + escapeHTML ( response [ 'filename' ] ) ) ;
$ ( '[data-toggle="tooltip"]' ) . tooltip ( ) ;
2016-06-11 16:15:37 +03:00
} else {
var name = response [ 0 ] [ 'data' ] [ 'filename' ] ;
OC . Notification . showTemporary ( OC . L10N . translate ( 'files_sharing' , 'Could not upload "{filename}"' , { filename : name } ) ) ;
$ ( '#public-upload ul li[data-name="' + escapeHTML ( name ) + '"]' ) . html ( output ( { isUploading : false , name : escapeHTML ( name ) } ) ) ;
$ ( '[data-toggle="tooltip"]' ) . tooltip ( ) ;
2016-06-09 22:44:54 +03:00
}
2016-06-11 16:15:37 +03:00
2016-06-10 15:47:40 +03:00
} ,
progressall : function ( e , data ) {
var progress = parseInt ( data . loaded / data . total * 100 , 10 ) ;
if ( progress === 100 ) {
$ ( '#drop-upload-done-indicator' ) . removeClass ( 'hidden' ) ;
$ ( '#drop-upload-progress-indicator' ) . addClass ( 'hidden' ) ;
} else {
$ ( '#drop-upload-done-indicator' ) . addClass ( 'hidden' ) ;
$ ( '#drop-upload-progress-indicator' ) . removeClass ( 'hidden' ) ;
}
2016-06-09 13:05:02 +03:00
}
} ) ;
2016-06-09 18:45:16 +03:00
$ ( '#public-upload .button.icon-upload' ) . click ( function ( e ) {
e . preventDefault ( ) ;
$ ( '#public-upload #emptycontent input' ) . focus ( ) . trigger ( 'click' ) ;
} ) ;
2016-06-11 16:15:37 +03:00
} ,
/ * *
* @ returns { Function } from Handlebars
* @ private
* /
template : function ( ) {
if ( ! this . _template ) {
this . _template = Handlebars . compile ( TEMPLATE ) ;
}
return this . _template ;
2016-06-09 13:05:02 +03:00
}
} ;
$ ( document ) . ready ( function ( ) {
2016-06-09 18:45:16 +03:00
if ( $ ( '#upload-only-interface' ) . val ( ) === "1" ) {
2016-06-09 13:05:02 +03:00
$ ( '.avatardiv' ) . avatar ( $ ( '#sharingUserId' ) . val ( ) , 128 , true ) ;
}
OCA . Files _Sharing _Drop = Drop ;
OCA . Files _Sharing _Drop . initialize ( ) ;
} ) ;
} ) ( jQuery ) ;