5 initialize: function(options){
6 if (this.getInstance(options.identifier).instance) options.restore = true;
10 //Init and save cookies
11 if (options.restore) this.init();
13 this.addEvent('uniPipeCreate', this.saveSessionPipe);
14 this.addEvent('uniPipeDelete', this.saveSessionPipe);
17 saveSessionPipe:function(){
19 this.pipes.each(function(pipe) {
20 if (pipe.type == 'uni') {
21 uniPipe.push({'casttype':pipe.type, 'pubid':pipe.pipe.pubid, 'properties':pipe.properties});
25 this.setSession({'uniPipe': JSON.stringify(uniPipe)});
28 restoreUniPipe: function(resp){
29 var pipes = JSON.parse(decodeURIComponent(resp.data.sessions.uniPipe));
31 for (var i = 0; i < pipes.length; i++){
32 this.newPipe('uni',{'pipe': pipes[i]});
35 this.fireEvent('restoreEnd');
36 this.restoring = false;
41 this.createCookie();//Create cookie if needed
42 this.saveCookie();//Save cookie
45 restoreCallback: function(resp){
46 if (resp.raw!='ERR' && this.status == 0) {
47 this.fireEvent('init');
48 this.fireEvent('ready');
50 } else if (this.status == 0) {
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);
65 this.restoring = true;
66 this.fireEvent('restoreStart');
68 this.getSession('uniPipe', this.restoreUniPipe.bind(this), options);
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
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};
91 //No instance found, just return the cookie
95 removeInstance: function(identifier){
96 if (!this.cookie) return;
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);
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
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;
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;
131 * Create a cookie instance (add to the instance array of the cookie the current application)
132 * @param object APE_Cookie
134 createInstance: function(cookie) {
135 cookie.instance.push({
136 identifier: this.options.identifier,
137 pubid: this.getPubid(),
138 sessid: this.getSessid()
143 * Create ape cookie if needed (but do not write it)
145 createCookie: function(){
147 //No Cookie or no ape instance in cookie, lets create the cookie
152 this.createInstance(tmp);
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});
162 clearSession: function(){
164 this.removeInstance(this.options.identifier);
168 removeCookie: function(){
169 Cookie.dispose('APE_Cookie', {domain:this.options.domain});