Version 3.18.1
Show:

File: uploader/js/file-flash.js

  1. /**
  2. * The FileFlash class provides a wrapper for a file pointer stored in Flash. The File wrapper
  3. * also implements the mechanics for uploading a file and tracking its progress.
  4. * @module file-flash
  5. */
  6. /**
  7. * The class provides a wrapper for a file pointer in Flash.
  8. * @class FileFlash
  9. * @extends Base
  10. * @constructor
  11. * @param {Object} config Configuration object.
  12. */
  13. var FileFlash = function(o) {
  14. FileFlash.superclass.constructor.apply(this, arguments);
  15. };
  16. Y.extend(FileFlash, Y.Base, {
  17. /**
  18. * Construction logic executed during FileFlash instantiation.
  19. *
  20. * @method initializer
  21. * @protected
  22. */
  23. initializer : function (cfg) {
  24. if (!this.get("id")) {
  25. this._set("id", Y.guid("file"));
  26. }
  27. },
  28. /**
  29. * Handler of events dispatched by the Flash player.
  30. *
  31. * @method _swfEventHandler
  32. * @param {Event} event The event object received from the Flash player.
  33. * @protected
  34. */
  35. _swfEventHandler: function (event) {
  36. if (event.id === this.get("id")) {
  37. switch (event.type) {
  38. /**
  39. * Signals that this file's upload has started.
  40. *
  41. * @event uploadstart
  42. * @param event {Event} The event object for the `uploadstart` with the
  43. * following payload:
  44. * <dl>
  45. * <dt>uploader</dt>
  46. * <dd>The Y.SWF instance of Flash uploader that's handling the upload.</dd>
  47. * </dl>
  48. */
  49. case "uploadstart":
  50. this.fire("uploadstart", {uploader: this.get("uploader")});
  51. break;
  52. case "uploadprogress":
  53. /**
  54. * Signals that progress has been made on the upload of this file.
  55. *
  56. * @event uploadprogress
  57. * @param event {Event} The event object for the `uploadprogress` with the
  58. * following payload:
  59. * <dl>
  60. * <dt>originEvent</dt>
  61. * <dd>The original event fired by the Flash uploader instance.</dd>
  62. * <dt>bytesLoaded</dt>
  63. * <dd>The number of bytes of the file that has been uploaded.</dd>
  64. * <dt>bytesTotal</dt>
  65. * <dd>The total number of bytes in the file (the file size)</dd>
  66. * <dt>percentLoaded</dt>
  67. * <dd>The fraction of the file that has been uploaded, out of 100.</dd>
  68. * </dl>
  69. */
  70. this.fire("uploadprogress", {originEvent: event,
  71. bytesLoaded: event.bytesLoaded,
  72. bytesTotal: event.bytesTotal,
  73. percentLoaded: Math.min(100, Math.round(10000*event.bytesLoaded/event.bytesTotal)/100)
  74. });
  75. this._set("bytesUploaded", event.bytesLoaded);
  76. break;
  77. case "uploadcomplete":
  78. /**
  79. * Signals that this file's upload has completed, but data has not yet been received from the server.
  80. *
  81. * @event uploadfinished
  82. * @param event {Event} The event object for the `uploadfinished` with the
  83. * following payload:
  84. * <dl>
  85. * <dt>originEvent</dt>
  86. * <dd>The original event fired by the Flash player instance.</dd>
  87. * </dl>
  88. */
  89. this.fire("uploadfinished", {originEvent: event});
  90. break;
  91. case "uploadcompletedata":
  92. /**
  93. * Signals that this file's upload has completed and data has been received from the server.
  94. *
  95. * @event uploadcomplete
  96. * @param event {Event} The event object for the `uploadcomplete` with the
  97. * following payload:
  98. * <dl>
  99. * <dt>originEvent</dt>
  100. * <dd>The original event fired by the Flash player instance.</dd>
  101. * <dt>data</dt>
  102. * <dd>The data returned by the server.</dd>
  103. * </dl>
  104. */
  105. this.fire("uploadcomplete", {originEvent: event,
  106. data: event.data});
  107. break;
  108. case "uploadcancel":
  109. /**
  110. * Signals that this file's upload has been cancelled.
  111. *
  112. * @event uploadcancel
  113. * @param event {Event} The event object for the `uploadcancel` with the
  114. * following payload:
  115. * <dl>
  116. * <dt>originEvent</dt>
  117. * <dd>The original event fired by the Flash player instance.</dd>
  118. * </dl>
  119. */
  120. this.fire("uploadcancel", {originEvent: event});
  121. break;
  122. case "uploaderror":
  123. /**
  124. * Signals that this file's upload has encountered an error.
  125. *
  126. * @event uploaderror
  127. * @param event {Event} The event object for the `uploaderror` with the
  128. * following payload:
  129. * <dl>
  130. * <dt>originEvent</dt>
  131. * <dd>The original event fired by the Flash player instance.</dd>
  132. * <dt>status</dt>
  133. * <dd>The status code reported by the Flash Player. If it's an HTTP error,
  134. * then this corresponds to the HTTP status code received by the uploader.</dd>
  135. * <dt>statusText</dt>
  136. * <dd>The text of the error event reported by the Flash Player.</dd>
  137. * <dt>source</dt>
  138. * <dd>Either "http" (if it's an HTTP error), or "io" (if it's a network transmission
  139. * error.)</dd>
  140. * </dl>
  141. */
  142. this.fire("uploaderror", {originEvent: event, status: event.status, statusText: event.message, source: event.source});
  143. }
  144. }
  145. },
  146. /**
  147. * Starts the upload of a specific file.
  148. *
  149. * @method startUpload
  150. * @param url {String} The URL to upload the file to.
  151. * @param parameters {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
  152. * @param fileFieldName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
  153. */
  154. startUpload: function(url, parameters, fileFieldName) {
  155. if (this.get("uploader")) {
  156. var myUploader = this.get("uploader"),
  157. fileField = fileFieldName || "Filedata",
  158. id = this.get("id"),
  159. params = parameters || null;
  160. this._set("bytesUploaded", 0);
  161. myUploader.on("uploadstart", this._swfEventHandler, this);
  162. myUploader.on("uploadprogress", this._swfEventHandler, this);
  163. myUploader.on("uploadcomplete", this._swfEventHandler, this);
  164. myUploader.on("uploadcompletedata", this._swfEventHandler, this);
  165. myUploader.on("uploaderror", this._swfEventHandler, this);
  166. myUploader.callSWF("upload", [id, url, params, fileField]);
  167. }
  168. },
  169. /**
  170. * Cancels the upload of a specific file, if currently in progress.
  171. *
  172. * @method cancelUpload
  173. */
  174. cancelUpload: function () {
  175. if (this.get("uploader")) {
  176. this.get("uploader").callSWF("cancel", [this.get("id")]);
  177. this.fire("uploadcancel");
  178. }
  179. }
  180. }, {
  181. /**
  182. * The identity of the class.
  183. *
  184. * @property NAME
  185. * @type String
  186. * @default 'file'
  187. * @readOnly
  188. * @protected
  189. * @static
  190. */
  191. NAME: 'file',
  192. /**
  193. * The type of transport.
  194. *
  195. * @property TYPE
  196. * @type String
  197. * @default 'flash'
  198. * @readOnly
  199. * @protected
  200. * @static
  201. */
  202. TYPE: "flash",
  203. /**
  204. * Static property used to define the default attribute configuration of
  205. * the File.
  206. *
  207. * @property ATTRS
  208. * @type {Object}
  209. * @protected
  210. * @static
  211. */
  212. ATTRS: {
  213. /**
  214. * A String containing the unique id of the file wrapped by the FileFlash instance.
  215. * The id is supplied by the Flash player uploader.
  216. *
  217. * @attribute id
  218. * @type {String}
  219. * @initOnly
  220. */
  221. id: {
  222. writeOnce: "initOnly",
  223. value: null
  224. },
  225. /**
  226. * The size of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
  227. *
  228. * @attribute size
  229. * @type {Number}
  230. * @initOnly
  231. */
  232. size: {
  233. writeOnce: "initOnly",
  234. value: 0
  235. },
  236. /**
  237. * The name of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
  238. *
  239. * @attribute name
  240. * @type {String}
  241. * @initOnly
  242. */
  243. name: {
  244. writeOnce: "initOnly",
  245. value: null
  246. },
  247. /**
  248. * The date that the file wrapped by FileFlash was created on. This value is supplied by the Flash player uploader.
  249. *
  250. * @attribute dateCreated
  251. * @type {Date}
  252. * @initOnly
  253. */
  254. dateCreated: {
  255. writeOnce: "initOnly",
  256. value: null
  257. },
  258. /**
  259. * The date that the file wrapped by FileFlash was last modified on. This value is supplied by the Flash player uploader.
  260. *
  261. * @attribute dateModified
  262. * @type {Date}
  263. * @initOnly
  264. */
  265. dateModified: {
  266. writeOnce: "initOnly",
  267. value: null
  268. },
  269. /**
  270. * The number of bytes of the file that has been uploaded to the server. This value is
  271. * non-zero only while a file is being uploaded.
  272. *
  273. * @attribute bytesUploaded
  274. * @type {Date}
  275. * @readOnly
  276. */
  277. bytesUploaded: {
  278. readOnly: true,
  279. value: 0
  280. },
  281. /**
  282. * The type of the file wrapped by FileFlash. This value is provided by the Flash player
  283. * uploader.
  284. *
  285. * @attribute type
  286. * @type {String}
  287. * @initOnly
  288. */
  289. type: {
  290. writeOnce: "initOnly",
  291. value: null
  292. },
  293. /**
  294. * The instance of Y.SWF wrapping the Flash player uploader associated with this file.
  295. *
  296. * @attribute uploder
  297. * @type {SWF}
  298. * @initOnly
  299. */
  300. uploader: {
  301. writeOnce: "initOnly",
  302. value: null
  303. }
  304. }
  305. });
  306. Y.FileFlash = FileFlash;