Menu

[r766]: / branches / code-cleanup / swfupload / swfupload.js  Maximize  Restore  History

Download this file

363 lines (319 with data), 13.3 kB

  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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* SWFUpload v2.0 by Jacob Roberts, Feb 2008, http://www.swfupload.org, http://linebyline.blogspot.com
* -------- -------- -------- -------- -------- -------- -------- --------
* SWFUpload is (c) 2006 Lars Huring and Mammon Media and is released under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* Simplified javascript interface by Batiste Bieler. Use of jquery for simplicity.
*/
var SWFUpload = function(init_settings) {
this.init(init_settings);
};
SWFUpload.instances = {};
SWFUpload.movieCount = 0;
SWFUpload.FILE_STATUS = {
QUEUED : -1,
IN_PROGRESS: -2,
ERROR: -3,
COMPLETE: -4,
CANCELLED: -5
};
SWFUpload.prototype.init = function(init_settings) {
this.settings = {};
this.eventQueue = [];
this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
// Setup global control tracking
SWFUpload.instances[this.movieName] = this;
// Load the settings. Load the Flash movie.
this.initSettings(init_settings);
this.loadFlash();
}
SWFUpload.prototype.initSettings = function(init_settings) {
// non-overrideable event handler
this.flashReady_handler = SWFUpload.flashReady;
// overrideable event handler that can receive several parameters, typicaly : file, error_code, message. Look at the flash source for more details.
possible_handlers = ['uploadReady', 'fileDialogStart', 'fileQueued', 'fileQueueError', 'fileDialogComplete', 'uploadProgress', 'uploadError', 'uploadSuccess', 'uploadComplete', 'debug']
var self = this;
var getHandler = function(name) {
if(init_settings[name])
return init_settings[name];
else if(self[name])
return self[name];
else
return function(){self.debug(handler_name+" is not implemented")};
}
var constructHandler = function(handler_name) {
var func = getHandler(handler_name);
return function(param1, param2, param3) {
setTimeout(function() {
self.eventQueue[self.eventQueue.length] = function() {
func.call(self, param1, param2, param3);
};
self.executeNextEvent();
}, 0);
}
}
for(index in possible_handlers) {
var handler_name = possible_handlers[index];
this[handler_name] = constructHandler(handler_name);
}
// Gets called when a file upload is about to be started.
this["uploadStart"] = function(file) {
var movie_element = self.getMovieElement();
self.eventQueue[self.eventQueue.length] = function() {
// true is needed here to start upload
var uploadStart = getHandler("uploadStart")(file);
movie_element.ReturnUploadStart(uploadStart);
};
setTimeout(function() { self.executeNextEvent();}, 0);
};
// Set default_value to undefined to trigger an error if the
// user need to override a value.
var add = function(param_name, default_value) {
if(!default_value && !init_settings[param_name])
self.debug("Error: " + param_name+" need to be defined in settings")
self.addSetting(param_name, init_settings[param_name], default_value);
}
// Debug setting
add("debug_enabled", false);
// File Settings
add("file_types", "*.*");
add("file_types_description", "All Files");
add("file_size_limit", "1024");
add("file_upload_limit", "0");
add("file_queue_limit", "0");
// Flash Settings
add("upload_url", undefined);
add("flash_url", undefined);
// Changing this value will break linux flash player
add("file_post_name", "FileData");
add("post_params", {});
};
SWFUpload.prototype.loadFlash = function() {
if (document.getElementById(this.movieName)) {
return false;
}
// Append the container and load the flash
var container = $('<div style="width:1px;height:1px"></div>');
container.html(this.getFlashHTML());
$('body').append(container);
};
// Generates the embed/object tags needed to embed the flash in to the document
SWFUpload.prototype.getFlashHTML = function() {
var html = "";
html +='<object id="' + this.movieName + '" data="' + this.getSetting("flash_url")+'" type="application/x-shockwave-flash" width="1" height="1">';
html +='<param name="movie" value="' + this.getSetting("flash_url") + '" />';
html +='<param name="flashvars" value="' + this.getFlashVars() +'" />';
html +='</object>';
return html;
};
/* This private method builds the parameter string that will be passed to flash. */
SWFUpload.prototype.getFlashVars = function() {
// Build a string from the post param object
var param_string = this.buildParamString();
// Build the parameter string
var html = "";
html += "movieName=" + encodeURIComponent(this.movieName);
html += "&uploadURL=" + encodeURIComponent(this.getSetting("upload_url"));
html += "&params=" + encodeURIComponent(param_string);
html += "&filePostName=" + encodeURIComponent(this.getSetting("file_post_name"));
html += "&fileTypes=" + encodeURIComponent(this.getSetting("file_types"));
html += "&fileTypesDescription=" + encodeURIComponent(this.getSetting("file_types_description"));
html += "&fileSizeLimit=" + encodeURIComponent(this.getSetting("file_size_limit"));
html += "&fileUploadLimit=" + encodeURIComponent(this.getSetting("file_upload_limit"));
html += "&fileQueueLimit=" + encodeURIComponent(this.getSetting("file_queue_limit"));
html += "&debugEnabled=" + encodeURIComponent(this.getSetting("debug_enabled"));
return html;
};
SWFUpload.prototype.getMovieElement = function() {
if (!this.movieElement) {
if(!document.getElementById(this.movieName)) {
this.debug("Movie is not in DOM.")
}
this.movieElement = document.getElementById(this.movieName);
}
return this.movieElement;
};
SWFUpload.prototype.buildParamString = function() {
var post_params = this.getSetting("post_params");
var param_string_pairs = [];
// Retrieve the user defined parameters
for (var name in post_params) {
param_string_pairs.push(encodeURIComponent(name) + "=" + encodeURIComponent(post_params[name]));
}
return param_string_pairs.join("&");
};
// Saves a setting. If the value given is undefined or null then the default_value is used.
SWFUpload.prototype.addSetting = function(name, value, default_value) {
if (!value)
this.settings[name] = default_value;
else
this.settings[name] = value;
return this.settings[name];
};
// Gets a setting. Returns empty string if not found.
SWFUpload.prototype.getSetting = function(name) {
if (this.settings[name])
return this.settings[name];
return "";
};
/* Flash control methods */
// helper to call movie clip functions
SWFUpload.prototype.movie_helper = function(func_name, func, file_id, name, value) {
var movie_element = this.getMovieElement();
try {
return movie_element[func_name](file_id, name, value);
}
catch(ex) {
this.debug("Could not call "+func_name);
}
};
// these functions are a little bit stupid
SWFUpload.prototype.selectFile = function() {
this.movie_helper("SelectFile");
};
SWFUpload.prototype.selectFiles = function() {
this.movie_helper("SelectFiles");
};
// Cancels a the file upload. You must specify a file_id */
SWFUpload.prototype.cancelUpload = function(file_id) {
this.movie_helper("CancelUpload", file_id);
};
// Stops the current upload. The file is re-queued. If nothing is currently uploading then nothing happens. */
SWFUpload.prototype.stopUpload = function() {
this.movie_helper("StopUpload");
};
SWFUpload.prototype.getStats = function() {
return this.movie_helper("GetStats");
};
// Start the upload. If a file_id is specified that file is uploaded. Otherwise the first file in the queue is uploaded. This call uses setTimeout since Flash will be calling back in to JavaScript
SWFUpload.prototype.startUpload = function(file_id) {
var self = this;
var movie_element = this.getMovieElement();
if (movie_element && movie_element.StartUpload) {
setTimeout(
function() {
try {
movie_element.StartUpload(file_id);
}
catch (ex) {
self.debug("Could not call StartUpload: " + ex);
}
}, 0
);
} else {
this.debug("Could not find Flash element");
}
};
SWFUpload.prototype.cancelQueue = function() {
this.stopUpload();
var stats;
do {
stats = this.getStats();
this.cancelUpload();
} while (stats.files_queued !== 0);
};
SWFUpload.prototype.getFile = function(file_id) {
if (typeof(file_id) === "number") {
return this.movie_helper("GetFileByIndex", file_id);
} else {
return this.movie_helper("GetFile", file_id);
}
};
// Internal Event Callers. These event callers ensure that your custom event handlers are called safely and in order.
// This is the callback method that the Flash movie will call when it has been loaded and is ready to go.
SWFUpload.prototype.flashReady = function() {
var movie_element = this.getMovieElement();
if (!movie_element || !movie_element.StartUpload) {
this.debug("ExternalInterface methods failed to initialize.");
return;
}
var self = this;
this.eventQueue[this.eventQueue.length] = function() {
if (self.uploadReady)
self.uploadReady();
};
setTimeout(function() { self.executeNextEvent();}, 0);
};
// Events are placed in a queue and then executed.
SWFUpload.prototype.executeNextEvent = function() {
var f = this.eventQueue.shift();
if(f)
f();
}
// Gets called when a file upload is about to be started.
SWFUpload.prototype.uploadStart = function(file) {
var self = this;
var movie_element = this.getMovieElement();
if (this.uploadStart_handler) {
this.eventQueue[this.eventQueue.length] = function() {
// true is needed here to start upload
var uploadStart = self.uploadStart_handler(file);
movie_element.ReturnUploadStart(uploadStart);
};
setTimeout(function() { self.executeNextEvent();}, 0);
} else {
this.debug("uploadStart event not defined");
}
};
// Handle errors that occur when an attempt to queue a file fails.
SWFUpload.prototype.fileQueueError = function(file, error_code, message) {
var msg = [];
msg[-100] = "Upload limit reached";
msg[-110] = "File too big";
msg[-120] = "Zero byte file";
msg[-130] = "File extension is not allowed";
var m = "Error Code: " + msg[error_code] + ", Message: " + msg;
if(file) {
m += "File name: " + file.name + ", File size: " + file.size;
}
this.debug(m);
};
SWFUpload.prototype.uploadErrorMessage = function(error_code) {
var msg = [];
msg[-200] = "HTTP Error";
msg[-210] = "No backend file";
msg[-220] = "IO Error";
msg[-230] = "Security error";
msg[-240] = "Upload limit reached";
msg[-250] = "Upload failed";
msg[-260] = "File ID specified for upload was not found";
msg[-270] = "uploadStart callback returned false";
msg[-280] = "The file upload was cancelled";
msg[-290] = "The file upload was stopped";
return msg[error_code];
}
// Called when an error occurs during upload. For HTTP errors 'message' will contain the HTTP STATUS CODE
SWFUpload.prototype.uploadError = function(file, error_code, message) {
if(!file){
var file = {'name':'None', 'size':'None'};
}
var msg = this.uploadErrorMessage(error_code);
this.debug("Error Code: "+msg[index]+", File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
};
// Override this method in your settings to call your own debug message handler
SWFUpload.prototype.debug = function(message) {
if (this.getSetting("debug_enabled")) {
this.debugMessage(message);
}
};
SWFUpload.prototype.debugMessage = function(message) {
var console = $("#swfupload-console");
if (!console[0]) {
console = $('<textarea id="swfupload-console" cols="80" rows="20"></textarea>');
$('body').append(console);
}
if (typeof(message) === "object") {
var exception_message = "";
var exception_values = [];
for (var key in message) {
exception_values.push(key + ": " + message[key]);
}
exception_message = exception_values.join("\n");
exception_values = exception_message.split("\n");
exception_message = "Exception: " + exception_values.join("\Exception: ");
console.val(console.val()+exception_message);
} else {
console.val(console.val()+message+"\n");
}
};
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.