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 | */ |
Akron | 3b253d3 | 2018-07-15 10:16:06 +0200 | [diff] [blame] | 12 | create : function (name = 'korap') { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 13 | var obj = Object.create(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 14 | obj._name = name.toLowerCase(); |
| 15 | obj._hash = {}; |
| 16 | obj._parse(); |
| 17 | return obj; |
| 18 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 19 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 20 | /** |
| 21 | * Get a value based on a key. |
| 22 | * The value can be complex, as the value is stored as JSON. |
| 23 | */ |
| 24 | get : function (key) { |
| 25 | return this._hash[key.toLowerCase()]; |
| 26 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 27 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Set a value based on a key. |
| 30 | * The value can be complex, as the value is stored as JSON. |
| 31 | */ |
| 32 | set : function (key, value) { |
| 33 | this._hash[key] = value; |
| 34 | this._store(); |
| 35 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 36 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Clears the session by removing the cookie |
| 39 | */ |
| 40 | clear : function () { |
| 41 | document.cookie = this._name + '=; expires=-1'; |
| 42 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 43 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 44 | /* Store cookie */ |
| 45 | _store : function () { |
| 46 | /* |
| 47 | var date = new Date(); |
| 48 | date.setYear(date.getFullYear() + 1); |
| 49 | */ |
| 50 | document.cookie = |
| 51 | this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';'; |
| 52 | }, |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 53 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | /* Parse cookie */ |
| 55 | _parse : function () { |
| 56 | var c = document.cookie; |
| 57 | var part = document.cookie.split(';'); |
| 58 | for(var i = 0; i < part.length; i++) { |
| 59 | var pair = part[i].split('='); |
| 60 | var name = pair[0].trim().toLowerCase(); |
| 61 | if (name === this._name) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 62 | if (pair.length === 1 || pair[1].length === 0) |
| 63 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 64 | this._hash = JSON.parse(decodeURIComponent(pair[1])); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 65 | return; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 66 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | }; |
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 | }); |