@stimulus-library/mixins 1.0.0-alpha.2 → 1.0.0-alpha.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/LICENSE +21 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/use_click_outside.js +1 -1
- package/dist/use_dirty_form_tracking.d.ts +10 -0
- package/dist/use_dirty_form_tracking.js +154 -0
- package/dist/use_event_bus.js +1 -1
- package/dist/use_event_listener.js +1 -1
- package/dist/use_fullscreen.js +1 -1
- package/dist/use_geolocation.js +1 -1
- package/dist/use_hover.js +1 -1
- package/dist/use_injected_html.js +1 -1
- package/dist/use_intersection.js +1 -1
- package/dist/use_interval.js +1 -1
- package/dist/use_mutation_observer.js +1 -1
- package/dist/use_resize_observer.js +1 -1
- package/dist/use_temporary_content.js +1 -1
- package/dist/use_timeout.js +1 -1
- package/package.json +17 -8
- /package/dist/{create_mixin.d.ts → use_mixin.d.ts} +0 -0
- /package/dist/{create_mixin.js → use_mixin.js} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Sub-Xaero
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isElementInViewport } from "@stimulus-library/utilities";
|
|
2
2
|
import { useEventListener } from "./use_event_listener";
|
|
3
|
-
import { useMixin } from "./
|
|
3
|
+
import { useMixin } from "./use_mixin";
|
|
4
4
|
export function useClickOutside(controller, element, callback) {
|
|
5
5
|
callback = callback.bind(controller);
|
|
6
6
|
const handler = (event) => {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus";
|
|
2
|
+
export declare function useDirtyInputTracking(controller: Controller, element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): {
|
|
3
|
+
restore: () => void;
|
|
4
|
+
teardown: () => void;
|
|
5
|
+
};
|
|
6
|
+
export declare function useDirtyFormTracking(controller: Controller, form: HTMLFormElement): {
|
|
7
|
+
restore: () => void;
|
|
8
|
+
teardown: () => void;
|
|
9
|
+
};
|
|
10
|
+
export declare function isDirty(element: HTMLElement): boolean;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { useMixin } from "./use_mixin";
|
|
2
|
+
import { getOtherRadiosInGroup, isElementCheckable, isHTMLInputElement, isHTMLSelectElement } from "@stimulus-library/utilities";
|
|
3
|
+
import { useEventListener } from "./use_event_listener";
|
|
4
|
+
const CACHE_ATTR_NAME = "data-detect-dirty-load-value";
|
|
5
|
+
export function useDirtyInputTracking(controller, element) {
|
|
6
|
+
let setup = () => {
|
|
7
|
+
cacheLoadValues(element);
|
|
8
|
+
checkDirty(element);
|
|
9
|
+
useEventListener(controller, element, ["input", "change"], () => checkDirty(element), { debounce: 10 });
|
|
10
|
+
};
|
|
11
|
+
let teardown = () => {
|
|
12
|
+
};
|
|
13
|
+
useMixin(controller, setup, teardown);
|
|
14
|
+
return {
|
|
15
|
+
restore: () => restoreElementFromLoadValue(element),
|
|
16
|
+
teardown,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function useDirtyFormTracking(controller, form) {
|
|
20
|
+
let teardowns = [];
|
|
21
|
+
let restores = [];
|
|
22
|
+
let setup = () => {
|
|
23
|
+
form.querySelectorAll("input, select, textarea").forEach((element) => {
|
|
24
|
+
let functions = useDirtyInputTracking(controller, element);
|
|
25
|
+
teardowns.push(functions.teardown);
|
|
26
|
+
restores.push(functions.restore);
|
|
27
|
+
});
|
|
28
|
+
useEventListener(controller, form, 'input-dirtied', () => {
|
|
29
|
+
console.log("input-dirtied");
|
|
30
|
+
form.setAttribute('data-dirty', 'true');
|
|
31
|
+
form.dispatchEvent(new CustomEvent('form-dirtied', {
|
|
32
|
+
bubbles: true,
|
|
33
|
+
cancelable: true,
|
|
34
|
+
detail: {
|
|
35
|
+
target: form,
|
|
36
|
+
},
|
|
37
|
+
}));
|
|
38
|
+
});
|
|
39
|
+
useEventListener(controller, form, 'input-cleaned', () => {
|
|
40
|
+
console.log("input-cleaned");
|
|
41
|
+
if (form.querySelectorAll('[data-dirty="true"]').length === 0) {
|
|
42
|
+
form.removeAttribute('data-dirty');
|
|
43
|
+
form.dispatchEvent(new CustomEvent('form-cleaned', {
|
|
44
|
+
bubbles: true,
|
|
45
|
+
cancelable: true,
|
|
46
|
+
detail: {
|
|
47
|
+
target: form,
|
|
48
|
+
},
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
let teardown = () => {
|
|
54
|
+
teardowns.forEach((teardown) => teardown());
|
|
55
|
+
};
|
|
56
|
+
let restore = () => {
|
|
57
|
+
restores.forEach((restore) => restore());
|
|
58
|
+
};
|
|
59
|
+
useMixin(controller, setup, teardown);
|
|
60
|
+
return {
|
|
61
|
+
restore,
|
|
62
|
+
teardown,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function getElementValue(element) {
|
|
66
|
+
return isElementCheckable(element) ? element.checked : element.value;
|
|
67
|
+
}
|
|
68
|
+
function getElementLoadValue(element) {
|
|
69
|
+
let value = element.getAttribute(CACHE_ATTR_NAME);
|
|
70
|
+
if (isElementCheckable(element)) {
|
|
71
|
+
return value == null ? element.defaultChecked : value == "true";
|
|
72
|
+
}
|
|
73
|
+
else if (isHTMLSelectElement(element)) {
|
|
74
|
+
let options = Array.from(element.options);
|
|
75
|
+
options.forEach((option) => {
|
|
76
|
+
if (option.defaultSelected) {
|
|
77
|
+
element.value = option.value;
|
|
78
|
+
return option.value;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else if (value !== null) {
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
function elementHasCachedLoadValue(element) {
|
|
88
|
+
return element.hasAttribute(CACHE_ATTR_NAME);
|
|
89
|
+
}
|
|
90
|
+
function checkDirty(element) {
|
|
91
|
+
if (isHTMLInputElement(element) && element.type == "radio") {
|
|
92
|
+
getOtherRadiosInGroup(element).forEach((radio) => radio.removeAttribute('data-dirty'));
|
|
93
|
+
}
|
|
94
|
+
if (isElementDirty(element)) {
|
|
95
|
+
element.setAttribute('data-dirty', "true");
|
|
96
|
+
element.form?.setAttribute('data-dirty', "true");
|
|
97
|
+
console.log("input-dirtied event");
|
|
98
|
+
element.dispatchEvent(new CustomEvent('input-dirtied', {
|
|
99
|
+
bubbles: true,
|
|
100
|
+
cancelable: true,
|
|
101
|
+
detail: {
|
|
102
|
+
target: element,
|
|
103
|
+
},
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
element.removeAttribute('data-dirty');
|
|
108
|
+
element.dispatchEvent(new CustomEvent('input-cleaned', {
|
|
109
|
+
bubbles: true,
|
|
110
|
+
cancelable: true,
|
|
111
|
+
detail: {
|
|
112
|
+
target: element,
|
|
113
|
+
},
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function isElementDirty(element) {
|
|
118
|
+
return getElementValue(element) !== getElementLoadValue(element);
|
|
119
|
+
}
|
|
120
|
+
function restoreElementFromLoadValue(element) {
|
|
121
|
+
let cacheValue = element.getAttribute(CACHE_ATTR_NAME);
|
|
122
|
+
if (isElementCheckable(element)) {
|
|
123
|
+
element.setAttribute(CACHE_ATTR_NAME, element.checked.toString());
|
|
124
|
+
element.checked = cacheValue == null ? element.defaultChecked : cacheValue == "true";
|
|
125
|
+
}
|
|
126
|
+
else if (isHTMLSelectElement(element)) {
|
|
127
|
+
if (cacheValue == null) {
|
|
128
|
+
let options = Array.from(element.options);
|
|
129
|
+
options.forEach((option) => {
|
|
130
|
+
if (option.defaultSelected) {
|
|
131
|
+
element.value = option.value;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
element.value = cacheValue;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
element.value = cacheValue == null ? element.defaultValue : cacheValue;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function cacheLoadValues(element) {
|
|
145
|
+
if (!elementHasCachedLoadValue(element) && isElementCheckable(element)) {
|
|
146
|
+
element.setAttribute(CACHE_ATTR_NAME, element.checked.toString());
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
element.setAttribute(CACHE_ATTR_NAME, element.value.toString());
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function isDirty(element) {
|
|
153
|
+
return element.hasAttribute("data-dirty");
|
|
154
|
+
}
|
package/dist/use_event_bus.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { debounce, EventBus, wrapArray } from "@stimulus-library/utilities";
|
|
2
|
-
import { useMixin } from "./
|
|
2
|
+
import { useMixin } from "./use_mixin";
|
|
3
3
|
export function useEventBus(controller, eventNameOrNames, handler, opts) {
|
|
4
4
|
let options = opts;
|
|
5
5
|
if (options?.debounce) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { debounce, wrapArray } from "@stimulus-library/utilities";
|
|
2
|
-
import { useMixin } from "./
|
|
2
|
+
import { useMixin } from "./use_mixin";
|
|
3
3
|
export function useEventListener(controller, element, eventNameOrNames, handler, opts) {
|
|
4
4
|
if (opts?.debounce) {
|
|
5
5
|
handler = debounce(handler.bind(controller), opts.debounce);
|
package/dist/use_fullscreen.js
CHANGED
package/dist/use_geolocation.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { reactive } from "@stimulus-library/utilities";
|
|
2
|
-
import { useMixin } from "./
|
|
2
|
+
import { useMixin } from "./use_mixin";
|
|
3
3
|
export function useGeolocation(controller, options = {}, update, error) {
|
|
4
4
|
// Ensure passed functions are bound to the correct controller scope
|
|
5
5
|
if (update) {
|
package/dist/use_hover.js
CHANGED
package/dist/use_intersection.js
CHANGED
package/dist/use_interval.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useMixin } from "./
|
|
1
|
+
import { useMixin } from "./use_mixin";
|
|
2
2
|
import { useTimeout } from "./use_timeout";
|
|
3
3
|
import { isHTMLInputElement } from "@stimulus-library/utilities";
|
|
4
4
|
export function useTemporaryContent(controller, target, content, timeout, teardownCallback) {
|
package/dist/use_timeout.js
CHANGED
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"ruby on rails",
|
|
10
10
|
"ruby-on-rails"
|
|
11
11
|
],
|
|
12
|
-
"version": "1.0.0-alpha.
|
|
12
|
+
"version": "1.0.0-alpha.3",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": {
|
|
15
15
|
"name": "Sub-Xaero",
|
|
@@ -30,21 +30,30 @@
|
|
|
30
30
|
"dev": "tsc --watch",
|
|
31
31
|
"prepack": "npm run build",
|
|
32
32
|
"release": "standard-version",
|
|
33
|
-
"test": "
|
|
33
|
+
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha --parallel -r ts-node/register 'test/**/*.test.ts' -R dot",
|
|
34
34
|
"test:treeshake": "agadoo dist"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@hotwired/stimulus": "^3.0.0",
|
|
38
|
-
"@stimulus-library/utilities": "^1.0.0-alpha.
|
|
38
|
+
"@stimulus-library/utilities": "^1.0.0-alpha.3"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
+
"@types/chai": "^4.3.5",
|
|
42
|
+
"@types/mocha": "^10.0.1",
|
|
43
|
+
"@types/sinon": "^10.0.15",
|
|
44
|
+
"@types/sinon-chai": "^3.2.9",
|
|
41
45
|
"agadoo": "^3.0.0",
|
|
42
|
-
"
|
|
46
|
+
"chai": "^4.3.7",
|
|
43
47
|
"fast-glob": "^3.2.12",
|
|
44
|
-
"lerna": "^
|
|
45
|
-
"
|
|
48
|
+
"lerna": "^7.0.0",
|
|
49
|
+
"mocha": "^10.2.0",
|
|
50
|
+
"rimraf": "^5.0.1",
|
|
51
|
+
"sinon": "^15.1.0",
|
|
52
|
+
"sinon-chai": "^3.7.0",
|
|
46
53
|
"standard-version": "^9.5.0",
|
|
47
|
-
"
|
|
54
|
+
"ts-node": "^10.9.1",
|
|
55
|
+
"typescript": "^5.1.3",
|
|
48
56
|
"vite": "^4.1.1"
|
|
49
|
-
}
|
|
57
|
+
},
|
|
58
|
+
"gitHead": "ab34837bf603c7fce42f3d1e5b881607284568ef"
|
|
50
59
|
}
|
|
File without changes
|
|
File without changes
|