jails.stdlib 1.1.0 โ 2.0.0
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/channel/README.md +97 -0
- package/channel/index.d.ts +9 -0
- package/channel/index.js +30 -0
- package/channel/index.ts +39 -0
- package/channel/index.umd.js +1 -0
- package/form-validation/index.js +97 -89
- package/form-validation/index.ts +9 -11
- package/form-validation/index.umd.js +1 -1
- package/form-validation/readme.md +52 -27
- package/package.json +1 -1
- package/messenger/README.md +0 -109
- package/messenger/index.d.ts +0 -9
- package/messenger/index.js +0 -39
- package/messenger/index.ts +0 -38
- package/messenger/index.umd.js +0 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# channel
|
|
2
|
+
|
|
3
|
+
```ts
|
|
4
|
+
Channel({
|
|
5
|
+
target?: Window | HTMLIFrameElement,
|
|
6
|
+
accept?: string[],
|
|
7
|
+
origin?: string
|
|
8
|
+
})
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# ๐ฌ Channel API Documentation
|
|
13
|
+
|
|
14
|
+
The `channel` utility provides a lightweight, secure interface for sending and receiving messages between windows or iframes using `postMessage`.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## โ
Features
|
|
19
|
+
|
|
20
|
+
* Easy dispatch and subscription to custom message actions.
|
|
21
|
+
* Secure origin validation.
|
|
22
|
+
* Works with iframes or separate window contexts.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ๐งช Usage
|
|
27
|
+
|
|
28
|
+
### Parent Page
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { Channel } from 'jails.stdlib/channel'
|
|
32
|
+
|
|
33
|
+
const channel = Channel({
|
|
34
|
+
target: document.getElementById('myIframe'), // <iframe id="myIframe" />
|
|
35
|
+
accept: ['*'] // Allowed origins
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// Send message to iframe
|
|
39
|
+
channel.send('hello', { user: 'Alice' })
|
|
40
|
+
|
|
41
|
+
// Add more listeners later
|
|
42
|
+
channel.on('hi', (data) => {
|
|
43
|
+
console.log('Iframe said hi!', data)
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Iframe Page
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const channel = Channel({
|
|
51
|
+
target: window.parent,
|
|
52
|
+
accept: ['*']
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## ๐งน API
|
|
59
|
+
|
|
60
|
+
### `Channel(options)`
|
|
61
|
+
|
|
62
|
+
Creates a new Channel instance.
|
|
63
|
+
|
|
64
|
+
#### Options:
|
|
65
|
+
|
|
66
|
+
| Option | Type | Description |
|
|
67
|
+
| --------- | ----------------------------- | --------------------------------------- |
|
|
68
|
+
| `target` | `Window \| HTMLIFrameElement` | Target to send messages to |
|
|
69
|
+
| `accept` | `string[]` | Allowed origins (`['*']` to accept all) |
|
|
70
|
+
| `origin` | `string` | The origin used when sending messages, `default`: location.origin |
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
### `.emit(event: string, payload?: any)`
|
|
75
|
+
|
|
76
|
+
Sends a message to the target window.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
channel.emit('say:hi', { name: 'Bob' })
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
### `.on(event: string, callback: function )`
|
|
85
|
+
|
|
86
|
+
Listen to an event from the target window.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
channel.on('logout', () => console.log('Logging out...'))
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## ๐ Security Notes
|
|
95
|
+
|
|
96
|
+
* Always **specify allowed origins** in `accept` to avoid cross-site scripting risks.
|
|
97
|
+
* Avoid using `'*'` unless absolutely necessary (e.g., during development).
|
package/channel/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const a = ({
|
|
2
|
+
target: o = null,
|
|
3
|
+
accept: i = [],
|
|
4
|
+
origin: d = location.origin
|
|
5
|
+
}) => {
|
|
6
|
+
const t = (o == null ? void 0 : o.contentWindow) || o, s = {};
|
|
7
|
+
return window.addEventListener("message", (n) => {
|
|
8
|
+
if (i.includes("*") || i.includes(n.origin)) {
|
|
9
|
+
const { payload: e, event: l } = n.data;
|
|
10
|
+
s[l] && s[l].apply(null, e);
|
|
11
|
+
} else
|
|
12
|
+
throw {
|
|
13
|
+
type: "ACCESS DENIED",
|
|
14
|
+
message: "Cant receive message from: " + n.origin
|
|
15
|
+
};
|
|
16
|
+
}), {
|
|
17
|
+
on(n, e) {
|
|
18
|
+
s[n] = e;
|
|
19
|
+
},
|
|
20
|
+
emit(n, ...e) {
|
|
21
|
+
t.postMessage({ event: n, payload: e }, d);
|
|
22
|
+
},
|
|
23
|
+
remove(n) {
|
|
24
|
+
delete s[n];
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export {
|
|
29
|
+
a as Channel
|
|
30
|
+
};
|
package/channel/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
|
|
2
|
+
export const Channel = ({
|
|
3
|
+
target = null as any,
|
|
4
|
+
accept = [] as any,
|
|
5
|
+
origin = location.origin
|
|
6
|
+
}) => {
|
|
7
|
+
|
|
8
|
+
const win = target?.contentWindow || target
|
|
9
|
+
const events = {}
|
|
10
|
+
|
|
11
|
+
window.addEventListener('message', (e) => {
|
|
12
|
+
if( accept.includes('*') || accept.includes(e.origin) ) {
|
|
13
|
+
const { payload, event } = e.data
|
|
14
|
+
if( events[event] ) {
|
|
15
|
+
events[event].apply(null, payload)
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
throw {
|
|
19
|
+
type : 'ACCESS DENIED',
|
|
20
|
+
message : 'Cant receive message from: ' + e.origin
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
|
|
27
|
+
on(name, callback) {
|
|
28
|
+
events[name] = callback
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
emit(event, ...payload) {
|
|
32
|
+
win.postMessage({ event, payload }, origin)
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
remove(name) {
|
|
36
|
+
delete events[name]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(n,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(n=typeof globalThis!="undefined"?globalThis:n||self,i(n.channel={}))})(this,(function(n){"use strict";const i=({target:s=null,accept:d=[],origin:f=location.origin})=>{const u=(s==null?void 0:s.contentWindow)||s,t={};return window.addEventListener("message",e=>{if(d.includes("*")||d.includes(e.origin)){const{payload:o,event:l}=e.data;t[l]&&t[l].apply(null,o)}else throw{type:"ACCESS DENIED",message:"Cant receive message from: "+e.origin}}),{on(e,o){t[e]=o},emit(e,...o){u.postMessage({event:e,payload:o},f)},remove(e){delete t[e]}}};n.Channel=i,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/form-validation/index.js
CHANGED
|
@@ -1,96 +1,104 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var A = (
|
|
1
|
+
var C = Object.defineProperty, G = Object.defineProperties;
|
|
2
|
+
var H = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var h = Object.getOwnPropertySymbols;
|
|
4
|
+
var L = Object.prototype.hasOwnProperty, M = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var A = (t, r, e) => r in t ? C(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, v = (t, r) => {
|
|
6
6
|
for (var e in r || (r = {}))
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var e of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
L.call(r, e) && A(t, e, r[e]);
|
|
8
|
+
if (h)
|
|
9
|
+
for (var e of h(r))
|
|
10
|
+
M.call(r, e) && A(t, e, r[e]);
|
|
11
|
+
return t;
|
|
12
|
+
}, w = (t, r) => G(t, H(r));
|
|
13
|
+
var y = (t, r) => {
|
|
14
|
+
var e = {};
|
|
15
|
+
for (var a in t)
|
|
16
|
+
L.call(t, a) && r.indexOf(a) < 0 && (e[a] = t[a]);
|
|
17
|
+
if (t != null && h)
|
|
18
|
+
for (var a of h(t))
|
|
19
|
+
r.indexOf(a) < 0 && M.call(t, a) && (e[a] = t[a]);
|
|
20
|
+
return e;
|
|
21
|
+
};
|
|
22
|
+
const S = "form-validation", p = "[data-validation]", J = "[data-mask]";
|
|
23
|
+
function W({
|
|
24
|
+
main: t,
|
|
16
25
|
elm: r,
|
|
17
26
|
state: e,
|
|
18
|
-
on:
|
|
19
|
-
emit:
|
|
20
|
-
dependencies:
|
|
21
|
-
trigger:
|
|
27
|
+
on: a,
|
|
28
|
+
emit: g,
|
|
29
|
+
dependencies: N,
|
|
30
|
+
trigger: O
|
|
22
31
|
}) {
|
|
23
|
-
var
|
|
24
|
-
const
|
|
25
|
-
let
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}), r.setValues = (
|
|
29
|
-
e.set((
|
|
32
|
+
var V;
|
|
33
|
+
const u = y(N, []), f = (V = r.querySelector("input,select,textarea")) == null ? void 0 : V.form;
|
|
34
|
+
let m = I(f);
|
|
35
|
+
t((s) => {
|
|
36
|
+
a("input", "input, textarea, select", j), a("input", J, K), a("input", p, b("input")), a("change", p, b("change")), a("blur", p, b("blur")), a("focus", "input, textarea, select", _), a("blur", "input, textarea, select", $), f.addEventListener("reset", T), f.addEventListener("submit", q), z();
|
|
37
|
+
}), r.setValues = (s) => {
|
|
38
|
+
e.set((o) => o.form.values = v(v({}, o.form.values), s));
|
|
30
39
|
};
|
|
31
|
-
const
|
|
32
|
-
if (!
|
|
40
|
+
const z = () => {
|
|
41
|
+
if (!u)
|
|
33
42
|
throw new Error(
|
|
34
|
-
"<form-validation> - No
|
|
43
|
+
"<form-validation> - No entities provided in dependencies"
|
|
35
44
|
);
|
|
36
|
-
const
|
|
37
|
-
e.set((
|
|
38
|
-
},
|
|
39
|
-
const
|
|
40
|
-
return
|
|
41
|
-
},
|
|
42
|
-
const
|
|
43
|
-
e.set((
|
|
44
|
-
|
|
45
|
+
const s = E();
|
|
46
|
+
e.set((o) => o.form.values = s);
|
|
47
|
+
}, E = () => {
|
|
48
|
+
const s = {};
|
|
49
|
+
return m.forEach((o) => s[o] = ""), s;
|
|
50
|
+
}, _ = (s) => {
|
|
51
|
+
const o = s.target.name;
|
|
52
|
+
e.set((n) => {
|
|
53
|
+
n.form.touched[o] = !0, n.form.focused[o] = !0;
|
|
45
54
|
});
|
|
46
|
-
},
|
|
47
|
-
const
|
|
48
|
-
e.set((
|
|
49
|
-
|
|
55
|
+
}, $ = (s) => {
|
|
56
|
+
const o = s.target.name;
|
|
57
|
+
e.set((n) => {
|
|
58
|
+
n.form.focused[o] = !1;
|
|
50
59
|
});
|
|
51
|
-
},
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
q || d.push(K);
|
|
60
|
+
}, b = (s) => (o) => {
|
|
61
|
+
const n = o.target, c = n.name, l = F(n, f), k = n.dataset.validation.split(/\s/), d = [], x = e.get();
|
|
62
|
+
k.forEach((i) => {
|
|
63
|
+
if (i in u) {
|
|
64
|
+
const D = u[i];
|
|
65
|
+
if (!D.validate(l, n, f)) {
|
|
66
|
+
const B = D.message(l, n, f);
|
|
67
|
+
d.push(B);
|
|
68
|
+
}
|
|
61
69
|
}
|
|
62
|
-
}), d.length ?
|
|
63
|
-
|
|
64
|
-
})) : (
|
|
65
|
-
|
|
66
|
-
})) : (
|
|
67
|
-
delete
|
|
70
|
+
}), d.length ? s === "input" ? (m.add(n.name), e.set((i) => {
|
|
71
|
+
i.form.isValid = !1, x.form.errors[c] && d[0] != x.form.errors[c] && (i.form.errors[c] = d[0]);
|
|
72
|
+
})) : (s === "blur" || s === "change") && (m.add(n.name), e.set((i) => {
|
|
73
|
+
i.form.errors[c] = d[0], i.form.isValid = !1;
|
|
74
|
+
})) : (m.delete(n.name), e.set((i) => {
|
|
75
|
+
delete i.form.errors[c], m.size || (i.form.isValid = !0);
|
|
68
76
|
}));
|
|
69
|
-
},
|
|
70
|
-
const { name:
|
|
71
|
-
e.set((
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
if (Object.keys(
|
|
76
|
-
|
|
77
|
+
}, j = (s) => {
|
|
78
|
+
const { name: o } = s.target, n = F(s.target, f);
|
|
79
|
+
e.set((c) => c.form.values[o] = n);
|
|
80
|
+
}, q = (s) => {
|
|
81
|
+
s.preventDefault(), O("blur", p);
|
|
82
|
+
const n = e.get().form.errors;
|
|
83
|
+
if (Object.keys(n).length)
|
|
84
|
+
g(`${S}:error`, { errors: n });
|
|
77
85
|
else {
|
|
78
|
-
const
|
|
79
|
-
|
|
86
|
+
const l = Q(s.target);
|
|
87
|
+
g(`${S}:submit`, v({}, l));
|
|
80
88
|
}
|
|
81
|
-
},
|
|
82
|
-
let
|
|
83
|
-
const { mask:
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
const
|
|
87
|
-
|
|
89
|
+
}, K = (s) => {
|
|
90
|
+
let o = s.target.value;
|
|
91
|
+
const { mask: n } = s.target.dataset;
|
|
92
|
+
n.split(/s/).forEach((l) => {
|
|
93
|
+
if (u[l] && u[l].mask) {
|
|
94
|
+
const k = u[l].mask;
|
|
95
|
+
o = k(o, s.target, s.target.form);
|
|
88
96
|
}
|
|
89
|
-
}), e.set((
|
|
90
|
-
},
|
|
91
|
-
|
|
92
|
-
form:
|
|
93
|
-
values:
|
|
97
|
+
}), e.set((l) => l.form.values[s.target.name] = o || "");
|
|
98
|
+
}, T = () => {
|
|
99
|
+
m = I(f), e.set({
|
|
100
|
+
form: w(v({}, P.form), {
|
|
101
|
+
values: E()
|
|
94
102
|
})
|
|
95
103
|
});
|
|
96
104
|
};
|
|
@@ -103,19 +111,19 @@ const P = {
|
|
|
103
111
|
isValid: !1,
|
|
104
112
|
focused: {}
|
|
105
113
|
}
|
|
106
|
-
}, Q = (
|
|
107
|
-
const r = new FormData(
|
|
108
|
-
for (let [
|
|
109
|
-
e[
|
|
114
|
+
}, Q = (t) => {
|
|
115
|
+
const r = new FormData(t), e = {};
|
|
116
|
+
for (let [a, g] of r)
|
|
117
|
+
e[a] = g;
|
|
110
118
|
return { formData: r, data: e };
|
|
111
|
-
},
|
|
112
|
-
const { name: e, type:
|
|
113
|
-
return
|
|
114
|
-
},
|
|
119
|
+
}, F = (t, r) => {
|
|
120
|
+
const { name: e, type: a } = t;
|
|
121
|
+
return a == "checkbox" ? t.checked ? t.value : "" : r[e].value;
|
|
122
|
+
}, I = (t) => {
|
|
115
123
|
const r = /* @__PURE__ */ new Set();
|
|
116
|
-
return Array.from(
|
|
124
|
+
return Array.from(t.elements).filter((e) => e.name && e.dataset.validation).forEach((e) => r.add(e.name)), r;
|
|
117
125
|
};
|
|
118
126
|
export {
|
|
119
|
-
|
|
127
|
+
W as default,
|
|
120
128
|
P as model
|
|
121
129
|
};
|
package/form-validation/index.ts
CHANGED
|
@@ -12,7 +12,7 @@ export default function formValidation({
|
|
|
12
12
|
trigger,
|
|
13
13
|
}) {
|
|
14
14
|
//
|
|
15
|
-
const {
|
|
15
|
+
const { ...entities } = dependencies
|
|
16
16
|
const form = elm.querySelector('input,select,textarea')?.form
|
|
17
17
|
let fields = getFields(form)
|
|
18
18
|
|
|
@@ -36,9 +36,9 @@ export default function formValidation({
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const initialValues = () => {
|
|
39
|
-
if (!
|
|
39
|
+
if (!entities) {
|
|
40
40
|
throw new Error(
|
|
41
|
-
'<form-validation> - No
|
|
41
|
+
'<form-validation> - No entities provided in dependencies'
|
|
42
42
|
)
|
|
43
43
|
}
|
|
44
44
|
const fields = getInitialValues()
|
|
@@ -75,13 +75,11 @@ export default function formValidation({
|
|
|
75
75
|
const currentState = state.get()
|
|
76
76
|
|
|
77
77
|
validationList.forEach((validation) => {
|
|
78
|
-
if (validation in
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
input,
|
|
82
|
-
form
|
|
83
|
-
)
|
|
78
|
+
if (validation in entities) {
|
|
79
|
+
const entity = entities[validation]
|
|
80
|
+
const ok = entity.validate( value, input, form )
|
|
84
81
|
if (!ok) {
|
|
82
|
+
const message = entity.message( value, input, form)
|
|
85
83
|
errorsList.push(message)
|
|
86
84
|
}
|
|
87
85
|
}
|
|
@@ -145,8 +143,8 @@ export default function formValidation({
|
|
|
145
143
|
const allMasks = mask.split(/s/)
|
|
146
144
|
|
|
147
145
|
allMasks.forEach((mask) => {
|
|
148
|
-
if (mask && mask
|
|
149
|
-
const fn =
|
|
146
|
+
if (entities[mask] && entities[mask].mask) {
|
|
147
|
+
const fn = entities[mask].mask
|
|
150
148
|
value = fn(value, e.target, e.target.form)
|
|
151
149
|
}
|
|
152
150
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(t,s){typeof exports=="object"&&typeof module!="undefined"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(t=typeof globalThis!="undefined"?globalThis:t||self,s(t["form-validation"]={}))})(this,(function(t){"use strict";var Q=Object.defineProperty,R=Object.defineProperties;var U=Object.getOwnPropertyDescriptors;var E=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;var O=(t,s,n)=>s in t?Q(t,s,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[s]=n,b=(t,s)=>{for(var n in s||(s={}))T.call(s,n)&&O(t,n,s[n]);if(E)for(var n of E(s))_.call(s,n)&&O(t,n,s[n]);return t},j=(t,s)=>R(t,U(s));var F=(t,s)=>{var n={};for(var i in t)T.call(t,i)&&s.indexOf(i)<0&&(n[i]=t[i]);if(t!=null&&E)for(var i of E(t))s.indexOf(i)<0&&_.call(t,i)&&(n[i]=t[i]);return n};const s="form-validation",n="[data-validation]",i="[data-mask]";function I({main:d,elm:f,state:o,on:l,emit:k,dependencies:z,trigger:$}){var A;const g=F(z,[]),v=(A=f.querySelector("input,select,textarea"))==null?void 0:A.form;let p=D(v);d(e=>{l("input","input, textarea, select",B),l("input",i,G),l("input",n,V("input")),l("change",n,V("change")),l("blur",n,V("blur")),l("focus","input, textarea, select",K),l("blur","input, textarea, select",P),v.addEventListener("reset",H),v.addEventListener("submit",C),q()}),f.setValues=e=>{o.set(r=>r.form.values=b(b({},r.form.values),e))};const q=()=>{if(!g)throw new Error("<form-validation> - No entities provided in dependencies");const e=S();o.set(r=>r.form.values=e)},S=()=>{const e={};return p.forEach(r=>e[r]=""),e},K=e=>{const r=e.target.name;o.set(a=>{a.form.touched[r]=!0,a.form.focused[r]=!0})},P=e=>{const r=e.target.name;o.set(a=>{a.form.focused[r]=!1})},V=e=>r=>{const a=r.target,m=a.name,u=x(a,v),y=a.dataset.validation.split(/\s/),h=[],L=o.get();y.forEach(c=>{if(c in g){const w=g[c];if(!w.validate(u,a,v)){const J=w.message(u,a,v);h.push(J)}}}),h.length?e==="input"?(p.add(a.name),o.set(c=>{c.form.isValid=!1,L.form.errors[m]&&h[0]!=L.form.errors[m]&&(c.form.errors[m]=h[0])})):(e==="blur"||e==="change")&&(p.add(a.name),o.set(c=>{c.form.errors[m]=h[0],c.form.isValid=!1})):(p.delete(a.name),o.set(c=>{delete c.form.errors[m],p.size||(c.form.isValid=!0)}))},B=e=>{const{name:r}=e.target,a=x(e.target,v);o.set(m=>m.form.values[r]=a)},C=e=>{e.preventDefault(),$("blur",n);const a=o.get().form.errors;if(Object.keys(a).length)k(`${s}:error`,{errors:a});else{const u=N(e.target);k(`${s}:submit`,b({},u))}},G=e=>{let r=e.target.value;const{mask:a}=e.target.dataset;a.split(/s/).forEach(u=>{if(g[u]&&g[u].mask){const y=g[u].mask;r=y(r,e.target,e.target.form)}}),o.set(u=>u.form.values[e.target.name]=r||"")},H=()=>{p=D(v),o.set({form:j(b({},M.form),{values:S()})})}}const M={form:{errors:{},values:{},touched:{},isValid:!1,focused:{}}},N=d=>{const f=new FormData(d),o={};for(let[l,k]of f)o[l]=k;return{formData:f,data:o}},x=(d,f)=>{const{name:o,type:l}=d;return l=="checkbox"?d.checked?d.value:"":f[o].value},D=d=>{const f=new Set;return Array.from(d.elements).filter(o=>o.name&&o.dataset.validation).forEach(o=>f.add(o.name)),f};t.default=I,t.model=M,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
|
|
@@ -44,9 +44,9 @@ on('form-validation:error', ({ errors }) => {})
|
|
|
44
44
|
##### main.ts
|
|
45
45
|
```ts
|
|
46
46
|
import * as formValidation from 'jails.stdlib/form-validation'
|
|
47
|
-
import
|
|
47
|
+
import * as entities from './my-entities'
|
|
48
48
|
|
|
49
|
-
jails.register('form-validation', formValidation, { ...
|
|
49
|
+
jails.register('form-validation', formValidation, { ...entities })
|
|
50
50
|
jails.start()
|
|
51
51
|
```
|
|
52
52
|
|
|
@@ -84,37 +84,62 @@ jails.start()
|
|
|
84
84
|
```
|
|
85
85
|
|
|
86
86
|
##### my-custom-rules/index.ts
|
|
87
|
+
You need to provide a map of entities that will be used to validate the kind of your form fields.
|
|
88
|
+
They all need to implement: `validate()`, `message()`. Optional: `mask()`.
|
|
89
|
+
|
|
90
|
+
##### Example
|
|
91
|
+
|
|
87
92
|
```ts
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
93
|
+
import * as formValidation from 'jails.stdlib/form-validation'
|
|
94
|
+
import { email, number, required } from './my-entities'
|
|
95
|
+
|
|
96
|
+
jails.register('form-validation', formValidation, { email, number, required })
|
|
97
|
+
jails.start()
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**./my-entities.ts**
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
export const email = {
|
|
104
|
+
validate( value, input, form ) {
|
|
105
|
+
if (!value) return true
|
|
106
|
+
if (!value.match(/(.*)@(.*)\.\w{2,}/)) return false
|
|
107
|
+
return true
|
|
108
|
+
},
|
|
109
|
+
mask() {
|
|
110
|
+
// I don't have a mask
|
|
107
111
|
},
|
|
112
|
+
message( value, input, form ) {
|
|
113
|
+
return `Invalid Email`
|
|
114
|
+
}
|
|
115
|
+
}
|
|
108
116
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
117
|
+
export const required = {
|
|
118
|
+
validate(value, input, form) {
|
|
119
|
+
if (!value) return false
|
|
120
|
+
return true
|
|
121
|
+
},
|
|
122
|
+
mask(value, input, form) {
|
|
123
|
+
// I don't have a mask
|
|
124
|
+
},
|
|
125
|
+
message(value, input, form) {
|
|
126
|
+
return `This field is required`
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export const number = {
|
|
131
|
+
validate(value, input, form) {
|
|
132
|
+
if (value.match(/\D/g)) return false
|
|
133
|
+
return true
|
|
134
|
+
},
|
|
135
|
+
mask(value, input, form) {
|
|
136
|
+
return value.replace(/\D/, '')
|
|
137
|
+
},
|
|
138
|
+
message() {
|
|
139
|
+
return `This field accepts only number`
|
|
113
140
|
}
|
|
114
141
|
}
|
|
115
142
|
```
|
|
116
|
-
|
|
117
|
-
To see how to inject this dependency, go back to the [usage](#usage) section.
|
|
118
143
|
|
|
119
144
|
|
|
120
145
|
##### element.setValues(...data)
|
package/package.json
CHANGED
package/messenger/README.md
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
# messenger
|
|
2
|
-
|
|
3
|
-
```ts
|
|
4
|
-
messenger({
|
|
5
|
-
target?: Window | HTMLIFrameElement,
|
|
6
|
-
accept?: string[],
|
|
7
|
-
actions?: Record<string, (payload: any) => void>,
|
|
8
|
-
origin?: string
|
|
9
|
-
})
|
|
10
|
-
```
|
|
11
|
-
# ๐ฌ Messenger API Documentation
|
|
12
|
-
|
|
13
|
-
The `messenger` utility provides a lightweight, secure interface for sending and receiving messages between windows or iframes using `postMessage`.
|
|
14
|
-
|
|
15
|
-
---
|
|
16
|
-
|
|
17
|
-
## โ
Features
|
|
18
|
-
|
|
19
|
-
* Easy dispatch and subscription to custom message actions.
|
|
20
|
-
* Secure origin validation.
|
|
21
|
-
* Works with iframes or separate window contexts.
|
|
22
|
-
* Extensible via `subscribe`.
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
## ๐งช Usage
|
|
27
|
-
|
|
28
|
-
### Parent Page
|
|
29
|
-
|
|
30
|
-
```ts
|
|
31
|
-
import { messenger } from 'jails.stdlib/messenger'
|
|
32
|
-
|
|
33
|
-
const msg = messenger({
|
|
34
|
-
target: document.getElementById('myIframe'), // <iframe id="myIframe" />
|
|
35
|
-
accept: ['https://child-app.com'], // Allowed origins
|
|
36
|
-
actions: {
|
|
37
|
-
reply(data) {
|
|
38
|
-
console.log('Received from iframe:', data)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
// Send message to iframe
|
|
44
|
-
msg.dispatch('init', { user: 'Alice' })
|
|
45
|
-
|
|
46
|
-
// Add more listeners later
|
|
47
|
-
msg.subscribe({
|
|
48
|
-
status: (msg) => console.log('Status update:', msg)
|
|
49
|
-
})
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### Iframe Page
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
const msg = messenger({
|
|
56
|
-
target: window.parent,
|
|
57
|
-
accept: ['https://parent-app.com'],
|
|
58
|
-
actions: {
|
|
59
|
-
init(data){
|
|
60
|
-
console.log('Init from parent:', data)
|
|
61
|
-
msg.dispatch('reply', { received: true })
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
---
|
|
68
|
-
|
|
69
|
-
## ๐งน API
|
|
70
|
-
|
|
71
|
-
### `messenger(options)`
|
|
72
|
-
|
|
73
|
-
Creates a new Messenger instance.
|
|
74
|
-
|
|
75
|
-
#### Options:
|
|
76
|
-
|
|
77
|
-
| Option | Type | Description |
|
|
78
|
-
| --------- | ----------------------------- | --------------------------------------- |
|
|
79
|
-
| `target` | `Window \| HTMLIFrameElement` | Target to send messages to |
|
|
80
|
-
| `accept` | `string[]` | Allowed origins (`['*']` to accept all) |
|
|
81
|
-
| `actions` | `Record<string, Function>` | Message handlers keyed by action name |
|
|
82
|
-
| `origin` | `string` | The origin used when sending messages |
|
|
83
|
-
|
|
84
|
-
---
|
|
85
|
-
|
|
86
|
-
### `.dispatch(action: string, payload?: any)`
|
|
87
|
-
|
|
88
|
-
Sends a message to the target window.
|
|
89
|
-
|
|
90
|
-
```ts
|
|
91
|
-
msg.dispatch('sayHello', { name: 'Bob' })
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
---
|
|
95
|
-
|
|
96
|
-
### `.subscribe(newActions: Record<string, Function>)`
|
|
97
|
-
|
|
98
|
-
Adds more actions to the messenger at runtime.
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
msg.subscribe({ logout: () => console.log('Logging out...') })
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
---
|
|
105
|
-
|
|
106
|
-
## ๐ Security Notes
|
|
107
|
-
|
|
108
|
-
* Always **specify allowed origins** in `accept` to avoid cross-site scripting risks.
|
|
109
|
-
* Avoid using `'*'` unless absolutely necessary (e.g., during development).
|
package/messenger/index.d.ts
DELETED
package/messenger/index.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
var w = Object.defineProperty;
|
|
2
|
-
var l = Object.getOwnPropertySymbols;
|
|
3
|
-
var a = Object.prototype.hasOwnProperty, p = Object.prototype.propertyIsEnumerable;
|
|
4
|
-
var r = (n, i, s) => i in n ? w(n, i, { enumerable: !0, configurable: !0, writable: !0, value: s }) : n[i] = s, d = (n, i) => {
|
|
5
|
-
for (var s in i || (i = {}))
|
|
6
|
-
a.call(i, s) && r(n, s, i[s]);
|
|
7
|
-
if (l)
|
|
8
|
-
for (var s of l(i))
|
|
9
|
-
p.call(i, s) && r(n, s, i[s]);
|
|
10
|
-
return n;
|
|
11
|
-
};
|
|
12
|
-
const f = ({
|
|
13
|
-
target: n = null,
|
|
14
|
-
accept: i = [],
|
|
15
|
-
actions: s = {},
|
|
16
|
-
origin: c = location.origin
|
|
17
|
-
} = {}) => {
|
|
18
|
-
const m = (n == null ? void 0 : n.contentWindow) || n;
|
|
19
|
-
return window.addEventListener("message", (e) => {
|
|
20
|
-
if (i.includes("*") || i.includes(e.origin)) {
|
|
21
|
-
const { action: o, payload: u } = e.data;
|
|
22
|
-
o in s && s[o](u);
|
|
23
|
-
} else
|
|
24
|
-
throw {
|
|
25
|
-
type: "ACCESS DENIED",
|
|
26
|
-
message: "Cant receive message from: " + e.origin
|
|
27
|
-
};
|
|
28
|
-
}), {
|
|
29
|
-
dispatch(e, o) {
|
|
30
|
-
m.postMessage({ action: e, payload: o }, c);
|
|
31
|
-
},
|
|
32
|
-
subscribe(e) {
|
|
33
|
-
s = d(d({}, e), s);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
export {
|
|
38
|
-
f as messenger
|
|
39
|
-
};
|
package/messenger/index.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export const messenger = ({
|
|
3
|
-
|
|
4
|
-
target = null as any,
|
|
5
|
-
accept = [] as any,
|
|
6
|
-
actions = {} as any,
|
|
7
|
-
origin = location.origin
|
|
8
|
-
|
|
9
|
-
} = {}) => {
|
|
10
|
-
|
|
11
|
-
const win = target?.contentWindow || target
|
|
12
|
-
|
|
13
|
-
window.addEventListener('message', ( event ) => {
|
|
14
|
-
|
|
15
|
-
if( accept.includes('*') || accept.includes(event.origin) ) {
|
|
16
|
-
const { action, payload } = event.data
|
|
17
|
-
if( action in actions ) {
|
|
18
|
-
actions[ action ]( payload as any )
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
throw {
|
|
22
|
-
type : 'ACCESS DENIED',
|
|
23
|
-
message : 'Cant receive message from: ' + event.origin
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
|
|
30
|
-
dispatch( action: string, payload?: any ) {
|
|
31
|
-
win.postMessage({ action, payload }, origin)
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
subscribe( actions_: any ) {
|
|
35
|
-
actions = { ...actions_, ...actions }
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
package/messenger/index.umd.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(i,e){typeof exports=="object"&&typeof module!="undefined"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(i=typeof globalThis!="undefined"?globalThis:i||self,e(i.messenger={}))})(this,(function(i){"use strict";var p=Object.defineProperty;var u=Object.getOwnPropertySymbols;var g=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable;var l=(i,e,n)=>e in i?p(i,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):i[e]=n,f=(i,e)=>{for(var n in e||(e={}))g.call(e,n)&&l(i,n,e[n]);if(u)for(var n of u(e))y.call(e,n)&&l(i,n,e[n]);return i};const e=({target:n=null,accept:t=[],actions:o={},origin:r=location.origin}={})=>{const c=(n==null?void 0:n.contentWindow)||n;return window.addEventListener("message",s=>{if(t.includes("*")||t.includes(s.origin)){const{action:d,payload:m}=s.data;d in o&&o[d](m)}else throw{type:"ACCESS DENIED",message:"Cant receive message from: "+s.origin}}),{dispatch(s,d){c.postMessage({action:s,payload:d},r)},subscribe(s){o=f(f({},s),o)}}};i.messenger=e,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|