Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Simple cookie based session library that stores |
| 3 | * values in JSON encoded cookies. |
| 4 | * |
| 5 | * @author Nils Diewald |
| 6 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 7 | define({ |
| 8 | /** |
| 9 | * Create a new session. |
| 10 | * Expects a name or defaults to 'korap' |
| 11 | */ |
| 12 | create : function (name) { |
| 13 | var obj = Object.create(this); |
| 14 | if (name === undefined) |
| 15 | name = 'korap'; |
| 16 | obj._name = name.toLowerCase(); |
| 17 | obj._hash = {}; |
| 18 | obj._parse(); |
| 19 | return obj; |
| 20 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 21 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 22 | /** |
| 23 | * Get a value based on a key. |
| 24 | * The value can be complex, as the value is stored as JSON. |
| 25 | */ |
| 26 | get : function (key) { |
| 27 | return this._hash[key.toLowerCase()]; |
| 28 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 29 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 30 | /** |
| 31 | * Set a value based on a key. |
| 32 | * The value can be complex, as the value is stored as JSON. |
| 33 | */ |
| 34 | set : function (key, value) { |
| 35 | this._hash[key] = value; |
| 36 | this._store(); |
| 37 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 38 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 39 | /** |
| 40 | * Clears the session by removing the cookie |
| 41 | */ |
| 42 | clear : function () { |
| 43 | document.cookie = this._name + '=; expires=-1'; |
| 44 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 45 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 46 | /* Store cookie */ |
| 47 | _store : function () { |
| 48 | /* |
| 49 | var date = new Date(); |
| 50 | date.setYear(date.getFullYear() + 1); |
| 51 | */ |
| 52 | document.cookie = |
| 53 | this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';'; |
| 54 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 55 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 56 | /* Parse cookie */ |
| 57 | _parse : function () { |
| 58 | var c = document.cookie; |
| 59 | var part = document.cookie.split(';'); |
| 60 | for(var i = 0; i < part.length; i++) { |
| 61 | var pair = part[i].split('='); |
| 62 | var name = pair[0].trim().toLowerCase(); |
| 63 | if (name === this._name) { |
| 64 | if (pair.length === 1 || pair[1].length === 0) |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 65 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 66 | this._hash = JSON.parse(decodeURIComponent(pair[1])); |
| 67 | return; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 68 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 69 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 70 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 71 | }); |