@cocreate/state 1.21.0 → 1.22.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 +20 -0
- package/package.json +7 -7
- package/src/index.js +59 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
## [1.22.1](https://github.com/CoCreate-app/CoCreate-state/compare/v1.22.0...v1.22.1) (2024-04-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump cocreate dependencies ([3972fe5](https://github.com/CoCreate-app/CoCreate-state/commit/3972fe5147f4e95b04df3188c3520ead5fd40a95))
|
|
7
|
+
* state management ([13177bd](https://github.com/CoCreate-app/CoCreate-state/commit/13177bdf44ff2d3248e6ccce1b2c74222727d0c6))
|
|
8
|
+
|
|
9
|
+
# [1.22.0](https://github.com/CoCreate-app/CoCreate-state/compare/v1.21.0...v1.22.0) (2024-04-29)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* improved state handeling and history api ([1b26d24](https://github.com/CoCreate-app/CoCreate-state/commit/1b26d24670d01020df4f2f054ee3dd100a3f5643))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* popstate event handling ([df2432b](https://github.com/CoCreate-app/CoCreate-state/commit/df2432bc5090ef2942218d8ddb652f321e2c246c))
|
|
20
|
+
|
|
1
21
|
# [1.21.0](https://github.com/CoCreate-app/CoCreate-state/compare/v1.20.1...v1.21.0) (2024-04-28)
|
|
2
22
|
|
|
3
23
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/state",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.1",
|
|
4
4
|
"description": "Data attributes which can be used for components in CoCreate framework combined as a group of attributes that dynamically change attributes & fetch custom data.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -58,11 +58,11 @@
|
|
|
58
58
|
"webpack-log": "^3.0.1"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@cocreate/actions": "^1.
|
|
62
|
-
"@cocreate/crud-client": "^1.
|
|
63
|
-
"@cocreate/element-prototype": "^1.
|
|
64
|
-
"@cocreate/local-storage": "^1.
|
|
65
|
-
"@cocreate/observer": "^1.
|
|
66
|
-
"@cocreate/uuid": "^1.
|
|
61
|
+
"@cocreate/actions": "^1.18.1",
|
|
62
|
+
"@cocreate/crud-client": "^1.33.8",
|
|
63
|
+
"@cocreate/element-prototype": "^1.22.2",
|
|
64
|
+
"@cocreate/local-storage": "^1.14.3",
|
|
65
|
+
"@cocreate/observer": "^1.16.0",
|
|
66
|
+
"@cocreate/uuid": "^1.11.1"
|
|
67
67
|
}
|
|
68
68
|
}
|
package/src/index.js
CHANGED
|
@@ -54,26 +54,20 @@ function _setAttributeValues(el, attrValues) {
|
|
|
54
54
|
|
|
55
55
|
delete attrValues['overwrite']
|
|
56
56
|
|
|
57
|
-
Object.keys(attrValues)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
_setAttributeValue(el, key, attrValues[key], isOverwrite);
|
|
64
|
-
_setAttributeValue(el, `state-${key}`, attrValues[key], isOverwrite);
|
|
65
|
-
if (key == 'array' || key == 'object' || key == 'name') {
|
|
66
|
-
_setAttributeValue(el, `fetch-${key}`, attrValues[key], isOverwrite);
|
|
67
|
-
_setAttributeValue(el, `state-fetch-${key}`, attrValues[key], isOverwrite);
|
|
68
|
-
}
|
|
69
|
-
if (key == 'template') {
|
|
70
|
-
_setAttributeValue(el, 'template_id', attrValues[key], isOverwrite);
|
|
71
|
-
}
|
|
72
|
-
if (key == 'template_id') {
|
|
73
|
-
_setAttributeValue(el, 'template', attrValues[key], isOverwrite);
|
|
74
|
-
}
|
|
57
|
+
for (const key of Object.keys(attrValues)) {
|
|
58
|
+
_setAttributeValue(el, key, attrValues[key], isOverwrite);
|
|
59
|
+
_setAttributeValue(el, `state-${key}`, attrValues[key], isOverwrite);
|
|
60
|
+
if (key == 'array' || key == 'object' || key == 'name') {
|
|
61
|
+
_setAttributeValue(el, `fetch-${key}`, attrValues[key], isOverwrite);
|
|
62
|
+
_setAttributeValue(el, `state-fetch-${key}`, attrValues[key], isOverwrite);
|
|
75
63
|
}
|
|
76
|
-
|
|
64
|
+
if (key == 'template') {
|
|
65
|
+
_setAttributeValue(el, 'template_id', attrValues[key], isOverwrite);
|
|
66
|
+
}
|
|
67
|
+
if (key == 'template_id') {
|
|
68
|
+
_setAttributeValue(el, 'template', attrValues[key], isOverwrite);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
77
71
|
}
|
|
78
72
|
|
|
79
73
|
async function _setAttributeValue(element, attribute, value, isOverwrite) {
|
|
@@ -103,18 +97,33 @@ async function stateAttributes(element) {
|
|
|
103
97
|
elements.push(...nestedElements)
|
|
104
98
|
}
|
|
105
99
|
|
|
100
|
+
let changeState = false
|
|
106
101
|
for (let i = 0; i < elements.length; i++) {
|
|
107
102
|
let attrValues = await _getAttributeValues(elements[i]);
|
|
103
|
+
if (attrValues.src === '$back' || attrValues.href === '$back') {
|
|
104
|
+
changeState = true
|
|
105
|
+
window.history.back();
|
|
106
|
+
break;
|
|
107
|
+
} else if (attrValues.src === '$forward' || attrValues.href === '$forward') {
|
|
108
|
+
changeState = true
|
|
109
|
+
window.history.forward();
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
108
113
|
let state_to = elements[i].getAttribute('state_to');
|
|
109
114
|
Object.assign(statedAttributes, { [`${state_to}`]: attrValues });
|
|
110
115
|
_getStateId(attrValues, state_to);
|
|
111
116
|
}
|
|
112
117
|
statedAttributes = JSON.stringify(statedAttributes)
|
|
113
|
-
|
|
114
|
-
|
|
118
|
+
let href = element.closest('href')
|
|
119
|
+
if (!changeState && !href) {
|
|
120
|
+
// TODO: Handle $title when adding statedAttributes
|
|
121
|
+
let title = element.getAttribute('title') || document.title || '';
|
|
122
|
+
history.pushState({ statedAttributes, title, url: location.href }, title, location.href);
|
|
115
123
|
}
|
|
116
124
|
|
|
117
|
-
|
|
125
|
+
if (!changeState)
|
|
126
|
+
localStorage.setItem('statedAttributes', statedAttributes);
|
|
118
127
|
|
|
119
128
|
document.dispatchEvent(new CustomEvent('stateEnd', {
|
|
120
129
|
detail: {}
|
|
@@ -147,6 +156,34 @@ function _getStateId(attrValues, state_to) {
|
|
|
147
156
|
_setAttributeValues(element, attrValues);
|
|
148
157
|
}
|
|
149
158
|
|
|
159
|
+
window.onload = function () {
|
|
160
|
+
if (!history.length) {
|
|
161
|
+
const statedAttributes = localStorage.getItem('statedAttributes') || '';
|
|
162
|
+
history.replaceState({ statedAttributes, url: location.href, title: document.title }, document.title, location.href);
|
|
163
|
+
} else if (!history.state || history.state.url !== location.href) {
|
|
164
|
+
const statedAttributes = localStorage.getItem('statedAttributes') || '';
|
|
165
|
+
history.pushState({ statedAttributes, url: location.href, title: document.title }, document.title, location.href);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
window.addEventListener('popstate', function (event) {
|
|
170
|
+
if (event.state) {
|
|
171
|
+
if (event.state.statedAttributes) {
|
|
172
|
+
localStorage.setItem('statedAttributes', event.state.statedAttributes);
|
|
173
|
+
let elements = document.querySelectorAll('[state_id]')
|
|
174
|
+
initElements(elements)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (event.state.url && event.state.url !== window.location.href) {
|
|
178
|
+
if (event.state.title)
|
|
179
|
+
sessionStorage.setItem('currentPageTitle', event.state.title); // Store title in session storage
|
|
180
|
+
|
|
181
|
+
location.href = event.state.url; // Navigate if the URL is different
|
|
182
|
+
} else if (event.state.title) {
|
|
183
|
+
document.title = event.state.title; // Update the title if provided
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
});
|
|
150
187
|
|
|
151
188
|
observer.init({
|
|
152
189
|
name: 'CoCreateState',
|