@cocreate/local-storage 1.3.49 → 1.4.1
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 +14 -0
- package/package.json +3 -2
- package/src/index.js +72 -47
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.4.1](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.4.0...v1.4.1) (2023-01-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies ([3c15125](https://github.com/CoCreate-app/CoCreate-local-storage/commit/3c15125aba798feef3e09f4813d9794bb58156b2))
|
|
7
|
+
|
|
8
|
+
# [1.4.0](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.3.49...v1.4.0) (2023-01-29)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* import @cocreate/local-storage ([68bd564](https://github.com/CoCreate-app/CoCreate-local-storage/commit/68bd56424466d0188b5b012968405105326028bb))
|
|
14
|
+
|
|
1
15
|
## [1.3.49](https://github.com/CoCreate-app/CoCreate-local-storage/compare/v1.3.48...v1.3.49) (2023-01-27)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/local-storage",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
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",
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"webpack-log": "^3.0.1"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@cocreate/
|
|
63
|
+
"@cocreate/actions": "^1.5.55",
|
|
64
|
+
"@cocreate/docs": "^1.5.9"
|
|
64
65
|
}
|
|
65
66
|
}
|
package/src/index.js
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
|
+
import action from '@cocreate/actions'
|
|
1
2
|
|
|
2
3
|
const CoCreateLocalStorage = {
|
|
3
|
-
|
|
4
|
+
support: true,
|
|
5
|
+
storage: new Map(),
|
|
6
|
+
|
|
4
7
|
init: function() {
|
|
5
8
|
var elements = document.querySelectorAll('[localstorage-set], [localstorage-get]');
|
|
6
9
|
this.initElements(elements)
|
|
7
10
|
window.addEventListener('storage', function(e) {
|
|
8
11
|
elements = document.querySelectorAll(`[localstorage-get="${e.key}"]`)
|
|
9
|
-
for (let element of elements)
|
|
10
|
-
this.
|
|
12
|
+
for (let element of elements) {
|
|
13
|
+
let value = this.getItem(key)
|
|
14
|
+
if (value != null){
|
|
15
|
+
element.value = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
11
18
|
});
|
|
12
19
|
},
|
|
13
20
|
|
|
@@ -21,79 +28,97 @@ const CoCreateLocalStorage = {
|
|
|
21
28
|
element.addEventListener('input', (e) => {
|
|
22
29
|
let isRealtime = element.getAttribute('realtime')
|
|
23
30
|
if (isRealtime == "false") return;
|
|
24
|
-
this.set(e.target)
|
|
25
31
|
|
|
26
32
|
let key = e.target.getAttribute('localstorage-set');
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
this.setItem(key, e.target.value);
|
|
34
|
+
|
|
35
|
+
let elements = document.querySelectorAll(`[localstorage-get="${key}"]`)
|
|
36
|
+
for (let el of elements) {
|
|
37
|
+
if (el != element) {
|
|
38
|
+
let value = this.getItem(key)
|
|
39
|
+
if (value != null){
|
|
40
|
+
el.value = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
31
44
|
}
|
|
32
45
|
})
|
|
33
46
|
},
|
|
34
47
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
element.value = value;
|
|
41
|
-
}
|
|
48
|
+
getItem: function(key) {
|
|
49
|
+
if (this.support)
|
|
50
|
+
return window.localStorage.getItem(key)
|
|
51
|
+
else
|
|
52
|
+
return this.storage.get(key)
|
|
42
53
|
},
|
|
43
54
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
55
|
+
setItem: function(key, value) {
|
|
56
|
+
if (this.support)
|
|
57
|
+
window.localStorage.setItem(key, value);
|
|
58
|
+
else
|
|
59
|
+
this.storage.set(key, value)
|
|
49
60
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
let key = element.getAttribute('localstorage-remove');
|
|
54
|
-
if (key) {
|
|
61
|
+
|
|
62
|
+
removeItem: function(key) {
|
|
63
|
+
if (this.support)
|
|
55
64
|
window.localStorage.removeItem(key)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
else
|
|
66
|
+
this.storage.delete(key)
|
|
67
|
+
},
|
|
68
|
+
|
|
59
69
|
runStorage: function(btn) {
|
|
60
70
|
const form = btn.form;
|
|
61
|
-
let storage = window.localStorage;
|
|
62
71
|
if (!form) return;
|
|
63
72
|
|
|
64
73
|
let set_els = form.querySelectorAll('[localstorage-set]')
|
|
65
|
-
set_els
|
|
66
|
-
let key =
|
|
67
|
-
if (
|
|
68
|
-
|
|
74
|
+
for (let el of set_els) {
|
|
75
|
+
let key = el.getAttribute('localstorage-set');
|
|
76
|
+
if (el.value && key){
|
|
77
|
+
this.setItem(key, el.value);
|
|
69
78
|
}
|
|
70
|
-
}
|
|
79
|
+
}
|
|
71
80
|
let get_els = form.querySelectorAll('[localstorage-get]')
|
|
72
|
-
get_els
|
|
73
|
-
let key =
|
|
81
|
+
for (let el of get_els) {
|
|
82
|
+
let key = el.getAttribute('localstorage-get');
|
|
74
83
|
if (!key) return;
|
|
75
|
-
let value =
|
|
84
|
+
let value = this.getItem(key)
|
|
76
85
|
if (value != null){
|
|
77
|
-
|
|
86
|
+
el.value = value;
|
|
78
87
|
}
|
|
79
|
-
}
|
|
88
|
+
}
|
|
80
89
|
let remove_els = form.querySelectorAll('[localstorage-remove]')
|
|
81
|
-
remove_els
|
|
82
|
-
|
|
83
|
-
let key =
|
|
90
|
+
for (let el of remove_els) {
|
|
91
|
+
el.value = '';
|
|
92
|
+
let key = el.getAttribute('localstorage-remove');
|
|
84
93
|
if (key) {
|
|
85
|
-
|
|
94
|
+
this.removeItem(key)
|
|
86
95
|
}
|
|
87
|
-
}
|
|
96
|
+
}
|
|
88
97
|
|
|
89
98
|
document.dispatchEvent(new CustomEvent('localStorage', {
|
|
90
99
|
detail: {}
|
|
91
100
|
}))
|
|
92
101
|
|
|
93
|
-
}
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
checkSupport: function() {
|
|
105
|
+
if (!('localStorage' in window)) {
|
|
106
|
+
console.log("This browser doesn't support localStorage.");
|
|
107
|
+
this.support = false;
|
|
108
|
+
} else {
|
|
109
|
+
try {
|
|
110
|
+
let test = window.localStorage.getItem('test')
|
|
111
|
+
if (window.CoCreateConfig)
|
|
112
|
+
if (window.CoCreateConfig.localStorage === false)
|
|
113
|
+
this.support = false;
|
|
114
|
+
} catch(e) {
|
|
115
|
+
this.support = false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
94
119
|
}
|
|
95
120
|
|
|
96
|
-
|
|
121
|
+
action.init({
|
|
97
122
|
name: "localStorage",
|
|
98
123
|
endEvent: "localStorage",
|
|
99
124
|
callback: (btn, data) => {
|
|
@@ -101,7 +126,7 @@ CoCreate.action.init({
|
|
|
101
126
|
},
|
|
102
127
|
})
|
|
103
128
|
|
|
104
|
-
|
|
129
|
+
action.init({
|
|
105
130
|
name: "localStorageRemove",
|
|
106
131
|
endEvent: "localStorageRemoved",
|
|
107
132
|
callback: (btn, data) => {
|