File: 5up.js

package info (click to toggle)
collabtive 2.0%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,708 kB
  • ctags: 27,425
  • sloc: php: 100,872; sh: 72; makefile: 50
file content (196 lines) | stat: -rw-r--r-- 4,829 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 *
 * @access public
 * @return void
 **/
function finisher(){
blindtoggle('form_file');
toggleClass('addfile','addfile-active','addfile');
toggleClass('add_file_butn','butn_link_active','butn_link');
toggleClass('sm_files','smooth','nosmooth');
changeFileview($('fileviewtype').value,$('folderparent').value);
$("statusrow").setAttribute("style","display:none;");
$("fileInfo1").innerHTML = "";
systemMsg("fileadded");
}
function checkCompat(){
try{
		formDataChk = new FormData();
	}
	catch(e)
	{
		formDataChk = false;
	}
	try{
		xhrChk = new XMLHttpRequest();
	}
	catch(e)
	{
		xhrChk = false;
	}

	if(xhrChk && formDataChk)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function getSelectedOptions(oList)
{
   var sdValues = [];
   for(var i = 1; i < oList.options.length; i++)
   {
      if(oList.options[i].selected == true)
      {
      sdValues.push(oList.options[i].value);
      }
   }
   return sdValues;
}
function html5up(theFiles,theInfoEl,theIndicator,theTarget)
{
	//File upload element
	this.theFiles = document.getElementById(theFiles);
	//HTML Element where the file info / list is displayed
	this.infoEl = document.getElementById(theInfoEl);
	//script where the upload is sent
	this.theTarget = theTarget;
	//HTML Element where the upload progress is displayed
	indicator = document.getElementById(theIndicator);
}

//gets information about the files selected in theFiles and prints them to an HTML element
html5up.prototype.fileInfo = function()
{
   var theFiles = this.theFiles.files;
   //String to be written to the HTML element where the fileinfo is displayed
   var outstr = "";

   outstr += "<ol style = 'margin-left:120px;'>";
   //loop through all the files from the file input field
   for(var i=0;i<theFiles.length;i++)
   {
	   if (theFiles[i]) {
	      //Determine the size of the file
		  var fileSize = 0;
	      if (theFiles[i].size > 1024 * 1024)
	      {
	        fileSize = (Math.round(theFiles[i].size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
	      }
		  else
	      {
		    fileSize = (Math.round(theFiles[i].size * 100 / 1024) / 100).toString() + 'KB';
		  }

		//Write a list item and the filename into the file info
		outstr += "<li>" + theFiles[i].name + "</li>";
	    }
    }
	outstr += "</ol>";
	//Write the file info to the HTML element
	this.infoEl.innerHTML = outstr;
}

//uploads files to the script specified in theTarget
html5up.prototype.upload = function() {
	var theFiles = this.theFiles.files;
	var myData = new FormData();
	var upfolder = $('upfolder').value;
	var sendtos = getSelectedOptions($('sendto'));
	myData.append("upfolder",upfolder);

	//Loop through the files and create an upload form object
	for(var i = 0;i<theFiles.length;i++)
	{
		myData.append("html5upFile"+i, theFiles[i]);
	}

	for(var i=0;i<sendtos.length;i++)
	{
		myData.append("sendto[]",sendtos[i]);
	}
	//console.log(sendtos);
	//Create new XMLHttpRequest
	var xhr = new XMLHttpRequest();

	//Add event listeners
 	xhr.upload.addEventListener("progress", this.progress, false);
    xhr.addEventListener("load", this.complete, false);
    xhr.addEventListener("error", this.failed, false);
    xhr.addEventListener("abort", this.canceled, false);

	//Open the connection
	xhr.open("POST",this.theTarget,true);
    //Send the upload form with the files
	xhr.send(myData);

}

//check if the browser can support 5up.js
html5up.prototype.checkBrowser = function()
{
	try{
		formDataChk = new FormData();
	}
	catch(e)
	{
		formDataChk = false;
	}
	try{
		xhrChk = new XMLHttpRequest();
	}
	catch(e)
	{
		xhrChk = false;
	}

	if(xhrChk && formDataChk)
	{
		return true;
	}
	else
	{
		return false;
	}


}

//Event handlers
//display progress. you can overwrite this on runtime to implement different display options
html5up.prototype.progress = function(evt) {
	if (evt.lengthComputable) {
        //Compute the progress percentage
		var percentComplete = Math.round(evt.loaded * 100 / evt.total);
        var total = percentComplete.toString();
        //Update the indicator Element with the percentage
		//indicator.innerHTML  =  total + "%";
        indicator.setAttribute("style","width:"+total + "%");
		//document.title = total + "%";
       }
       else {
         indicator.innerHTML  = "N/A";
       }
}

//this is fired when the UL is complete
html5up.prototype.complete = function(evt) {
		//console.log(evt.target.responseText);

        indicator.setAttribute("style","width:100%");
        window.setTimeout("finisher()",600);
        //document.title = "100%";
      }

//fired when there is an error
html5up.prototype.failed = function(evt) {
        //console.log(evt.target.responseText);
      }
//fired when the upload has been canceled / connection lost
html5up.prototype.canceled = function(evt) {
        alert("The upload has been canceled.");
      }