@cocreate/local-storage 1.7.1 → 1.7.3

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,17 @@
1
+ ## [1.7.3](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.7.2...v1.7.3) (2023-05-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Update dependencies versions for [@cocreate](https://github.com/cocreate) libraries ([a3ebbb8](https://github.com/CoCreate-app/CoCreate-local-storage/commit/a3ebbb882d7137522328c79ddf611a33cfa12bc3))
7
+
8
+ ## [1.7.2](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.7.1...v1.7.2) (2023-05-19)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * update packages to latest version. This commit updates various packages in the dependencies section of the package.json file to their latest published versions, thereby fixing multiple bugs and improving overall performance. ([c689cc4](https://github.com/CoCreate-app/CoCreate-local-storage/commit/c689cc4a6b70756932abfbfb0f3e99e5443d8fbb))
14
+
1
15
  ## [1.7.1](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.7.0...v1.7.1) (2023-05-10)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/local-storage",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
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",
@@ -59,8 +59,8 @@
59
59
  "webpack-log": "^3.0.1"
60
60
  },
61
61
  "dependencies": {
62
- "@cocreate/actions": "^1.8.8",
63
- "@cocreate/docs": "^1.7.8",
64
- "@cocreate/observer": "^1.7.6"
62
+ "@cocreate/actions": "^1.8.11",
63
+ "@cocreate/docs": "^1.7.12",
64
+ "@cocreate/observer": "^1.7.9"
65
65
  }
66
66
  }
package/src/index.js CHANGED
@@ -2,36 +2,53 @@ import observer from '@cocreate/observer';
2
2
  import action from '@cocreate/actions'
3
3
 
4
4
  const CoCreateLocalStorage = {
5
- support: true,
5
+ support: true,
6
6
  storage: new Map(),
7
7
 
8
- init: function() {
8
+ /**
9
+ * Listen for storage events and update elements if needed. This is called on window. storage and when it's time to get / set data
10
+ */
11
+ init: function () {
9
12
  const self = this
10
- var elements = document.querySelectorAll('[localstorage-set], [localstorage-get]');
11
- this.initElements(elements)
12
- window.addEventListener('storage', function(e) {
13
+ var elements = document.querySelectorAll('[localstorage-set], [localstorage-get]');
14
+ this.initElements(elements)
15
+ /**
16
+ * @param e
17
+ */
18
+ window.addEventListener('storage', function (e) {
13
19
  elements = document.querySelectorAll(`[localstorage-get="${e.key}"]`)
14
20
  for (let element of elements) {
15
21
  let value = self.getItem(e.key)
16
- if (value != null){
22
+ // Set the value of the element.
23
+ if (value != null) {
17
24
  element.setValue(value)
18
25
  }
19
26
  }
20
- });
21
- },
27
+ });
28
+ },
22
29
 
23
- initElements: function(elements) {
24
- for (let element of elements)
25
- this.initElement(element)
26
- },
30
+ /**
31
+ * @param elements
32
+ */
33
+ initElements: function (elements) {
34
+ for (let element of elements)
35
+ this.initElement(element)
36
+ },
27
37
 
28
- initElement: function(element) {
38
+ /**
39
+ * @param element
40
+ *
41
+ * @return { Object } The binding
42
+ */
43
+ initElement: function (element) {
29
44
  this.getItem(element)
30
45
  element.addEventListener('input', (e) => {
31
46
  let isRealtime = element.getAttribute('realtime')
32
- if (isRealtime == "false") return;
47
+ // Returns if realtime false.
48
+ if (isRealtime == "false") return;
33
49
 
34
50
  let key = e.target.getAttribute('localstorage-set');
51
+ // Set the key and value in the localstorage.
35
52
  if (key)
36
53
  this.setItem(key, e.target.value);
37
54
 
@@ -39,54 +56,79 @@ const CoCreateLocalStorage = {
39
56
  for (let el of elements) {
40
57
  if (el != element) {
41
58
  let value = this.getItem(key)
42
- if (value != null){
59
+ // Set the value of the element.
60
+ if (value != null) {
43
61
  el.setValue(value)
44
62
  }
45
63
  }
46
-
64
+
47
65
  }
48
- })
66
+ })
49
67
  },
50
-
51
- getItem: function(key) {
68
+
69
+ /**
70
+ * @param key
71
+ *
72
+ * @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
73
+ */
74
+ getItem: function (key) {
75
+ // Returns the value of the key in the storage.
52
76
  if (this.support)
53
77
  return window.localStorage.getItem(key)
54
78
  else
55
79
  return this.storage.get(key)
56
- },
57
-
58
- setItem: function(key, value) {
80
+ },
81
+
82
+ /**
83
+ * @param key - key name to store the value under
84
+ * @param value - value to store
85
+ */
86
+ setItem: function (key, value) {
87
+ // Set the value of the item.
59
88
  if (this.support)
60
89
  window.localStorage.setItem(key, value);
61
90
  else
62
91
  this.storage.set(key, value)
63
92
  },
64
93
 
65
- removeItem: function(key) {
94
+ /**
95
+ * @param key - The key name to delete from the storage
96
+ */
97
+ removeItem: function (key) {
98
+ // Removes an item from the storage.
66
99
  if (this.support)
67
100
  window.localStorage.removeItem(key)
68
101
  else
69
102
  this.storage.delete(key)
70
- },
71
-
72
- runStorage: function(btn) {
103
+ },
104
+
105
+ /**
106
+ * @param btn
107
+ *
108
+ * @return { undefined } No return
109
+ */
110
+ runStorage: function (btn) {
73
111
  const form = btn.form;
112
+ // If the form is not a form return false.
74
113
  if (!form) return;
75
-
114
+
76
115
  let set_els = form.querySelectorAll('[localstorage-set]')
77
116
  for (let el of set_els) {
78
117
  let key = el.getAttribute('localstorage-set');
79
118
  let value = el.getValue()
80
- if (value && key){
119
+ // Sets the value of an item.
120
+ if (value && key) {
81
121
  this.setItem(key, value);
82
122
  }
83
123
  }
84
124
  let get_els = form.querySelectorAll('[localstorage-get]')
85
125
  for (let el of get_els) {
86
126
  let key = el.getAttribute('localstorage-get');
127
+ // If key is not set return false.
87
128
  if (!key) return;
88
129
  let value = this.getItem(key)
89
- if (value != null){
130
+ // Set the value of the element.
131
+ if (value != null) {
90
132
  el.setValue(value)
91
133
  }
92
134
  }
@@ -94,60 +136,56 @@ const CoCreateLocalStorage = {
94
136
  for (let el of remove_els) {
95
137
  el.setValue('')
96
138
  let key = el.getAttribute('localstorage-remove');
139
+ // Removes an item from the list.
97
140
  if (key) {
98
141
  this.removeItem(key)
99
142
  }
100
143
  }
101
-
144
+
102
145
  document.dispatchEvent(new CustomEvent('localStorage', {
103
- detail: {}
104
- }))
105
-
146
+ detail: {}
147
+ }))
148
+
106
149
  },
107
150
 
108
- checkSupport: function() {
151
+ checkSupport: function () {
152
+ // Check if the browser supports localStorage.
109
153
  if (!('localStorage' in window)) {
110
154
  console.log("This browser doesn't support localStorage.");
111
155
  this.support = false;
112
- } else {
156
+ } else {
113
157
  try {
114
158
  let test = window.localStorage.getItem('test')
159
+ // If the CoCreateConfig. localStorage is false this method will set support to false.
115
160
  if (window.CoCreateConfig)
161
+ // If the server supports local storage
116
162
  if (window.CoCreateConfig.localStorage === false)
117
163
  this.support = false;
118
- } catch(e) {
164
+ } catch (e) {
119
165
  this.support = false;
120
166
  } finally {
167
+ // Initialize the component. If support is enabled the method init is called.
121
168
  if (this.support)
122
169
  this.init()
123
170
  }
124
171
  }
125
- }
172
+ }
126
173
  }
127
174
 
128
- // observer.init({
129
- // name: 'CoCreateLocalstorage',
130
- // observe: ['addedNodes'],
131
- // target: '[localstorage-key]',
132
- // callback: function(mutation) {
133
- // CoCreateLocalStorage._initSessionIds(mutation.target);
134
- // }
135
- // });
136
-
137
175
  action.init({
138
- name: "localStorage",
139
- endEvent: "localStorage",
140
- callback: (btn, data) => {
141
- CoCreateLocalStorage.runStorage(btn)
142
- },
176
+ name: "localStorage",
177
+ endEvent: "localStorage",
178
+ callback: (btn, data) => {
179
+ CoCreateLocalStorage.runStorage(btn)
180
+ },
143
181
  })
144
182
 
145
183
  action.init({
146
- name: "localStorageRemove",
147
- endEvent: "localStorageRemoved",
148
- callback: (btn, data) => {
149
- CoCreateLocalStorage.runStorage(btn)
150
- },
184
+ name: "localStorageRemove",
185
+ endEvent: "localStorageRemoved",
186
+ callback: (btn, data) => {
187
+ CoCreateLocalStorage.runStorage(btn)
188
+ },
151
189
  })
152
190
 
153
191
  CoCreateLocalStorage.checkSupport()