storage.js.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: storage.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: storage.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * Helper object for interfacing with the LocalStorage. The LocalStorage
  21. * browser API allows for offline storage. That API is very unsophisticated,
  22. * and is essentially a lame key-value store. This object sits on top
  23. * and provides a number of useful utilities, including rudimentarycache
  24. * cache expiration.
  25. *
  26. * @constructor
  27. * @this {LocalStorageWrapper}
  28. * @param {String} namespace - A namespace to use in grouping access to localstorage. This keeps access clean and organized, while also making it possible to have multiple LocalStorage connections.
  29. */
  30. function LocalStorageWrapper(namespace) {
  31. this.namespace = namespace;
  32. }
  33. /**
  34. * A method for adding a key/value pair to LocalStorage.
  35. * Note that both parameters must be strings (JSON.stringify is your friend).
  36. *
  37. * @param {String} key - The name of the key.
  38. * @param {String} value - The value.
  39. */
  40. LocalStorageWrapper.prototype.set = function(key, value) {
  41. localStorage.setItem(namespace+"_"+key+"_value", value);
  42. localStorage.setItem(namespace+"_"+key+"_timestamp", $.now());
  43. };
  44. /**
  45. * A method for removing a key from LocalStorage.
  46. *
  47. * @param {String} key - The name of the key to remove.
  48. */
  49. LocalStorageWrapper.prototype.remove = function(key) {
  50. localStorage.removeItem(namespace+"_"+key+"_value");
  51. localStorage.removeItem(namespace+"_"+key+"_timestamp");
  52. };
  53. /**
  54. * A method for retrieving the value associated with the given key.
  55. *
  56. * @param {String} key - The name of the key to retrieve the value for.
  57. */
  58. LocalStorageWrapper.prototype.get = function(key) {
  59. return localStorage.getItem(namespace+"_"+key+"_value");
  60. };
  61. /**
  62. * A test for whether the given key is in LocalStorage.
  63. *
  64. * @param {String} key - The key to test existence for.
  65. */
  66. LocalStorageWrapper.prototype.has = function(key) {
  67. return localStorage.getItem(namespace+"_"+key+"_value") !== null;
  68. };
  69. /**
  70. * A test for whether the server has the newer version. This function
  71. * assumes that the server trip takes about 5 seconds. This method
  72. * is largely deprecated.
  73. *
  74. * @param {String} key - The key to check.
  75. * @param {Integer} server_time - The server's time as an epoch (in milliseconds)
  76. */
  77. LocalStorageWrapper.prototype.is_new = function(key, server_time) {
  78. var stored_time = localStorage.getItem(namespace+"_"+key+"_timestamp");
  79. return (server_time >= stored_time+5000);
  80. };</code></pre>
  81. </article>
  82. </section>
  83. </div>
  84. <nav>
  85. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BlockPy.html">BlockPy</a></li><li><a href="BlockPyCorgis.html">BlockPyCorgis</a></li><li><a href="BlockPyDialog.html">BlockPyDialog</a></li><li><a href="BlockPyEditor.html">BlockPyEditor</a></li><li><a href="BlockPyEngine.html">BlockPyEngine</a></li><li><a href="BlockPyEnglish.html">BlockPyEnglish</a></li><li><a href="BlockPyFeedback.html">BlockPyFeedback</a></li><li><a href="BlockPyHistory.html">BlockPyHistory</a></li><li><a href="BlockPyPresentation.html">BlockPyPresentation</a></li><li><a href="BlockPyPrinter.html">BlockPyPrinter</a></li><li><a href="BlockPyServer.html">BlockPyServer</a></li><li><a href="BlockPyToolbar.html">BlockPyToolbar</a></li><li><a href="LocalStorageWrapper.html">LocalStorageWrapper</a></li><li><a href="PythonToBlocks.html">PythonToBlocks</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BlockPyInterface">BlockPyInterface</a></li><li><a href="global.html#cloneNode">cloneNode</a></li><li><a href="global.html#encodeHTML">encodeHTML</a></li><li><a href="global.html#expandArray">expandArray</a></li><li><a href="global.html#EXTENDED_ERROR_EXPLANATION">EXTENDED_ERROR_EXPLANATION</a></li><li><a href="global.html#indent">indent</a></li><li><a href="global.html#instructor_module">instructor_module</a></li><li><a href="global.html#prettyPrintDateTime">prettyPrintDateTime</a></li><li><a href="global.html#randomInteger">randomInteger</a></li><li><a href="global.html#set_button_loaded">set_button_loaded</a></li><li><a href="global.html#timerGuard">timerGuard</a></li></ul>
  86. </nav>
  87. <br class="clear">
  88. <footer>
  89. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Sun Mar 26 2017 09:45:03 GMT-0400 (Eastern Daylight Time)
  90. </footer>
  91. <script> prettyPrint(); </script>
  92. <script src="scripts/linenumber.js"> </script>
  93. </body>
  94. </html>