OSDN Git Service

9ed0aea14237f0c8a148716811cbba063910f066
[pettanr/clientJs.git] / 0.6.x / js / 04_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         amountQueue : function(){\r
32                 return X_NET_QUEUE_LIST.length;\r
33         }\r
34 \r
35 };\r
36 \r
37 \r
38 var X_NET_IWrapper = function(){};\r
39         X_NET_IWrapper.prototype.load   = function(){};\r
40         X_NET_IWrapper.prototype.cancel = function(){};\r
41         X_NET_IWrapper.prototype.reset  = function(){};\r
42 \r
43 \r
44 var X_NET_TYPE_XHR   = 1,\r
45         X_NET_TYPE_JSONP = 2,\r
46         X_NET_TYPE_FORM  = 3,\r
47         X_NET_TYPE_IMAGE = 4,\r
48 \r
49         X_NET_QUEUE_LIST = [],\r
50 \r
51         X_NET_XHRWrapper,\r
52         X_NET_JSONPWrapper,\r
53         X_NET_FormWrapper,\r
54         X_NET_ImageWrapper,\r
55 \r
56         X_NET_currentWrapper,\r
57         X_NET_currentQueue,\r
58 \r
59         X_NET_Queue = X.EventDispatcher.inherits(\r
60                 'XNetQueue',\r
61                 X.Class.POOL_OBJECT,\r
62                 {\r
63                         type : 0,\r
64                         data : null,\r
65                         \r
66                         Constructor : function( type, data ){\r
67                                 this.type = type;\r
68                                 this.data = data;                               \r
69                                 \r
70                                 this.listen( X.Event.COMPLETE, X_NET_proxyDispatch );\r
71                                 \r
72                                 X_NET_QUEUE_LIST[ X_NET_QUEUE_LIST.length ] = this;\r
73                                 !X_NET_currentQueue && X_NET_shiftQueue();\r
74                         },\r
75                         \r
76                         busy : function(){\r
77                                 return this === X_NET_currentQueue && X_NET_currentWrapper._busy;\r
78                         },\r
79                         \r
80                         cancel : function(){\r
81                                 var i = X_NET_QUEUE_LIST.indexOf( this );\r
82                                 if( i !== -1 ){\r
83                                         X_NET_QUEUE_LIST.splice( i, 1 );\r
84                                         this.asyncDispatch( 0, { type : X.Event.CANCELED } );\r
85                                 } else\r
86                                 if( this === X_NET_currentQueue ){\r
87                                         X_NET_currentWrapper.cancel();\r
88                                 };\r
89                         }\r
90                 }\r
91         );\r
92 \r
93 function X_NET_proxyDispatch( e ){\r
94         switch( e.type ){\r
95                 case X.Event.BEFORE_KILL_INSTANCE :\r
96                         break;\r
97                 case X.Event.KILL_INSTANCE :\r
98                         break;                  \r
99                 case X.Event.KILL_INSTANCE_CANCELED :\r
100                         break;\r
101                 case X.Event.PROGRESS :\r
102                         console.log( 'q: ' + e.type );\r
103                         this.dispatch( e );\r
104                         break;\r
105                 case X.Event.SUCCESS :\r
106                 case X.Event.ERROR :\r
107                 case X.Event.TIMEOUT :\r
108                 case X.Event.CANCELED :\r
109                         console.log( 'q: ' + e.type );\r
110                         this.dispatch( e );\r
111                         this.asyncDispatch( 0, { type : X.Event.COMPLETE } );\r
112                         break;\r
113                 case X.Event.COMPLETE :\r
114                         console.log( 'complete. then kill()' );\r
115                         this.kill();\r
116                         X_NET_shiftQueue();\r
117                         break;          \r
118         };\r
119 };\r
120 \r
121 function X_NET_shiftQueue(){\r
122         var queue;\r
123         \r
124         if( X_NET_currentQueue ){\r
125                 if( X_NET_currentWrapper._busy ) return;\r
126                 X_NET_currentWrapper\r
127                         .unlisten( [ X.Event.PROGRESS, X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_currentQueue, X_NET_proxyDispatch )\r
128                         .reset();\r
129                 X_NET_currentQueue = X_NET_currentWrapper = null;\r
130         };\r
131         \r
132         if( !X_NET_QUEUE_LIST.length ) return;\r
133 \r
134         queue = X_NET_QUEUE_LIST.shift();\r
135         \r
136         switch( queue.type ){\r
137                 case X_NET_TYPE_XHR :\r
138                         X_NET_currentWrapper = X_NET_XHRWrapper;\r
139                         break;\r
140                 case X_NET_TYPE_JSONP :\r
141                         X_NET_currentWrapper = X_NET_JSONPWrapper;\r
142                         break;\r
143                 case X_NET_TYPE_FORM :\r
144                         X_NET_currentWrapper = X_NET_FormWrapper;\r
145                         break;\r
146                 case X_NET_TYPE_IMAGE :\r
147                         X_NET_currentWrapper = X_NET_ImageWrapper;\r
148                         break;\r
149         };\r
150         \r
151         X_NET_currentWrapper.listen( [X.Event.PROGRESS, X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_currentQueue = queue, X_NET_proxyDispatch );\r
152         \r
153         X_NET_currentWrapper.load( queue.data );\r
154 };\r
155 \r