2011-09-01 01:37:30 +02:00
/ * *
2013-04-13 21:00:13 +02:00
* EGroupware eTemplate2 - JS Number object
2011-09-01 01:37:30 +02:00
*
* @ license http : //opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @ package etemplate
* @ subpackage api
* @ link http : //www.egroupware.org
* @ author Nathan Gray
* @ copyright Nathan Gray 2011
* @ version $Id$
* /
/ * e g w : u s e s
et2 _core _inputWidget ;
2014-04-01 22:48:40 +02:00
phpgwapi . Resumable . resumable ;
2011-09-01 01:37:30 +02:00
* /
/ * *
* Class which implements file upload
2015-08-07 16:18:07 +02:00
*
2013-04-13 21:00:13 +02:00
* @ augments et2 _inputWidget
2015-08-07 16:18:07 +02:00
* /
2016-02-29 21:40:43 +01:00
var et2 _file = ( function ( ) { "use strict" ; return et2 _inputWidget . extend (
2013-04-13 21:00:13 +02:00
{
2011-09-01 01:37:30 +02:00
attributes : {
"multiple" : {
"name" : "Multiple files" ,
"type" : "boolean" ,
"default" : false ,
"description" : "Allow the user to select more than one file to upload at a time. Subject to browser support."
} ,
"max_file_size" : {
"name" : "Maximum file size" ,
"type" : "integer" ,
2014-04-01 22:48:40 +02:00
"default" : 0 ,
"description" : "Largest file accepted, in bytes. Subject to server limitations. 8MB = 8388608"
2011-09-01 01:37:30 +02:00
} ,
2012-03-30 00:45:58 +02:00
"mime" : {
"name" : "Allowed file types" ,
"type" : "string" ,
"default" : et2 _no _init ,
"description" : "Mime type (eg: image/png) or regex (eg: /^text\//i) for allowed file types"
} ,
2011-09-01 01:37:30 +02:00
"blur" : {
2013-04-13 21:00:13 +02:00
"name" : "Placeholder" ,
"type" : "string" ,
"default" : "" ,
"description" : "This text get displayed if an input-field is empty and does not have the input-focus (blur). It can be used to show a default value or a kind of help-text."
2011-09-01 01:37:30 +02:00
} ,
"progress" : {
"name" : "Progress node" ,
"type" : "string" ,
"default" : et2 _no _init ,
2013-09-10 14:57:40 +02:00
"description" : "The ID of an alternate node (div) to display progress and results. The Node is fetched with et2 getWidgetById so you MUST use the id assigned in XET-File (it may not be available at creation time, so we (re)check on createStatus time)"
2011-09-09 02:05:18 +02:00
} ,
"onStart" : {
"name" : "Start event handler" ,
"type" : "any" ,
"default" : et2 _no _init ,
"description" : "A (js) function called when an upload starts. Return true to continue with upload, false to cancel."
} ,
"onFinish" : {
"name" : "Finish event handler" ,
"type" : "any" ,
"default" : et2 _no _init ,
"description" : "A (js) function called when all files to be uploaded are finished."
2013-08-03 21:12:38 +02:00
} ,
drop _target : {
"name" : "Optional, additional drop target for HTML5 uploads" ,
"type" : "string" ,
"default" : et2 _no _init ,
"description" : "The ID of an additional drop target for HTML5 drag-n-drop file uploads"
2014-03-21 17:27:09 +01:00
} ,
label : {
"name" : "Label of file upload" ,
"type" : "string" ,
"default" : "Choose file..." ,
"description" : "String caption to be displayed on file upload span"
2014-10-01 16:19:36 +02:00
} ,
progress _dropdownlist : {
"name" : "List on files in progress like dropdown" ,
"type" : "boolean" ,
"default" : false ,
"description" : "Style list of files in uploading progress like dropdown list with a total upload progress indicator"
} ,
2015-01-14 10:05:34 +01:00
onFinishOne : {
"name" : "Finish event handler for each one" ,
"type" : "any" ,
"default" : et2 _no _init ,
"description" : "A (js) function called when a file to be uploaded is finished."
2017-01-25 10:28:10 +01:00
} ,
accept : {
"name" : "Acceptable extensions" ,
"type" : "string" ,
"default" : '' ,
"description" : "Define types of files that the server accepts. Multiple types can be seperated by comma and the default is to accept everything."
2015-08-07 16:18:07 +02:00
}
2011-09-01 01:37:30 +02:00
} ,
2011-09-02 00:07:30 +02:00
asyncOptions : { } ,
2013-04-13 21:00:13 +02:00
/ * *
* Constructor
2015-08-07 16:18:07 +02:00
*
2013-04-13 21:00:13 +02:00
* @ memberOf et2 _file
* /
2017-04-10 18:39:04 +02:00
init : function ( _parent , attrs ) {
2013-04-13 21:00:13 +02:00
this . _super . apply ( this , arguments ) ;
2011-09-01 01:37:30 +02:00
this . node = null ;
2011-09-08 22:33:46 +02:00
this . input = null ;
this . progress = null ;
2014-03-21 17:27:09 +01:00
this . span = null ;
2018-04-10 10:17:08 +02:00
// Contains all submit buttons need to be disabled during upload process
this . disabled _buttons = jQuery ( "input[type='submit'], button" ) ;
2017-04-10 18:39:04 +02:00
if ( ! this . options . value ) this . options . value = { } ;
2011-09-08 22:33:46 +02:00
if ( ! this . options . id ) {
console . warn ( "File widget needs an ID. Used 'file_widget'." ) ;
this . options . id = "file_widget" ;
}
2011-09-01 01:37:30 +02:00
2012-03-30 00:45:58 +02:00
// Legacy - id ending in [] means multiple
if ( this . options . id . substr ( - 2 ) == "[]" )
{
this . options . multiple = true ;
}
2017-04-10 18:39:04 +02:00
// If ID ends in /, it's a directory - allow multiple
else if ( this . options . id . substr ( - 1 ) === "/" )
{
this . options . multiple = true ;
attrs . multiple = true ;
}
2012-03-30 00:45:58 +02:00
2011-09-02 00:07:30 +02:00
// Set up the URL to have the request ID & the widget ID
var instance = this . getInstanceManager ( ) ;
2015-08-07 16:18:07 +02:00
2011-09-02 00:07:30 +02:00
var self = this ;
2012-03-30 00:45:58 +02:00
2012-03-29 01:27:18 +02:00
this . asyncOptions = jQuery . extend ( {
2011-09-02 00:07:30 +02:00
// Callbacks
2015-08-07 16:18:07 +02:00
onStart : function ( event , file _count ) {
return self . onStart ( event , file _count ) ;
2012-03-30 00:45:58 +02:00
} ,
2015-08-07 16:18:07 +02:00
onFinish : function ( event , file _count ) {
2014-04-01 22:48:40 +02:00
self . onFinish . apply ( self , [ event , file _count ] )
2012-03-30 00:45:58 +02:00
} ,
2015-08-07 16:18:07 +02:00
onStartOne : function ( event , file _name , index , file _count ) {
2012-03-30 00:45:58 +02:00
} ,
2011-09-02 00:07:30 +02:00
onFinishOne : function ( event , response , name , number , total ) { return self . finishUpload ( event , response , name , number , total ) ; } ,
2011-09-06 21:55:52 +02:00
onProgress : function ( event , progress , name , number , total ) { return self . onProgress ( event , progress , name , number , total ) ; } ,
onError : function ( event , name , error ) { return self . onError ( event , name , error ) ; } ,
2011-09-08 22:33:46 +02:00
beforeSend : function ( form ) { return self . beforeSend ( form ) ; } ,
2014-04-01 22:48:40 +02:00
2016-03-19 17:16:59 +01:00
target : egw . ajaxUrl ( "EGroupware\\Api\\Etemplate\\Widget\\File::ajax_upload" ) ,
2014-04-01 22:48:40 +02:00
query : function ( file ) { return self . beforeSend ( file ) ; } ,
// Disable checking for already uploaded chunks
testChunks : false
2012-03-29 01:27:18 +02:00
} , this . asyncOptions ) ;
2011-09-02 00:07:30 +02:00
this . asyncOptions . fieldName = this . options . id ;
2011-09-01 01:37:30 +02:00
this . createInputWidget ( ) ;
2017-10-13 14:51:45 +02:00
this . set _readonly ( this . options . readonly ) ;
2011-09-01 01:37:30 +02:00
} ,
2011-09-08 22:33:46 +02:00
destroy : function ( ) {
2013-07-20 19:20:55 +02:00
this . _super . apply ( this , arguments ) ;
2013-08-03 21:12:38 +02:00
this . set _drop _target ( null ) ;
2011-09-08 22:33:46 +02:00
this . node = null ;
this . input = null ;
2014-03-21 17:27:09 +01:00
this . span = null ;
2011-09-08 22:33:46 +02:00
this . progress = null ;
} ,
2015-08-07 16:18:07 +02:00
2011-09-01 01:37:30 +02:00
createInputWidget : function ( ) {
2016-06-02 16:51:15 +02:00
this . node = jQuery ( document . createElement ( "div" ) ) . addClass ( "et2_file" ) ;
this . span = jQuery ( document . createElement ( "span" ) )
2016-04-07 19:13:06 +02:00
. addClass ( 'et2_file_span et2_button' )
2014-03-21 17:27:09 +01:00
. appendTo ( this . node ) ;
2016-04-07 19:13:06 +02:00
if ( this . options . label != '' ) this . span . addClass ( 'et2_button_text' ) ;
2014-03-21 17:27:09 +01:00
var span = this . span ;
2016-06-02 16:51:15 +02:00
this . input = jQuery ( document . createElement ( "input" ) )
2011-09-01 01:37:30 +02:00
. attr ( "type" , "file" ) . attr ( "placeholder" , this . options . blur )
2014-03-21 17:27:09 +01:00
. addClass ( "et2_file_upload" )
. appendTo ( this . node )
. hover ( function ( e ) {
2016-06-02 16:51:15 +02:00
jQuery ( span )
2014-03-21 17:27:09 +01:00
. toggleClass ( 'et2_file_spanHover' ) ;
2015-08-07 16:18:07 +02:00
} )
2014-03-21 17:27:09 +01:00
. on ( {
mousedown : function ( e ) {
2016-06-02 16:51:15 +02:00
jQuery ( span ) . addClass ( 'et2_file_spanActive' ) ;
2014-03-21 17:27:09 +01:00
} ,
mouseup : function ( e ) {
2016-06-02 16:51:15 +02:00
jQuery ( span ) . removeClass ( 'et2_file_spanActive' ) ;
2014-03-21 17:27:09 +01:00
}
} ) ;
2017-01-25 10:28:10 +01:00
if ( this . options . accept ) this . input . attr ( 'accept' , this . options . accept ) ;
2016-04-08 15:47:05 +02:00
var self = this ;
// trigger native input upload file
2016-04-18 12:37:08 +02:00
if ( ! this . options . readonly ) this . span . click ( function ( ) { self . input . click ( ) } ) ;
2011-09-02 00:07:30 +02:00
// Check for File interface, should fall back to normal form submit if missing
if ( typeof File != "undefined" && typeof ( new XMLHttpRequest ( ) ) . upload != "undefined" )
{
2014-04-01 22:48:40 +02:00
this . resumable = new Resumable ( this . asyncOptions ) ;
this . resumable . assignBrowse ( this . input ) ;
this . resumable . on ( 'fileAdded' , jQuery . proxy ( this . _fileAdded , this ) ) ;
this . resumable . on ( 'fileProgress' , jQuery . proxy ( this . _fileProgress , this ) ) ;
this . resumable . on ( 'fileSuccess' , jQuery . proxy ( this . finishUpload , this ) ) ;
this . resumable . on ( 'complete' , jQuery . proxy ( this . onFinish , this ) ) ;
2011-09-02 00:07:30 +02:00
}
2011-09-06 21:55:52 +02:00
else
{
// This may be a problem submitting via ajax
}
2013-09-10 00:04:48 +02:00
if ( this . options . progress )
{
2013-09-10 10:08:47 +02:00
var widget = this . getRoot ( ) . getWidgetById ( this . options . progress ) ;
if ( widget )
2013-09-10 00:04:48 +02:00
{
2013-09-10 14:57:40 +02:00
//may be not available at createInputWidget time
2016-06-02 16:51:15 +02:00
this . progress = jQuery ( widget . getDOMNode ( ) ) ;
2013-09-10 00:04:48 +02:00
}
}
if ( ! this . progress )
{
2016-06-02 16:51:15 +02:00
this . progress = jQuery ( document . createElement ( "div" ) ) . appendTo ( this . node ) ;
2013-09-10 00:04:48 +02:00
}
2011-09-01 01:37:30 +02:00
this . progress . addClass ( "progress" ) ;
2012-03-30 00:45:58 +02:00
if ( this . options . multiple )
{
2011-09-01 01:37:30 +02:00
this . input . attr ( "multiple" , "multiple" ) ;
}
2012-03-30 00:45:58 +02:00
2011-09-01 01:37:30 +02:00
this . setDOMNode ( this . node [ 0 ] ) ;
2013-08-03 21:12:38 +02:00
} ,
2015-08-07 16:18:07 +02:00
2013-08-03 21:12:38 +02:00
/ * *
* Set a widget or DOM node as a HTML5 file drop target
2015-08-07 16:18:07 +02:00
*
2013-08-03 21:12:38 +02:00
* @ param String new _target widget ID or DOM node ID to be used as a new target
* /
set _drop _target : function ( new _target )
2015-08-07 16:18:07 +02:00
{
2013-08-03 21:12:38 +02:00
// Cancel old drop target
if ( this . options . drop _target )
{
var widget = this . getRoot ( ) . getWidgetById ( this . options . drop _target ) ;
var drop _target = widget && widget . getDOMNode ( ) || document . getElementById ( this . options . drop _target ) ;
2014-04-01 22:48:40 +02:00
if ( drop _target )
{
this . resumable . unAssignDrop ( drop _target ) ;
}
2013-08-03 21:12:38 +02:00
}
2015-08-07 16:18:07 +02:00
2013-08-03 21:12:38 +02:00
this . options . drop _target = new _target ;
2015-08-07 16:18:07 +02:00
2013-08-27 18:29:13 +02:00
if ( ! this . options . drop _target ) return ;
2015-08-07 16:18:07 +02:00
2013-08-03 21:12:38 +02:00
// Set up new drop target
var widget = this . getRoot ( ) . getWidgetById ( this . options . drop _target ) ;
var drop _target = widget && widget . getDOMNode ( ) || document . getElementById ( this . options . drop _target ) ;
if ( drop _target )
{
2015-02-10 20:49:31 +01:00
this . resumable . assignDrop ( [ drop _target ] ) ;
2013-08-03 21:12:38 +02:00
}
else
{
this . egw ( ) . debug ( "warn" , "Did not find file drop target %s" , this . options . drop _target ) ;
}
2012-03-30 00:45:58 +02:00
} ,
attachToDOM : function ( ) {
this . _super . apply ( this , arguments ) ;
2013-09-25 17:52:42 +02:00
// Override parent's change, file widget will fire change when finished uploading
this . input . unbind ( "change.et2_inputWidget" ) ;
2011-09-01 01:37:30 +02:00
} ,
2011-09-06 21:55:52 +02:00
getValue : function ( ) {
var value = this . options . value ? this . options . value : this . input . val ( ) ;
return value ;
} ,
2013-04-22 18:52:03 +02:00
/ * *
2015-08-07 16:18:07 +02:00
* Set the value of the file widget .
*
2013-08-27 19:20:08 +02:00
* If you pass a FileList or list of files , it will trigger the async upload
2015-08-07 16:18:07 +02:00
*
2013-08-27 19:20:08 +02:00
* @ param { FileList | File [ ] | false } value List of files to be uploaded , or false to reset .
2014-07-07 22:26:48 +02:00
* @ param { Event } event Most browsers require the user to initiate file transfers in some way .
* Pass the event in , if you have it .
2013-04-22 18:52:03 +02:00
* /
2014-07-07 22:26:48 +02:00
set _value : function ( value , event ) {
2013-04-22 18:52:03 +02:00
if ( ! value || typeof value == "undefined" )
{
value = { } ;
}
if ( jQuery . isEmptyObject ( value ) )
{
this . options . value = { } ;
2015-01-14 10:05:34 +01:00
if ( this . resumable . progress ( ) == 1 ) this . progress . empty ( ) ;
2013-04-22 18:52:03 +02:00
// Reset the HTML element
this . input . wrap ( '<form>' ) . closest ( 'form' ) . get ( 0 ) . reset ( ) ;
this . input . unwrap ( ) ;
return ;
}
2015-08-07 16:18:07 +02:00
2018-01-11 19:46:28 +01:00
var addFile = jQuery . proxy ( function ( i , file ) {
this . resumable . addFile ( file , event ) ;
} , this ) ;
2013-08-27 19:20:08 +02:00
if ( typeof value == 'object' && value . length && typeof value [ 0 ] == 'object' && value [ 0 ] . name )
2013-04-22 18:52:03 +02:00
{
2014-07-07 22:26:48 +02:00
try
{
this . input [ 0 ] . files = value ;
2018-01-11 19:46:28 +01:00
jQuery . each ( value , addFile ) ;
2014-07-07 22:26:48 +02:00
}
catch ( e )
{
var self = this ;
var args = arguments ;
2018-01-11 19:46:28 +01:00
jQuery . each ( value , addFile ) ;
2014-07-07 22:26:48 +02:00
}
2013-04-22 18:52:03 +02:00
}
} ,
2015-08-07 16:18:07 +02:00
2014-03-21 17:27:09 +01:00
/ * *
* Set the value for label
* The label is used as caption for span tag which customize the HTML file upload styling
2015-08-07 16:18:07 +02:00
*
2014-03-21 17:27:09 +01:00
* @ param { string } value text value of label
* /
set _label : function ( value )
{
if ( this . span != null && value != null )
{
this . span . text ( value ) ;
}
} ,
2013-04-22 18:52:03 +02:00
2011-09-01 01:37:30 +02:00
getInputNode : function ( ) {
2014-01-30 10:07:02 +01:00
if ( typeof this . input == 'undefined' ) return false ;
2011-09-01 01:37:30 +02:00
return this . input [ 0 ] ;
} ,
2012-03-30 00:45:58 +02:00
set _mime : function ( mime ) {
if ( ! mime )
{
this . options . mime = null ;
}
if ( mime . indexOf ( "/" ) != 0 )
{
// Lower case it now, if it's not a regex
this . options . mime = mime . toLowerCase ( ) ;
}
else
{
// Convert into a js regex
2013-04-13 21:00:13 +02:00
var parts = mime . substr ( 1 ) . match ( /(.*)\/([igm]?)$/ ) ;
2012-03-30 00:45:58 +02:00
this . options . mime = new RegExp ( parts [ 1 ] , parts . length > 2 ? parts [ 2 ] : "" ) ;
}
} ,
set _multiple : function ( _multiple ) {
this . options . multiple = _multiple ;
if ( _multiple )
{
return this . input . attr ( "multiple" , "multiple" ) ;
}
return this . input . removeAttr ( "multiple" ) ;
} ,
/ * *
* Check to see if the provided file ' s mimetype matches
*
* @ param f File object
* @ return boolean
* /
checkMime : function ( f ) {
// If missing, let the server handle it
if ( ! this . options . mime || ! f . type ) return true ;
var is _preg = ( typeof this . options . mime == "object" ) ;
if ( ! is _preg && f . type . toLowerCase ( ) == this . options . mime || is _preg && this . options . mime . test ( f . type ) )
{
return true ;
}
// Not right mime
return false ;
} ,
2014-04-01 22:48:40 +02:00
_fileAdded : function ( file , event ) {
2014-07-07 22:26:48 +02:00
// Manual additions have no event
if ( typeof event == 'undefined' )
{
event = { } ;
}
2014-04-01 22:48:40 +02:00
// Trigger start of uploading, calls callback
if ( ! this . resumable . isUploading ( ) )
{
if ( ! ( this . onStart ( event , this . resumable . files . length ) ) ) return ;
}
// Here 'this' is the input
if ( this . checkMime ( file . file ) )
{
if ( this . createStatus ( event , file ) )
{
2018-03-28 18:20:11 +02:00
// Disable buttons
2018-04-10 10:17:08 +02:00
this . disabled _buttons
2018-03-28 18:20:11 +02:00
. not ( "[disabled]" )
. attr ( "disabled" , true )
. addClass ( 'et2_button_ro' )
. removeClass ( 'et2_clickable' )
. css ( 'cursor' , 'default' ) ;
2018-04-10 10:17:08 +02:00
2014-04-01 22:48:40 +02:00
// Actually start uploading
this . resumable . upload ( ) ;
}
}
else
{
// Wrong mime type - show in the list of files
2015-09-02 21:52:47 +02:00
return this . createStatus (
this . egw ( ) . lang ( "File is of wrong type (%1 != %2)!" , file . file . type , this . options . mime ) ,
file
2014-04-01 22:48:40 +02:00
) ;
}
} ,
2015-08-07 16:18:07 +02:00
2011-09-08 22:33:46 +02:00
/ * *
* Add in the request id
* /
beforeSend : function ( form ) {
var instance = this . getInstanceManager ( ) ;
2015-08-07 16:18:07 +02:00
2014-04-01 22:48:40 +02:00
return {
request _id : instance . etemplate _exec _id ,
widget _id : this . id
} ;
2011-09-08 22:33:46 +02:00
} ,
2011-09-06 21:55:52 +02:00
/ * *
* Disables submit buttons while uploading
* /
onStart : function ( event , file _count ) {
2014-04-01 22:48:40 +02:00
// Hide any previous errors
this . hideMessage ( ) ;
2011-09-09 02:05:18 +02:00
event . data = this ;
2015-08-07 16:18:07 +02:00
2014-10-01 16:19:36 +02:00
//Add dropdown_progress
if ( this . options . progress _dropdownlist )
{
this . _build _progressDropDownList ( ) ;
}
2015-08-07 16:18:07 +02:00
2014-04-01 22:48:40 +02:00
// Callback
2013-04-20 21:21:42 +02:00
if ( this . options . onStart ) return et2 _call ( this . options . onStart , event , file _count ) ;
2011-09-06 21:55:52 +02:00
return true ;
} ,
/ * *
* Re - enables submit buttons when done
* /
2014-04-01 22:48:40 +02:00
onFinish : function ( ) {
2016-04-20 13:38:07 +02:00
this . disabled _buttons . attr ( "disabled" , false ) . css ( 'cursor' , 'pointer' ) . removeClass ( 'et2_button_ro' ) ;
2015-08-07 16:18:07 +02:00
2014-04-01 22:48:40 +02:00
var file _count = this . resumable . files . length ;
// Remove files from list
2015-05-11 21:33:57 +02:00
while ( this . resumable . files . length > 0 )
2014-04-01 22:48:40 +02:00
{
2015-05-11 21:33:57 +02:00
this . resumable . removeFile ( this . resumable . files [ this . resumable . files . length - 1 ] ) ;
2014-04-01 22:48:40 +02:00
}
2014-09-09 14:59:06 +02:00
var event = jQuery . Event ( 'upload' ) ;
2015-08-07 16:18:07 +02:00
2011-09-09 02:05:18 +02:00
event . data = this ;
2014-04-01 22:48:40 +02:00
var result = false ;
2015-08-07 16:18:07 +02:00
2014-11-21 09:58:58 +01:00
//Remove progress_dropDown_fileList class and unbind the click handler from body
if ( this . options . progress _dropdownlist )
{
this . progress . removeClass ( "progress_dropDown_fileList" ) ;
jQuery ( this . node ) . find ( 'span' ) . removeClass ( 'totalProgress_loader' ) ;
jQuery ( 'body' ) . off ( 'click' ) ;
2015-08-07 16:18:07 +02:00
}
2014-03-25 22:03:35 +01:00
if ( this . options . onFinish && ! jQuery . isEmptyObject ( this . getValue ( ) ) )
{
2014-04-01 22:48:40 +02:00
result = et2 _call ( this . options . onFinish , event , file _count ) ;
}
else
{
result = ( file _count == 0 || ! jQuery . isEmptyObject ( this . getValue ( ) ) ) ;
}
if ( result )
{
// Fire legacy change action when done
this . change ( this . input ) ;
2014-03-25 22:03:35 +01:00
}
2014-10-01 16:19:36 +02:00
} ,
2015-08-07 16:18:07 +02:00
2014-10-01 16:19:36 +02:00
/ * *
* Build up dropdown progress with total count indicator
2015-08-07 16:18:07 +02:00
*
2014-10-01 16:19:36 +02:00
* @ todo Implement totalProgress bar instead of ajax - loader , in order to show how much percent of uploading is completed
* /
_build _progressDropDownList : function ( )
{
this . progress . addClass ( "progress_dropDown_fileList" ) ;
2015-08-07 16:18:07 +02:00
2014-10-01 16:19:36 +02:00
//Add uploading indicator and bind hover handler on it
2015-01-13 14:25:12 +01:00
jQuery ( this . node ) . find ( 'span' ) . addClass ( 'totalProgress_loader' ) ;
2015-08-07 16:18:07 +02:00
2016-07-29 12:45:41 +02:00
jQuery ( this . node ) . find ( 'span.et2_file_span' ) . hover ( function ( ) {
2014-10-01 16:19:36 +02:00
jQuery ( '.progress_dropDown_fileList' ) . show ( ) ;
} ) ;
//Bind click handler to dismiss the dropdown while uploading
jQuery ( 'body' ) . on ( 'click' , function ( event ) {
if ( event . target . className != 'remove' )
{
jQuery ( '.progress_dropDown_fileList' ) . hide ( ) ;
2015-08-07 16:18:07 +02:00
}
2014-10-01 16:19:36 +02:00
} ) ;
2015-08-07 16:18:07 +02:00
2011-09-06 21:55:52 +02:00
} ,
2015-08-07 16:18:07 +02:00
2011-09-01 01:37:30 +02:00
/ * *
2011-09-02 00:07:30 +02:00
* Creates the elements used for displaying the file , and it ' s upload status , and
* attaches them to the DOM
2012-03-30 00:45:58 +02:00
*
* @ param _event Either the event , or an error message
2011-09-01 01:37:30 +02:00
* /
2014-04-01 22:48:40 +02:00
createStatus : function ( _event , file ) {
2012-03-30 00:45:58 +02:00
var error = ( typeof _event == "object" ? "" : _event ) ;
2014-04-01 22:48:40 +02:00
if ( this . options . max _file _size && file . size > this . options . max _file _size ) {
error = this . egw ( ) . lang ( "File too large. Maximum %1" , et2 _vfsSize . prototype . human _size ( this . options . max _file _size ) ) ;
2011-09-06 21:55:52 +02:00
}
2015-08-07 16:18:07 +02:00
2013-09-10 14:57:40 +02:00
if ( this . options . progress )
{
var widget = this . getRoot ( ) . getWidgetById ( this . options . progress ) ;
if ( widget )
{
2016-06-02 16:51:15 +02:00
this . progress = jQuery ( widget . getDOMNode ( ) ) ;
2013-09-10 14:57:40 +02:00
this . progress . addClass ( "progress" ) ;
}
}
2011-09-02 00:07:30 +02:00
if ( this . progress )
{
2014-04-01 22:48:40 +02:00
var fileName = file . fileName || 'file' ;
2017-04-18 17:34:22 +02:00
var status = jQuery ( "<li data-file='" + fileName . replace ( /'/g , '"' ) + "'>" + fileName
2011-09-06 21:55:52 +02:00
+ "<div class='remove'/><span class='progressBar'><p/></span></li>" )
2011-09-09 02:05:18 +02:00
. appendTo ( this . progress ) ;
2016-06-02 16:51:15 +02:00
jQuery ( "div.remove" , status ) . on ( 'click' , file , jQuery . proxy ( this . cancel , this ) ) ;
2011-09-06 21:55:52 +02:00
if ( error != "" )
{
2013-08-10 01:34:42 +02:00
status . addClass ( "message ui-state-error" ) ;
2011-09-06 21:55:52 +02:00
status . append ( "<div>" + error + "</diff>" ) ;
2016-06-02 16:51:15 +02:00
jQuery ( ".progressBar" , status ) . css ( "display" , "none" ) ;
2011-09-06 21:55:52 +02:00
}
}
return error == "" ;
} ,
2014-04-01 22:48:40 +02:00
_fileProgress : function ( file ) {
2011-09-06 21:55:52 +02:00
if ( this . progress )
{
2017-04-18 17:34:22 +02:00
jQuery ( "li[data-file='" + file . fileName . replace ( /'/g , '"' ) + "'] > span.progressBar > p" ) . css ( "width" , Math . ceil ( file . progress ( ) * 100 ) + "%" ) ;
2011-09-06 21:55:52 +02:00
2011-09-01 01:37:30 +02:00
}
2011-09-02 00:07:30 +02:00
return true ;
2011-09-01 01:37:30 +02:00
} ,
2011-09-06 21:55:52 +02:00
onError : function ( event , name , error ) {
2013-09-10 14:57:40 +02:00
console . warn ( event , name , error ) ;
2011-09-06 21:55:52 +02:00
} ,
2011-09-01 01:37:30 +02:00
/ * *
2011-09-02 00:07:30 +02:00
* A file upload is finished , update the UI
2011-09-01 01:37:30 +02:00
* /
2014-04-01 22:48:40 +02:00
finishUpload : function ( file , response ) {
var name = file . fileName || 'file' ;
2011-09-06 21:55:52 +02:00
if ( typeof response == 'string' ) response = jQuery . parseJSON ( response ) ;
2012-03-30 00:45:58 +02:00
if ( response . response [ 0 ] && typeof response . response [ 0 ] . data . length == 'undefined' ) {
2017-04-10 18:39:04 +02:00
if ( typeof this . options . value !== 'object' || ! this . options . multiple )
{
this . set _value ( { } ) ;
}
2011-09-06 21:55:52 +02:00
for ( var key in response . response [ 0 ] . data ) {
2012-03-30 00:45:58 +02:00
if ( typeof response . response [ 0 ] . data [ key ] == "string" )
{
// Message from server - probably error
2017-04-18 17:34:22 +02:00
jQuery ( "[data-file='" + name . replace ( /'/g , '"' ) + "']" , this . progress )
2012-03-30 00:45:58 +02:00
. addClass ( "error" )
. css ( "display" , "block" )
. text ( response . response [ 0 ] . data [ key ] ) ;
}
else
{
this . options . value [ key ] = response . response [ 0 ] . data [ key ] ;
2017-04-13 16:52:01 +02:00
// If not multiple, we already destroyed the status, so re-create it
if ( ! this . options . multiple )
{
this . createStatus ( { } , file ) ;
}
2012-03-30 00:45:58 +02:00
if ( this . progress )
{
2017-04-18 17:34:22 +02:00
jQuery ( "[data-file='" + name . replace ( /'/g , '"' ) + "']" , this . progress ) . addClass ( "message success" ) ;
2012-03-30 00:45:58 +02:00
}
}
2011-09-06 21:55:52 +02:00
}
}
else if ( this . progress )
2011-09-02 00:07:30 +02:00
{
2017-04-18 17:34:22 +02:00
jQuery ( "[data-file='" + name . replace ( /'/g , '"' ) + "']" , this . progress )
2013-08-10 01:34:42 +02:00
. addClass ( "ui-state-error" )
2012-03-30 00:45:58 +02:00
. css ( "display" , "block" )
. text ( this . egw ( ) . lang ( "Server error" ) ) ;
2011-09-06 21:55:52 +02:00
}
2015-01-14 10:05:34 +01:00
var event = jQuery . Event ( 'upload' ) ;
2015-08-07 16:18:07 +02:00
2015-01-14 10:05:34 +01:00
event . data = this ;
2015-08-07 16:18:07 +02:00
2014-04-01 22:48:40 +02:00
// Callback
2013-04-20 21:21:42 +02:00
if ( this . options . onFinishOne )
2012-04-24 18:33:56 +02:00
{
2014-04-01 22:48:40 +02:00
return et2 _call ( this . options . onFinishOne , event , response , name ) ;
2012-04-24 18:33:56 +02:00
}
2011-09-06 21:55:52 +02:00
return true ;
} ,
/ * *
2013-04-22 18:52:03 +02:00
* Remove a file from the list of values
2014-09-18 20:56:43 +02:00
*
* @ param { File | string } File object , or file name , to remove
2011-09-06 21:55:52 +02:00
* /
2014-04-01 22:48:40 +02:00
remove _file : function ( file )
2013-04-22 18:52:03 +02:00
{
2013-09-10 14:57:40 +02:00
//console.info(filename);
2014-09-18 20:56:43 +02:00
if ( typeof file == 'string' )
{
file = { fileName : file } ;
}
2013-04-22 18:52:03 +02:00
for ( var key in this . options . value )
{
2014-04-01 22:48:40 +02:00
if ( this . options . value [ key ] . name == file . fileName )
2011-09-06 21:55:52 +02:00
{
2013-04-22 18:52:03 +02:00
delete this . options . value [ key ] ;
2017-04-18 17:34:22 +02:00
jQuery ( '[data-file="' + file . fileName . replace ( /'/g , '"' ) + '"]' , this . node ) . remove ( ) ;
2011-09-06 21:55:52 +02:00
return ;
}
2011-09-02 00:07:30 +02:00
}
2014-09-18 20:56:43 +02:00
if ( file . isComplete && ! file . isComplete ( ) && file . cancel ) file . cancel ( ) ;
2013-04-22 18:52:03 +02:00
} ,
/ * *
* Cancel a file - event callback
* /
cancel : function ( e )
{
e . preventDefault ( ) ;
// Look for file name in list
2016-06-02 16:51:15 +02:00
var target = jQuery ( e . target ) . parents ( "li" ) ;
2015-08-07 16:18:07 +02:00
2014-04-01 22:48:40 +02:00
this . remove _file ( e . data ) ;
2011-09-06 21:55:52 +02:00
// In case it didn't make it to the list (error)
2011-09-09 02:05:18 +02:00
target . remove ( ) ;
2016-06-02 16:51:15 +02:00
jQuery ( e . target ) . remove ( ) ;
2016-04-18 12:37:08 +02:00
} ,
/ * *
* Set readonly
*
* @ param { boolean } _ro boolean readonly state , true means readonly
* /
set _readonly : function ( _ro )
{
if ( typeof _ro != "undefined" )
{
this . options . readonly = _ro ;
this . span . toggleClass ( 'et2_file_ro' , _ro ) ;
if ( this . options . readonly )
{
this . span . unbind ( 'click' ) ;
}
else
{
var self = this ;
this . span . off ( ) . bind ( 'click' , function ( ) { self . input . click ( ) } ) ;
}
}
2011-09-01 01:37:30 +02:00
}
2016-02-29 21:40:43 +01:00
} ) ; } ) . call ( this ) ;
2011-09-01 01:37:30 +02:00
et2 _register _widget ( et2 _file , [ "file" ] ) ;