OSDN Git Service

Version 0.6.131, cleanup X.EventDispatcher.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 01_XNetXHR.js
index 6c3c520..fb35d5e 100644 (file)
@@ -42,7 +42,7 @@ var X_Net_XHR_W3C      = ( !X_UA.IE7 || !X_URL_IS_LOCAL ) && window[ 'XMLHttpReq
                'for(;i<5;){',\r
                        'try{',\r
                                'return[v[++i],new ActiveXObject(n[i])]',\r
-                       '}catch(e){return false}',\r
+                       '}catch(e){}',\r
                '}'\r
        ].join( '' ) ) )();\r
 \r
@@ -85,15 +85,16 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
        X_NET_XHRWrapper = X_Class_override(\r
                new X.EventDispatcher(),\r
                {\r
-                                               \r
+                       \r
+                       _rawType   : X_EventDispatcher_EVENT_TARGET_TYPE.XHR,\r
                        _rawObject : X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X,\r
-                       _isXHR     : true,  // X.EventDispatcher で使用 for ie8-\r
                        _isXDR     : false, // for ie8\r
                        \r
                        _method    : '',\r
                        _type      : '',\r
                        _busy      : false,\r
                        _canceled  : false,\r
+                       _error     : false,\r
                        _percent   : 0,\r
                        _timerID   : 0,\r
                        \r
@@ -104,7 +105,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        async    = obj[ 'async' ],\r
                                        user     = obj[ 'user' ],\r
                                        password = obj[ 'password' ],\r
-                                       headers  = obj[ 'headers' ],\r
+                                       headers  = obj[ 'headers' ] || {},\r
                                        postbody = obj[ 'postbody' ],\r
                                        timeout  = obj[ 'timeout' ] || 20000,\r
                                        temp;\r
@@ -147,7 +148,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                                        break;\r
                                                case 'json' :\r
                                                case 'moz-json' :\r
-                                                       raw.responseType = this._type;\r
+                                                       raw.responseType = X_UA.Gecko ? this._type : ''; // Iron 37 でエラー\r
                                                        break;\r
                                                case 'document' :\r
                                                case 'xml' :\r
@@ -167,7 +168,13 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                // http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r_1.html\r
                                // raw.overrideMimeType()\r
                                if( !X_Net_XHR_ACTIVE_X && X.Type.isFunction( raw.setRequestHeader ) ){\r
+                                       \r
+                                       // http://nakigao.sitemix.jp/blog/?p=2040\r
+                                       // SafariでHTTP/412のエラー\r
+                                       headers[ 'If-Modified-Since' ] = ( new Date ).toUTCString();\r
+                                       \r
                                        for( p in headers ){\r
+                                               if( X_EMPTY_OBJECT[ p ] ) continue;\r
                                                raw.setRequestHeader( p, headers[ p ] ); // Opera8.01+, MSXML3+\r
                                        };              \r
                                };\r
@@ -175,17 +182,17 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                if( raw.timeout !== undefined ){\r
                                        raw.timeout = timeout;\r
                                } else {\r
-                                       //this._timerID = X.Timer.once( timeout, this, this.onTimeout );\r
+                                       this._timerID = X.Timer.once( timeout, this, this.onTimeout );\r
                                };      \r
                                \r
                                // send 前にフラグを立てる,回線が早いと raw.send() 内で onload -> _busy = false ののち、 _busy = true するため。\r
                                this._busy = true;\r
                                \r
 \r
-                               //raw.send( postbody || '' );\r
-                               this._timerID = X.Timer.once( 16, this, this._lazySend, [ postbody || '', timeout ] );\r
+                               raw.send( postbody || '' );\r
+                               //this._timerID = X.Timer.once( 16, this, this._lazySend, [ postbody || '', timeout ] );\r
                        },\r
-                       \r
+                       /*\r
                        // send() 内で onload するケースがあり、そのときはイベントリスナが間に合わないので、タイマーをかませる。\r
                        _lazySend : function( postbody, timeout ){\r
                                if( this._rawObject.timeout === undefined ){\r
@@ -198,7 +205,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                // sendをonreadystatechangeの前に記述すると、ieでは動作しなくなります、、、。\r
                                // konquerorでエラーが発生するのでここでは、とりあえず、send('') としました。\r
                                this._rawObject.send( postbody );                               \r
-                       },\r
+                       }, */\r
                        \r
                        cancel : function(){\r
                                /* X.Net.XHR.CANCELABLE && */ this._rawObject.abort && this._rawObject.abort();\r
@@ -215,8 +222,14 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                // domes.lingua.heliohost.org/dom-intro/load-save2.html\r
                                // 規定上は open() を呼び出すと XMLHttpRequest オブジェクトが未送信状態に戻りますが、\r
                                // Opera 10.10、Safari 4.1 では、同一オリジン制限に違反した XMLHttpRequest オブジェクトは再度 open() しても未送信状態に戻りません。\r
-                               if( X_UA.Opera || X_UA.Webkit ){\r
-                                       \r
+                               \r
+                               // Timeout した Gecko の xhr.response に触るとエラー??\r
+                               if( X_UA.Opera || X_UA.Webkit || X_UA.Gecko ){\r
+                                       if( this._error ){\r
+                                               X_EventDispatcher_toggleAllEvents( this, false );\r
+                                               this._rawObject = new XMLHttpRequest();\r
+                                               X_EventDispatcher_toggleAllEvents( this, true );\r
+                                       };\r
                                };\r
 \r
                                // XMLHttpRequest の使い方\r
@@ -225,7 +238,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                this._rawObject.abort && this._rawObject.abort();\r
 \r
                                this._method   = this._type = '';\r
-                               this._canceled = this._busy = false;\r
+                               this._canceled = this._busy = this._error = false;\r
                                this._timerID && X.Timer.remove( this._timerID );\r
                                this._percent  = this._timerID = 0;\r
                        },\r
@@ -276,7 +289,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        case 'load' :\r
                                                \r
                                                if( !live ) return this.reset();\r
-                                               if( this._percent === 100 ) return;\r
+                                               if( !this._busy ) return;\r
                                                \r
                                                \r
                                                this._percent = 100;\r
@@ -325,9 +338,9 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                                                        break;\r
                                                        };\r
 \r
-                                                       this.asyncDispatch( { type : X.Event.SUCCESS, status : status || 200, data : data } );                                                  \r
+                                                       this.asyncDispatch( 32, { type : X.Event.SUCCESS, status : status || 200, data : data } );                                                      \r
                                                } else {\r
-                                                       live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status || 0, percent : 100 } );\r
+                                                       live && this.asyncDispatch( 32, { type : X.Event.ERROR, status : raw.status || 0, percent : 100 } );\r
                                                };\r
                                                break;\r
                                        \r
@@ -340,12 +353,14 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        \r
                                        case 'error' :\r
                                        //console.dir( e );\r
-                                               this._busy = false;\r
-                                               live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status } );\r
+                                               this._busy  = false;\r
+                                               this._error = X_UA.Opera || X_UA.Webkit;\r
+                                               live && this.asyncDispatch( 32, { type : X.Event.ERROR, status : raw.status } );\r
                                                break;\r
 \r
                                        case 'timeout' : // Gecko 12.0 https://developer.mozilla.org/ja/docs/XMLHttpRequest/Synchronous_and_Asynchronous_Requests\r
-                                               this._busy = false;\r
+                                               this._busy  = false;\r
+                                               this._error = !!X_UA.Gecko;\r
                                                this.asyncDispatch( X.Event.TIMEOUT );\r
                                                break;\r
                                };\r