OSDN Git Service

Version 0.6.147, fix X.UA & X.URL & X.EventDispatcher & X.XHR.
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 06_XURL.js
index 96be643..daa6a8e 100644 (file)
@@ -1,33 +1,61 @@
 \r
+// ------------------------------------------------------------------------- //\r
+// ------------ local variables -------------------------------------------- //\r
+// ------------------------------------------------------------------------- //\r
 var X_URL_BASE_URL = ( function( parts ){\r
                var last = 1 < parts.length && parts[ parts.length - 1 ];\r
                if( last !== false && ( last === '' || //末尾が/で終わるとき\r
-                       last.indexOf( '.' ) !== -1 ) ){//末尾がファイル名で終わる時\r
-                       \r
+                       last.indexOf( '.' ) !== -1 ) ){    //末尾がファイル名で終わる時\r
                        --parts.length;\r
                };\r
                return parts.join( '/' );\r
-       })( location.href.split( '?' )[ 0 ].split( '#' )[ 0 ].split( '/' ) ),\r
+       })( X_URL_cleanup( location.href ).split( '/' ) ),\r
        \r
-       X_URL_IS_LOCAL = location.protocol === 'file:' || location.hostname === 'localhost' || location.hostname === '127.0.0.1';\r
+       X_URL_IS_FILE  = location.protocol === 'file:',\r
+       \r
+       X_URL_IS_LOCAL = X_URL_IS_FILE || location.hostname === 'localhost' || location.hostname === '127.0.0.1',\r
+       \r
+       X_URL_PARAMS = X_URL_ParamToObj( location.search.slice( 1 ) );\r
 \r
+// ------------------------------------------------------------------------- //\r
+// --- interface ----------------------------------------------------------- //\r
+// ------------------------------------------------------------------------- //\r
 \r
-X.URL = {\r
+/**\r
+ * @namespace X.URL\r
+ * @alias X.URL\r
+ */\r
+X[ 'URL' ] = {\r
 \r
-       BASE_URL : X_URL_BASE_URL,\r
+       'BASE_URL'       : X_URL_BASE_URL,\r
+       \r
+       'IS_FILE'        : X_URL_IS_FILE,\r
+       \r
+       'IS_LOCAL'       : X_URL_IS_LOCAL,\r
+       \r
+       'PARAMS'         : X_URL_PARAMS,\r
+       \r
+       'toAbsolutePath' : X_URL_toAbsolutePath,\r
+       \r
+       'isSameDomain'   : X_URL_isSameDomain,\r
+       \r
+       'isSameProtocol' : X_URL_isSameProtocol,\r
        \r
-       IS_LOCAL : X_URL_IS_LOCAL,\r
+       'isLocal'        : X_URL_isLocal,\r
        \r
-       toAbsolutePath : X_URL_toAbsolutePath\r
+       'cleanup'        : X_URL_cleanup,\r
+       \r
+       'getEXT'         : X_URL_getEXT\r
 };\r
 \r
-\r
-\r
-       /*\r
-        * original\r
-        * AS3で相対パスを絶対パスに変換する\r
-        * http://www.shin-go.net/motionlab/?p=449\r
-        */\r
+// ------------------------------------------------------------------------- //\r
+// --- implements ---------------------------------------------------------- //\r
+// ------------------------------------------------------------------------- //\r
+/*\r
+ * original\r
+ * AS3で相対パスを絶対パスに変換する\r
+ * http://www.shin-go.net/motionlab/?p=449\r
+ */\r
 function X_URL_toAbsolutePath( path ){\r
        var s  = '/',\r
                ss = '//',\r
@@ -50,4 +78,59 @@ function X_URL_toAbsolutePath( path ){
                if( i ) path = path.substr( i );\r
        };\r
        return [ _ary[ 0 ], ss, ary.join( s ), s, path ].join( '' );\r
-};
\ No newline at end of file
+};\r
+\r
+function X_URL_isSameDomain( path ){\r
+       path = X_URL_cleanup( X_URL_toAbsolutePath( path ) );\r
+       return path === X_URL_BASE_URL || path.indexOf( X_URL_BASE_URL + '/' ) === 0;\r
+};\r
+\r
+function X_URL_isSameProtocol( path ){\r
+       return X_URL_toAbsolutePath( path ).indexOf( location.protocol ) === 0;\r
+};\r
+\r
+function X_URL_isLocal( path ){\r
+       return X_URL_toAbsolutePath( path ).indexOf( 'file:' ) === 0;\r
+};\r
+\r
+function X_URL_cleanup( path ){\r
+       return path.split( '?' )[ 0 ].split( '#' )[ 0 ];\r
+};\r
+\r
+function X_URL_getEXT( path ){\r
+       path = X_URL_cleanup( path ).split( '.' );\r
+       return path.length ? path.pop() : '';\r
+};\r
+\r
+function X_URL_objToParam( data ){\r
+       var result = [], k, n = -1;\r
+       for( k in data ){\r
+               if( n !== -1 ) result[ ++n ] = '&';\r
+               result[ ++n ] = k;\r
+               result[ ++n ] = '=';\r
+               result[ ++n ] = encodeURIComponent( data[ k ] );\r
+       }\r
+       return result.join( '' );\r
+};\r
+\r
+function X_URL_ParamToObj( str ){\r
+       var parts = str.split( '&' ),\r
+               i     = 0,\r
+               l     = parts.length,\r
+               obj   = {},\r
+               pair, p;\r
+\r
+       if( !str ) return obj;\r
+       \r
+       for( ; i < l; ++i ){\r
+               pair = parts[ i ];\r
+               p    = pair.indexOf( '=' );\r
+               if( p === -1 ){\r
+                       obj[ decodeURIComponent( pair ) ] = true;\r
+               } else {\r
+                       obj[ decodeURIComponent( pair.substr( 0, p ) ) ] = X_String_parse( decodeURIComponent( pair.substr( p + 1 ) ) );\r
+               };\r
+       };\r
+\r
+       return obj;     \r
+};\r