[BUGFIX] Check if client supports localStorage
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Private / TypeScript / Storage / Client.ts
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Module: TYPO3/CMS/Backend/Storage/Client
16 * Wrapper for localStorage
17 * @exports TYPO3/CMS/Backend/Storage/Client
18 */
19 class Client {
20 private keyPrefix: string = 't3-';
21
22 /**
23 * @returns {boolean}
24 */
25 private static isCapable(): boolean {
26 return localStorage !== null;
27 }
28
29 /**
30 * Simple localStorage wrapper, to get value from localStorage
31 * @param {string} key
32 * @returns {string}
33 */
34 public get = (key: string): string => {
35 return Client.isCapable() ? localStorage.getItem(this.keyPrefix + key) : null;
36 }
37
38 /**
39 * Simple localStorage wrapper, to set value from localStorage
40 *
41 * @param {string} key
42 * @param {string} value
43 * @returns {string}
44 */
45 public set = (key: string, value: string): void => {
46 if (Client.isCapable()) {
47 localStorage.setItem(this.keyPrefix + key, value);
48 }
49 }
50
51 /**
52 * Simple localStorage wrapper, to unset value from localStorage
53 *
54 * @param {string} key
55 */
56 public unset = (key: string): void => {
57 if (Client.isCapable()) {
58 localStorage.removeItem(this.keyPrefix + key);
59 }
60 }
61
62 /**
63 * Removes values from localStorage by a specific prefix of the key
64 *
65 * @param {string} prefix
66 */
67 public unsetByPrefix = (prefix: string): void => {
68 if (!Client.isCapable()) {
69 return;
70 }
71
72 prefix = this.keyPrefix + prefix;
73
74 const keysToDelete: Array<string> = [];
75 for (let i = 0; i < localStorage.length; ++i) {
76 if (localStorage.key(i).substring(0, prefix.length) === prefix) {
77 // Remove the global key prefix, as it gets prepended in unset again
78 const key = localStorage.key(i).substr(this.keyPrefix.length);
79
80 // We can't delete the key here as this interferes with the size of the localStorage
81 keysToDelete.push(key);
82 }
83 }
84
85 for (let key of keysToDelete) {
86 this.unset(key);
87 }
88 }
89
90 /**
91 * Simple localStorage wrapper, to clear localStorage
92 */
93 public clear = (): void => {
94 if (Client.isCapable()) {
95 localStorage.clear();
96 }
97 }
98
99 /**
100 * Checks if a key was set before, useful to not do all the undefined checks all the time
101 *
102 * @param {string} key
103 * @returns {boolean}
104 */
105 public isset = (key: string): boolean => {
106 if (Client.isCapable()) {
107 const value = this.get(key);
108 return (typeof value !== 'undefined' && value !== null);
109 }
110
111 return false;
112 }
113 }
114
115 export = new Client();