Repository: flex-asjs
Updated Branches:
  refs/heads/develop b88f92e7b -> ef400cfc2


Added support for JS upload progress events


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/ef400cfc
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/ef400cfc
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/ef400cfc

Branch: refs/heads/develop
Commit: ef400cfc253279b11d9e0a62e611e0e0579fb582
Parents: b88f92e
Author: Harbs <ha...@in-tools.com>
Authored: Fri Jul 14 12:52:43 2017 +0300
Committer: Harbs <ha...@in-tools.com>
Committed: Fri Jul 14 12:52:43 2017 +0300

----------------------------------------------------------------------
 .../org/apache/flex/events/ProgressEvent.as     |  10 ++
 .../Network/src/main/flex/NetworkClasses.as     |   1 +
 .../flex/org/apache/flex/net/URLBinaryLoader.as |  48 ++------
 .../org/apache/flex/net/URLBinaryUploader.as    | 102 +++++++++++++++++
 .../main/flex/org/apache/flex/net/URLStream.as  |  52 ++++++---
 .../flex/org/apache/flex/net/URLUploadStream.as | 114 +++++++++++++++++++
 6 files changed, 278 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ef400cfc/frameworks/projects/Core/src/main/flex/org/apache/flex/events/ProgressEvent.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Core/src/main/flex/org/apache/flex/events/ProgressEvent.as
 
b/frameworks/projects/Core/src/main/flex/org/apache/flex/events/ProgressEvent.as
index cc15416..be1606d 100644
--- 
a/frameworks/projects/Core/src/main/flex/org/apache/flex/events/ProgressEvent.as
+++ 
b/frameworks/projects/Core/src/main/flex/org/apache/flex/events/ProgressEvent.as
@@ -75,6 +75,16 @@ package org.apache.flex.events
          */
                public static const PROGRESS:String = "progress";
 
+        /**
+         *  For upload progress events.
+         *  
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+               public static const UPLOAD_PROGRESS:String = "uploadprogress";
+
                /**
                 * Create a copy/clone of the ProgressEvent object.
                 *

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ef400cfc/frameworks/projects/Network/src/main/flex/NetworkClasses.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Network/src/main/flex/NetworkClasses.as 
b/frameworks/projects/Network/src/main/flex/NetworkClasses.as
index 51482f3..c4b5177 100644
--- a/frameworks/projects/Network/src/main/flex/NetworkClasses.as
+++ b/frameworks/projects/Network/src/main/flex/NetworkClasses.as
@@ -30,6 +30,7 @@ internal class NetworkClasses
 {      
        import org.apache.flex.net.URLBinaryLoader; URLBinaryLoader;
     import org.apache.flex.net.HTTPConstants; HTTPConstants;
+    import org.apache.flex.net.URLBinaryUploader; URLBinaryUploader;
 }
 
 }

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ef400cfc/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
index 326ccbe..7d46cbc 100644
--- 
a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
+++ 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as
@@ -27,7 +27,7 @@ package org.apache.flex.net
 
 
        /**
-        *  The BinaryUploader class is a relatively low-level class designed 
to get
+        *  The URLBinaryLoader class is a relatively low-level class designed 
to get
         *  binary data over HTTP the intent is to create similar classes for 
text and URL vars.  
         *  
         *  @langversion 3.0
@@ -59,7 +59,7 @@ package org.apache.flex.net
                public var endian:String = Endian.BIG_ENDIAN;
                
 
-        private var stream:URLStream;
+        protected var stream:URLStream;
         
                /**
                 *  The number of bytes loaded so far.
@@ -84,13 +84,17 @@ package org.apache.flex.net
         public function URLBinaryLoader()
         {
             super();
+            createStream();
+        }
+        protected function createStream():void
+        {
             stream = new URLStream();
         }
         private function progressFunction(stream:URLStream):void
         {
             bytesLoaded = stream.bytesLoaded;
             bytesTotal = stream.bytesTotal;
-            dispatchEvent(new 
ProgressEvent(ProgressEvent.PROGRESS,false,false,bytesLoaded,bytesTotal));
+            dispatchEvent(new 
ProgressEvent("progress",false,false,bytesLoaded,bytesTotal));
             if(onProgress)
                 onProgress(this);
         }
@@ -98,14 +102,14 @@ package org.apache.flex.net
         private function statusFunction(stream:URLStream):void
         {
             requestStatus = stream.requestStatus;
-            dispatchEvent(new 
DetailEvent(HTTPConstants.STATUS,false,false,""+requestStatus));
+            dispatchEvent(new 
DetailEvent("httpStatus",false,false,""+requestStatus));
             if(onStatus)
                 onStatus(this);
         }
         
         private function errorFunction(stream:URLStream):void
         {
-            dispatchEvent(new 
DetailEvent(HTTPConstants.COMMUNICATION_ERROR,false,false,""+requestStatus));
+            dispatchEvent(new 
DetailEvent("communicationError",false,false,""+requestStatus));
             if(onError)
                 onError(this);
             cleanupCallbacks();
@@ -114,13 +118,13 @@ package org.apache.flex.net
         private function completeFunction(stream:URLStream):void
         {
                        data = stream.response;
-            dispatchEvent(new 
org.apache.flex.events.Event(HTTPConstants.COMPLETE));
+            dispatchEvent(new org.apache.flex.events.Event("complete"));
             if(onComplete)
                 onComplete(this);
             cleanupCallbacks();
         }
         
-        private function setupCallbacks():void
+        protected function setupCallbacks():void
         {
             stream.onProgress = progressFunction;
 
@@ -153,37 +157,9 @@ package org.apache.flex.net
         public function close():void
         {
             stream.close();
+            cleanupCallbacks();
                        //TODO do we need a callback for canceling?
         }
-        
-        private function completeHandler(event:Event):void
-        {
-            data = stream.response;
-            if (data)
-            {
-                dispatchEvent(event);
-                               if(onComplete)
-                                       onComplete(this);
-
-            }
-            else
-            {
-                // TODO dipatch error event?
-                dispatchEvent(new Event(HTTPConstants.IO_ERROR));
-                               if(onError)
-                                       onError(this);
-            }
-                       cleanupCallbacks();
-        }
-        
-        private function progressHandler(event:ProgressEvent):void
-        {
-            this.bytesLoaded = event.current;
-            this.bytesTotal = event.total;
-            dispatchEvent(event);
-                       if(onProgress)
-                               onProgress(this);
-        }
     }
 }
 

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ef400cfc/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryUploader.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryUploader.as
 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryUploader.as
new file mode 100644
index 0000000..6650d38
--- /dev/null
+++ 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryUploader.as
@@ -0,0 +1,102 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package org.apache.flex.net
+{
+
+       import org.apache.flex.events.ProgressEvent;
+       import org.apache.flex.net.URLUploadStream;
+       import org.apache.flex.net.URLBinaryUploader;
+
+       /**
+        * @see URLBinaryLoader
+        * The URLBinaryUploader class subclasses URLBinaryLoader to offer 
upload progress events.
+        *  
+        * This class is only used for JS implementations because Flash upload 
events need to be handled differently.
+        * In Flash, URLLoader does not dispatch upload events. It only 
dispatches download events.
+        * To get upload events in Flash, you need to use 
File/FileReference.upload() and attach event listeners to that.
+        *
+        * Care should be taken when using this class because it attaches a 
progress listener to the xhr.upload object.
+        * Doing so causes browsers to send OPTIONS requests. This will return 
an unauthorized response from servers not
+        * configured to allow CORS OPTIONS requests. See this S.O. post for 
details. https://stackoverflow.com/a/17057853
+        *  
+        * @langversion 3.0
+        * @playerversion Flash 10.2
+        * @playerversion AIR 2.6
+        * @productversion FlexJS 0.9.0
+        */
+       public class URLBinaryUploader extends URLBinaryLoader
+       {
+               public function URLBinaryUploader()
+               {
+                       super();
+               }
+        override protected function createStream():void
+        {
+            stream = new URLUploadStream();
+        }
+
+               /**
+                * @flexjsignorecoercion org.apache.flex.net.URLUploadStream
+                */
+        override protected function setupCallbacks():void
+        {
+                       super.setupCallbacks();
+                       (stream as URLUploadStream).onUploadProgress = 
uploadProgressFunction;
+        }
+               override protected function cleanupCallbacks():void
+               {
+                       super.cleanupCallbacks();
+                       onUploadProgress = null;
+               }
+
+               /**
+                *  Callback for upload progress event.
+                *  
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.9.0
+                */             
+               public var onUploadProgress:Function;
+
+               /**
+                *  Convenience function for upoad progress event to allow 
chaining.
+                *  
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.9.0
+                */             
+               public function 
uploadProgress(callback:Function):URLBinaryUploader
+               {
+                       onUploadProgress = callback;
+                       return this;
+               }
+
+        private function uploadProgressFunction(stream:URLStream):void
+        {
+            bytesLoaded = stream.bytesLoaded;
+            bytesTotal = stream.bytesTotal;
+            dispatchEvent(new 
ProgressEvent("uploadprogress",false,false,bytesLoaded,bytesTotal));
+            if(onUploadProgress)
+                onUploadProgress(this);
+        }
+       }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ef400cfc/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
index 3946fa3..e1609b5 100644
--- a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
+++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as
@@ -38,19 +38,26 @@ package org.apache.flex.net
         import flash.net.URLVariables;
         import flash.utils.ByteArray;
     }
-        
+    
+       /**
+        * The URLStream class deals with the underlying platform-specifc 
architecture for HTTP Requests
+        * It makes the request and stores the response, dispatching events.
+        */
     public class URLStream extends EventDispatcher
     {
         COMPILE::JS 
         {
-            private var xhr:XMLHttpRequest;
+            protected var xhr:XMLHttpRequest;
         }
             
         COMPILE::SWF
         {
             private var flashUrlStream:flash.net.URLStream
         }
-            
+        
+               /**
+                * constructor
+                */
         public function URLStream()
         {
             super();
@@ -76,6 +83,10 @@ package org.apache.flex.net
                 */        
                public var bytesTotal:uint = 0;
 
+               /**
+                * The BinaryData reponse received from the request. This can 
be a response or an error response.
+                * The client should check the status to know how to interpret 
the response.
+                */
         public function get response():BinaryData
         {
             COMPILE::JS
@@ -90,6 +101,13 @@ package org.apache.flex.net
                        }
         }
 
+               /**
+                * loads the request
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.7.0
+                */
         public function load(urlRequest:org.apache.flex.net.URLRequest):void
         {
             COMPILE::JS {
@@ -114,8 +132,7 @@ package org.apache.flex.net
                        xhr.setRequestHeader("Content-type", 
urlRequest.contentType);
                                }
                                var requestData:Object = urlRequest.data is 
BinaryData ? (urlRequest.data as BinaryData).data : 
HTTPUtils.encodeUrlVariables(urlRequest.data);
-                               xhr.send(requestData);
-//                             
xhr.send(HTTPUtils.encodeUrlVariables(urlRequest.data));
+                               send(requestData);
             }
             COMPILE::SWF 
             {
@@ -152,6 +169,15 @@ package org.apache.flex.net
                 flashUrlStream.load(req);
             }
         }
+               /**
+                * send is a protected function in js so a subclass can attach 
an upload listener
+                * without rewriting the whole load() function
+                */
+               COMPILE::JS
+               protected function send(requestData:Object):void
+               {
+                       xhr.send(requestData);
+               }
 
                /**
                 *  HTTP status changed (Flash only).
@@ -221,7 +247,7 @@ package org.apache.flex.net
         }
 
                /**
-                *  Upload is progressing (Flash only).
+                *  Download is progressing (Flash only).
                 *
                 *  @langversion 3.0
                 *  @playerversion Flash 10.2
@@ -241,7 +267,7 @@ package org.apache.flex.net
         }
 
                /**
-                *  Upload is progressing (JS only).
+                *  Download is progressing (JS only).
                 *
                 *  @langversion 3.0
                 *  @playerversion Flash 10.2
@@ -278,26 +304,26 @@ package org.apache.flex.net
                        if(xhr.status == 0)
                        {
                                //Error. We don't know if there's a network 
error or a CORS error so there's no detail
-                               dispatchEvent(new 
DetailEvent(HTTPConstants.COMMUNICATION_ERROR));
+                               dispatchEvent(new 
DetailEvent("communicationError"));
                                if(onError)
                                        onError(this);
                        }
                        else if(xhr.status < 200)
                        {
-                               dispatchEvent(new 
DetailEvent(HTTPConstants.COMMUNICATION_ERROR,false,false,""+requestStatus));
+                               dispatchEvent(new 
DetailEvent("communicationError",false,false,""+requestStatus));
                                if(onError)
                                        onError(this);
                        }
                        else if(xhr.status < 300)
                        {
-                               dispatchEvent(new 
org.apache.flex.events.Event(HTTPConstants.COMPLETE));
+                               dispatchEvent(new 
org.apache.flex.events.Event("complete"));
                                if(onComplete)
                                        onComplete(this);
                                
                        }
                        else
                        {
-                               dispatchEvent(new 
DetailEvent(HTTPConstants.COMMUNICATION_ERROR,false,false,""+requestStatus));
+                               dispatchEvent(new 
DetailEvent("communicationError",false,false,""+requestStatus));
                                if(onError)
                                        onError(this);
                        }
@@ -317,7 +343,7 @@ package org.apache.flex.net
                        if(value != requestStatus)
                        {
                                requestStatus = value;
-                               dispatchEvent(new 
DetailEvent(HTTPConstants.STATUS,false,false,""+value));
+                               dispatchEvent(new 
DetailEvent("httpStatus",false,false,""+value));
                                if(onStatus)
                                        onStatus(this);
                        }
@@ -376,7 +402,7 @@ package org.apache.flex.net
                 *  @playerversion AIR 2.6
                 *  @productversion FlexJS 0.7.0
                 */
-               private function cleanupCallbacks():void
+               protected function cleanupCallbacks():void
                {
                        onComplete = null;
                        onError = null;

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ef400cfc/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLUploadStream.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLUploadStream.as
 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLUploadStream.as
new file mode 100644
index 0000000..81f6c9e
--- /dev/null
+++ 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLUploadStream.as
@@ -0,0 +1,114 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.net
+{   
+    import org.apache.flex.events.ProgressEvent;
+    
+       /**
+        * @see URLStream
+        * The URLUploadStream extends the URLStream class to support upload 
progress events.
+        *
+        * This class is only used for JS implementations because Flash upload 
events need to be handled differently.
+        * In Flash, URLLoader does not dispatch upload events. It only 
dispatches download events.
+        * To get upload events in Flash, you need to use 
File/FileReference.upload() and attach event listeners to that.
+        *
+        * Care should be taken when using this class because it attaches a 
progress listener to the xhr.upload object.
+        * Doing so causes browsers to send OPTIONS requests. This will return 
an unauthorized response from servers not
+        * configured to allow CORS OPTIONS requests. See this S.O. post for 
details. https://stackoverflow.com/a/17057853
+        *
+        * @langversion 3.0
+        * @playerversion Flash 10.2
+        * @playerversion AIR 2.6
+        * @productversion FlexJS 0.9.0
+        */
+    public class URLUploadStream extends URLStream
+    {
+            
+        public function URLUploadStream()
+        {
+            super();
+        }
+        
+        COMPILE::JS
+        override protected function send(requestData:Object):void
+               {
+                       xhr.upload.onprogress = xhr_uploadprogress;
+                       xhr.send(requestData);
+               }
+
+               /**
+                *  Upload is progressing (JS only).
+                *
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.7.0
+                */
+               COMPILE::JS
+        private function xhr_uploadprogress(progress:Object):void
+        {
+                       var progEv:ProgressEvent = new 
ProgressEvent("uploadprogress");
+                       progEv.current = bytesLoaded = progress["loaded"];
+                       progEv.total = bytesTotal = progress["total"];
+                       
+            dispatchEvent(progEv);
+                       if(onUploadProgress)
+                               onUploadProgress(this);
+        }
+
+               /**
+                *  Cleanup all callbacks.
+                *
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.7.0
+                */
+               override protected function cleanupCallbacks():void
+               {
+                       super.cleanupCallbacks();
+                       onUploadProgress = null;
+               }
+               
+               /**
+                *  Callback for upload progress event.
+                *  
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.7.0
+                */             
+               public var onUploadProgress:Function;
+
+               /**
+                *  Convenience function for upload progress event to allow 
chaining.
+                *  
+                *  @langversion 3.0
+                *  @playerversion Flash 10.2
+                *  @playerversion AIR 2.6
+                *  @productversion FlexJS 0.7.0
+                */             
+               public function 
uploadProgress(callback:Function):org.apache.flex.net.URLStream
+               {
+                       onUploadProgress = callback;
+                       return this;
+               }
+       }
+}
+

Reply via email to