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