@pechynho/stimulus-typescript 0.0.9 → 0.0.11
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/dist/utils.d.ts +2 -0
- package/dist/utils.js +45 -1
- package/package.json +1 -1
- package/src/utils.ts +62 -1
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ActionEvent, Application, Controller } from "@hotwired/stimulus";
|
|
2
2
|
export declare const camelCase: (value: string) => string;
|
|
3
3
|
export declare const capitalize: (value: string) => string;
|
|
4
|
+
export declare function addStimulusAction(element: HTMLElement, identifier: string, method: string, event?: string, params?: Record<string, object | string | number | boolean>): void;
|
|
5
|
+
export declare function removeStimulusAction(element: HTMLElement, identifier: string, method: string, event?: string, removeParams?: boolean): void;
|
|
4
6
|
export declare const isActionEvent: (value: any) => value is ActionEvent;
|
|
5
7
|
export declare const getController: <T extends Controller>(app: Application, element: HTMLElement, identifier: string) => T | null;
|
|
6
8
|
export declare const getControllerAsync: <T extends Controller>(app: Application, element: HTMLElement, identifier: string, timeout?: number, poll?: number) => Promise<T | null>;
|
package/dist/utils.js
CHANGED
|
@@ -6,8 +6,52 @@ export const camelCase = (value) => {
|
|
|
6
6
|
export const capitalize = (value) => {
|
|
7
7
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
8
8
|
};
|
|
9
|
+
export function addStimulusAction(element, identifier, method, event, params) {
|
|
10
|
+
const existing = element.dataset.action;
|
|
11
|
+
const actions = existing !== undefined && existing.trim() !== ''
|
|
12
|
+
? existing.trim().split(/\s+/)
|
|
13
|
+
: [];
|
|
14
|
+
const target = `${identifier}#${method}`;
|
|
15
|
+
const descriptor = event !== undefined ? `${event}->${target}` : target;
|
|
16
|
+
if (!actions.includes(descriptor)) {
|
|
17
|
+
actions.push(descriptor);
|
|
18
|
+
}
|
|
19
|
+
element.dataset.action = actions.join(' ');
|
|
20
|
+
if (params !== undefined) {
|
|
21
|
+
for (const [key, value] of Object.entries(params)) {
|
|
22
|
+
const serialized = typeof value === 'object'
|
|
23
|
+
? JSON.stringify(value)
|
|
24
|
+
: String(value);
|
|
25
|
+
element.dataset[`${camelCase(identifier)}${capitalize(camelCase(key))}Param`] = String(serialized);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function removeStimulusAction(element, identifier, method, event, removeParams = false) {
|
|
30
|
+
const existing = element.dataset.action;
|
|
31
|
+
if (existing === undefined || existing.trim() === '') {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const target = `${identifier}#${method}`;
|
|
35
|
+
const descriptor = event !== undefined ? `${event}->${target}` : target;
|
|
36
|
+
const actions = existing.trim().split(/\s+/).filter((a) => a !== descriptor);
|
|
37
|
+
if (actions.length > 0) {
|
|
38
|
+
element.dataset.action = actions.join(' ');
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
delete element.dataset.action;
|
|
42
|
+
}
|
|
43
|
+
if (removeParams) {
|
|
44
|
+
const prefix = `${camelCase(identifier)}`;
|
|
45
|
+
const suffix = 'Param';
|
|
46
|
+
for (const key of Object.keys(element.dataset)) {
|
|
47
|
+
if (key.startsWith(prefix) && key.endsWith(suffix)) {
|
|
48
|
+
delete element.dataset[key];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
9
53
|
export const isActionEvent = (value) => {
|
|
10
|
-
return value instanceof Event && 'params' in value && typeof value.params
|
|
54
|
+
return value instanceof Event && 'params' in value && typeof value.params === 'object';
|
|
11
55
|
};
|
|
12
56
|
export const getController = (app, element, identifier) => {
|
|
13
57
|
return app.getControllerForElementAndIdentifier(element, identifier);
|
package/package.json
CHANGED
package/src/utils.ts
CHANGED
|
@@ -10,8 +10,69 @@ export const capitalize = (value: string): string => {
|
|
|
10
10
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
export function addStimulusAction(
|
|
14
|
+
element: HTMLElement,
|
|
15
|
+
identifier: string,
|
|
16
|
+
method: string,
|
|
17
|
+
event?: string,
|
|
18
|
+
params?: Record<string, object | string | number | boolean>,
|
|
19
|
+
): void {
|
|
20
|
+
const existing = element.dataset.action;
|
|
21
|
+
const actions = existing !== undefined && existing.trim() !== ''
|
|
22
|
+
? existing.trim().split(/\s+/)
|
|
23
|
+
: [];
|
|
24
|
+
const target = `${identifier}#${method}`;
|
|
25
|
+
const descriptor = event !== undefined ? `${event}->${target}` : target;
|
|
26
|
+
if (!actions.includes(descriptor)) {
|
|
27
|
+
actions.push(descriptor);
|
|
28
|
+
}
|
|
29
|
+
element.dataset.action = actions.join(' ');
|
|
30
|
+
if (params !== undefined) {
|
|
31
|
+
for (const [key, value] of Object.entries(params)) {
|
|
32
|
+
const serialized = typeof value === 'object'
|
|
33
|
+
? JSON.stringify(value)
|
|
34
|
+
: String(value);
|
|
35
|
+
element.dataset[`${camelCase(identifier)}${capitalize(camelCase(key))}Param`] = String(serialized);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function removeStimulusAction(
|
|
41
|
+
element: HTMLElement,
|
|
42
|
+
identifier: string,
|
|
43
|
+
method: string,
|
|
44
|
+
event?: string,
|
|
45
|
+
removeParams: boolean = false,
|
|
46
|
+
): void {
|
|
47
|
+
const existing = element.dataset.action;
|
|
48
|
+
if (existing === undefined || existing.trim() === '') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const target = `${identifier}#${method}`;
|
|
53
|
+
const descriptor = event !== undefined ? `${event}->${target}` : target;
|
|
54
|
+
const actions = existing.trim().split(/\s+/).filter((a) => a !== descriptor);
|
|
55
|
+
|
|
56
|
+
if (actions.length > 0) {
|
|
57
|
+
element.dataset.action = actions.join(' ');
|
|
58
|
+
} else {
|
|
59
|
+
delete element.dataset.action;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (removeParams) {
|
|
63
|
+
const prefix = `${camelCase(identifier)}`;
|
|
64
|
+
const suffix = 'Param';
|
|
65
|
+
|
|
66
|
+
for (const key of Object.keys(element.dataset)) {
|
|
67
|
+
if (key.startsWith(prefix) && key.endsWith(suffix)) {
|
|
68
|
+
delete element.dataset[key];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
13
74
|
export const isActionEvent = (value: any): value is ActionEvent => {
|
|
14
|
-
return value instanceof Event && 'params' in value && typeof value.params
|
|
75
|
+
return value instanceof Event && 'params' in value && typeof value.params === 'object';
|
|
15
76
|
}
|
|
16
77
|
|
|
17
78
|
export const getController = <T extends Controller>(app: Application, element: HTMLElement, identifier: string): T | null => {
|