initial commit
[namibia] / public / js / vendor / ape-source / Request / Request.js
1 APE.Request = new Class({
2         initialize: function(ape) {
3                 this.ape = ape;
4                 this.stack = new APE.Request.Stack(ape);
5                 this.cycledStack = new APE.Request.CycledStack(ape);
6                 this.chl = 1;
7                 this.callbackChl = new $H;
8
9                 //Fix presto bug (see send method)
10                 if (Browser.Engine.presto){
11                         this.requestVar = {
12                                 updated: false,
13                                 args: []
14                         };
15                         this.requestObserver.periodical(10, this);
16                 }
17         },
18
19         send: function(cmd, params, options, noWatch) {
20                 if (this.ape.requestDisabled) return;
21                 //Opera dirty fix
22                 if (Browser.Engine.presto && !noWatch) {
23                         this.requestVar.updated = true;
24                         this.requestVar.args.push([cmd, params, options]);
25                         return;
26                 }
27
28                 var opt = {};
29                 if (!options) options = {};
30
31                 opt.event = options.event || true;
32                 opt.requestCallback = options.requestCallback || null;
33                 opt.callback = options.callback;
34                 var ret = this.ape.transport.send(this.parseCmd(cmd, params, opt), opt);
35
36                 $clear(this.ape.pollerObserver);
37                 this.ape.pollerObserver = this.ape.poller.delay(this.ape.options.pollTime, this.ape);
38
39                 return ret;
40         },
41
42         parseCmd: function(cmd, params, options) {
43                 var queryString = '';
44                 var a = [];
45                 var o = {};
46                 if ($type(cmd) == 'array') {
47                         var tmp, evParams;
48                         for (var i = 0; i < cmd.length; i++) {
49                                 tmp = cmd[i];
50
51                                 o = {};
52                                 o.cmd = tmp.cmd;
53                                 o.chl = this.chl++;
54                                 if (!tmp.options) tmp.options = {};
55
56                                 tmp.params ? o.params = tmp.params : null;
57                                 evParams = $extend({}, o.params);
58
59
60                                 if (!$defined(tmp.options.sessid) || tmp.options.sessid !== false) o.sessid = this.ape.sessid;
61                                 a.push(o);
62
63                                 var ev = 'cmd_' + tmp.cmd.toLowerCase();
64
65                                 if (tmp.options.callback) this.callbackChl.set(o.chl, tmp.options.callback);
66                                 if (tmp.options.requestCallback) options.requestCallback = tmp.options.requestCallback;
67                                 if (options.event) {
68                                         //Request is on a pipe, fire the event on the pipe
69                                         if (o.params && o.params.pipe) {
70                                                 var pipe = this.ape.getPipe(o.params.pipe);
71                                                 if (pipe) evParams = [evParams, pipe];
72                                         }
73
74                                         this.ape.fireEvent('onCmd', [tmp.cmd, evParams]);
75
76                                         if (pipe) pipe.fireEvent(ev, evParams);
77
78                                         this.ape.fireEvent(ev, evParams);
79                                 }
80                         }
81                 } else {
82                         o.cmd = cmd;
83                         o.chl = this.chl++;
84
85                         params ? o.params = params : null;
86                         var evParams = $extend({}, params);
87
88
89                         if (!$defined(options.sessid) || options.sessid !== false) o.sessid = this.ape.sessid;
90                         a.push(o);
91                         
92                         var ev = 'cmd_' + cmd.toLowerCase();
93                         if (options.callback) this.callbackChl.set(o.chl, options.callback);
94
95                         if (options.event) {
96                                 //Request is on a pipe, fire the event on the pipe
97                                 if (params && params.pipe) { 
98                                         var pipe = this.ape.getPipe(params.pipe);
99                                         if (pipe) evParams = [evParams, pipe];
100                                 }
101                                 this.ape.fireEvent('onCmd', [cmd, evParams]);
102
103                                 if (pipe) pipe.fireEvent(ev, evParams);
104
105                                 this.ape.fireEvent(ev, evParams);
106                         }
107                 }
108
109                 var transport = this.ape.options.transport;
110                 return JSON.stringify(a, function(key, value) {
111                                 if (typeof(value) == 'string') {
112                                         value = encodeURIComponent(value);
113                                         //In case of JSONP data have to be escaped two times
114                                         if (transport == 2) value = encodeURIComponent(value);
115                                         return value;
116                                 } else return value;
117                         });
118         },
119
120         /****
121          * This method is only used by opera.
122          * Opera have a bug, when request are sent trought user action (ex : a click), opera throw a security violation when trying to make a XHR.
123          * The only way is to set a class var and watch when this var change
124          */
125         requestObserver: function(){
126                 if (this.requestVar.updated) {
127                         var args = this.requestVar.args.shift();
128                         this.requestVar.updated = (this.requestVar.args.length>0) ? true : false;
129                         args[3] = true; //Set noWatch argument to true
130                         this.send.run(args, this);
131                 }
132         }
133 });