[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/amd/src/ -> localstorage.js (source)

   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 localstorage, with cacherev expiration.
  18   *
  19   * @module     core/localstorage
  20   * @package    core
  21   * @class      localstorage
  22   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      2.9
  25   */
  26  define(['core/config'], function(config) {
  27  
  28      // Private functions and variables.
  29      /** @var {boolean} supported - Is localstorage supported in this browser? */
  30      var supported = false;
  31      /** @var {string} prefix - Prefix to use on all cache keys */
  32      var prefix = '';
  33      /** @var {jsrevPrefix} jsrevPrefix - Key to store the current jsrev version for the cache */
  34      var jsrevPrefix = '';
  35      /** @var {Object} localStorage - Browsers localStorage object */
  36      var localStorage = null;
  37  
  38      /**
  39       * Check if the browser supports local storage.
  40       *
  41       * @method detectSupport
  42       * @return {boolean} True if the browser supports local storage.
  43       */
  44      var detectSupport = function() {
  45          if (config.jsrev == -1) {
  46              // Disable cache if debugging.
  47              return false;
  48          }
  49          if (typeof (window.localStorage) === "undefined") {
  50              return false;
  51          }
  52          var testKey = 'test';
  53          try {
  54              localStorage = window.localStorage;
  55              if (localStorage === null) {
  56                  return false;
  57              }
  58              // MDL-51461 - Some browsers misreport availability of local storage
  59              // so check it is actually usable.
  60              localStorage.setItem(testKey, '1');
  61              localStorage.removeItem(testKey);
  62              return true;
  63          } catch (ex) {
  64              return false;
  65          }
  66      };
  67  
  68      /**
  69       * Add a unique prefix to all keys so multiple moodle sites do not share caches.
  70       *
  71       * @method prefixKey
  72       * @param {string} key The cache key to prefix.
  73       * @return {string} The new key
  74       */
  75      var prefixKey = function(key) {
  76          return prefix + key;
  77      };
  78  
  79      /**
  80       * Check the current jsrev version and clear the cache if it has been bumped.
  81       *
  82       * @method validateCache
  83       */
  84      var validateCache = function() {
  85          var cacheVersion = localStorage.getItem(jsrevPrefix);
  86          if (cacheVersion === null) {
  87              localStorage.setItem(jsrevPrefix, config.jsrev);
  88              return;
  89          }
  90          var moodleVersion = config.jsrev;
  91  
  92          if (moodleVersion != cacheVersion) {
  93              localStorage.clear();
  94              localStorage.setItem(jsrevPrefix, config.jsrev);
  95          }
  96      };
  97  
  98      /**
  99       * Hash a string, used to make shorter key prefixes.
 100       *
 101       * @method hashString
 102       * @param {String} source The string to hash
 103       * @return {Number}
 104       */
 105      var hashString = function(source) {
 106          // From http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery.
 107          /* jshint bitwise: false */
 108          /* eslint no-bitwise: "off" */
 109          var hash = 0;
 110          var i, chr, len;
 111          if (source.length === 0) {
 112              return hash;
 113          }
 114          for (i = 0, len = source.length; i < len; i++) {
 115              chr = source.charCodeAt(i);
 116              hash = ((hash << 5) - hash) + chr;
 117              hash |= 0; // Convert to 32bit integer
 118          }
 119          return hash;
 120      };
 121  
 122      /**
 123       * Init this module.
 124       *
 125       * This computes the hash prefixes from jsrev and friends.
 126       */
 127      var init = function() {
 128          supported = detectSupport();
 129          var hashSource = config.wwwroot + '/' + config.jsrev;
 130  
 131          var hash = hashString(hashSource);
 132          prefix = hash + '/';
 133          hashSource = config.wwwroot + '/';
 134          hash = hashString(hashSource);
 135          jsrevPrefix = hash + '/jsrev';
 136      };
 137  
 138      // Run the module init.
 139      init();
 140  
 141      return /** @alias module:core/localstorage */ {
 142          /**
 143           * Get a value from local storage. Remember - all values must be strings.
 144           *
 145           * @method get
 146           * @param {string} key The cache key to check.
 147           * @return {boolean|string} False if the value is not in the cache, or some other error - a string otherwise.
 148           */
 149          get: function(key) {
 150              if (!supported) {
 151                  return false;
 152              }
 153              validateCache();
 154              key = prefixKey(key);
 155  
 156              return localStorage.getItem(key);
 157          },
 158  
 159          /**
 160           * Set a value to local storage. Remember - all values must be strings.
 161           *
 162           * @method set
 163           * @param {string} key The cache key to set.
 164           * @param {string} value The value to set.
 165           * @return {boolean} False if the value can't be saved in the cache, or some other error - true otherwise.
 166           */
 167          set: function(key, value) {
 168              if (!supported) {
 169                  return false;
 170              }
 171              validateCache();
 172              key = prefixKey(key);
 173              // This can throw exceptions when the storage limit is reached.
 174              try {
 175                  localStorage.setItem(key, value);
 176              } catch (e) {
 177                  return false;
 178              }
 179              return true;
 180          }
 181  
 182      };
 183  });


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1