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-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-02 00:07:30 +02:00
// Set up the URL to have the request ID & the widget ID
var instance = this . getInstanceManager ( ) ;
var self = this ;
this . asyncOptions = {
// Callbacks
onStartOne : function ( event , file _name ) { return self . createStatus ( event , file _name ) ; } ,
onFinishOne : function ( event , response , name , number , total ) { return self . finishUpload ( event , response , name , number , total ) ; } ,
sendBoundary : window . FormData || jQuery . browser . mozilla ,
url : egw _json _request . prototype . _assembleAjaxUrl ( "etemplate_widget_file::ajax_upload" ) +
"&request_id=" + instance . etemplate _exec _id
} ;
this . asyncOptions . fieldName = this . options . id ;
2011-09-01 01:37:30 +02:00
this . createInputWidget ( ) ;
} ,
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
. appendTo ( 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-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 ] ) ;
} ,
getInputNode : function ( ) {
return this . input [ 0 ] ;
} ,
/ * *
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-02 00:07:30 +02:00
createStatus : function ( event , file _name ) {
if ( this . progress )
{
$j ( "<li file='" + file _name + "'>" + file _name + "<div class='progressBar'><p/></div></li>" ) . appendTo ( this . progress ) ;
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-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 ) {
if ( this . progress )
{
$j ( "[file='" + name + "']" , this . progress ) . addClass ( "upload_finished" ) ;
}
2011-09-01 01:37:30 +02:00
}
} ) ;
et2 _register _widget ( et2 _file , [ "file" ] ) ;