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