API Docs for: 3.8.0
Show:

File: file/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.          
  156.         if (this.get("uploader")) {

  157.             var myUploader = this.get("uploader"),
  158.                 fileField = fileFieldName || "Filedata",
  159.                 id = this.get("id"),
  160.                 params = parameters || null;

  161.             this._set("bytesUploaded", 0);
  162.            
  163.             myUploader.on("uploadstart", this._swfEventHandler, this);
  164.             myUploader.on("uploadprogress", this._swfEventHandler, this);
  165.             myUploader.on("uploadcomplete", this._swfEventHandler, this);
  166.             myUploader.on("uploadcompletedata", this._swfEventHandler, this);
  167.             myUploader.on("uploaderror", this._swfEventHandler, this);

  168.             myUploader.callSWF("upload", [id, url, params, fileField]);
  169.          }

  170.         },

  171.        /**
  172.         * Cancels the upload of a specific file, if currently in progress.
  173.         *
  174.         * @method cancelUpload
  175.         */  
  176.         cancelUpload: function () {
  177.          if (this.get("uploader")) {
  178.            this.get("uploader").callSWF("cancel", [this.get("id")]);
  179.            this.fire("uploadcancel");
  180.          }
  181.         }

  182.     }, {

  183.        /**
  184.         * The identity of the class.
  185.         *
  186.         * @property NAME
  187.         * @type String
  188.         * @default 'file'
  189.         * @readOnly
  190.         * @protected
  191.         * @static
  192.         */
  193.         NAME: 'file',

  194.        /**
  195.         * The type of transport.
  196.         *
  197.         * @property TYPE
  198.         * @type String
  199.         * @default 'flash'
  200.         * @readOnly
  201.         * @protected
  202.         * @static
  203.         */
  204.         TYPE: "flash",

  205.        /**
  206.         * Static property used to define the default attribute configuration of
  207.         * the File.
  208.         *
  209.         * @property ATTRS
  210.         * @type {Object}
  211.         * @protected
  212.         * @static
  213.         */
  214.         ATTRS: {

  215.        /**
  216.         * A String containing the unique id of the file wrapped by the FileFlash instance.
  217.         * The id is supplied by the Flash player uploader.
  218.         *
  219.         * @attribute id
  220.         * @type {String}
  221.         * @initOnly
  222.         */
  223.         id: {
  224.             writeOnce: "initOnly",
  225.             value: null
  226.         },

  227.        /**
  228.         * The size of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
  229.         *
  230.         * @attribute size
  231.         * @type {Number}
  232.         * @initOnly
  233.         */
  234.         size: {
  235.             writeOnce: "initOnly",
  236.             value: 0
  237.         },

  238.        /**
  239.         * The name of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
  240.         *
  241.         * @attribute name
  242.         * @type {String}
  243.         * @initOnly
  244.         */
  245.         name: {
  246.             writeOnce: "initOnly",
  247.             value: null
  248.         },

  249.        /**
  250.         * The date that the file wrapped by FileFlash was created on. This value is supplied by the Flash player uploader.
  251.         *
  252.         * @attribute dateCreated
  253.         * @type {Date}
  254.         * @initOnly
  255.         */
  256.         dateCreated: {
  257.             writeOnce: "initOnly",
  258.             value: null
  259.         },

  260.        /**
  261.         * The date that the file wrapped by FileFlash was last modified on. This value is supplied by the Flash player uploader.
  262.         *
  263.         * @attribute dateModified
  264.         * @type {Date}
  265.         * @initOnly
  266.         */
  267.         dateModified: {
  268.             writeOnce: "initOnly",
  269.             value: null
  270.         },

  271.        /**
  272.         * The number of bytes of the file that has been uploaded to the server. This value is
  273.         * non-zero only while a file is being uploaded.
  274.         *
  275.         * @attribute bytesUploaded
  276.         * @type {Date}
  277.         * @readOnly
  278.         */
  279.         bytesUploaded: {
  280.             readOnly: true,
  281.             value: 0
  282.         },

  283.        /**
  284.         * The type of the file wrapped by FileFlash. This value is provided by the Flash player
  285.         * uploader.
  286.         *
  287.         * @attribute type
  288.         * @type {String}
  289.         * @initOnly
  290.         */
  291.         type: {
  292.             writeOnce: "initOnly",
  293.             value: null
  294.         },

  295.        /**
  296.         * The instance of Y.SWF wrapping the Flash player uploader associated with this file.
  297.         *
  298.         * @attribute uploder
  299.         * @type {SWF}
  300.         * @initOnly
  301.         */
  302.         uploader: {
  303.             writeOnce: "initOnly",
  304.             value: null
  305.         }
  306.         }
  307.     });

  308.     Y.FileFlash = FileFlash;
  309.