1 // TODO store a wrapper object with doc title, modified date and document as string in localstorage. 2 // in open document window show wrapper object and only parse document on demand. 3 // when many large documents are stored in LS, opening of window takes a rather long time 4 mindmaps.LocalStorage = (function() { 5 return { 6 put : function(key, value) { 7 localStorage.setItem(key, value); 8 }, 9 get : function(key) { 10 return localStorage.getItem(key); 11 }, 12 clear : function() { 13 localStorage.clear(); 14 } 15 }; 16 })(); 17 18 /** 19 * @namespace 20 */ 21 mindmaps.LocalDocumentStorage = (function() { 22 var prefix = "mindmaps.document."; 23 24 var getDocumentByKey = function(key) { 25 var json = localStorage.getItem(key); 26 if (json === null) { 27 return null; 28 } 29 30 /** 31 * Catch any SytaxErrors when document can't be parsed. 32 */ 33 try { 34 return mindmaps.Document.fromJSON(json); 35 } catch (error) { 36 console.error("Error while loading document from local storage", 37 error); 38 return null; 39 } 40 }; 41 42 /** 43 * Public API 44 * @scope mindmaps.LocalDocumentStorage 45 */ 46 return { 47 /** 48 * Saves a document to the localstorage. Overwrites the old document if 49 * one with the same id exists. 50 * 51 * @param {mindmaps.Document} doc 52 * 53 * @returns {Boolean} true if save was successful, false otherwise. 54 */ 55 saveDocument : function(doc) { 56 try { 57 localStorage.setItem(prefix + doc.id, doc.serialize()); 58 return true; 59 } catch (error) { 60 // QUOTA_EXCEEDED 61 console.error("Error while saving document to local storage", 62 error); 63 return false; 64 } 65 }, 66 67 /** 68 * Loads a document from the local storage. 69 * 70 * @param {String} docId 71 * 72 * @returns {mindmaps.Document} the document or null if not found. 73 */ 74 loadDocument : function(docId) { 75 return getDocumentByKey(prefix + docId); 76 }, 77 78 /** 79 * Finds all documents in the local storage object. 80 * 81 * @returns {Array} an Array of documents 82 */ 83 getDocuments : function() { 84 var documents = []; 85 // search localstorage for saved documents 86 for ( var i = 0, max = localStorage.length; i < max; i++) { 87 var key = localStorage.key(i); 88 // value is a document if key confirms to prefix 89 if (key.indexOf(prefix) == 0) { 90 var doc = getDocumentByKey(key); 91 if (doc) { 92 documents.push(doc); 93 } 94 } 95 } 96 return documents; 97 }, 98 99 /** 100 * Gets all document ids found in the local storage object. 101 * 102 * @returns {Array} an Array of document ids 103 */ 104 getDocumentIds : function() { 105 var ids = []; 106 // search localstorage for saved documents 107 for ( var i = 0, max = localStorage.length; i < max; i++) { 108 var key = localStorage.key(i); 109 // value is a document if key confirms to prefix 110 if (key.indexOf(prefix) == 0) { 111 ids.push(key.substring(prefix.length)); 112 } 113 } 114 return ids; 115 }, 116 117 /** 118 * Deletes a document from the local storage. 119 * 120 * @param {mindmaps.Document} doc 121 */ 122 deleteDocument : function(doc) { 123 localStorage.removeItem(prefix + doc.id); 124 }, 125 126 /** 127 * Deletes all documents from the local storage. 128 */ 129 deleteAllDocuments : function() { 130 this.getDocuments().forEach(this.deleteDocument); 131 } 132 }; 133 })(); 134