@cocreate/local-storage 1.16.0 → 1.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/local-storage",
3
- "version": "1.16.0",
3
+ "version": "1.16.2",
4
4
  "description": "A Simple component similar to CoCreate-input & CoCreate-fetch which Save and fetch data for inputs, textarea, selects from localstorage. Easily configured using HTML5 attributes and/or JavaScript API",
5
5
  "keywords": [
6
6
  "local-storage",
@@ -45,20 +45,16 @@
45
45
  },
46
46
  "main": "./src/index.js",
47
47
  "devDependencies": {
48
- "@babel/core": "^7.9.6",
49
- "@babel/preset-env": "^7.9.6",
50
- "babel-loader": "^8.1.0",
51
- "clean-webpack-plugin": "^3.0.0",
52
- "file-loader": "^6.2.0",
48
+ "css-loader": "^5.1.3",
49
+ "esbuild": "^0.25.2",
50
+ "esbuild-loader": "^4.3.0",
53
51
  "mini-css-extract-plugin": "^1.5.0",
54
- "style-loader": "^3.3.1",
55
- "terser-webpack-plugin": "^5.1.1",
56
52
  "webpack": "^5.24.4",
57
53
  "webpack-cli": "^4.5.0",
58
54
  "webpack-log": "^3.0.1"
59
55
  },
60
56
  "dependencies": {
61
- "@cocreate/actions": "^1.18.2",
62
- "@cocreate/observer": "^1.16.1"
57
+ "@cocreate/actions": "^1.21.0",
58
+ "@cocreate/observer": "^1.18.0"
63
59
  }
64
60
  }
package/src/index.js CHANGED
@@ -1,234 +1,247 @@
1
- import observer from '@cocreate/observer';
2
- import actions from '@cocreate/actions'
1
+ import Observer from "@cocreate/observer";
2
+ import Actions from "@cocreate/actions";
3
3
 
4
4
  const CoCreateLocalStorage = {
5
- support: true,
6
- storage: new Map(),
7
-
8
- init: function () {
9
- const self = this
10
- var elements = document.querySelectorAll('[localstorage-set], [localstorage-get]');
11
- this.initElements(elements)
12
-
13
- window.addEventListener('storage', function (e) {
14
- elements = document.querySelectorAll(`[localstorage-get="${e.key}"]`)
15
- for (let element of elements) {
16
- // TODO: return if localstorage-selector, etc...
17
- // - Decide how best to handle, considering localstorage is also handled by event.
18
- if (element.hasAttribute('localstorage-attribute') || element.hasAttribute('localstorage-value') || element.hasAttribute('localstorage-key'))
19
- return
20
- let value = self.getItem(e.key)
21
- if (value != null) {
22
- element.setValue(value)
23
- }
24
- }
25
- });
26
- },
27
-
28
- /**
29
- * Initializes a collection of elements.
30
- * @param {Array<Element>} elements - An array of elements to be initialized.
31
- */
32
- initElements: function (elements) {
33
- for (let element of elements)
34
- this.initElement(element)
35
- },
36
-
37
- /**
38
- * Initializes a single element.
39
- * @param {Element} element - The element to initialize. This function expects an 'Element' type.
40
- */
41
- initElement: function (element) {
42
- let value = this.getItem(element)
43
- if (value)
44
- element.setValue(value)
45
- else {
46
- let inputEvent = new CustomEvent('input', {
47
- bubbles: true
48
- });
49
-
50
- Object.defineProperty(inputEvent, 'target', {
51
- writable: false,
52
- value: element
53
- });
54
- element.dispatchEvent(inputEvent);
55
- }
56
- element.addEventListener('input', (e) => {
57
- let isRealtime = element.getAttribute('realtime')
58
- // Returns if realtime false.
59
- if (!isRealtime && isRealtime === "false") return;
60
-
61
- let key = e.target.getAttribute('localstorage-set');
62
- // Set the key and value in the localstorage.
63
- if (key)
64
- this.setItem(key, e.target.value);
65
-
66
- let elements = document.querySelectorAll(`[localstorage-get="${key}"]`)
67
- for (let el of elements) {
68
- if (el != element) {
69
- let value = this.getItem(key)
70
- // Set the value of the element.
71
- if (value != null) {
72
- el.setValue(value)
73
- }
74
- }
75
-
76
- }
77
- })
78
- },
79
-
80
- /**
81
- * @param key
82
- *
83
- * @return { Mixed } The value associated with the key or undefined if not found. Note that this will be returned as a string if the key is an array
84
- */
85
- getItem: function (key) {
86
- // Returns the value of the key in the storage.
87
- if (this.support)
88
- return window.localStorage.getItem(key)
89
- else
90
- return this.storage.get(key)
91
- },
92
-
93
- /**
94
- * @param key - key name to store the value under
95
- * @param value - value to store
96
- */
97
- setItem: function (key, value) {
98
- try {
99
- if (this.support) {
100
- const oldValue = localStorage.getItem(key);
101
-
102
- window.localStorage.setItem(key, value);
103
-
104
- const keys = ['organization_id', 'user_id', 'clientId', 'session_id'];
105
- if (keys.includes(key)) {
106
- const updateEvent = new CustomEvent('updateAttributes', {
107
- detail: { key, newValue: value, oldValue }
108
- });
109
- window.dispatchEvent(updateEvent);
110
- }
111
- } else
112
- this.storage.set(key, value);
113
- } catch (error) {
114
- if (error instanceof DOMException && (error.code === 22 || error.code === 1014 || error.name === 'QuotaExceededError' || error.name === 'NS_ERROR_DOM_QUOTA_REACHED')) {
115
- // TODO: create a fallback method, perhaps setting in indexeddb if above a specific size
116
- console.log('Local storage limit exceeded. Falling back to IndexedDB.');
117
- } else {
118
- console.error('Error setting item:', error);
119
- }
120
- }
121
- },
122
-
123
- /**
124
- * @param key - The key name to delete from the storage
125
- */
126
- removeItem: function (key) {
127
- // Removes an item from the storage.
128
- if (this.support)
129
- window.localStorage.removeItem(key)
130
- else
131
- this.storage.delete(key)
132
- },
133
-
134
- /**
135
- * @param action
136
- *
137
- * @return { undefined } No return
138
- */
139
- runStorage: async function (action) {
140
- const form = action.form;
141
- // If the form is not a form return false.
142
- if (!form) return;
143
-
144
- let set_els = form.querySelectorAll('[localstorage-set]')
145
- for (let el of set_els) {
146
- let key = el.getAttribute('localstorage-set');
147
- let value = await el.getValue()
148
- // Sets the value of an item.
149
- if (value && key) {
150
- this.setItem(key, value);
151
- }
152
- }
153
- let get_els = form.querySelectorAll('[localstorage-get]')
154
- for (let el of get_els) {
155
- let key = el.getAttribute('localstorage-get');
156
- // If key is not set return false.
157
- if (!key) return;
158
- let value = this.getItem(key)
159
- // Set the value of the element.
160
- if (value != null) {
161
- el.setValue(value)
162
- }
163
- }
164
- let remove_els = form.querySelectorAll('[localstorage-remove]')
165
- for (let el of remove_els) {
166
- el.setValue('')
167
- let key = el.getAttribute('localstorage-remove');
168
- // Removes an item from the list.
169
- if (key) {
170
- this.removeItem(key)
171
- }
172
- }
173
-
174
- document.dispatchEvent(new CustomEvent('localStorage', {
175
- detail: {}
176
- }))
177
-
178
- },
179
-
180
- checkSupport: function () {
181
- // Check if the browser supports localStorage.
182
- if (!('localStorage' in window)) {
183
- console.log("This browser doesn't support localStorage.");
184
- this.support = false;
185
- } else {
186
- try {
187
- let test = window.localStorage.getItem('test')
188
- // If the CoCreateConfig. localStorage is false this method will set support to false.
189
- if (window.CoCreateConfig)
190
- // If the server supports local storage
191
- if (window.CoCreateConfig.localStorage === false)
192
- this.support = false;
193
- } catch (e) {
194
- this.support = false;
195
- } finally {
196
- // Initialize the component. If support is enabled the method init is called.
197
- if (this.support)
198
- this.init()
199
- }
200
- }
201
- }
202
- }
203
-
204
- actions.init(
205
- {
206
- name: "localStorage",
207
- endEvent: "localStorage",
208
- callback: (data) => {
209
- CoCreateLocalStorage.runStorage(data)
210
- },
211
- },
212
- {
213
- name: "localStorageRemove",
214
- endEvent: "localStorageRemoved",
215
- callback: (data) => {
216
- CoCreateLocalStorage.runStorage(data)
217
- }
218
- }
219
- )
220
-
221
- observer.init({
222
- observe: ['addedNodes'],
223
- selector: "[localstorage-set]",
224
- callback: async function (mutation) {
225
- let key = mutation.target.getAttribute('localstorage-set')
226
- let value = await mutation.target.getValue()
227
- if (key && value)
228
- localStorage.setItem(key, value)
229
- }
5
+ support: true,
6
+ storage: new Map(),
7
+
8
+ init: function () {
9
+ const self = this;
10
+ var elements = document.querySelectorAll(
11
+ "[localstorage-set], [localstorage-get]"
12
+ );
13
+ this.initElements(elements);
14
+
15
+ window.addEventListener("storage", function (e) {
16
+ elements = document.querySelectorAll(
17
+ `[localstorage-get="${e.key}"]`
18
+ );
19
+ for (let element of elements) {
20
+ // TODO: return if localstorage-query, etc...
21
+ // - Decide how best to handle, considering localstorage is also handled by event.
22
+ if (
23
+ element.hasAttribute("localstorage-attribute") ||
24
+ element.hasAttribute("localstorage-value") ||
25
+ element.hasAttribute("localstorage-key")
26
+ )
27
+ return;
28
+ let value = self.getItem(e.key);
29
+ if (value != null) {
30
+ element.setValue(value);
31
+ }
32
+ }
33
+ });
34
+ },
35
+
36
+ /**
37
+ * Initializes a collection of elements.
38
+ * @param {Array<Element>} elements - An array of elements to be initialized.
39
+ */
40
+ initElements: function (elements) {
41
+ for (let element of elements) this.initElement(element);
42
+ },
43
+
44
+ /**
45
+ * Initializes a single element.
46
+ * @param {Element} element - The element to initialize. This function expects an 'Element' type.
47
+ */
48
+ initElement: function (element) {
49
+ let value = this.getItem(element);
50
+ if (value) element.setValue(value);
51
+ else {
52
+ let inputEvent = new CustomEvent("input", {
53
+ bubbles: true
54
+ });
55
+
56
+ Object.defineProperty(inputEvent, "target", {
57
+ writable: false,
58
+ value: element
59
+ });
60
+ element.dispatchEvent(inputEvent);
61
+ }
62
+ element.addEventListener("input", (e) => {
63
+ let isRealtime = element.getAttribute("realtime");
64
+ // Returns if realtime false.
65
+ if (!isRealtime && isRealtime === "false") return;
66
+
67
+ let key = e.target.getAttribute("localstorage-set");
68
+ // Set the key and value in the localstorage.
69
+ if (key) this.setItem(key, e.target.value);
70
+
71
+ let elements = document.querySelectorAll(
72
+ `[localstorage-get="${key}"]`
73
+ );
74
+ for (let el of elements) {
75
+ if (el != element) {
76
+ let value = this.getItem(key);
77
+ // Set the value of the element.
78
+ if (value != null) {
79
+ el.setValue(value);
80
+ }
81
+ }
82
+ }
83
+ });
84
+ },
85
+
86
+ /**
87
+ * @param key
88
+ *
89
+ * @return { Mixed } The value associated with the key or undefined if not found. Note that this will be returned as a string if the key is an array
90
+ */
91
+ getItem: function (key) {
92
+ // Returns the value of the key in the storage.
93
+ if (this.support) return window.localStorage.getItem(key);
94
+ else return this.storage.get(key);
95
+ },
96
+
97
+ /**
98
+ * @param key - key name to store the value under
99
+ * @param value - value to store
100
+ */
101
+ setItem: function (key, value) {
102
+ try {
103
+ if (this.support) {
104
+ const oldValue = localStorage.getItem(key);
105
+
106
+ window.localStorage.setItem(key, value);
107
+
108
+ const keys = [
109
+ "organization_id",
110
+ "user_id",
111
+ "clientId",
112
+ "session_id"
113
+ ];
114
+ if (keys.includes(key)) {
115
+ const updateEvent = new CustomEvent("updateAttributes", {
116
+ detail: { key, newValue: value, oldValue }
117
+ });
118
+ window.dispatchEvent(updateEvent);
119
+ }
120
+ } else this.storage.set(key, value);
121
+ } catch (error) {
122
+ if (
123
+ error instanceof DOMException &&
124
+ (error.code === 22 ||
125
+ error.code === 1014 ||
126
+ error.name === "QuotaExceededError" ||
127
+ error.name === "NS_ERROR_DOM_QUOTA_REACHED")
128
+ ) {
129
+ // TODO: create a fallback method, perhaps setting in indexeddb if above a specific size
130
+ console.log(
131
+ "Local storage limit exceeded. Falling back to IndexedDB."
132
+ );
133
+ } else {
134
+ console.error("Error setting item:", error);
135
+ }
136
+ }
137
+ },
138
+
139
+ /**
140
+ * @param key - The key name to delete from the storage
141
+ */
142
+ removeItem: function (key) {
143
+ // Removes an item from the storage.
144
+ if (this.support) window.localStorage.removeItem(key);
145
+ else this.storage.delete(key);
146
+ },
147
+
148
+ /**
149
+ * @param action
150
+ *
151
+ * @return { undefined } No return
152
+ */
153
+ runStorage: async function (action) {
154
+ const form = action.form;
155
+ // If the form is not a form return false.
156
+ if (!form) return;
157
+
158
+ let set_els = form.querySelectorAll("[localstorage-set]");
159
+ for (let el of set_els) {
160
+ let key = el.getAttribute("localstorage-set");
161
+ let value = await el.getValue();
162
+ // Sets the value of an item.
163
+ if (value && key) {
164
+ this.setItem(key, value);
165
+ }
166
+ }
167
+ let get_els = form.querySelectorAll("[localstorage-get]");
168
+ for (let el of get_els) {
169
+ let key = el.getAttribute("localstorage-get");
170
+ // If key is not set return false.
171
+ if (!key) return;
172
+ let value = this.getItem(key);
173
+ // Set the value of the element.
174
+ if (value != null) {
175
+ el.setValue(value);
176
+ }
177
+ }
178
+ let remove_els = form.querySelectorAll("[localstorage-remove]");
179
+ for (let el of remove_els) {
180
+ el.setValue("");
181
+ let key = el.getAttribute("localstorage-remove");
182
+ // Removes an item from the list.
183
+ if (key) {
184
+ this.removeItem(key);
185
+ }
186
+ }
187
+
188
+ action.element.dispatchEvent(
189
+ new CustomEvent("localStorage", {
190
+ detail: {}
191
+ })
192
+ );
193
+ },
194
+
195
+ checkSupport: function () {
196
+ // Check if the browser supports localStorage.
197
+ if (!("localStorage" in window)) {
198
+ console.log("This browser doesn't support localStorage.");
199
+ this.support = false;
200
+ } else {
201
+ try {
202
+ let test = window.localStorage.getItem("test");
203
+ // If the CoCreateConfig. localStorage is false this method will set support to false.
204
+ if (window.CoCreateConfig)
205
+ if (window.CoCreateConfig.localStorage === false)
206
+ // If the server supports local storage
207
+ this.support = false;
208
+ } catch (e) {
209
+ this.support = false;
210
+ } finally {
211
+ // Initialize the component. If support is enabled the method init is called.
212
+ if (this.support) this.init();
213
+ }
214
+ }
215
+ }
216
+ };
217
+
218
+ Actions.init(
219
+ {
220
+ name: "localStorage",
221
+ endEvent: "localStorage",
222
+ callback: (data) => {
223
+ CoCreateLocalStorage.runStorage(data);
224
+ }
225
+ },
226
+ {
227
+ name: "localStorageRemove",
228
+ endEvent: "localStorageRemoved",
229
+ callback: (data) => {
230
+ CoCreateLocalStorage.runStorage(data);
231
+ }
232
+ }
233
+ );
234
+
235
+ Observer.init({
236
+ types: ["addedNodes"],
237
+ selector: "[localstorage-set]",
238
+ callback: async function (mutation) {
239
+ let key = mutation.target.getAttribute("localstorage-set");
240
+ let value = await mutation.target.getValue();
241
+ if (key && value) localStorage.setItem(key, value);
242
+ }
230
243
  });
231
244
 
232
- CoCreateLocalStorage.checkSupport()
245
+ CoCreateLocalStorage.checkSupport();
233
246
 
234
- export default CoCreateLocalStorage;
247
+ export default CoCreateLocalStorage;