@cocreate/local-storage 1.14.3 → 1.15.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.14.4...v1.15.0) (2024-05-08)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * action property typo ([f17a85f](https://github.com/CoCreate-app/CoCreate-local-storage/commit/f17a85f7c4221044af7c0f7c92004beae7269f10))
7
+ * dispatch event if value null ([9975383](https://github.com/CoCreate-app/CoCreate-local-storage/commit/99753839b55a54497e91e5d29ad17585b1cb83e3))
8
+
9
+
10
+ ### Features
11
+
12
+ * dispatch updateAttributes event for operators ([ee86894](https://github.com/CoCreate-app/CoCreate-local-storage/commit/ee86894c119e5d375abe681ab03df5a9999860b9))
13
+
14
+ ## [1.14.4](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.14.3...v1.14.4) (2024-04-29)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * bump cocreate dependencies ([5d9a3f1](https://github.com/CoCreate-app/CoCreate-local-storage/commit/5d9a3f12c47f893fd5ee1a93f8e63640611a3c2e))
20
+
1
21
  ## [1.14.3](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.14.2...v1.14.3) (2024-04-26)
2
22
 
3
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/local-storage",
3
- "version": "1.14.3",
3
+ "version": "1.15.0",
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",
@@ -58,7 +58,7 @@
58
58
  "webpack-log": "^3.0.1"
59
59
  },
60
60
  "dependencies": {
61
- "@cocreate/actions": "^1.14.0",
62
- "@cocreate/observer": "^1.14.0"
61
+ "@cocreate/actions": "^1.18.1",
62
+ "@cocreate/observer": "^1.16.0"
63
63
  }
64
64
  }
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import observer from '@cocreate/observer';
2
- import action from '@cocreate/actions'
2
+ import actions from '@cocreate/actions'
3
3
 
4
4
  const CoCreateLocalStorage = {
5
5
  support: true,
@@ -39,7 +39,20 @@ const CoCreateLocalStorage = {
39
39
  * @param {Element} element - The element to initialize. This function expects an 'Element' type.
40
40
  */
41
41
  initElement: function (element) {
42
- this.getItem(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
+ }
43
56
  element.addEventListener('input', (e) => {
44
57
  let isRealtime = element.getAttribute('realtime')
45
58
  // Returns if realtime false.
@@ -83,9 +96,19 @@ const CoCreateLocalStorage = {
83
96
  */
84
97
  setItem: function (key, value) {
85
98
  try {
86
- if (this.support)
99
+ if (this.support) {
100
+ const oldValue = localStorage.getItem(key);
101
+
87
102
  window.localStorage.setItem(key, value);
88
- else
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
89
112
  this.storage.set(key, value);
90
113
  } catch (error) {
91
114
  if (error instanceof DOMException && (error.code === 22 || error.code === 1014 || error.name === 'QuotaExceededError' || error.name === 'NS_ERROR_DOM_QUOTA_REACHED')) {
@@ -109,12 +132,12 @@ const CoCreateLocalStorage = {
109
132
  },
110
133
 
111
134
  /**
112
- * @param btn
135
+ * @param action
113
136
  *
114
137
  * @return { undefined } No return
115
138
  */
116
- runStorage: async function (btn) {
117
- const form = btn.form;
139
+ runStorage: async function (action) {
140
+ const form = action.form;
118
141
  // If the form is not a form return false.
119
142
  if (!form) return;
120
143
 
@@ -178,21 +201,22 @@ const CoCreateLocalStorage = {
178
201
  }
179
202
  }
180
203
 
181
- action.init({
182
- name: "localStorage",
183
- endEvent: "localStorage",
184
- callback: (data) => {
185
- CoCreateLocalStorage.runStorage(data.element)
204
+ actions.init(
205
+ {
206
+ name: "localStorage",
207
+ endEvent: "localStorage",
208
+ callback: (data) => {
209
+ CoCreateLocalStorage.runStorage(data)
210
+ },
186
211
  },
187
- })
188
-
189
- action.init({
190
- name: "localStorageRemove",
191
- endEvent: "localStorageRemoved",
192
- callback: (data) => {
193
- CoCreateLocalStorage.runStorage(data.element)
194
- },
195
- })
212
+ {
213
+ name: "localStorageRemove",
214
+ endEvent: "localStorageRemoved",
215
+ callback: (data) => {
216
+ CoCreateLocalStorage.runStorage(data)
217
+ }
218
+ }
219
+ )
196
220
 
197
221
  observer.init({
198
222
  observe: ['addedNodes'],