OSDN Git Service

0900ae979ce8a7c61ddd677bf80be97366e8c0a4
[pettanr/clientJs.git] / 0.6.x / js / 05_net / 00_XNet.js
1 // TODO  onlineevent offlineevent, netspeed\r
2 X.Net = {\r
3 \r
4         xhrGet : function( url ){\r
5                 return new X_NET_Queue( X_NET_TYPE_XHR, { method : 'GET', url : url } );\r
6         },\r
7         \r
8         xhrPost : function( url, data ){\r
9                 return new X_NET_Queue( X_NET_TYPE_XHR, { method : 'POST', url : url, postbody : postbody } );\r
10         },\r
11         \r
12         formGet : function(){\r
13                 \r
14         },\r
15         \r
16         formPost : function(){\r
17                 \r
18         },\r
19         \r
20         // TODO chashe\r
21         // TODO iframe useful or not.\r
22         // TODO file: では http: は使えない\r
23         jsonp : function( url, useIframe ){\r
24                 return new X_NET_Queue( X_NET_TYPE_JSONP, url );\r
25         },\r
26         \r
27         image : function( url, needSize, useIframe ){\r
28                 return new X_NET_Queue( X_NET_TYPE_IMAGE, url );\r
29         },\r
30         \r
31         // <script>, <link>\r
32         \r
33         amountQueue : function(){\r
34                 return X_NET_QUEUE_LIST.length;\r
35         }\r
36 \r
37 };\r
38 \r
39 \r
40 var X_NET_IWrapper = function(){};\r
41         X_NET_IWrapper.prototype.load   = function(){};\r
42         X_NET_IWrapper.prototype.cancel = function(){};\r
43         X_NET_IWrapper.prototype.reset  = function(){};\r
44 \r
45 \r
46 var X_NET_TYPE_XHR   = 1,\r
47         X_NET_TYPE_JSONP = 2,\r
48         X_NET_TYPE_FORM  = 3,\r
49         X_NET_TYPE_IMAGE = 4,\r
50 \r
51         X_NET_QUEUE_LIST = [],\r
52 \r
53         X_NET_XHRWrapper,\r
54         X_NET_JSONPWrapper,\r
55         X_NET_FormWrapper,\r
56         X_NET_ImageWrapper,\r
57 \r
58         X_NET_currentWrapper,\r
59         X_NET_currentQueue,\r
60 \r
61         X_NET_Queue = X.EventDispatcher.inherits(\r
62                 'XNetQueue',\r
63                 X.Class.POOL_OBJECT,\r
64                 {\r
65                         type : 0,\r
66                         data : null,\r
67                         \r
68                         Constructor : function( type, data ){\r
69                                 this.type = type;\r
70                                 this.data = data;                               \r
71                                 \r
72                                 this.listen( X.Event.COMPLETE, X_NET_proxyDispatch );\r
73                                 \r
74                                 X_NET_QUEUE_LIST[ X_NET_QUEUE_LIST.length ] = this;\r
75                                 !X_NET_currentQueue && X_NET_shiftQueue();\r
76                         },\r
77                         \r
78                         busy : function(){\r
79                                 return this === X_NET_currentQueue && X_NET_currentWrapper._busy;\r
80                         },\r
81                         \r
82                         cancel : function(){\r
83                                 var i = X_NET_QUEUE_LIST.indexOf( this );\r
84                                 if( i !== -1 ){\r
85                                         X_NET_QUEUE_LIST.splice( i, 1 );\r
86                                         this.asyncDispatch( 0, { type : X.Event.CANCELED } );\r
87                                 } else\r
88                                 if( this === X_NET_currentQueue ){\r
89                                         X_NET_currentWrapper.cancel();\r
90                                 };\r
91                         }\r
92                 }\r
93         );\r
94 \r
95 function X_NET_proxyDispatch( e ){\r
96         switch( e.type ){\r
97                 case X.Event.BEFORE_KILL_INSTANCE :\r
98                         break;\r
99                 case X.Event.KILL_INSTANCE :\r
100                         break;                  \r
101                 case X.Event.KILL_INSTANCE_CANCELED :\r
102                         break;\r
103                 case X.Event.PROGRESS :\r
104                         console.log( 'q: ' + e.type );\r
105                         this.dispatch( e );\r
106                         break;\r
107                 case X.Event.SUCCESS :\r
108                 case X.Event.ERROR :\r
109                 case X.Event.TIMEOUT :\r
110                 case X.Event.CANCELED :\r
111                         console.log( 'q: ' + e.type );\r
112                         this.dispatch( e );\r
113                         this.asyncDispatch( 0, { type : X.Event.COMPLETE } );\r
114                         break;\r
115                 case X.Event.COMPLETE :\r
116                         console.log( 'complete. then kill()' );\r
117                         this.kill();\r
118                         X_NET_shiftQueue();\r
119                         break;          \r
120         };\r
121 };\r
122 \r
123 function X_NET_shiftQueue(){\r
124         var queue;\r
125         \r
126         if( X_NET_currentQueue ){\r
127                 if( X_NET_currentWrapper._busy ) return;\r
128                 X_NET_currentWrapper\r
129                         .unlisten( [ X.Event.PROGRESS, X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_currentQueue, X_NET_proxyDispatch )\r
130                         .reset();\r
131                 X_NET_currentQueue = X_NET_currentWrapper = null;\r
132         };\r
133         \r
134         if( !X_NET_QUEUE_LIST.length ) return;\r
135 \r
136         queue = X_NET_QUEUE_LIST.shift();\r
137         \r
138         switch( queue.type ){\r
139                 case X_NET_TYPE_XHR :\r
140                         X_NET_currentWrapper = X_NET_XHRWrapper;\r
141                         break;\r
142                 case X_NET_TYPE_JSONP :\r
143                         X_NET_currentWrapper = X_NET_JSONPWrapper;\r
144                         break;\r
145                 case X_NET_TYPE_FORM :\r
146                         X_NET_currentWrapper = X_NET_FormWrapper;\r
147                         break;\r
148                 case X_NET_TYPE_IMAGE :\r
149                         X_NET_currentWrapper = X_NET_ImageWrapper;\r
150                         break;\r
151         };\r
152         \r
153         X_NET_currentWrapper.listen( [X.Event.PROGRESS, X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_currentQueue = queue, X_NET_proxyDispatch );\r
154         \r
155         X_NET_currentWrapper.load( queue.data );\r
156 };\r
157 \r