vzaar 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ // Constructor
2
+ // file is a SWFUpload file object
3
+ // targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
4
+ // Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements
5
+ function FileProgress(file, targetID) {
6
+ this.fileProgressID = file.id;
7
+
8
+ this.opacity = 100;
9
+ this.height = 0;
10
+
11
+
12
+ this.fileProgressWrapper = document.getElementById(this.fileProgressID);
13
+ if (!this.fileProgressWrapper) {
14
+ this.fileProgressWrapper = document.createElement("div");
15
+ this.fileProgressWrapper.className = "progressWrapper";
16
+ this.fileProgressWrapper.id = this.fileProgressID;
17
+
18
+ this.fileProgressElement = document.createElement("div");
19
+ this.fileProgressElement.className = "progressContainer";
20
+
21
+ var progressCancel = document.createElement("a");
22
+ progressCancel.className = "progressCancel";
23
+ progressCancel.href = "#";
24
+ progressCancel.style.visibility = "hidden";
25
+ progressCancel.appendChild(document.createTextNode(" "));
26
+
27
+ var progressText = document.createElement("div");
28
+ progressText.className = "progressName";
29
+ progressText.appendChild(document.createTextNode(file.name));
30
+
31
+ var progressBar = document.createElement("div");
32
+ progressBar.className = "progressBarInProgress";
33
+
34
+ var progressStatus = document.createElement("div");
35
+ progressStatus.className = "progressBarStatus";
36
+ progressStatus.innerHTML = " ";
37
+
38
+ this.fileProgressElement.appendChild(progressCancel);
39
+ this.fileProgressElement.appendChild(progressText);
40
+ this.fileProgressElement.appendChild(progressStatus);
41
+ this.fileProgressElement.appendChild(progressBar);
42
+
43
+ this.fileProgressWrapper.appendChild(this.fileProgressElement);
44
+
45
+ document.getElementById(targetID).appendChild(this.fileProgressWrapper);
46
+ } else {
47
+ this.fileProgressElement = this.fileProgressWrapper.firstChild;
48
+ this.reset();
49
+ }
50
+
51
+ this.height = this.fileProgressWrapper.offsetHeight;
52
+ this.setTimer(null);
53
+
54
+
55
+ }
56
+
57
+ FileProgress.prototype.setTimer = function (timer) {
58
+ this.fileProgressElement["FP_TIMER"] = timer;
59
+ };
60
+ FileProgress.prototype.getTimer = function (timer) {
61
+ return this.fileProgressElement["FP_TIMER"] || null;
62
+ };
63
+
64
+ FileProgress.prototype.reset = function () {
65
+ this.fileProgressElement.className = "progressContainer";
66
+
67
+ this.fileProgressElement.childNodes[2].innerHTML = " ";
68
+ this.fileProgressElement.childNodes[2].className = "progressBarStatus";
69
+
70
+ this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
71
+ this.fileProgressElement.childNodes[3].style.width = "0%";
72
+
73
+ this.appear();
74
+ };
75
+
76
+ FileProgress.prototype.setProgress = function (percentage) {
77
+ this.fileProgressElement.className = "progressContainer green";
78
+ this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
79
+ this.fileProgressElement.childNodes[3].style.width = percentage + "%";
80
+
81
+ this.appear();
82
+ };
83
+ FileProgress.prototype.setComplete = function () {
84
+ this.fileProgressElement.className = "progressContainer blue";
85
+ this.fileProgressElement.childNodes[3].className = "progressBarComplete";
86
+ this.fileProgressElement.childNodes[3].style.width = "";
87
+
88
+ var oSelf = this;
89
+ this.setTimer(setTimeout(function () {
90
+ oSelf.disappear();
91
+ }, 10000));
92
+ };
93
+ FileProgress.prototype.setError = function () {
94
+ this.fileProgressElement.className = "progressContainer red";
95
+ this.fileProgressElement.childNodes[3].className = "progressBarError";
96
+ this.fileProgressElement.childNodes[3].style.width = "";
97
+
98
+ var oSelf = this;
99
+ this.setTimer(setTimeout(function () {
100
+ oSelf.disappear();
101
+ }, 5000));
102
+ };
103
+ FileProgress.prototype.setCancelled = function () {
104
+ this.fileProgressElement.className = "progressContainer";
105
+ this.fileProgressElement.childNodes[3].className = "progressBarError";
106
+ this.fileProgressElement.childNodes[3].style.width = "";
107
+
108
+ var oSelf = this;
109
+ this.setTimer(setTimeout(function () {
110
+ oSelf.disappear();
111
+ }, 2000));
112
+ };
113
+ FileProgress.prototype.setStatus = function (status) {
114
+ this.fileProgressElement.childNodes[2].innerHTML = status;
115
+ };
116
+
117
+ // Show/Hide the cancel button
118
+ FileProgress.prototype.toggleCancel = function (show, swfUploadInstance) {
119
+ this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
120
+ if (swfUploadInstance) {
121
+ var fileID = this.fileProgressID;
122
+ this.fileProgressElement.childNodes[0].onclick = function () {
123
+ swfUploadInstance.cancelUpload(fileID);
124
+ return false;
125
+ };
126
+ }
127
+ };
128
+
129
+ FileProgress.prototype.appear = function () {
130
+ if (this.getTimer() !== null) {
131
+ clearTimeout(this.getTimer());
132
+ this.setTimer(null);
133
+ }
134
+
135
+ if (this.fileProgressWrapper.filters) {
136
+ try {
137
+ this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
138
+ } catch (e) {
139
+ // If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
140
+ this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
141
+ }
142
+ } else {
143
+ this.fileProgressWrapper.style.opacity = 1;
144
+ }
145
+
146
+ this.fileProgressWrapper.style.height = "";
147
+
148
+ this.height = this.fileProgressWrapper.offsetHeight;
149
+ this.opacity = 100;
150
+ this.fileProgressWrapper.style.display = "";
151
+
152
+ };
153
+
154
+ // Fades out and clips away the FileProgress box.
155
+ FileProgress.prototype.disappear = function () {
156
+
157
+ var reduceOpacityBy = 15;
158
+ var reduceHeightBy = 4;
159
+ var rate = 30; // 15 fps
160
+
161
+ if (this.opacity > 0) {
162
+ this.opacity -= reduceOpacityBy;
163
+ if (this.opacity < 0) {
164
+ this.opacity = 0;
165
+ }
166
+
167
+ if (this.fileProgressWrapper.filters) {
168
+ try {
169
+ this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
170
+ } catch (e) {
171
+ // If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
172
+ this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
173
+ }
174
+ } else {
175
+ this.fileProgressWrapper.style.opacity = this.opacity / 100;
176
+ }
177
+ }
178
+
179
+ if (this.height > 0) {
180
+ this.height -= reduceHeightBy;
181
+ if (this.height < 0) {
182
+ this.height = 0;
183
+ }
184
+
185
+ this.fileProgressWrapper.style.height = this.height + "px";
186
+ }
187
+
188
+ if (this.height > 0 || this.opacity > 0) {
189
+ var oSelf = this;
190
+ this.setTimer(setTimeout(function () {
191
+ oSelf.disappear();
192
+ }, rate));
193
+ } else {
194
+ this.fileProgressWrapper.style.display = "none";
195
+ this.setTimer(null);
196
+ }
197
+ };
@@ -0,0 +1,259 @@
1
+ /* Demo Note: This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
2
+ The FileProgress class is not part of SWFUpload.
3
+ */
4
+
5
+
6
+ /* **********************
7
+ Event Handlers
8
+ These are my custom event handlers to make my
9
+ web application behave the way I went when SWFUpload
10
+ completes different tasks. These aren't part of the SWFUpload
11
+ package. They are part of my application. Without these none
12
+ of the actions SWFUpload makes will show up in my application.
13
+ ********************** */
14
+ var trackFiles = [];
15
+ var trackFilesCount = 0;
16
+ var trackSentURL = false;
17
+ var forceDone = false;
18
+ var forceFile = null;
19
+ var master = null;
20
+ var MacMinSizeUpload = 150000; // 150k, this is not cool :(
21
+ var MacDelay = 10000; // 10 secs.
22
+
23
+
24
+ function fileQueued(file) {
25
+ try {
26
+ var progress = new FileProgress(file, this.customSettings.progressTarget);
27
+ progress.setStatus("Pending...");
28
+ progress.toggleCancel(true, this);
29
+
30
+ } catch (ex) {
31
+ this.debug(ex);
32
+ }
33
+
34
+ }
35
+
36
+ function fileQueueError(file, errorCode, message) {
37
+ try {
38
+ if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
39
+ alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
40
+ return;
41
+ }
42
+
43
+ var progress = new FileProgress(file, this.customSettings.progressTarget);
44
+ progress.setError();
45
+ progress.toggleCancel(false);
46
+
47
+ switch (errorCode) {
48
+ case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
49
+ progress.setStatus("File is too big.");
50
+ this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
51
+ break;
52
+ case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
53
+ progress.setStatus("Cannot upload Zero Byte files.");
54
+ this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
55
+ break;
56
+ case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
57
+ progress.setStatus("Invalid File Type.");
58
+ this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
59
+ break;
60
+ default:
61
+ if (file !== null) {
62
+ progress.setStatus("Unhandled Error");
63
+ }
64
+ this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
65
+ break;
66
+ }
67
+ } catch (ex) {
68
+ this.debug(ex);
69
+ }
70
+ }
71
+
72
+ function fileDialogComplete(numFilesSelected, numFilesQueued) {
73
+ try {
74
+ this.startUpload();
75
+ } catch (ex) {
76
+ this.debug(ex);
77
+ }
78
+ }
79
+
80
+ function GetXmlHttpObject() {
81
+ if (window.XMLHttpRequest) {
82
+ return new XMLHttpRequest();
83
+ }
84
+ if (window.ActiveXObject) {
85
+ return new ActiveXObject("Microsoft.XMLHTTP");
86
+ }
87
+ return null;
88
+ }
89
+
90
+ function uploadStart(file) {
91
+ try {
92
+ /* I don't want to do any file validation or anything, I'll just update the UI and
93
+ return true to indicate that the upload should start.
94
+ It's important to update the UI here because in Linux no uploadProgress events are called. The best
95
+ we can do is say we are uploading.
96
+ */
97
+
98
+ var req = GetXmlHttpObject();
99
+ req.open("GET", '/vzaar/signature', false);
100
+ // This does not work in IE
101
+ //req.overrideMimeType('application/json');
102
+ req.send(null);
103
+ res = json_parse(req.responseText);
104
+ this.customSettings['guid'] = res['guid'];
105
+ this.setPostParams(res);
106
+ this.removePostParam('guid');
107
+
108
+ var progress = new FileProgress(file, this.customSettings.progressTarget);
109
+ progress.setStatus("Uploading...");
110
+ progress.toggleCancel(true, this);
111
+ trackFiles[trackFilesCount++] = file.name;
112
+ updateDisplay.call(this, file);
113
+ }
114
+ catch (ex) {}
115
+
116
+ return true;
117
+ }
118
+
119
+ function uploadProgress(file, bytesLoaded, bytesTotal) {
120
+ try {
121
+ var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
122
+ var progress = new FileProgress(file, this.customSettings.progressTarget);
123
+ var animPic = document.getElementById("loadanim");
124
+
125
+ if (animPic != null) {
126
+ animPic.style.display = 'block';
127
+ }
128
+ progress.setProgress(percent);
129
+ progress.setStatus("Uploading..."+(isMacUser && file.size < MacMinSizeUpload ? ' ...Finishing up, 10 second delay' : ''));
130
+ updateDisplay.call(this, file);
131
+
132
+
133
+ } catch (ex) {
134
+ this.debug(ex);
135
+ }
136
+ }
137
+
138
+ function uploadSuccess(file, serverData) {
139
+ try {
140
+ var progress = new FileProgress(file, this.customSettings.progressTarget);
141
+ progress.setComplete();
142
+ progress.setStatus("Complete.");
143
+ progress.toggleCancel(false);
144
+
145
+ // For some reason the handler is called twice for the first file
146
+ // in a queue, so to prevent the same file from being uploaded twice
147
+ // we log files that have been already uploaded to the server.
148
+ if (this.customSettings['uploaded_files'].indexOf(file.id) == -1) {
149
+ var req = GetXmlHttpObject();
150
+ params = "guid=" + this.customSettings['guid'];
151
+ params += '&';
152
+ params += "filename=" + encodeURIComponent(file.name);
153
+ req.open("POST", '/vzaar/process_video', true);
154
+ req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
155
+ req.setRequestHeader("Content-length", params.length);
156
+ req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
157
+ req.send(params);
158
+ this.customSettings['uploaded_files'].push(file.id);
159
+ }
160
+
161
+ } catch (ex) {
162
+ this.debug(ex);
163
+ }
164
+ }
165
+
166
+ function uploadComplete(file) {
167
+ }
168
+
169
+ function uploadError(file, errorCode, message) {
170
+ try {
171
+ var progress = new FileProgress(file, this.customSettings.progressTarget);
172
+ progress.setError();
173
+ progress.toggleCancel(false);
174
+
175
+ switch (errorCode) {
176
+ case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
177
+ progress.setStatus("Upload Error: " + message);
178
+ this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
179
+ break;
180
+ case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
181
+ progress.setStatus("Upload Failed.");
182
+ this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
183
+ break;
184
+ case SWFUpload.UPLOAD_ERROR.IO_ERROR:
185
+ progress.setStatus("Server (IO) Error");
186
+ this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
187
+ break;
188
+ case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
189
+ progress.setStatus("Security Error");
190
+ this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
191
+ break;
192
+ case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
193
+ progress.setStatus("Upload limit exceeded.");
194
+ this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
195
+ break;
196
+ case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
197
+ progress.setStatus("Failed Validation. Upload skipped.");
198
+ this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
199
+ break;
200
+ case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
201
+ progress.setStatus("Cancelled");
202
+ progress.setCancelled();
203
+ break;
204
+ case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
205
+ progress.setStatus("Stopped");
206
+ break;
207
+ default:
208
+ progress.setStatus("Unhandled Error: " + errorCode);
209
+ this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
210
+ break;
211
+ }
212
+ } catch (ex) {
213
+ this.debug(ex);
214
+ }
215
+ }
216
+
217
+ // This event comes from the Queue Plugin
218
+ function queueComplete(numFilesUploaded) {
219
+ var status = document.getElementById("divStatus");
220
+ status.innerHTML = numFilesUploaded + " file" + (numFilesUploaded === 1 ? "" : "s") + " uploaded.";
221
+ }
222
+
223
+
224
+ function updateDisplay(file) {
225
+
226
+ // isMacUser Patch Begin
227
+ if ( isMacUser ) {
228
+ if (file == null && forceDone) {
229
+ master.cancelUpload(forceFile.id,false);
230
+ pauseProcess(500); // allow flash? to update itself
231
+ master.uploadSuccess(forceFile,null);
232
+ master.uploadComplete(forceFile);
233
+ forceDone = false;
234
+ return;
235
+ }
236
+ // check for small files less < 150k
237
+ // note: dialup users will get bad results.
238
+ if (file.size < MacMinSizeUpload && !forceDone) {
239
+ master = this;
240
+ if (!forceDone) {
241
+ forceFile = file;
242
+ // wait <n> seconds before enforcing upload done!
243
+ setTimeout("updateDisplay("+null+")",MacDelay);
244
+ forceDone = true;
245
+ }
246
+ }
247
+ } // isMacUser Patch End
248
+ }
249
+
250
+
251
+ // this should *not* be needed, just testing an idea
252
+ function pauseProcess(millis) {
253
+ var sDate = new Date();
254
+ var cDate = null;
255
+
256
+ do {
257
+ cDate = new Date();
258
+ } while(cDate-sDate < millis);
259
+ }