| 1 | efrain | 1 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 2 | //
 | 
        
           |  |  | 3 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 4 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 5 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 6 | // (at your option) any later version.
 | 
        
           |  |  | 7 | //
 | 
        
           |  |  | 8 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 11 | // GNU General Public License for more details.
 | 
        
           |  |  | 12 | //
 | 
        
           |  |  | 13 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 14 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | /**
 | 
        
           |  |  | 17 |  * Simple API for set/get to sessionstorage, with cacherev expiration.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * Session storage will only persist for as long as the browser window
 | 
        
           |  |  | 20 |  * stays open.
 | 
        
           |  |  | 21 |  *
 | 
        
           |  |  | 22 |  * See:
 | 
        
           |  |  | 23 |  * https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
 | 
        
           |  |  | 24 |  *
 | 
        
           |  |  | 25 |  * @module     core/sessionstorage
 | 
        
           |  |  | 26 |  * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
 | 
        
           |  |  | 27 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 28 |  */
 | 
        
           |  |  | 29 | define(['core/config', 'core/storagewrapper'], function(config, StorageWrapper) {
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 |     // Private functions and variables.
 | 
        
           |  |  | 32 |     /** @var {Object} StorageWrapper - Wraps browsers sessionStorage object */
 | 
        
           |  |  | 33 |     var storage = new StorageWrapper(window.sessionStorage);
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     return /** @alias module:core/sessionstorage */ {
 | 
        
           |  |  | 36 |         /**
 | 
        
           |  |  | 37 |          * Get a value from session storage. Remember - all values must be strings.
 | 
        
           |  |  | 38 |          *
 | 
        
           |  |  | 39 |          * @method get
 | 
        
           |  |  | 40 |          * @param {string} key The cache key to check.
 | 
        
           |  |  | 41 |          * @return {boolean|string} False if the value is not in the cache, or some other error - a string otherwise.
 | 
        
           |  |  | 42 |          */
 | 
        
           |  |  | 43 |         get: function(key) {
 | 
        
           |  |  | 44 |             return storage.get(key);
 | 
        
           |  |  | 45 |         },
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |         /**
 | 
        
           |  |  | 48 |          * Set a value to session storage. Remember - all values must be strings.
 | 
        
           |  |  | 49 |          *
 | 
        
           |  |  | 50 |          * @method set
 | 
        
           |  |  | 51 |          * @param {string} key The cache key to set.
 | 
        
           |  |  | 52 |          * @param {string} value The value to set.
 | 
        
           |  |  | 53 |          * @return {boolean} False if the value can't be saved in the cache, or some other error - true otherwise.
 | 
        
           |  |  | 54 |          */
 | 
        
           |  |  | 55 |         set: function(key, value) {
 | 
        
           |  |  | 56 |             return storage.set(key, value);
 | 
        
           |  |  | 57 |         }
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |     };
 | 
        
           |  |  | 60 | });
 |