summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/source/prado/activefileupload/activefileupload.js
blob: 5f20944e09d3b6cba67935626a9769dc527913a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Prado.WebUI.TActiveFileUpload = Class.create(Prado.WebUI.Control,
{
	onInit : function(options)
	{
		this.options = options || {};
		Prado.WebUI.TActiveFileUpload.register(this);
		
		this.input = $(options.inputID);
		this.flag = $(options.flagID);
		this.form = $(options.formID);
		
		this.indicator = $(options.indicatorID);
		this.complete = $(options.completeID);
		this.error = $(options.errorID);
		
		// set up events
		if (options.autoPostBack){
			this.observe(this.input,"change",this.fileChanged.bind(this));
		}
	},
	
	fileChanged : function(){
		// show the upload indicator, and hide the complete and error indicators (if they areSn't already).
		this.flag.value = '1';
		this.complete.style.display = 'none';
		this.error.style.display = 'none';
		this.indicator.style.display = '';

		// set the form to submit in the iframe, submit it, and then reset it.
		this.oldtargetID = this.form.target;
		this.oldFormAction = this.form.action;
		this.oldFormMethod = this.form.method;
		this.oldFormEnctype = this.form.enctype;

		this.form.action += (this.form.action.indexOf('?')!=-1 ? '&' : '?')+'TActiveFileUpload_InputId='+this.options.inputID+'&TActiveFileUpload_TargetId='+this.options.targetID;
		this.form.target = this.options.targetID;
		this.form.method = 'POST';
		this.form.enctype = 'multipart/form-data';
		this.form.submit();

		this.form.action = this.oldFormAction;
		this.form.target = this.oldtargetID;
		this.form.method = this.oldFormMethod;
		this.form.enctype = this.oldFormEnctype;
	},
	
	finishUpload : function(options){

		if (this.options.targetID == options.targetID)
         		{
				this.finishoptions = options;
         			var e = this;
         			var callback =
         			{
         				'CallbackParameter' : options || '',
         				'onSuccess' : function() { e.finishCallBack(true); },
					'onFailure' : function() { e.finishCallBack(false); }
         			};

         			Object.extend(callback, this.options);

         			var request = new Prado.CallbackRequest(this.options.EventTarget, callback);
         			request.dispatch();
         		}
		else
			this.finishCallBack(true);

	},

	finishCallBack : function(success){
		// hide the display indicator.
		this.flag.value = '';
		this.indicator.style.display = 'none';
       		// show the complete indicator.
       		if ((this.finishoptions.errorCode == 0) && (success)) {
       			this.complete.style.display = '';
       			this.input.value = '';
       		} else {
       			this.error.style.display = '';
       		}
	}

});

Object.extend(Prado.WebUI.TActiveFileUpload, 
{
	//class methods

	controls : {},

	register : function(control)
	{
		Prado.WebUI.TActiveFileUpload.controls[control.options.ID] = control;
	},
	
	onFileUpload : function(options)
	{
		Prado.WebUI.TActiveFileUpload.controls[options.clientID].finishUpload(options);
	},
	
	fileChanged : function(controlID){
		Prado.WebUI.TActiveFileUpload.controls[controlID].fileChanged();
	}
});