Team:Team:Freiburg software/Code/qooxdoo/Upload.js

From 2009.igem.org

/**
 * File Upload Widget
 * 
 * @author Frederic Fournaise (http://frederic.fournaise.free.fr/qooxdoo-file-upload-widget.php), David Nellessen (mail@davidn.de)
 * @version 20070528
 * @copyright   Frederic Fournaise
 * @license  GNU Lesser General Public License, see http://www.opensource.org/licenses/lgpl-license.php 
 *
 * Change:
 * - 6.10.2009 (David Nellessen, mail@davidn.de): Changed to 8.0.x API; use qx.ui.embed.Iframe for creating the Irame and implemented onLoad event; Added Button for submitting form (to fire sending event); added property filename which is set to the filename of the uploaded file
 * - (David Nellessen): changed a lot of stuff
 * 
 *
 **/
 
qx.Class.define("qooxwaveclient.Upload",{
  extend : qx.ui.container.Composite,  //changed

  construct : function(vUploadCGI, deltaid, store, addButton, enctype){
	this.base(arguments);
	this.setLayout(new qx.ui.layout.Canvas());  //changed

	this.upload = new qx.ui.groupbox.GroupBox();
	
	this.store = store;
	this.deltaid = deltaid;
	this.addButton = addButton;
	this.vUploadCGI = vUploadCGI;
	if(!enctype) this.enctype = "application/x-www-form-urlencoded";
	else this.enctype = enctype;
	
	var vUniqueId = (new Date).valueOf();
	var vFrameName = "frame_" + vUniqueId;
	//vFrameName = "_blank";
	this.uploadFrm = "upload_" + vUniqueId;
	this.inputfile = null;
	
	//form
	this.input=new qx.ui.embed.Html();
	this.input.set({ height : 30, width  : 300 });  //changed
	this.input.setHtml('<form enctype="'+this.enctype+'" target="'+vFrameName+'" action="'+this.vUploadCGI+'" method="post" id="'+this.uploadFrm+'" name="'+this.uploadFrm+'"><input type="file" name="upload"/></form>');
	this.add(this.input);   //changed

	//submit button //changed
	if(this.addButton){
		this.submitButton = new qx.ui.form.Button("submit");
		this.add(this.submitButton, {left : 305});
		this.submitButton.addListener("execute", this.submit, this);
	}
	
	if(false){ //testin

		this.testingButton = new qx.ui.form.Button("Simulate Onclick");
		this.add(this.testingButton, {left : 305});
		this.testingButton.addListener("execute", function(){
			d=document.forms[this.uploadFrm];
			d.upload.click();
			alert(d.upload.value);
		}, this);
	}

	//iframe // changed
	this._iframeWidget = new qx.ui.embed.Iframe ("javascript:void(0)");
	this._iframeWidget.setFrameName(vFrameName);
	this._iframeWidget.addListener("load", this._onload, this);
	this._iframeWidget.setHeight(1);
	this.add(this._iframeWidget, {top: 30});
	this._iframeWidget.setOpacity(0);
  },
  
  events: {
    "sending" : "qx.event.type.Event",
    "error" : "qx.event.type.Event",
    "onload" : "qx.event.type.Event"
  },
  
  properties :{
    isSent : {init:false, check: "Boolean"},
    fileName : {init: new String(), check: "String", event : "ChangeFilename"}
  },
  
  members :{
	_onload : function(){  //changed
	    if (this.getIsSent()){
		if(this.addButton){
	    	this.submitButton.setEnabled(true);
	    	this.submitButton.setIcon(null);
		}
		this.submitDelta("completed");
		this.fireEvent("onload");  
	    }
	},
	_onSubmit : function () { //changed
		if(this.addButton){
			this.submitButton.setEnabled(false);
			this.submitButton.setIcon("qooxwaveclient/ajax-loader.gif");
		}
		this.submitDelta("submitted");
		this.fireEvent("sending");
	},
	submit : function () {
		this.debug("FIRE EXECUTE");
		d=document.forms[this.uploadFrm];
		this.setFileName(d.upload.value);
		if(d.upload.value){
		  d.submit();
		  this.setIsSent( true);
		  this._onSubmit();
	    }
		else this.fireEvent("error");
	},
	submitDelta : function (event) {
		var delta = new Object();
		var id = this.deltaid;
    	var value = this.getFileName();
    	delta[id] = "{'event' : '" + event + "', 'value' : { 'filename' : '" + value + "'} }";
		this.store.submitEventDelta(delta);
	},
	modefieUri :function (param){
		this.debug("PARAMETER TO SEND" + param);
		d=document.forms[this.uploadFrm];
		d.action = this.vUploadCGI + "?" + param;
	}
  }
});