2011-09-01 01:37:30 +02:00
/ * *
* eGroupWare eTemplate2 - JS Number object
*
* @ 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$
* /
"use strict" ;
/ * e g w : u s e s
et2 _core _inputWidget ;
2011-09-02 00:07:30 +02:00
phpgwapi . jquery . jquery . html5 _upload ;
2011-09-01 01:37:30 +02:00
* /
/ * *
* Class which implements file upload
* /
var et2 _file = et2 _inputWidget . extend ( {
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" ,
"default" : "8388608" ,
"description" : "Largest file accepted, in bytes. Subject to server limitations. 8Mb = 8388608"
} ,
"blur" : {
"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."
} ,
"progress" : {
"name" : "Progress node" ,
"type" : "string" ,
"default" : et2 _no _init ,
"description" : "The ID of an alternate node (div) to display progress and results."
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."
2011-09-01 01:37:30 +02:00
}
} ,
2011-09-02 00:07:30 +02:00
asyncOptions : { } ,
2011-09-01 01:37:30 +02:00
init : function ( ) {
this . _super . apply ( this , arguments )
this . node = null ;
2011-09-08 22:33:46 +02:00
this . input = null ;
this . progress = null ;
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
2011-09-02 00:07:30 +02:00
// Set up the URL to have the request ID & the widget ID
var instance = this . getInstanceManager ( ) ;
2011-09-08 22:33:46 +02:00
2011-09-02 00:07:30 +02:00
var self = this ;
this . asyncOptions = {
// Callbacks
2011-09-06 21:55:52 +02:00
onStart : function ( event , file _count ) { return self . onStart ( event , file _count ) ; } ,
onFinish : function ( event , file _count ) { return self . onFinish ( event , file _count ) ; } ,
onStartOne : function ( event , file _name , index , file _count ) { return self . createStatus ( event , file _name , index , file _count ) ; } ,
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-02 00:07:30 +02:00
sendBoundary : window . FormData || jQuery . browser . mozilla ,
2011-09-08 22:33:46 +02:00
beforeSend : function ( form ) { return self . beforeSend ( form ) ; } ,
url : egw _json _request . prototype . _assembleAjaxUrl ( "etemplate_widget_file::ajax_upload::etemplate" )
2011-09-02 00:07:30 +02:00
} ;
this . asyncOptions . fieldName = this . options . id ;
2011-09-01 01:37:30 +02:00
this . createInputWidget ( ) ;
} ,
2011-09-08 22:33:46 +02:00
destroy : function ( ) {
this . node = null ;
this . input = null ;
this . progress = null ;
} ,
2011-09-01 01:37:30 +02:00
createInputWidget : function ( ) {
this . node = $j ( document . createElement ( "div" ) ) . addClass ( "et2_file" ) ;
this . input = $j ( document . createElement ( "input" ) )
. attr ( "type" , "file" ) . attr ( "placeholder" , this . options . blur )
2011-09-02 00:07:30 +02:00
. a ppendTo ( this . node ) ;
// Check for File interface, should fall back to normal form submit if missing
if ( typeof File != "undefined" && typeof ( new XMLHttpRequest ( ) ) . upload != "undefined" )
{
this . input . html5 _upload ( this . asyncOptions ) ;
}
2011-09-06 21:55:52 +02:00
else
{
// This may be a problem submitting via ajax
}
2011-09-01 01:37:30 +02:00
this . progress = this . options . progress ?
$j ( document . getElementById ( this . options . progress ) ) :
$j ( document . createElement ( "div" ) ) . appendTo ( this . node ) ;
this . progress . addClass ( "progress" ) ;
if ( this . options . multiple ) {
this . input . attr ( "multiple" , "multiple" ) ;
}
this . setDOMNode ( this . node [ 0 ] ) ;
} ,
2011-09-06 21:55:52 +02:00
getValue : function ( ) {
var value = this . options . value ? this . options . value : this . input . val ( ) ;
return value ;
} ,
2011-09-01 01:37:30 +02:00
getInputNode : function ( ) {
return this . input [ 0 ] ;
} ,
2011-09-08 22:33:46 +02:00
/ * *
* Add in the request id
* /
beforeSend : function ( form ) {
var instance = this . getInstanceManager ( ) ;
// Form object available, mess with it directly
if ( form ) {
return form . append ( "request_id" , instance . etemplate _exec _id ) ;
}
// No Form object, add in as string
return 'Content-Disposition: form-data; name="request_id"\r\n\r\n' + instance . etemplate _exec _id + '\r\n' ;
} ,
2011-09-06 21:55:52 +02:00
/ * *
* Disables submit buttons while uploading
* /
onStart : function ( event , file _count ) {
this . disabled _buttons = $j ( "input[type='submit'], button" ) . not ( "[disabled]" ) . attr ( "disabled" , true ) ;
2011-09-09 02:05:18 +02:00
event . data = this ;
if ( this . options . onStart && typeof this . options . onStart == 'function' ) return this . options . onStart ( event , file _count ) ;
2011-09-06 21:55:52 +02:00
return true ;
} ,
/ * *
* Re - enables submit buttons when done
* /
onFinish : function ( event , file _count ) {
this . disabled _buttons . attr ( "disabled" , false ) ;
2011-09-09 02:05:18 +02:00
event . data = this ;
if ( this . options . onFinish && typeof this . options . onFinish == 'function' ) return this . options . onFinish ( event , file _count ) ;
2011-09-06 21:55:52 +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
2011-09-01 01:37:30 +02:00
* /
2011-09-06 21:55:52 +02:00
createStatus : function ( event , file _name , index , file _count ) {
var error = ""
if ( this . input [ 0 ] . files [ index ] ) {
var file = this . input [ 0 ] . files [ index ] ;
if ( file . size > this . options . max _file _size ) {
error = egw . lang ( "File too large" ) ;
}
}
2011-09-02 00:07:30 +02:00
if ( this . progress )
{
2011-09-06 21:55:52 +02:00
var status = $j ( "<li file='" + file _name + "'>" + file _name
+ "<div class='remove'/><span class='progressBar'><p/></span></li>" )
2011-09-09 02:05:18 +02:00
. appendTo ( this . progress ) ;
$j ( "div.remove" , status ) . click ( this , this . cancel ) ;
2011-09-06 21:55:52 +02:00
if ( error != "" )
{
status . addClass ( "message validation_error" ) ;
status . append ( "<div>" + error + "</diff>" ) ;
$j ( ".progressBar" , status ) . css ( "display" , "none" ) ;
}
}
return error == "" ;
} ,
onProgress : function ( event , percent , name , number , total ) {
if ( this . progress )
{
$j ( "li[file='" + name + "'] > span.progressBar > p" ) . css ( "width" , Math . ceil ( percent * 100 ) + "%" ) ;
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 ) {
console . warn ( event , name , error ) ;
} ,
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
* /
2011-09-02 00:07:30 +02:00
finishUpload : function ( event , response , name , number , total ) {
2011-09-06 21:55:52 +02:00
if ( typeof response == 'string' ) response = jQuery . parseJSON ( response ) ;
if ( response . response [ 0 ] . data && typeof response . response [ 0 ] . data . length == 'undefined' ) {
if ( typeof this . options . value != 'object' ) this . options . value = { } ;
for ( var key in response . response [ 0 ] . data ) {
this . options . value [ key ] = response . response [ 0 ] . data [ key ] ;
}
if ( this . progress )
{
$j ( "[file='" + name + "']" , this . progress ) . addClass ( "message success" ) ;
}
}
else if ( this . progress )
2011-09-02 00:07:30 +02:00
{
2011-09-06 21:55:52 +02:00
$j ( "[file='" + name + "']" , this . progress ) . addClass ( "error" ) . css ( "display" , "block" ) ;
}
return true ;
} ,
/ * *
* Cancel a file
* /
cancel : function ( e ) {
e . preventDefault ( ) ;
// Look for file name in list
2011-09-09 02:05:18 +02:00
var target = $j ( e . target ) . parents ( "li.message" ) ;
console . info ( target ) ;
2011-09-06 21:55:52 +02:00
for ( var key in e . data . options . value ) {
if ( e . data . options . value [ key ] . name == target . attr ( "file" ) )
{
delete e . data . options . value [ key ] ;
2011-09-09 02:05:18 +02:00
target . remove ( ) ;
2011-09-06 21:55:52 +02:00
return ;
}
2011-09-02 00:07:30 +02:00
}
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 ( ) ;
2011-09-06 21:55:52 +02:00
$j ( e . target ) . remove ( ) ;
2011-09-01 01:37:30 +02:00
}
} ) ;
et2 _register _widget ( et2 _file , [ "file" ] ) ;