initial commit
[namibia] / public / ape-source / Core / Session.js
1 APE.Core = new Class({
2
3         Extends: APE.Core,
4
5         initialize: function(options){
6                 if (this.getInstance(options.identifier).instance) options.restore = true;
7
8                 this.parent(options);
9
10                 //Init and save cookies
11                 if (options.restore) this.init();
12
13                 this.addEvent('uniPipeCreate', this.saveSessionPipe);
14                 this.addEvent('uniPipeDelete', this.saveSessionPipe);
15         },
16         
17         saveSessionPipe:function(){
18                 var uniPipe = [];
19                 this.pipes.each(function(pipe) {
20                                 if (pipe.type == 'uni') {
21                                         uniPipe.push({'casttype':pipe.type, 'pubid':pipe.pipe.pubid, 'properties':pipe.properties});
22                                 }
23                 });
24
25                 this.setSession({'uniPipe': JSON.stringify(uniPipe)});
26         },
27
28         restoreUniPipe: function(resp){
29                 var pipes = JSON.parse(decodeURIComponent(resp.data.sessions.uniPipe));
30                 if (pipes) {
31                         for (var i = 0; i < pipes.length; i++){
32                                 this.newPipe('uni',{'pipe': pipes[i]});
33                         }
34                 }
35                 this.fireEvent('restoreEnd');
36                 this.restoring = false;
37         },
38
39         init: function(){
40                 this.initCookie();
41                 this.createCookie();//Create cookie if needed
42                 this.saveCookie();//Save cookie
43         },
44
45         restoreCallback: function(resp){
46                 if (resp.raw!='ERR' && this.status == 0) { 
47                         this.fireEvent('init');
48                         this.fireEvent('ready');
49                         this.status = 1;
50                 } else if (this.status == 0) {
51                         this.stopPoller();
52                 }
53         },
54
55         connect: function(args, options){
56                 var cookie = this.initCookie();
57                 if (!cookie) {//No cookie defined start a new connection
58                         this.addEvent('init',this.init);
59                         this.parent(args, options);
60                 } else {//Cookie or instance exist
61                         if (!options) options = {};
62                         if (!options.request) options.request = 'stack';
63                         options.requestCallback = this.restoreCallback.bind(this);
64
65                         this.restoring = true;
66                         this.fireEvent('restoreStart');
67                         this.startPoller();
68                         this.getSession('uniPipe', this.restoreUniPipe.bind(this), options);
69                 }
70         },
71
72         /***
73          * Read the cookie APE_Cookie and try to find the application identifier
74          * @param       String  identifier, can be used to force the identifier to find ortherwhise identifier defined in the options will be used
75          * @return      Boolean false if application identifier isn't found or an object with the instance and the cookie
76          */
77         getInstance: function(identifier) {
78                 var     tmp = Cookie.read('APE_Cookie', {'domain': document.domain});
79                 identifier = identifier || this.options.identifier;
80                 if (!tmp) return false;
81                 tmp = JSON.parse(tmp);
82                 //Cookie is corrupted or doest not contains instance
83                 if (!tmp || !tmp.instance) return false;
84                 //Get the instance of ape in cookie
85                 for(var i = 0; i < tmp.instance.length; i++){
86                         if(tmp.instance[i] && tmp.instance[i].identifier == identifier){
87                                 return {instance: tmp.instance[i], cookie: tmp};
88                         }
89                 }
90                 
91                 //No instance found, just return the cookie
92                 return {cookie: tmp};
93         },
94         
95         removeInstance: function(identifier){
96                 if (!this.cookie) return;
97
98                 for(var i = 0; i < this.cookie.instance.length; i++){
99                         if(this.cookie.instance[i].identifier == identifier){
100                                 this.cookie.instance.splice(i,1);
101                                 return;
102                         }
103                 }
104         },
105
106         /***
107          * Initialize cookie and some application variable is instance is found
108          * set this.cookie variable
109          * @return      boolean true if instance is found, else false
110          */
111         initCookie: function(){
112                 var tmp = this.getInstance();
113                 if(tmp && tmp.instance){ //Cookie exist, application instance exist
114                         this.sessid = tmp.instance.sessid;
115                         this.pubid = tmp.instance.pubid;
116                         tmp.cookie.frequency = tmp.cookie.frequency.toInt() + 1;
117                         this.cookie = tmp.cookie;
118                         return true;
119                 } else if (tmp.cookie) { //Cookie exist, no application instance
120                         this.createInstance(tmp.cookie);
121                         tmp.cookie.frequency = tmp.cookie.frequency.toInt() + 1;
122                         this.cookie = tmp.cookie;
123                         return false;
124                 } else { //No cookie
125                         this.cookie = null;
126                         return false;
127                 }
128         },
129
130         /***
131          * Create a cookie instance (add to the instance array of the cookie the current application)
132          * @param       object  APE_Cookie
133          */
134         createInstance: function(cookie) {
135                 cookie.instance.push({
136                         identifier: this.options.identifier,
137                         pubid: this.getPubid(),
138                         sessid: this.getSessid()
139                 });
140         },
141
142         /***
143          * Create ape cookie if needed (but do not write it)
144          */
145         createCookie: function(){
146                 if(!this.cookie){
147                         //No Cookie or no ape instance in cookie, lets create the cookie
148                         var tmp = {
149                                 frequency: 1,
150                                 instance: []
151                         };
152                         this.createInstance(tmp);
153                         this.cookie = tmp;
154                 }
155         },
156
157         saveCookie: function(){
158                 //Save cookie on the parent window (this is usefull with JSONP as domain in the iframe is different than the domain in the parent window)
159                 Cookie.write('APE_Cookie', JSON.stringify(this.cookie), {'domain': document.domain});
160         },
161
162         clearSession: function(){
163                 this.parent();
164                 this.removeInstance(this.options.identifier);
165                 this.saveCookie();
166         },
167
168         removeCookie: function(){
169                 Cookie.dispose('APE_Cookie', {domain:this.options.domain});
170         }
171 });