UNPKG

7.97 kBJavaScriptView Raw
1/*!
2 * OpenUI5
3 * (c) Copyright 2009-2022 SAP SE or an SAP affiliate company.
4 * Licensed under the Apache License, Version 2.0 - see LICENSE.txt.
5 */
6
7/*
8 * Provides methods to store and retrieve data based on Web Storage API.
9 */
10sap.ui.define([
11 'jquery.sap.global',
12 'sap/base/assert',
13 'sap/ui/util/Storage'
14], function(jQuery, assert, Storage) {
15 "use strict";
16
17 /**
18 * Map containing instances of different 'standard' storages.
19 * The map is used to limit the number of created storage objects.
20 * @private
21 */
22 var mStorages = {};
23
24 /**
25 * Returns a {@link jQuery.sap.storage.Storage Storage} object for a given HTML5 storage (type) and,
26 * as a convenience, provides static functions to access the default (session) storage.
27 *
28 * When called as a function, it returns an instance of {@link jQuery.sap.storage.Storage}, providing access
29 * to the storage of the given {@link jQuery.sap.storage.Type} or to the given HTML5 Storage object.
30 *
31 * The default session storage can be easily accessed with methods {@link jQuery.sap.storage.get},
32 * {@link jQuery.sap.storage.put}, {@link jQuery.sap.storage.remove}, {@link jQuery.sap.storage.clear},
33 * {@link jQuery.sap.storage.getType} and {@link jQuery.sap.storage.removeAll}
34 *
35 * @param {jQuery.sap.storage.Type | Storage}
36 * oStorage the type specifying the storage to use or an object implementing the browser's Storage API.
37 * @param {string} [sIdPrefix] Prefix used for the Ids. If not set a default prefix is used.
38 * @returns {jQuery.sap.storage.Storage}
39 *
40 * @version 1.90.11
41 * @since 0.11.0
42 * @namespace
43 * @public
44 *
45 * @borrows jQuery.sap.storage.Storage#get as get
46 * @borrows jQuery.sap.storage.Storage#put as put
47 * @borrows jQuery.sap.storage.Storage#remove as remove
48 * @borrows jQuery.sap.storage.Storage#clear as clear
49 * @borrows jQuery.sap.storage.Storage#getType as getType
50 * @borrows jQuery.sap.storage.Storage#removeAll as removeAll
51 * @borrows jQuery.sap.storage.Storage#isSupported as isSupported
52 */
53 jQuery.sap.storage = function(oStorage, sIdPrefix) {
54 // if nothing or the default was passed in, simply return ourself
55 if (!oStorage) {
56 oStorage = Storage.Type.session;
57 }
58
59 if (typeof (oStorage) === "string" && Storage.Type[oStorage]) {
60 var sKey = oStorage;
61 if (sIdPrefix && sIdPrefix != "state.key_") {
62 sKey = oStorage + "_" + sIdPrefix;
63 }
64
65 return mStorages[sKey] || (mStorages[sKey] = new Storage(oStorage, sIdPrefix));
66 }
67
68 // OK, tough but probably good for issue identification. As something was passed in, let's at least ensure our used API is fulfilled.
69 assert(oStorage instanceof Object && oStorage.clear && oStorage.setItem && oStorage.getItem && oStorage.removeItem, "storage: duck typing the storage");
70 return new Storage(oStorage, sIdPrefix);
71 };
72
73 /**
74 * @interface A Storage API for JavaScript.
75 *
76 * Provides methods to store data on the client using Web Storage API support by the browser. The data
77 * received by this API must be already serialized, in string format. Similarly, the API returns the retrieved
78 * data in serialized string format, so it is the responsibility of the caller to de-serialize it, if applicable.
79 *
80 * Attention: The Web Storage API stores the data on the client. Therefore do not use this API for confidential information.
81 *
82 * One can get access to the 'default' storage by using {@link jQuery.sap.storage} directly
83 * or alternatively via factory functionality available as <code>jQuery.sap.storage(jQuery.sap.storage.Type.session)</code>
84 * returning an object implementing this interface.
85 *
86 * A typical intended usage of this API is the storage of a string representing the state of a control.
87 * In such usage, the data is stored in the browser session, and
88 * the methods to be used are {@link #put} and {@link #get}.
89 * The method {@link #remove} can be used to delete the previously saved state.
90 *
91 * In sake of completeness, the method {@link #clear} is available.
92 * However, it should be called only in very particular situations,
93 * when a global erasing of data is required. If only keys with certain prefix
94 * should be deleted the method {@link #removeAll} should be used.
95 *
96 * @author SAP SE
97 * @version 1.90.11
98 * @since 0.11.0
99 * @public
100 * @name jQuery.sap.storage.Storage
101 */
102 jQuery.sap.storage.Storage = Storage;
103
104 /**
105 * Returns whether the given storage is suppported.
106 *
107 * @return {boolean} true if storage is supported, false otherwise (e.g. due to browser security settings)
108 * @public
109 * @name jQuery.sap.storage.Storage#isSupported
110 * @function
111 */
112
113 /**
114 * Stores the passed state string in the session, under the key
115 * sStorageKeyPrefix + sId.
116 *
117 * sStorageKeyPrefix is the id prefix defined for the storage instance (@see jQuery.sap#storage)
118 *
119 * @param {string} sId Id for the state to store
120 * @param {string} sStateToStore content to store
121 * @return {boolean} true if the data were successfully stored, false otherwise
122 * @public
123 * @name jQuery.sap.storage.Storage#put
124 * @function
125 */
126
127 /**
128 * Retrieves the state string stored in the session under the key
129 * sStorageKeyPrefix + sId.
130 *
131 * sStorageKeyPrefix is the id prefix defined for the storage instance (@see jQuery.sap#storage)
132 *
133 * @param {string} sId Id for the state to retrieve
134 * @return {string} the string from the storage, if the retrieval
135 * was successful, and null otherwise
136 * @public
137 * @name jQuery.sap.storage.Storage#get
138 * @function
139 */
140
141 /**
142 * Deletes the state string stored in the session under the key
143 * sStorageKeyPrefix + sId.s
144 *
145 * sStorageKeyPrefix is the id prefix defined for the storage instance (@see jQuery.sap#storage)
146 *
147 * @param {string} sId Id for the state to delete
148 * @return {boolean} true if the deletion
149 * was successful or the data doesn't exist under the specified key,
150 * and false if the feature is unavailable or a problem occurred
151 * @public
152 * @name jQuery.sap.storage.Storage#remove
153 * @function
154 */
155
156 /**
157 * Deletes all state strings stored in the session under the key prefix
158 * sStorageKeyPrefix + sIdPrefix.
159 *
160 * sStorageKeyPrefix is the id prefix defined for the storage instance (@see jQuery.sap#storage)
161 *
162 * @param {string} sIdPrefix Id prefix for the states to delete
163 * @return {boolean} true if the deletion
164 * was successful or the data doesn't exist under the specified key,
165 * and false if the feature is unavailable or a problem occurred
166 * @since 1.13.0
167 * @public
168 * @name jQuery.sap.storage.Storage#removeAll
169 * @function
170 */
171
172 /**
173 * Deletes all the entries saved in the session (Independent of the current Storage instance!).
174 *
175 * CAUTION: This method should be called only in very particular situations,
176 * when a global erasing of data is required. Given that the method deletes
177 * the data saved under any ID, it should not be called when managing data
178 * for specific controls.
179 *
180 * @return {boolean} true if execution of removal
181 * was successful or the data to remove doesn't exist,
182 * and false if the feature is unavailable or a problem occurred
183 * @public
184 * @name jQuery.sap.storage.Storage#clear
185 * @function
186 */
187
188 /**
189 * Returns the type of the storage.
190 * @returns {jQuery.sap.storage.Type | string} the type of the storage or "unknown"
191 * @public
192 * @name jQuery.sap.storage.Storage#getType
193 * @function
194 */
195
196
197 /**
198 * Enumeration of the storage types supported by {@link jQuery.sap.storage.Storage}
199 * @enum {string}
200 * @public
201 * @version 1.90.11
202 * @since 0.11.0
203 */
204 jQuery.sap.storage.Type = Storage.Type;
205
206 /**
207 * Indicates usage of the browser's localStorage feature
208 * @type {string}
209 * @public
210 * @name jQuery.sap.storage.Type.local
211 */
212
213 /**
214 * Indicates usage of the browser's sessionStorage feature
215 * @type {string}
216 * @public
217 * @name jQuery.sap.storage.Type.session
218 */
219
220 Object.assign(jQuery.sap.storage, Storage);
221
222 return jQuery;
223
224});
\No newline at end of file