uploader 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +275 -0
  3. data/Rakefile +66 -0
  4. data/TODO +2 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/uploader/uploads_controller.rb +122 -0
  7. data/app/helpers/uploader_helper.rb +17 -0
  8. data/app/views/uploads/_swf_javascript.html.erb +63 -0
  9. data/app/views/uploads/_swf_upload.html.erb +32 -0
  10. data/config/uploader_routes.rb +3 -0
  11. data/db/migrate/20090517040220_create_uploads.rb +38 -0
  12. data/lib/active_record/acts/uploader_upload.rb +235 -0
  13. data/lib/uploader/exceptions.rb +5 -0
  14. data/lib/uploader/initialize_routes.rb +8 -0
  15. data/lib/uploader/middleware/flash_session_cookie_middleware.rb +18 -0
  16. data/lib/uploader/mime_type_groups.rb +18 -0
  17. data/lib/uploader/tasks.rb +36 -0
  18. data/lib/uploader.rb +24 -0
  19. data/locales/ar.yml +17 -0
  20. data/locales/bg.yml +17 -0
  21. data/locales/ca.yml +17 -0
  22. data/locales/cs.yml +17 -0
  23. data/locales/da.yml +17 -0
  24. data/locales/de.yml +17 -0
  25. data/locales/el.yml +17 -0
  26. data/locales/en.yml +16 -0
  27. data/locales/es.yml +17 -0
  28. data/locales/fr.yml +17 -0
  29. data/locales/it.yml +17 -0
  30. data/locales/iw.yml +17 -0
  31. data/locales/ja.yml +17 -0
  32. data/locales/ko.yml +17 -0
  33. data/locales/lt.yml +17 -0
  34. data/locales/lv.yml +17 -0
  35. data/locales/nl.yml +17 -0
  36. data/locales/no.yml +18 -0
  37. data/locales/pl.yml +17 -0
  38. data/locales/pt.yml +17 -0
  39. data/locales/ro.yml +17 -0
  40. data/locales/ru.yml +17 -0
  41. data/locales/sk.yml +17 -0
  42. data/locales/sl.yml +17 -0
  43. data/locales/sr.yml +17 -0
  44. data/locales/sv.yml +17 -0
  45. data/locales/tl.yml +17 -0
  46. data/locales/uk.yml +17 -0
  47. data/locales/vi.yml +17 -0
  48. data/locales/zh-CN.yml +17 -0
  49. data/locales/zh-TW.yml +17 -0
  50. data/locales/zh.yml +17 -0
  51. data/pkg/uploader-0.1.0.gem +0 -0
  52. data/pkg/uploader-0.1.1.gem +0 -0
  53. data/pkg/uploader-0.1.2.gem +0 -0
  54. data/public/images/SWFUploadButton.png +0 -0
  55. data/public/images/file_icons/excel.gif +0 -0
  56. data/public/images/file_icons/file.gif +0 -0
  57. data/public/images/file_icons/mp3.gif +0 -0
  58. data/public/images/file_icons/pdf.gif +0 -0
  59. data/public/images/file_icons/text.gif +0 -0
  60. data/public/images/file_icons/word.gif +0 -0
  61. data/public/javascripts/swfupload/fileprogress.js +151 -0
  62. data/public/javascripts/swfupload/handlers.js +206 -0
  63. data/public/javascripts/swfupload/swfupload.cookies.js +53 -0
  64. data/public/javascripts/swfupload/swfupload.js +945 -0
  65. data/public/javascripts/swfupload/swfupload.queue.js +77 -0
  66. data/public/javascripts/swfupload/swfupload.swfobject.js +110 -0
  67. data/public/stylesheets/swfupload.css +26 -0
  68. data/public/swf/swfupload.swf +0 -0
  69. data/rails/init.rb +10 -0
  70. data/tasks/rails.rake +2 -0
  71. data/test/test_helper.rb +3 -0
  72. data/test/unit/upload_test.rb +5 -0
  73. data/uninstall.rb +0 -0
  74. data/uploader.gemspec +127 -0
  75. metadata +128 -0
@@ -0,0 +1,945 @@
1
+ /**
2
+ * SWFUpload: http://www.swfupload.org, http://swfupload.googlecode.com
3
+ *
4
+ * mmSWFUpload 1.0: Flash upload dialog - http://profandesign.se/swfupload/, http://www.vinterwebb.se/
5
+ *
6
+ * SWFUpload is (c) 2006-2007 Lars Huring, Olov Nilz�n and Mammon Media and is released under the MIT License:
7
+ * http://www.opensource.org/licenses/mit-license.php
8
+ *
9
+ * SWFUpload 2 is (c) 2007-2008 Jake Roberts and is released under the MIT License:
10
+ * http://www.opensource.org/licenses/mit-license.php
11
+ *
12
+ */
13
+
14
+
15
+ /* ******************* */
16
+ /* Constructor & Init */
17
+ /* ******************* */
18
+ var SWFUpload;
19
+
20
+ if (SWFUpload == undefined) {
21
+ SWFUpload = function (settings) {
22
+ this.initSWFUpload(settings);
23
+ };
24
+ }
25
+
26
+ SWFUpload.prototype.initSWFUpload = function (settings) {
27
+ try {
28
+ this.customSettings = {}; // A container where developers can place their own settings associated with this instance.
29
+ this.settings = settings;
30
+ this.eventQueue = [];
31
+ this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
32
+ this.movieElement = null;
33
+
34
+
35
+ // Setup global control tracking
36
+ SWFUpload.instances[this.movieName] = this;
37
+
38
+ // Load the settings. Load the Flash movie.
39
+ this.initSettings();
40
+ this.loadFlash();
41
+ this.displayDebugInfo();
42
+ } catch (ex) {
43
+ delete SWFUpload.instances[this.movieName];
44
+ throw ex;
45
+ }
46
+ };
47
+
48
+ /* *************** */
49
+ /* Static Members */
50
+ /* *************** */
51
+ SWFUpload.instances = {};
52
+ SWFUpload.movieCount = 0;
53
+ SWFUpload.version = "2.2.0 Beta 3";
54
+ SWFUpload.QUEUE_ERROR = {
55
+ QUEUE_LIMIT_EXCEEDED : -100,
56
+ FILE_EXCEEDS_SIZE_LIMIT : -110,
57
+ ZERO_BYTE_FILE : -120,
58
+ INVALID_FILETYPE : -130
59
+ };
60
+ SWFUpload.UPLOAD_ERROR = {
61
+ HTTP_ERROR : -200,
62
+ MISSING_UPLOAD_URL : -210,
63
+ IO_ERROR : -220,
64
+ SECURITY_ERROR : -230,
65
+ UPLOAD_LIMIT_EXCEEDED : -240,
66
+ UPLOAD_FAILED : -250,
67
+ SPECIFIED_FILE_ID_NOT_FOUND : -260,
68
+ FILE_VALIDATION_FAILED : -270,
69
+ FILE_CANCELLED : -280,
70
+ UPLOAD_STOPPED : -290
71
+ };
72
+ SWFUpload.FILE_STATUS = {
73
+ QUEUED : -1,
74
+ IN_PROGRESS : -2,
75
+ ERROR : -3,
76
+ COMPLETE : -4,
77
+ CANCELLED : -5
78
+ };
79
+ SWFUpload.BUTTON_ACTION = {
80
+ SELECT_FILE : -100,
81
+ SELECT_FILES : -110,
82
+ START_UPLOAD : -120
83
+ };
84
+ SWFUpload.CURSOR = {
85
+ ARROW : -1,
86
+ HAND : -2
87
+ };
88
+ SWFUpload.WINDOW_MODE = {
89
+ WINDOW : "window",
90
+ TRANSPARENT : "transparent",
91
+ OPAQUE : "opaque"
92
+ };
93
+
94
+ /* ******************** */
95
+ /* Instance Members */
96
+ /* ******************** */
97
+
98
+ // Private: initSettings ensures that all the
99
+ // settings are set, getting a default value if one was not assigned.
100
+ SWFUpload.prototype.initSettings = function () {
101
+ this.ensureDefault = function (settingName, defaultValue) {
102
+ this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
103
+ };
104
+
105
+ // Upload backend settings
106
+ this.ensureDefault("upload_url", "");
107
+ this.ensureDefault("file_post_name", "Filedata");
108
+ this.ensureDefault("post_params", {});
109
+ this.ensureDefault("use_query_string", false);
110
+ this.ensureDefault("requeue_on_error", false);
111
+ this.ensureDefault("http_success", []);
112
+
113
+ // File Settings
114
+ this.ensureDefault("file_types", "*.*");
115
+ this.ensureDefault("file_types_description", "All Files");
116
+ this.ensureDefault("file_size_limit", 0); // Default zero means "unlimited"
117
+ this.ensureDefault("file_upload_limit", 0);
118
+ this.ensureDefault("file_queue_limit", 0);
119
+
120
+ // Flash Settings
121
+ this.ensureDefault("flash_url", "swfupload.swf");
122
+ this.ensureDefault("prevent_swf_caching", true);
123
+
124
+ // Button Settings
125
+ this.ensureDefault("button_image_url", "");
126
+ this.ensureDefault("button_width", 1);
127
+ this.ensureDefault("button_height", 1);
128
+ this.ensureDefault("button_text", "");
129
+ this.ensureDefault("button_text_style", "color: #000000; font-size: 16pt;");
130
+ this.ensureDefault("button_text_top_padding", 0);
131
+ this.ensureDefault("button_text_left_padding", 0);
132
+ this.ensureDefault("button_action", SWFUpload.BUTTON_ACTION.SELECT_FILES);
133
+ this.ensureDefault("button_disabled", false);
134
+ this.ensureDefault("button_placeholder_id", null);
135
+ this.ensureDefault("button_cursor", SWFUpload.CURSOR.ARROW);
136
+ this.ensureDefault("button_window_mode", SWFUpload.WINDOW_MODE.WINDOW);
137
+
138
+ // Debug Settings
139
+ this.ensureDefault("debug", false);
140
+ this.settings.debug_enabled = this.settings.debug; // Here to maintain v2 API
141
+
142
+ // Event Handlers
143
+ this.settings.return_upload_start_handler = this.returnUploadStart;
144
+ this.ensureDefault("swfupload_loaded_handler", null);
145
+ this.ensureDefault("file_dialog_start_handler", null);
146
+ this.ensureDefault("file_queued_handler", null);
147
+ this.ensureDefault("file_queue_error_handler", null);
148
+ this.ensureDefault("file_dialog_complete_handler", null);
149
+
150
+ this.ensureDefault("upload_start_handler", null);
151
+ this.ensureDefault("upload_progress_handler", null);
152
+ this.ensureDefault("upload_error_handler", null);
153
+ this.ensureDefault("upload_success_handler", null);
154
+ this.ensureDefault("upload_complete_handler", null);
155
+
156
+ this.ensureDefault("debug_handler", this.debugMessage);
157
+
158
+ this.ensureDefault("custom_settings", {});
159
+
160
+ // Other settings
161
+ this.customSettings = this.settings.custom_settings;
162
+
163
+ // Update the flash url if needed
164
+ if (this.settings.prevent_swf_caching) {
165
+ this.settings.flash_url = this.settings.flash_url + "?swfuploadrnd=" + Math.floor(Math.random() * 999999999);
166
+ }
167
+
168
+ delete this.ensureDefault;
169
+ };
170
+
171
+ SWFUpload.prototype.loadFlash = function () {
172
+ if (this.settings.button_placeholder_id !== "") {
173
+ this.replaceWithFlash();
174
+ } else {
175
+ this.appendFlash();
176
+ }
177
+ };
178
+
179
+ // Private: appendFlash gets the HTML tag for the Flash
180
+ // It then appends the flash to the body
181
+ SWFUpload.prototype.appendFlash = function () {
182
+ var targetElement, container;
183
+
184
+ // Make sure an element with the ID we are going to use doesn't already exist
185
+ if (document.getElementById(this.movieName) !== null) {
186
+ throw "ID " + this.movieName + " is already in use. The Flash Object could not be added";
187
+ }
188
+
189
+ // Get the body tag where we will be adding the flash movie
190
+ targetElement = document.getElementsByTagName("body")[0];
191
+
192
+ if (targetElement == undefined) {
193
+ throw "Could not find the 'body' element.";
194
+ }
195
+
196
+ // Append the container and load the flash
197
+ container = document.createElement("div");
198
+ container.style.width = "1px";
199
+ container.style.height = "1px";
200
+ container.style.overflow = "hidden";
201
+
202
+ targetElement.appendChild(container);
203
+ container.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
204
+
205
+ // Fix IE Flash/Form bug
206
+ if (window[this.movieName] == undefined) {
207
+ window[this.movieName] = this.getMovieElement();
208
+ }
209
+
210
+
211
+ };
212
+
213
+ // Private: replaceWithFlash replaces the button_placeholder element with the flash movie.
214
+ SWFUpload.prototype.replaceWithFlash = function () {
215
+ var targetElement, tempParent;
216
+
217
+ // Make sure an element with the ID we are going to use doesn't already exist
218
+ if (document.getElementById(this.movieName) !== null) {
219
+ throw "ID " + this.movieName + " is already in use. The Flash Object could not be added";
220
+ }
221
+
222
+ // Get the element where we will be placing the flash movie
223
+ targetElement = document.getElementById(this.settings.button_placeholder_id);
224
+
225
+ if (targetElement == undefined) {
226
+ throw "Could not find the placeholder element.";
227
+ }
228
+
229
+ // Append the container and load the flash
230
+ tempParent = document.createElement("div");
231
+ tempParent.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
232
+ targetElement.parentNode.replaceChild(tempParent.firstChild, targetElement);
233
+
234
+ // Fix IE Flash/Form bug
235
+ if (window[this.movieName] == undefined) {
236
+ window[this.movieName] = this.getMovieElement();
237
+ }
238
+
239
+ };
240
+
241
+ // Private: getFlashHTML generates the object tag needed to embed the flash in to the document
242
+ SWFUpload.prototype.getFlashHTML = function () {
243
+ // Flash Satay object syntax: http://www.alistapart.com/articles/flashsatay
244
+ return ['<object id="', this.movieName, '" type="application/x-shockwave-flash" data="', this.settings.flash_url, '" width="', this.settings.button_width, '" height="', this.settings.button_height, '" class="swfupload">',
245
+ '<param name="wmode" value="', this.settings.button_window_mode , '" />',
246
+ '<param name="movie" value="', this.settings.flash_url, '" />',
247
+ '<param name="quality" value="high" />',
248
+ '<param name="menu" value="false" />',
249
+ '<param name="allowScriptAccess" value="always" />',
250
+ '<param name="flashvars" value="' + this.getFlashVars() + '" />',
251
+ '</object>'].join("");
252
+ };
253
+
254
+ // Private: getFlashVars builds the parameter string that will be passed
255
+ // to flash in the flashvars param.
256
+ SWFUpload.prototype.getFlashVars = function () {
257
+ // Build a string from the post param object
258
+ var paramString = this.buildParamString();
259
+ var httpSuccessString = this.settings.http_success.join(",");
260
+
261
+ // Build the parameter string
262
+ return ["movieName=", encodeURIComponent(this.movieName),
263
+ "&amp;uploadURL=", encodeURIComponent(this.settings.upload_url),
264
+ "&amp;useQueryString=", encodeURIComponent(this.settings.use_query_string),
265
+ "&amp;requeueOnError=", encodeURIComponent(this.settings.requeue_on_error),
266
+ "&amp;httpSuccess=", encodeURIComponent(httpSuccessString),
267
+ "&amp;params=", encodeURIComponent(paramString),
268
+ "&amp;filePostName=", encodeURIComponent(this.settings.file_post_name),
269
+ "&amp;fileTypes=", encodeURIComponent(this.settings.file_types),
270
+ "&amp;fileTypesDescription=", encodeURIComponent(this.settings.file_types_description),
271
+ "&amp;fileSizeLimit=", encodeURIComponent(this.settings.file_size_limit),
272
+ "&amp;fileUploadLimit=", encodeURIComponent(this.settings.file_upload_limit),
273
+ "&amp;fileQueueLimit=", encodeURIComponent(this.settings.file_queue_limit),
274
+ "&amp;debugEnabled=", encodeURIComponent(this.settings.debug_enabled),
275
+ "&amp;buttonImageURL=", encodeURIComponent(this.settings.button_image_url),
276
+ "&amp;buttonWidth=", encodeURIComponent(this.settings.button_width),
277
+ "&amp;buttonHeight=", encodeURIComponent(this.settings.button_height),
278
+ "&amp;buttonText=", encodeURIComponent(this.settings.button_text),
279
+ "&amp;buttonTextTopPadding=", encodeURIComponent(this.settings.button_text_top_padding),
280
+ "&amp;buttonTextLeftPadding=", encodeURIComponent(this.settings.button_text_left_padding),
281
+ "&amp;buttonTextStyle=", encodeURIComponent(this.settings.button_text_style),
282
+ "&amp;buttonAction=", encodeURIComponent(this.settings.button_action),
283
+ "&amp;buttonDisabled=", encodeURIComponent(this.settings.button_disabled),
284
+ "&amp;buttonCursor=", encodeURIComponent(this.settings.button_cursor)
285
+ ].join("");
286
+ };
287
+
288
+ // Public: getMovieElement retrieves the DOM reference to the Flash element added by SWFUpload
289
+ // The element is cached after the first lookup
290
+ SWFUpload.prototype.getMovieElement = function () {
291
+ if (this.movieElement == undefined) {
292
+ this.movieElement = document.getElementById(this.movieName);
293
+ }
294
+
295
+ if (this.movieElement === null) {
296
+ throw "Could not find Flash element";
297
+ }
298
+
299
+ return this.movieElement;
300
+ };
301
+
302
+ // Private: buildParamString takes the name/value pairs in the post_params setting object
303
+ // and joins them up in to a string formatted "name=value&amp;name=value"
304
+ SWFUpload.prototype.buildParamString = function () {
305
+ var postParams = this.settings.post_params;
306
+ var paramStringPairs = [];
307
+
308
+ if (typeof(postParams) === "object") {
309
+ for (var name in postParams) {
310
+ if (postParams.hasOwnProperty(name)) {
311
+ paramStringPairs.push(encodeURIComponent(name.toString()) + "=" + encodeURIComponent(postParams[name].toString()));
312
+ }
313
+ }
314
+ }
315
+
316
+ return paramStringPairs.join("&amp;");
317
+ };
318
+
319
+ // Public: Used to remove a SWFUpload instance from the page. This method strives to remove
320
+ // all references to the SWF, and other objects so memory is properly freed.
321
+ // Returns true if everything was destroyed. Returns a false if a failure occurs leaving SWFUpload in an inconsistant state.
322
+ // Credits: Major improvements provided by steffen
323
+ SWFUpload.prototype.destroy = function () {
324
+ try {
325
+ // Make sure Flash is done before we try to remove it
326
+ this.cancelUpload(null, false);
327
+
328
+ // Remove the SWFUpload DOM nodes
329
+ var movieElement = null;
330
+ movieElement = this.getMovieElement();
331
+
332
+ if (movieElement) {
333
+ // Loop through all the movie's properties and remove all function references (DOM/JS IE 6/7 memory leak workaround)
334
+ for (var i in movieElement) {
335
+ try {
336
+ if (typeof(movieElement[i]) === "function") {
337
+ movieElement[i] = null;
338
+ }
339
+ } catch (ex1) {}
340
+ }
341
+
342
+ // Remove the Movie Element from the page
343
+ try {
344
+ movieElement.parentNode.removeChild(movieElement);
345
+ } catch (ex) {}
346
+ }
347
+
348
+
349
+ // Remove IE form fix reference
350
+ window[this.movieName] = null;
351
+
352
+ // Destroy other references
353
+ SWFUpload.instances[this.movieName] = null;
354
+ delete SWFUpload.instances[this.movieName];
355
+
356
+ this.movieElement = null;
357
+ this.settings = null;
358
+ this.customSettings = null;
359
+ this.eventQueue = null;
360
+ this.movieName = null;
361
+
362
+
363
+ return true;
364
+ } catch (ex1) {
365
+ return false;
366
+ }
367
+ };
368
+
369
+ // Public: displayDebugInfo prints out settings and configuration
370
+ // information about this SWFUpload instance.
371
+ // This function (and any references to it) can be deleted when placing
372
+ // SWFUpload in production.
373
+ SWFUpload.prototype.displayDebugInfo = function () {
374
+ this.debug(
375
+ [
376
+ "---SWFUpload Instance Info---\n",
377
+ "Version: ", SWFUpload.version, "\n",
378
+ "Movie Name: ", this.movieName, "\n",
379
+ "Settings:\n",
380
+ "\t", "upload_url: ", this.settings.upload_url, "\n",
381
+ "\t", "flash_url: ", this.settings.flash_url, "\n",
382
+ "\t", "use_query_string: ", this.settings.use_query_string.toString(), "\n",
383
+ "\t", "requeue_on_error: ", this.settings.requeue_on_error.toString(), "\n",
384
+ "\t", "http_success: ", this.settings.http_success.join(", "), "\n",
385
+ "\t", "file_post_name: ", this.settings.file_post_name, "\n",
386
+ "\t", "post_params: ", this.settings.post_params.toString(), "\n",
387
+ "\t", "file_types: ", this.settings.file_types, "\n",
388
+ "\t", "file_types_description: ", this.settings.file_types_description, "\n",
389
+ "\t", "file_size_limit: ", this.settings.file_size_limit, "\n",
390
+ "\t", "file_upload_limit: ", this.settings.file_upload_limit, "\n",
391
+ "\t", "file_queue_limit: ", this.settings.file_queue_limit, "\n",
392
+ "\t", "debug: ", this.settings.debug.toString(), "\n",
393
+
394
+ "\t", "prevent_swf_caching: ", this.settings.prevent_swf_caching.toString(), "\n",
395
+
396
+ "\t", "button_placeholder_id: ", this.settings.button_placeholder_id.toString(), "\n",
397
+ "\t", "button_image_url: ", this.settings.button_image_url.toString(), "\n",
398
+ "\t", "button_width: ", this.settings.button_width.toString(), "\n",
399
+ "\t", "button_height: ", this.settings.button_height.toString(), "\n",
400
+ "\t", "button_text: ", this.settings.button_text.toString(), "\n",
401
+ "\t", "button_text_style: ", this.settings.button_text_style.toString(), "\n",
402
+ "\t", "button_text_top_padding: ", this.settings.button_text_top_padding.toString(), "\n",
403
+ "\t", "button_text_left_padding: ", this.settings.button_text_left_padding.toString(), "\n",
404
+ "\t", "button_action: ", this.settings.button_action.toString(), "\n",
405
+ "\t", "button_disabled: ", this.settings.button_disabled.toString(), "\n",
406
+
407
+ "\t", "custom_settings: ", this.settings.custom_settings.toString(), "\n",
408
+ "Event Handlers:\n",
409
+ "\t", "swfupload_loaded_handler assigned: ", (typeof this.settings.swfupload_loaded_handler === "function").toString(), "\n",
410
+ "\t", "file_dialog_start_handler assigned: ", (typeof this.settings.file_dialog_start_handler === "function").toString(), "\n",
411
+ "\t", "file_queued_handler assigned: ", (typeof this.settings.file_queued_handler === "function").toString(), "\n",
412
+ "\t", "file_queue_error_handler assigned: ", (typeof this.settings.file_queue_error_handler === "function").toString(), "\n",
413
+ "\t", "upload_start_handler assigned: ", (typeof this.settings.upload_start_handler === "function").toString(), "\n",
414
+ "\t", "upload_progress_handler assigned: ", (typeof this.settings.upload_progress_handler === "function").toString(), "\n",
415
+ "\t", "upload_error_handler assigned: ", (typeof this.settings.upload_error_handler === "function").toString(), "\n",
416
+ "\t", "upload_success_handler assigned: ", (typeof this.settings.upload_success_handler === "function").toString(), "\n",
417
+ "\t", "upload_complete_handler assigned: ", (typeof this.settings.upload_complete_handler === "function").toString(), "\n",
418
+ "\t", "debug_handler assigned: ", (typeof this.settings.debug_handler === "function").toString(), "\n"
419
+ ].join("")
420
+ );
421
+ };
422
+
423
+ /* Note: addSetting and getSetting are no longer used by SWFUpload but are included
424
+ the maintain v2 API compatibility
425
+ */
426
+ // Public: (Deprecated) addSetting adds a setting value. If the value given is undefined or null then the default_value is used.
427
+ SWFUpload.prototype.addSetting = function (name, value, default_value) {
428
+ if (value == undefined) {
429
+ return (this.settings[name] = default_value);
430
+ } else {
431
+ return (this.settings[name] = value);
432
+ }
433
+ };
434
+
435
+ // Public: (Deprecated) getSetting gets a setting. Returns an empty string if the setting was not found.
436
+ SWFUpload.prototype.getSetting = function (name) {
437
+ if (this.settings[name] != undefined) {
438
+ return this.settings[name];
439
+ }
440
+
441
+ return "";
442
+ };
443
+
444
+
445
+
446
+ // Private: callFlash handles function calls made to the Flash element.
447
+ // Calls are made with a setTimeout for some functions to work around
448
+ // bugs in the ExternalInterface library.
449
+ SWFUpload.prototype.callFlash = function (functionName, argumentArray) {
450
+ argumentArray = argumentArray || [];
451
+
452
+ var movieElement = this.getMovieElement();
453
+ var returnValue, returnString;
454
+
455
+ // Flash's method if calling ExternalInterface methods (code adapted from MooTools).
456
+ try {
457
+ returnString = movieElement.CallFunction('<invoke name="' + functionName + '" returntype="javascript">' + __flash__argumentsToXML(argumentArray, 0) + '</invoke>');
458
+ returnValue = eval(returnString);
459
+ } catch (ex) {
460
+ throw "Call to " + functionName + " failed";
461
+ }
462
+
463
+ // Unescape file post param values
464
+ if (returnValue != undefined && typeof returnValue.post === "object") {
465
+ returnValue = this.unescapeFilePostParams(returnValue);
466
+ }
467
+
468
+ return returnValue;
469
+ };
470
+
471
+
472
+ /* *****************************
473
+ -- Flash control methods --
474
+ Your UI should use these
475
+ to operate SWFUpload
476
+ ***************************** */
477
+
478
+ // WARNING: this function does not work in Flash Player 10
479
+ // Public: selectFile causes a File Selection Dialog window to appear. This
480
+ // dialog only allows 1 file to be selected.
481
+ SWFUpload.prototype.selectFile = function () {
482
+ this.callFlash("SelectFile");
483
+ };
484
+
485
+ // WARNING: this function does not work in Flash Player 10
486
+ // Public: selectFiles causes a File Selection Dialog window to appear/ This
487
+ // dialog allows the user to select any number of files
488
+ // Flash Bug Warning: Flash limits the number of selectable files based on the combined length of the file names.
489
+ // If the selection name length is too long the dialog will fail in an unpredictable manner. There is no work-around
490
+ // for this bug.
491
+ SWFUpload.prototype.selectFiles = function () {
492
+ this.callFlash("SelectFiles");
493
+ };
494
+
495
+
496
+ // Public: startUpload starts uploading the first file in the queue unless
497
+ // the optional parameter 'fileID' specifies the ID
498
+ SWFUpload.prototype.startUpload = function (fileID) {
499
+ this.callFlash("StartUpload", [fileID]);
500
+ };
501
+
502
+ // Public: cancelUpload cancels any queued file. The fileID parameter may be the file ID or index.
503
+ // If you do not specify a fileID the current uploading file or first file in the queue is cancelled.
504
+ // If you do not want the uploadError event to trigger you can specify false for the triggerErrorEvent parameter.
505
+ SWFUpload.prototype.cancelUpload = function (fileID, triggerErrorEvent) {
506
+ if (triggerErrorEvent !== false) {
507
+ triggerErrorEvent = true;
508
+ }
509
+ this.callFlash("CancelUpload", [fileID, triggerErrorEvent]);
510
+ };
511
+
512
+ // Public: stopUpload stops the current upload and requeues the file at the beginning of the queue.
513
+ // If nothing is currently uploading then nothing happens.
514
+ SWFUpload.prototype.stopUpload = function () {
515
+ this.callFlash("StopUpload");
516
+ };
517
+
518
+ /* ************************
519
+ * Settings methods
520
+ * These methods change the SWFUpload settings.
521
+ * SWFUpload settings should not be changed directly on the settings object
522
+ * since many of the settings need to be passed to Flash in order to take
523
+ * effect.
524
+ * *********************** */
525
+
526
+ // Public: getStats gets the file statistics object.
527
+ SWFUpload.prototype.getStats = function () {
528
+ return this.callFlash("GetStats");
529
+ };
530
+
531
+ // Public: setStats changes the SWFUpload statistics. You shouldn't need to
532
+ // change the statistics but you can. Changing the statistics does not
533
+ // affect SWFUpload accept for the successful_uploads count which is used
534
+ // by the upload_limit setting to determine how many files the user may upload.
535
+ SWFUpload.prototype.setStats = function (statsObject) {
536
+ this.callFlash("SetStats", [statsObject]);
537
+ };
538
+
539
+ // Public: getFile retrieves a File object by ID or Index. If the file is
540
+ // not found then 'null' is returned.
541
+ SWFUpload.prototype.getFile = function (fileID) {
542
+ if (typeof(fileID) === "number") {
543
+ return this.callFlash("GetFileByIndex", [fileID]);
544
+ } else {
545
+ return this.callFlash("GetFile", [fileID]);
546
+ }
547
+ };
548
+
549
+ // Public: addFileParam sets a name/value pair that will be posted with the
550
+ // file specified by the Files ID. If the name already exists then the
551
+ // exiting value will be overwritten.
552
+ SWFUpload.prototype.addFileParam = function (fileID, name, value) {
553
+ return this.callFlash("AddFileParam", [fileID, name, value]);
554
+ };
555
+
556
+ // Public: removeFileParam removes a previously set (by addFileParam) name/value
557
+ // pair from the specified file.
558
+ SWFUpload.prototype.removeFileParam = function (fileID, name) {
559
+ this.callFlash("RemoveFileParam", [fileID, name]);
560
+ };
561
+
562
+ // Public: setUploadUrl changes the upload_url setting.
563
+ SWFUpload.prototype.setUploadURL = function (url) {
564
+ this.settings.upload_url = url.toString();
565
+ this.callFlash("SetUploadURL", [url]);
566
+ };
567
+
568
+ // Public: setPostParams changes the post_params setting
569
+ SWFUpload.prototype.setPostParams = function (paramsObject) {
570
+ this.settings.post_params = paramsObject;
571
+ this.callFlash("SetPostParams", [paramsObject]);
572
+ };
573
+
574
+ // Public: addPostParam adds post name/value pair. Each name can have only one value.
575
+ SWFUpload.prototype.addPostParam = function (name, value) {
576
+ this.settings.post_params[name] = value;
577
+ this.callFlash("SetPostParams", [this.settings.post_params]);
578
+ };
579
+
580
+ // Public: removePostParam deletes post name/value pair.
581
+ SWFUpload.prototype.removePostParam = function (name) {
582
+ delete this.settings.post_params[name];
583
+ this.callFlash("SetPostParams", [this.settings.post_params]);
584
+ };
585
+
586
+ // Public: setFileTypes changes the file_types setting and the file_types_description setting
587
+ SWFUpload.prototype.setFileTypes = function (types, description) {
588
+ this.settings.file_types = types;
589
+ this.settings.file_types_description = description;
590
+ this.callFlash("SetFileTypes", [types, description]);
591
+ };
592
+
593
+ // Public: setFileSizeLimit changes the file_size_limit setting
594
+ SWFUpload.prototype.setFileSizeLimit = function (fileSizeLimit) {
595
+ this.settings.file_size_limit = fileSizeLimit;
596
+ this.callFlash("SetFileSizeLimit", [fileSizeLimit]);
597
+ };
598
+
599
+ // Public: setFileUploadLimit changes the file_upload_limit setting
600
+ SWFUpload.prototype.setFileUploadLimit = function (fileUploadLimit) {
601
+ this.settings.file_upload_limit = fileUploadLimit;
602
+ this.callFlash("SetFileUploadLimit", [fileUploadLimit]);
603
+ };
604
+
605
+ // Public: setFileQueueLimit changes the file_queue_limit setting
606
+ SWFUpload.prototype.setFileQueueLimit = function (fileQueueLimit) {
607
+ this.settings.file_queue_limit = fileQueueLimit;
608
+ this.callFlash("SetFileQueueLimit", [fileQueueLimit]);
609
+ };
610
+
611
+ // Public: setFilePostName changes the file_post_name setting
612
+ SWFUpload.prototype.setFilePostName = function (filePostName) {
613
+ this.settings.file_post_name = filePostName;
614
+ this.callFlash("SetFilePostName", [filePostName]);
615
+ };
616
+
617
+ // Public: setUseQueryString changes the use_query_string setting
618
+ SWFUpload.prototype.setUseQueryString = function (useQueryString) {
619
+ this.settings.use_query_string = useQueryString;
620
+ this.callFlash("SetUseQueryString", [useQueryString]);
621
+ };
622
+
623
+ // Public: setRequeueOnError changes the requeue_on_error setting
624
+ SWFUpload.prototype.setRequeueOnError = function (requeueOnError) {
625
+ this.settings.requeue_on_error = requeueOnError;
626
+ this.callFlash("SetRequeueOnError", [requeueOnError]);
627
+ };
628
+
629
+ // Public: setHTTPSuccess changes the http_success setting
630
+ SWFUpload.prototype.setHTTPSuccess = function (http_status_codes) {
631
+ if (typeof http_status_codes === "string") {
632
+ http_status_codes = http_status_codes.replace(" ", "").split(",");
633
+ }
634
+
635
+ this.settings.http_success = http_status_codes;
636
+ this.callFlash("SetHTTPSuccess", [http_status_codes]);
637
+ };
638
+
639
+
640
+ // Public: setDebugEnabled changes the debug_enabled setting
641
+ SWFUpload.prototype.setDebugEnabled = function (debugEnabled) {
642
+ this.settings.debug_enabled = debugEnabled;
643
+ this.callFlash("SetDebugEnabled", [debugEnabled]);
644
+ };
645
+
646
+ // Public: setButtonImageURL loads a button image sprite
647
+ SWFUpload.prototype.setButtonImageURL = function (buttonImageURL) {
648
+ if (buttonImageURL == undefined) {
649
+ buttonImageURL = "";
650
+ }
651
+
652
+ this.settings.button_image_url = buttonImageURL;
653
+ this.callFlash("SetButtonImageURL", [buttonImageURL]);
654
+ };
655
+
656
+ // Public: setButtonDimensions resizes the Flash Movie and button
657
+ SWFUpload.prototype.setButtonDimensions = function (width, height) {
658
+ this.settings.button_width = width;
659
+ this.settings.button_height = height;
660
+
661
+ var movie = this.getMovieElement();
662
+ if (movie != undefined) {
663
+ movie.style.width = width + "px";
664
+ movie.style.height = height + "px";
665
+ }
666
+
667
+ this.callFlash("SetButtonDimensions", [width, height]);
668
+ };
669
+ // Public: setButtonText Changes the text overlaid on the button
670
+ SWFUpload.prototype.setButtonText = function (html) {
671
+ this.settings.button_text = html;
672
+ this.callFlash("SetButtonText", [html]);
673
+ };
674
+ // Public: setButtonTextPadding changes the top and left padding of the text overlay
675
+ SWFUpload.prototype.setButtonTextPadding = function (left, top) {
676
+ this.settings.button_text_top_padding = top;
677
+ this.settings.button_text_left_padding = left;
678
+ this.callFlash("SetButtonTextPadding", [left, top]);
679
+ };
680
+
681
+ // Public: setButtonTextStyle changes the CSS used to style the HTML/Text overlaid on the button
682
+ SWFUpload.prototype.setButtonTextStyle = function (css) {
683
+ this.settings.button_text_style = css;
684
+ this.callFlash("SetButtonTextStyle", [css]);
685
+ };
686
+ // Public: setButtonDisabled disables/enables the button
687
+ SWFUpload.prototype.setButtonDisabled = function (isDisabled) {
688
+ this.settings.button_disabled = isDisabled;
689
+ this.callFlash("SetButtonDisabled", [isDisabled]);
690
+ };
691
+ // Public: setButtonAction sets the action that occurs when the button is clicked
692
+ SWFUpload.prototype.setButtonAction = function (buttonAction) {
693
+ this.settings.button_action = buttonAction;
694
+ this.callFlash("SetButtonAction", [buttonAction]);
695
+ };
696
+
697
+ // Public: setButtonCursor changes the mouse cursor displayed when hovering over the button
698
+ SWFUpload.prototype.setButtonCursor = function (cursor) {
699
+ this.settings.button_cursor = cursor;
700
+ this.callFlash("SetButtonCursor", [cursor]);
701
+ };
702
+
703
+ /* *******************************
704
+ Flash Event Interfaces
705
+ These functions are used by Flash to trigger the various
706
+ events.
707
+
708
+ All these functions a Private.
709
+
710
+ Because the ExternalInterface library is buggy the event calls
711
+ are added to a queue and the queue then executed by a setTimeout.
712
+ This ensures that events are executed in a determinate order and that
713
+ the ExternalInterface bugs are avoided.
714
+ ******************************* */
715
+
716
+ SWFUpload.prototype.queueEvent = function (handlerName, argumentArray) {
717
+ // Warning: Don't call this.debug inside here or you'll create an infinite loop
718
+
719
+ if (argumentArray == undefined) {
720
+ argumentArray = [];
721
+ } else if (!(argumentArray instanceof Array)) {
722
+ argumentArray = [argumentArray];
723
+ }
724
+
725
+ var self = this;
726
+ if (typeof this.settings[handlerName] === "function") {
727
+ // Queue the event
728
+ this.eventQueue.push(function () {
729
+ this.settings[handlerName].apply(this, argumentArray);
730
+ });
731
+
732
+ // Execute the next queued event
733
+ setTimeout(function () {
734
+ self.executeNextEvent();
735
+ }, 0);
736
+
737
+ } else if (this.settings[handlerName] !== null) {
738
+ throw "Event handler " + handlerName + " is unknown or is not a function";
739
+ }
740
+ };
741
+
742
+ // Private: Causes the next event in the queue to be executed. Since events are queued using a setTimeout
743
+ // we must queue them in order to garentee that they are executed in order.
744
+ SWFUpload.prototype.executeNextEvent = function () {
745
+ // Warning: Don't call this.debug inside here or you'll create an infinite loop
746
+
747
+ var f = this.eventQueue ? this.eventQueue.shift() : null;
748
+ if (typeof(f) === "function") {
749
+ f.apply(this);
750
+ }
751
+ };
752
+
753
+ // Private: unescapeFileParams is part of a workaround for a flash bug where objects passed through ExternalInterface cannot have
754
+ // properties that contain characters that are not valid for JavaScript identifiers. To work around this
755
+ // the Flash Component escapes the parameter names and we must unescape again before passing them along.
756
+ SWFUpload.prototype.unescapeFilePostParams = function (file) {
757
+ var reg = /[$]([0-9a-f]{4})/i;
758
+ var unescapedPost = {};
759
+ var uk;
760
+
761
+ if (file != undefined) {
762
+ for (var k in file.post) {
763
+ if (file.post.hasOwnProperty(k)) {
764
+ uk = k;
765
+ var match;
766
+ while ((match = reg.exec(uk)) !== null) {
767
+ uk = uk.replace(match[0], String.fromCharCode(parseInt("0x" + match[1], 16)));
768
+ }
769
+ unescapedPost[uk] = file.post[k];
770
+ }
771
+ }
772
+
773
+ file.post = unescapedPost;
774
+ }
775
+
776
+ return file;
777
+ };
778
+
779
+ SWFUpload.prototype.flashReady = function () {
780
+ // Check that the movie element is loaded correctly with its ExternalInterface methods defined
781
+ var movieElement = this.getMovieElement();
782
+
783
+ // Pro-actively unhook all the Flash functions
784
+ if (typeof(movieElement.CallFunction) === "unknown") { // We only want to do this in IE
785
+ this.debug("Removing Flash functions hooks (this should only run in IE and should prevent memory leaks)");
786
+ for (var key in movieElement) {
787
+ try {
788
+ if (typeof(movieElement[key]) === "function") {
789
+ movieElement[key] = null;
790
+ }
791
+ } catch (ex) {
792
+ }
793
+ }
794
+ }
795
+
796
+ this.queueEvent("swfupload_loaded_handler");
797
+ };
798
+
799
+
800
+ /* This is a chance to do something before the browse window opens */
801
+ SWFUpload.prototype.fileDialogStart = function () {
802
+ this.queueEvent("file_dialog_start_handler");
803
+ };
804
+
805
+
806
+ /* Called when a file is successfully added to the queue. */
807
+ SWFUpload.prototype.fileQueued = function (file) {
808
+ file = this.unescapeFilePostParams(file);
809
+ this.queueEvent("file_queued_handler", file);
810
+ };
811
+
812
+
813
+ /* Handle errors that occur when an attempt to queue a file fails. */
814
+ SWFUpload.prototype.fileQueueError = function (file, errorCode, message) {
815
+ file = this.unescapeFilePostParams(file);
816
+ this.queueEvent("file_queue_error_handler", [file, errorCode, message]);
817
+ };
818
+
819
+ /* Called after the file dialog has closed and the selected files have been queued.
820
+ You could call startUpload here if you want the queued files to begin uploading immediately. */
821
+ SWFUpload.prototype.fileDialogComplete = function (numFilesSelected, numFilesQueued) {
822
+ this.queueEvent("file_dialog_complete_handler", [numFilesSelected, numFilesQueued]);
823
+ };
824
+
825
+ SWFUpload.prototype.uploadStart = function (file) {
826
+ file = this.unescapeFilePostParams(file);
827
+ this.queueEvent("return_upload_start_handler", file);
828
+ };
829
+
830
+ SWFUpload.prototype.returnUploadStart = function (file) {
831
+ var returnValue;
832
+ if (typeof this.settings.upload_start_handler === "function") {
833
+ file = this.unescapeFilePostParams(file);
834
+ returnValue = this.settings.upload_start_handler.call(this, file);
835
+ } else if (this.settings.upload_start_handler != undefined) {
836
+ throw "upload_start_handler must be a function";
837
+ }
838
+
839
+ // Convert undefined to true so if nothing is returned from the upload_start_handler it is
840
+ // interpretted as 'true'.
841
+ if (returnValue === undefined) {
842
+ returnValue = true;
843
+ }
844
+
845
+ returnValue = !!returnValue;
846
+
847
+ this.callFlash("ReturnUploadStart", [returnValue]);
848
+ };
849
+
850
+
851
+
852
+ SWFUpload.prototype.uploadProgress = function (file, bytesComplete, bytesTotal) {
853
+ file = this.unescapeFilePostParams(file);
854
+ this.queueEvent("upload_progress_handler", [file, bytesComplete, bytesTotal]);
855
+ };
856
+
857
+ SWFUpload.prototype.uploadError = function (file, errorCode, message) {
858
+ file = this.unescapeFilePostParams(file);
859
+ this.queueEvent("upload_error_handler", [file, errorCode, message]);
860
+ };
861
+
862
+ SWFUpload.prototype.uploadSuccess = function (file, serverData) {
863
+ file = this.unescapeFilePostParams(file);
864
+ this.queueEvent("upload_success_handler", [file, serverData]);
865
+ };
866
+
867
+ SWFUpload.prototype.uploadComplete = function (file) {
868
+ file = this.unescapeFilePostParams(file);
869
+ this.queueEvent("upload_complete_handler", file);
870
+ };
871
+
872
+ /* Called by SWFUpload JavaScript and Flash functions when debug is enabled. By default it writes messages to the
873
+ internal debug console. You can override this event and have messages written where you want. */
874
+ SWFUpload.prototype.debug = function (message) {
875
+ this.queueEvent("debug_handler", message);
876
+ };
877
+
878
+
879
+ /* **********************************
880
+ Debug Console
881
+ The debug console is a self contained, in page location
882
+ for debug message to be sent. The Debug Console adds
883
+ itself to the body if necessary.
884
+
885
+ The console is automatically scrolled as messages appear.
886
+
887
+ If you are using your own debug handler or when you deploy to production and
888
+ have debug disabled you can remove these functions to reduce the file size
889
+ and complexity.
890
+ ********************************** */
891
+
892
+ // Private: debugMessage is the default debug_handler. If you want to print debug messages
893
+ // call the debug() function. When overriding the function your own function should
894
+ // check to see if the debug setting is true before outputting debug information.
895
+ SWFUpload.prototype.debugMessage = function (message) {
896
+ if (this.settings.debug) {
897
+ var exceptionMessage, exceptionValues = [];
898
+
899
+ // Check for an exception object and print it nicely
900
+ if (typeof message === "object" && typeof message.name === "string" && typeof message.message === "string") {
901
+ for (var key in message) {
902
+ if (message.hasOwnProperty(key)) {
903
+ exceptionValues.push(key + ": " + message[key]);
904
+ }
905
+ }
906
+ exceptionMessage = exceptionValues.join("\n") || "";
907
+ exceptionValues = exceptionMessage.split("\n");
908
+ exceptionMessage = "EXCEPTION: " + exceptionValues.join("\nEXCEPTION: ");
909
+ SWFUpload.Console.writeLine(exceptionMessage);
910
+ } else {
911
+ SWFUpload.Console.writeLine(message);
912
+ }
913
+ }
914
+ };
915
+
916
+ SWFUpload.Console = {};
917
+ SWFUpload.Console.writeLine = function (message) {
918
+ var console, documentForm;
919
+
920
+ try {
921
+ console = document.getElementById("SWFUpload_Console");
922
+
923
+ if (!console) {
924
+ documentForm = document.createElement("form");
925
+ document.getElementsByTagName("body")[0].appendChild(documentForm);
926
+
927
+ console = document.createElement("textarea");
928
+ console.id = "SWFUpload_Console";
929
+ console.style.fontFamily = "monospace";
930
+ console.setAttribute("wrap", "off");
931
+ console.wrap = "off";
932
+ console.style.overflow = "auto";
933
+ console.style.width = "700px";
934
+ console.style.height = "350px";
935
+ console.style.margin = "5px";
936
+ documentForm.appendChild(console);
937
+ }
938
+
939
+ console.value += message + "\n";
940
+
941
+ console.scrollTop = console.scrollHeight - console.clientHeight;
942
+ } catch (ex) {
943
+ alert("Exception: " + ex.name + " Message: " + ex.message);
944
+ }
945
+ };