• R/O
  • HTTP
  • SSH
  • HTTPS

clientJs: Commit

クライアント側 js 開発用


Commit MetaInfo

Revisionb072c0d95a33cb58007a5d2f50a5a9b4bd1f9341 (tree)
Time2015-05-11 10:36:23
Authoritozyun <itozyun@user...>
Commiteritozyun

Log Message

Version 0.6.146, fix XHR.send, add RegExp free encodeURIComponent, fix X.Object.deepCopy.

Change Summary

Incremental Difference

--- a/0.6.x/js/01_core/00_builtin.js
+++ b/0.6.x/js/01_core/00_builtin.js
@@ -140,79 +140,94 @@ Array.prototype.indexOf || (Array.prototype.indexOf = function( searchElement, f
140140 */
141141
142142 /*
143- * Window
143+ * original:
144144 * by https://web.archive.org/web/20100413085309/http://nurucom-archives.hp.infoseek.co.jp/digital/trans-uri.html
145145 */
146+var _builtin_skipEncodeURI = (function(){
147+ var encodeURIComponentTarget = '!\'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~',
148+ encodeURITarget = '#$&+,-/:;=?@',
149+ obj = {}, i;
150+ for( i = encodeURIComponentTarget.length; i; ){
151+ obj[ encodeURIComponentTarget.charCodeAt( --i ) ] = 2;
152+ };
153+ for( i = encodeURITarget.length; i; ){
154+ obj[ encodeURITarget.charCodeAt( --i ) ] = 1;
155+ };
156+ return obj;
157+})();
146158
147-/*
148- * //
149-// TransURI (UTF-8): transURI.js (Ver.041211)
150-//
151-// Copyright (C) http://nurucom-archives.hp.infoseek.co.jp/digital/
152-//
159+// /[^!#$&-;=?-Z_a-z~]/g
160+window.encodeURI || (window.encodeURI = function( x ){ return _builtin_encodeURI( x, 0 ); });
161+// /[^!'-*.0-9A-Z_a-z~-]/g
162+window.encodeURIComponent || (window.encodeURIComponent = function( x ){ return _builtin_encodeURI( x, 1 ); });
153163
154-EncodeURI=function(str){
155- return str.replace(/[^!#$&-;=?-Z_a-z~]/g,function(s){
156- var c=s.charCodeAt(0);
157- return (c<16?"%0"+c.toString(16):c<128?"%"+c.toString(16):c<2048?"%"+(c>>6|192).toString(16)+"%"+(c&63|128).toString(16):"%"+(c>>12|224).toString(16)+"%"+(c>>6&63|128).toString(16)+"%"+(c&63|128).toString(16)).toUpperCase()
158- })
164+function _builtin_encodeURI( x, kind ){
165+ var result = [],
166+ skip = _builtin_skipEncodeURI,
167+ p = '%',
168+ i = 0, l, chr, c;
169+
170+ x += '';
171+
172+ for( l = x.length; i < l; ++i ){
173+ if( !( kind < skip[ c = x.charCodeAt( i ) ] ) ){
174+ chr = (
175+ c < 16 ? '%0' + c.toString(16) :
176+ c < 128 ? p + c.toString(16) :
177+ c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :
178+ p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)
179+ ).toUpperCase();
180+ } else {
181+ chr = x.charAt( i );
182+ };
183+ result[ i ] = chr;
184+ };
185+
186+ return result.join( '' );
159187 };
160188
161-EncodeURIComponent=function(str){
162- return str.replace(/[^!'-*.0-9A-Z_a-z~-]/g,function(s){
163- var c=s.charCodeAt(0);
164- return (c<16?'%0'+c.toString(16):c<128?'%'+c.toString(16):c<2048?'%'+(c>>6|192).toString(16)+'%'+(c&63|128).toString(16):'%'+(c>>12|224).toString(16)+'%'+(c>>6&63|128).toString(16)+'%'+(c&63|128).toString(16)).toUpperCase()
165- })
166-};
167189
168-DecodeURI=function(str){
169- return str.replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig,function(s){
170- var c=parseInt(s.substring(1),16);
171- return String.fromCharCode(c<128?c:c<224?(c&31)<<6|parseInt(s.substring(4),16)&63:((c&15)<<6|parseInt(s.substring(4),16)&63)<<6|parseInt(s.substring(7),16)&63)
172- })
190+function _builtin_decodeURI( x ){
191+ var result = [],
192+ toInt = parseInt,
193+ toChrCode = String.fromCharCode,
194+ n = -1, i = 0, l, chr, decode, code, memory;
195+
196+ x += '';
197+
198+ for( l = x.length; i < l; ++i ){
199+ if( decode ){
200+ code = toInt( x.substr( i, 2 ), 16 );
201+ ++i;
202+ if( 127 < code ){
203+ if( 223 < code ){
204+ memory = ( code & 15 ) << 12;
205+ code = toInt( x.substr( i + 2, 2 ), 16 ) & 63; // 00%00%00
206+ i += 3;
207+ memory += code << 6;
208+ } else {
209+ memory = ( code & 63 ) << 6;
210+ };
211+ code = toInt( x.substr( i + 2, 2 ), 16 ) & 63;
212+ i += 3;
213+ code += memory;
214+ };
215+ // if( code !== code ) error
216+ //console.log( code );
217+ result[ ++n ] = toChrCode( code );
218+ decode = false;
219+ } else {
220+ chr = x.charAt( i );
221+ if( !( decode = chr === '%' ) ){
222+ result[ ++n ] = chr;
223+ };
224+ };
225+ };
226+ return result.join( '' );
173227 };
174- */
175-
176-/* 正規表現が使われているため、まだ投入しない itozyun*/
177-window.encodeURI || (window.encodeURI = function (x) {
178- return ("" + x).replace(/[^!#$&-;=?-Z_a-z~]/g, function (s) {
179- var c = s.charCodeAt(0), p = "%";
180- return (
181- c < 16 ? "%0" + c.toString(16) :
182- c < 128 ? p + c.toString(16) :
183- c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :
184- p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)
185- ).toUpperCase();
186- });
187-});
188-
189-window.encodeURIComponent || (window.encodeURIComponent = function (x) {
190- return ("" + x).replace(/[^!'-*.0-9A-Z_a-z~-]/g, function (s) {
191- var c = s.charCodeAt(0), p = "%";
192- return (
193- c < 16 ? "%0" + c.toString(16) :
194- c < 128 ? p + c.toString(16) :
195- c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :
196- p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)
197- ).toUpperCase();
198- });
199-});
200-
201-// 手抜き
202-window.decodeURI || (window.decodeURI = function (x) {
203- return ("" + x).replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig, function (s) {
204- var c = parseInt(s.substring(1), 16);
205- return String.fromCharCode(
206- c < 128 ? c :
207- c < 224 ? (c & 31) << 6 | parseInt(s.substring(4), 16) & 63 :
208- ((c & 15) << 6 | parseInt(s.substring(4), 16) & 63) << 6 | parseInt(s.substring(7), 16) & 63
209- );
210- });
211-});
212-
213-
214228
215-//window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);
229+window.decodeURI || (window.decodeURI = _builtin_decodeURI);
230+window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);
216231
217232
218233 /*
--- a/0.6.x/js/01_core/04_XObject.js
+++ b/0.6.x/js/01_core/04_XObject.js
@@ -117,7 +117,7 @@ function X_Object_deepCopy_( src, objSrc, objCopy, n ) {
117117 };
118118 for( k in src ){
119119 //if( X_EMPTY_OBJECT[ k ] ) continue;
120- ret[ k ] = clone( src[ k ], objSrc, objCopy, n );
120+ ret[ k ] = X_Object_deepCopy_( src[ k ], objSrc, objCopy, n );
121121 };
122122 return ret;
123123 };
--- a/0.6.x/js/01_core/05_XString.js
+++ b/0.6.x/js/01_core/05_XString.js
@@ -24,7 +24,9 @@ X[ 'String' ] = {
2424
2525 'toChrReferance' : X_String_toChrReferance,
2626
27- 'isNumberString' : X_String_isNumberString
27+ 'isNumberString' : X_String_isNumberString,
28+
29+ 'serialize' : X_String_serialize
2830 };
2931
3032 // ------------------------------------------------------------------------- //
@@ -108,3 +110,67 @@ function X_String_isNumberString( v ){
108110 return '' + n === v || '' + n === '0' + v;
109111 };
110112
113+// https://github.com/jquery/jquery/blob/master/src/serialize.js
114+function X_String_serialize( a, traditional ) {
115+ var prefix,
116+ list = [];
117+
118+ // If an array was passed in, assume that it is an array of form elements.
119+ if ( X_Type_isArray( a ) && false ) {
120+ // Serialize the form elements
121+ //jQuery.each( a, function() {
122+ // X_String_serialize_addParam( list, this.name, this.value );
123+ //});
124+
125+ } else {
126+ // If traditional, encode the 'old' way (the way 1.3.2 or older
127+ // did it), otherwise encode params recursively.
128+ for ( prefix in a ) {
129+ X_String_serialize_buildParams( list, prefix, a[ prefix ], traditional );
130+ }
131+ }
132+
133+ // Return the resulting serialization
134+ return list.join( '&' ).split( '%20' ).join( '+' );
135+};
136+
137+function X_String_serialize_addParam( list, key, value ){
138+ // If value is a function, invoke it and return its value
139+ value = X_Type_isFunction( value ) ? value() : ( value == null ? '' : value );
140+ list[ list.length ] = encodeURIComponent( key ) + '=' + encodeURIComponent( value );
141+};
142+
143+function X_String_serialize_buildParams( list, prefix, obj, traditional ) {
144+ var name;
145+
146+ if ( X_Type_isArray( obj ) ) {
147+ // Serialize array item.
148+ for( i = 0, l = obj.length; i < l; ++i ){
149+ v = obj[ i ];
150+ if ( traditional || prefix === '[]' ) {
151+ // Treat each array item as a scalar.
152+ X_String_serialize_addParam( list, prefix, v );
153+
154+ } else {
155+ // Item is non-scalar (array or object), encode its numeric index.
156+ X_String_serialize_buildParams(
157+ list,
158+ prefix + '[' + ( X_Type_isObject( v ) ? i : '' ) + ']',
159+ v,
160+ traditional
161+ );
162+ };
163+ };
164+
165+ } else
166+ if ( !traditional && X_Type_isObject( obj ) ) {
167+ // Serialize object item.
168+ for ( name in obj ) {
169+ X_String_serialize_buildParams( list, prefix + '[' + name + ']', obj[ name ], traditional );
170+ };
171+
172+ } else {
173+ // Serialize scalar item.
174+ X_String_serialize_addParam( list, prefix, obj );
175+ };
176+};
--- a/0.6.x/js/06_net/00_XNet.js
+++ b/0.6.x/js/06_net/00_XNet.js
@@ -74,7 +74,7 @@ X[ 'Net' ] = X_EventDispatcher[ 'inherits' ](
7474 alert( 'X.Net args error' );
7575 return;
7676 };
77-
77+ url = opt[ 'url' ];
7878 };
7979
8080 if( !X_Type_isString( url ) ){
@@ -107,7 +107,7 @@ X[ 'Net' ] = X_EventDispatcher[ 'inherits' ](
107107
108108 };
109109
110- opt = X_Object_clone( opt );
110+ opt = X_Object_deepCopy( opt );
111111 opt.netType = type;
112112 opt[ 'url' ] = url;
113113
@@ -156,7 +156,7 @@ var X_NET_TYPE_XHR = 1,
156156 X_NET_completePhase;
157157
158158 function X_NET_proxyDispatch( e ){
159- var i, flag;
159+ var i, flag, auth;
160160
161161 switch( e.type ){
162162 case X_EVENT_BEFORE_KILL_INSTANCE :
@@ -185,8 +185,16 @@ function X_NET_proxyDispatch( e ){
185185 case X_EVENT_PROGRESS :
186186 this[ 'dispatch' ]( e );
187187 break;
188- case X_EVENT_SUCCESS :
188+
189189 case X_EVENT_ERROR :
190+ if( e.status === 401 ){
191+ auth = X_Pair_get( this )[ 'auth' ];
192+ if( auth ){
193+ X_Pair_get( auth ).onAuthError( auth );
194+ };
195+ };
196+
197+ case X_EVENT_SUCCESS :
190198 case X_EVENT_TIMEOUT :
191199 X_NET_completePhase = true;
192200 this
@@ -205,6 +213,7 @@ function X_NET_proxyDispatch( e ){
205213 };
206214
207215 function X_NET_shiftQueue(){
216+ var auth, authSettings;
208217
209218 if( X_NET_currentQueue ){
210219 if( X_NET_currentWrapper._busy ) return;
@@ -222,6 +231,28 @@ function X_NET_shiftQueue(){
222231 switch( X_NET_currentData.netType ){
223232 case X_NET_TYPE_XHR :
224233 X_NET_currentWrapper = X_NET_XHRWrapper || X_TEMP.X_Net_XHR_init();
234+ // TODO OAuth2
235+ // oauth2.authState() -> NEED_AUTH -> COMPLETE, refresh 中なら後回し。
236+
237+ if( auth = X_NET_currentData[ 'auth' ] ){
238+ authSettings = X_Pair_get( auth );
239+ switch( auth.state() ){
240+ case 0 :
241+ case 1 :
242+ if( !( auth[ 'dispatch' ]( X_EVENT_NEED_AUTH ) & X_Callback_PREVENT_DEFAULT ) ){
243+ authSettings.lazyReq = X_NET_currentQueue;
244+ };
245+ X_NET_currentQueue = null;
246+ X_NET_shiftQueue();
247+ break;
248+ case 2 : // refresh token
249+ X_NET_QUEUE_LIST.push( X_NET_currentQueue );
250+ X_NET_currentQueue = null;
251+ X_NET_shiftQueue();
252+ return;
253+ };
254+ authSettings.updateRequest( auth, X_NET_currentData );
255+ };
225256 break;
226257 case X_NET_TYPE_JSONP :
227258 X_NET_currentWrapper = X_NET_JSONPWrapper || X_TEMP.X_NET_JSONP_init();
--- a/0.6.x/js/06_net/01_XNetXHR.js
+++ b/0.6.x/js/06_net/01_XNetXHR.js
@@ -103,6 +103,8 @@ X_TEMP.X_Net_XHR_init = function(){
103103 _percent : 0,
104104 _timerID : 0,
105105
106+ _auth : null,
107+
106108 load : function( obj ){
107109 var raw = this[ '_rawObject' ],
108110 method = obj[ 'method' ],
@@ -111,26 +113,26 @@ X_TEMP.X_Net_XHR_init = function(){
111113 username = obj[ 'username' ],
112114 password = obj[ 'password' ],
113115 headers = obj[ 'headers' ] || {},
114- postdata = obj[ 'postdata' ],
116+ postdata = obj[ 'postdata' ] || '',
115117 timeout = obj[ 'timeout' ] || 20000,
116118 tmp;
117-
119+
118120 this._dataType = obj[ 'dataType' ] || X_URL_getEXT( url );
119121
120122 if( X_Net_XHR_X_DOMAIN ){
121123 if( X_URL_isSameDomain( url ) ){ // isXDomain
122124 if( this._isXDR ){
123125 X_EventDispatcher_toggleAllEvents( this, false );
124- this[ '_rawObject' ] = X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X;
126+ this[ '_rawObject' ] = raw = X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X;
125127 X_EventDispatcher_toggleAllEvents( this, true );
126- this._isXDR = false;
128+ this._isXDR = false;
127129 };
128130 } else {
129131 if( !this._isXDR ){
130132 X_EventDispatcher_toggleAllEvents( this, false );
131- this[ '_rawObject' ] = X_Net_XHR_X_DOMAIN;
133+ this[ '_rawObject' ] = raw = X_Net_XHR_X_DOMAIN;
132134 X_EventDispatcher_toggleAllEvents( this, true );
133- this._isXDR = true;
135+ this._isXDR = true;
134136 };
135137 };
136138 };
@@ -199,7 +201,7 @@ X_TEMP.X_Net_XHR_init = function(){
199201 console.log( obj[ 'mimeType' ] || tmp );
200202 };
201203
202- if( !X_Net_XHR_ACTIVE_X && X_Type_isFunction( raw.setRequestHeader ) ){
204+ if( /* !X_Net_XHR_ACTIVE_X && !this._isXDR && */ X_Type_isFunction( raw.setRequestHeader ) ){
203205
204206 // http://nakigao.sitemix.jp/blog/?p=2040
205207 // json 取得時に SafariでHTTP/412のエラー。但し相手が audio の場合、この指定があるとロードに失敗する。 iOS8.2, iOS7.1 では遭遇せず
@@ -222,9 +224,8 @@ X_TEMP.X_Net_XHR_init = function(){
222224
223225 // send 前にフラグを立てる,回線が早いと raw.send() 内で onload -> _busy = false ののち、 _busy = true するため。
224226 this._busy = true;
225-
226- // TODO toString
227- raw.send( postdata || '' );
227+
228+ raw.send( X_Type_isString( postdata ) ? postdata : X_String_serialize( postdata ) );
228229 },
229230
230231 cancel : function(){
@@ -367,8 +368,9 @@ X_TEMP.X_Net_XHR_init = function(){
367368 };
368369
369370 this[ 'asyncDispatch' ]( 32, { type : X_EVENT_SUCCESS, status : status || 200, data : data } );
370- } else {
371- live && this[ 'asyncDispatch' ]( 32, { type : X_EVENT_ERROR, status : raw.status || 0, 'percent' : 100 } );
371+ } else
372+ if( live ){
373+ this[ 'asyncDispatch' ]( 32, { type : X_EVENT_ERROR, status : raw.status || 0, 'percent' : 100 } );
372374 };
373375 break;
374376
Show on old repository browser