]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/datastore.js
Merge branch '3.6-rc'
[friendica-addons.git] / jappixmini / jappix / js / datastore.js
1 /*
2
3 Jappix - An open social platform
4 These are the temporary/persistent data store functions
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 23/06/11
11
12 */
13
14 // Temporary: returns whether it is available or not
15 function hasDB() {
16         if(window.sessionStorage)
17                 return true;
18         
19         return false;
20 }
21
22 // Temporary: used to read a database entry
23 function getDB(type, id) {
24         try {
25                 return sessionStorage.getItem(type + '_' + id);
26         }
27         
28         catch(e) {
29                 logThis('Error while getting a temporary database entry (' + type + ' -> ' + id + '): ' + e, 1);
30                 
31                 return null;
32         }
33 }
34
35 // Temporary: used to update a database entry
36 function setDB(type, id, value) {
37         try {
38                 sessionStorage.setItem(type + '_' + id, value);
39                 
40                 return true;
41         }
42         
43         catch(e) {
44                 logThis('Error while writing a temporary database entry (' + type + ' -> ' + id + '): ' + e, 1);
45                 
46                 return false;
47         }
48 }
49
50 // Temporary: used to remove a database entry
51 function removeDB(type, id) {
52         try {
53                 sessionStorage.removeItem(type + '_' + id);
54                 
55                 return true;
56         }
57         
58         catch(e) {
59                 logThis('Error while removing a temporary database entry (' + type + ' -> ' + id + '): ' + e, 1);
60                 
61                 return false;
62         }
63 }
64
65 // Temporary: used to check a database entry exists
66 function existDB(type, id) {
67         var read = getDB(type, id);
68         
69         if(read != null)
70                 return true;
71         
72         return false;
73 }
74
75 // Temporary: used to clear all the database
76 function resetDB() {
77         try {
78                 sessionStorage.clear();
79                 
80                 logThis('Temporary database cleared.', 3);
81                 
82                 return true;
83         }
84         
85         catch(e) {
86                 logThis('Error while clearing temporary database: ' + e, 1);
87                 
88                 return false;
89         }
90 }
91
92 // Persistent: returns whether it is available or not
93 function hasPersistent() {
94         if(window.localStorage)
95                 return true;
96         
97         return false;
98 }
99
100 // Persistent: used to read a database entry
101 function getPersistent(type, id) {
102         try {
103                 return localStorage.getItem(type + '_' + id);
104         }
105         
106         catch(e) {
107                 logThis('Error while getting a persistent database entry (' + type + ' -> ' + id + '): ' + e, 1);
108                 
109                 return null;
110         }
111 }
112
113 // Persistent: used to update a database entry
114 function setPersistent(type, id, value) {
115         try {
116                 localStorage.setItem(type + '_' + id, value);
117                 
118                 return true;
119         }
120         
121         // Database might be full
122         catch(e) {
123                 logThis('Retrying: could not write a persistent database entry (' + type + ' -> ' + id + '): ' + e, 2);
124                 
125                 // Flush it!
126                 flushPersistent();
127                 
128                 // Set the item again
129                 try {
130                         localStorage.setItem(type + '_' + id, value);
131                         
132                         return true;
133                 }
134                 
135                 // New error!
136                 catch(e) {
137                         logThis('Aborted: error while writing a persistent database entry (' + type + ' -> ' + id + '): ' + e, 1);
138                         
139                         return false;
140                 }
141         }
142 }
143
144 // Persistent: used to remove a database entry
145 function removePersistent(type, id) {
146         try {
147                 localStorage.removeItem(type + '_' + id);
148                 
149                 return true;
150         }
151         
152         catch(e) {
153                 logThis('Error while removing a persistent database entry (' + type + ' -> ' + id + '): ' + e, 1);
154                 
155                 return false;
156         }
157 }
158
159 // Persistent: used to check a database entry exists
160 function existPersistent(type, id) {
161         var read = getPersistent(type, id);
162         
163         if(read != null)
164                 return true;
165         
166         return false;
167 }
168
169 // Persistent: used to clear all the database
170 function resetPersistent() {
171         try {
172                 localStorage.clear();
173                 
174                 logThis('Persistent database cleared.', 3);
175                 
176                 return true;
177         }
178         
179         catch(e) {
180                 logThis('Error while clearing persistent database: ' + e, 1);
181                 
182                 return false;
183         }
184 }
185
186 // Persistent: used to flush the database
187 function flushPersistent() {
188         try {
189                 // Get the stored session entry
190                 var session = getPersistent('session', 1);
191                 
192                 // Clear the persistent database
193                 localStorage.clear();
194                 
195                 // Restaure the stored session entry
196                 if(session)
197                         setPersistent('session', 1, session);
198                 
199                 logThis('Persistent database flushed.', 3);
200                 
201                 return true;
202         }
203         
204         catch(e) {
205                 logThis('Error while flushing persistent database: ' + e, 1);
206                 
207                 return false;
208         }
209 }