-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathlocal_storage.js
More file actions
437 lines (429 loc) · 11.6 KB
/
local_storage.js
File metadata and controls
437 lines (429 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* @module Data
* @submodule LocalStorage
*
* This module defines the p5 methods for working with local storage
*/
function storage(p5, fn){
/**
* Stores a value in the web browser's local storage.
*
* Web browsers can save small amounts of data using the built-in
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" target="_blank">localStorage object</a>.
* Data stored in `localStorage` can be retrieved at any point, even after
* refreshing a page or restarting the browser. Data are stored as key-value
* pairs.
*
* `storeItem()` makes it easy to store values in `localStorage` and
* <a href="#/p5/getItem">getItem()</a> makes it easy to retrieve them.
*
* The first parameter, `key`, is the name of the value to be stored as a
* string.
*
* The second parameter, `value`, is the value to be stored. Values can have
* any type.
*
* Note: Sensitive data such as passwords or personal information shouldn't be
* stored in `localStorage`.
*
* @method storeItem
* @for p5
* @param {String} key name of the value.
* @param {String|Number|Boolean|Object|Array} value value to be stored.
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Store the player's name.
* storeItem('name', 'Feist');
*
* // Store the player's score.
* storeItem('score', 1234);
*
* describe('The text "Feist: 1234" written in black on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER, CENTER);
* textSize(14);
*
* // Retrieve the name.
* let name = getItem('name');
*
* // Retrieve the score.
* let score = getItem('score');
*
* // Display the score.
* text(`${name}: ${score}`, 50, 50);
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Create an object.
* let p = { x: 50, y: 50 };
*
* // Store the object.
* storeItem('position', p);
*
* describe('A white circle on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Retrieve the object.
* let p = getItem('position');
*
* // Draw the circle.
* circle(p.x, p.y, 30);
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Create a p5.Color object.
* let c = color('deeppink');
*
* // Store the object.
* storeItem('color', c);
*
* describe('A pink circle on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Retrieve the object.
* let c = getItem('color');
*
* // Style the circle.
* fill(c);
*
* // Draw the circle.
* circle(50, 50, 30);
* }
*/
fn.storeItem = function(key, value) {
if (typeof key !== 'string') {
p5._friendlyError(
`The argument that you passed to storeItem() - ${key} is not a string.`,
'storeItem'
);
}
if (key.endsWith('p5TypeID')) {
p5._friendlyError(
`The argument that you passed to storeItem() - ${key} must not end with 'p5TypeID'.`,
'storeItem'
);
}
if (typeof value === 'undefined') {
p5._friendlyError('You cannot store undefined variables using storeItem().', 'storeItem');
}
let type = typeof value;
switch (type) {
case 'number':
case 'boolean':
value = value.toString();
break;
case 'object':
if (value instanceof p5.Color) {
type = 'p5.Color';
value = value.toString();
} else if (value instanceof p5.Vector) {
type = 'p5.Vector';
const coord = value.values;
value = coord;
}
value = JSON.stringify(value);
break;
case 'string':
default:
break;
}
localStorage.setItem(key, value);
const typeKey = `${key}p5TypeID`;
localStorage.setItem(typeKey, type);
};
/**
* Returns a value in the web browser's local storage.
*
* Web browsers can save small amounts of data using the built-in
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" target="_blank">localStorage object</a>.
* Data stored in `localStorage` can be retrieved at any point, even after
* refreshing a page or restarting the browser. Data are stored as key-value
* pairs.
*
* <a href="#/p5/storeItem">storeItem()</a> makes it easy to store values in
* `localStorage` and `getItem()` makes it easy to retrieve them.
*
* The first parameter, `key`, is the name of the value to be stored as a
* string.
*
* The second parameter, `value`, is the value to be retrieved a string. For
* example, calling `getItem('size')` retrieves the value with the key `size`.
*
* Note: Sensitive data such as passwords or personal information shouldn't be
* stored in `localStorage`.
*
* @method getItem
* @for p5
* @param {String} key name of the value.
* @return {String|Number|Boolean|Object|Array} stored item.
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Store the player's name.
* storeItem('name', 'Feist');
*
* // Store the player's score.
* storeItem('score', 1234);
*
* describe('The text "Feist: 1234" written in black on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER, CENTER);
* textSize(14);
*
* // Retrieve the name.
* let name = getItem('name');
*
* // Retrieve the score.
* let score = getItem('score');
*
* // Display the score.
* text(`${name}: ${score}`, 50, 50);
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Create an object.
* let p = { x: 50, y: 50 };
*
* // Store the object.
* storeItem('position', p);
*
* describe('A white circle on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Retrieve the object.
* let p = getItem('position');
*
* // Draw the circle.
* circle(p.x, p.y, 30);
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Create a p5.Color object.
* let c = color('deeppink');
*
* // Store the object.
* storeItem('color', c);
*
* describe('A pink circle on a gray background.');
* }
*
* function draw() {
* background(200);
*
* // Retrieve the object.
* let c = getItem('color');
*
* // Style the circle.
* fill(c);
*
* // Draw the circle.
* circle(50, 50, 30);
* }
*/
fn.getItem = function(key) {
let value = localStorage.getItem(key);
const type = localStorage.getItem(`${key}p5TypeID`);
if (typeof type === 'undefined') {
p5._friendlyError(
`Unable to determine type of item stored under ${key}in local storage. Did you save the item with something other than setItem()?`,
'getItem'
);
} else if (value !== null) {
switch (type) {
case 'number':
value = parseFloat(value);
break;
case 'boolean':
value = value === 'true';
break;
case 'object':
value = JSON.parse(value);
break;
case 'p5.Color':
value = this.color(JSON.parse(value));
break;
case 'p5.Vector':
value = JSON.parse(value);
value = this.createVector(...value);
break;
case 'string':
default:
break;
}
}
return value;
};
/**
* Removes all items in the web browser's local storage.
*
* Web browsers can save small amounts of data using the built-in
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" target="_blank">localStorage object</a>.
* Data stored in `localStorage` can be retrieved at any point, even after
* refreshing a page or restarting the browser. Data are stored as key-value
* pairs. Calling `clearStorage()` removes all data from `localStorage`.
*
* Note: Sensitive data such as passwords or personal information shouldn't be
* stored in `localStorage`.
*
* @method clearStorage
* @for p5
*
* @example
* // Double-click to clear localStorage.
*
* function setup() {
* createCanvas(100, 100);
*
* // Store the player's name.
* storeItem('name', 'Feist');
*
* // Store the player's score.
* storeItem('score', 1234);
*
* describe(
* 'The text "Feist: 1234" written in black on a gray background. The text "null: null" appears when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER, CENTER);
* textSize(14);
*
* // Retrieve the name.
* let name = getItem('name');
*
* // Retrieve the score.
* let score = getItem('score');
*
* // Display the score.
* text(`${name}: ${score}`, 50, 50);
* }
*
* // Clear localStorage when the user double-clicks.
* function doubleClicked() {
* clearStorage();
* }
*/
fn.clearStorage = function () {
const keys = Object.keys(localStorage);
keys.forEach(key => {
if (key.endsWith('p5TypeID')) {
this.removeItem(key.replace('p5TypeID', ''));
}
});
};
/**
* Removes an item from the web browser's local storage.
*
* Web browsers can save small amounts of data using the built-in
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" target="_blank">localStorage object</a>.
* Data stored in `localStorage` can be retrieved at any point, even after
* refreshing a page or restarting the browser. Data are stored as key-value
* pairs.
*
* <a href="#/p5/storeItem">storeItem()</a> makes it easy to store values in
* `localStorage` and `removeItem()` makes it easy to delete them.
*
* The parameter, `key`, is the name of the value to remove as a string. For
* example, calling `removeItem('size')` removes the item with the key `size`.
*
* Note: Sensitive data such as passwords or personal information shouldn't be
* stored in `localStorage`.
*
* @method removeItem
* @param {String} key name of the value to remove.
* @for p5
*
* @example
* // Double-click to remove an item from localStorage.
*
* function setup() {
* createCanvas(100, 100);
*
* // Store the player's name.
* storeItem('name', 'Feist');
*
* // Store the player's score.
* storeItem('score', 1234);
*
* describe(
* 'The text "Feist: 1234" written in black on a gray background. The text "Feist: null" appears when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER, CENTER);
* textSize(14);
*
* // Retrieve the name.
* let name = getItem('name');
*
* // Retrieve the score.
* let score = getItem('score');
*
* // Display the score.
* text(`${name}: ${score}`, 50, 50);
* }
*
* // Remove the word from localStorage when the user double-clicks.
* function doubleClicked() {
* removeItem('score');
* }
*/
fn.removeItem = function(key) {
if (typeof key !== 'string') {
p5._friendlyError(
`The argument that you passed to removeItem() - ${key} is not a string.`,
'removeItem'
);
}
localStorage.removeItem(key);
localStorage.removeItem(`${key}p5TypeID`);
};
}
export default storage;
if(typeof p5 !== 'undefined'){
storage(p5, p5.prototype);
}