@cat-of-summer/st-script 1.0.6 → 1.0.7
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/README.md +2 -2
- package/dist/mask.esm.min.js +1 -1
- package/dist/mask.esm.min.js.map +3 -3
- package/dist/mask.min.js +1 -1
- package/dist/mask.min.js.map +3 -3
- package/dist/observer.esm.min.js +2 -0
- package/dist/observer.esm.min.js.map +7 -0
- package/dist/observer.min.js +3 -0
- package/dist/observer.min.js.map +7 -0
- package/dist/validator.esm.min.js +2 -0
- package/dist/validator.esm.min.js.map +7 -0
- package/dist/validator.min.js +3 -0
- package/dist/validator.min.js.map +7 -0
- package/package.json +1 -1
- package/src/index.js +2 -2
- package/src/mask/_engine.js +29 -9
- package/src/mask/index.js +16 -1
- package/src/observer/_engine.js +71 -0
- package/src/observer/index.js +287 -0
- package/src/validator/index.js +156 -0
- package/dist/st_observer.esm.min.js +0 -2
- package/dist/st_observer.esm.min.js.map +0 -7
- package/dist/st_observer.min.js +0 -3
- package/dist/st_observer.min.js.map +0 -7
- package/dist/st_validator.esm.min.js +0 -2
- package/dist/st_validator.esm.min.js.map +0 -7
- package/dist/st_validator.min.js +0 -3
- package/dist/st_validator.min.js.map +0 -7
- package/src/st_observer/index.js +0 -426
- package/src/st_validator/index.js +0 -57
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/st_validator/index.js"],
|
|
4
|
-
"sourcesContent": ["class st_validator {\n\n\t#validator = null;\n #selector = null;\n\n constructor(param) {\n\n this.#validator = function (input) {\n if ((param.validator ? param.validator(input) : true) &&\n ((() => {\n if (param.regexp) {\n let regexp_obj = new RegExp(param.regexp);\n \n if (!param.attribute && !param.property)\n return regexp_obj.test(input.value);\n \n if (param.attribute && param.property)\n return input.hasAttribute(param.attribute) && regexp_obj.test(input[param.property]);\n \n return (param.attribute ? regexp_obj.test(input.getAttribute(param.attribute)) : true) && (param.property ? regexp_obj.test(input[param.property]) : true);\n } else {\n return param.attribute ? input.hasAttribute(param.attribute) : true;\n }\n })()))\n param.on_valid(input);\n else\n param.on_invalid(input);\n\n if (param.after_check) param.after_check(input);\n };\n\n this.#selector = param.selector;\n\n if (typeof param.onload == 'function')\n param.onload(this.#selector);\n else if (param.onload === true) \n this.apply();\n\n document.querySelectorAll(this.#selector).forEach(input => {\n (param.events ?? [\n 'input',\n 'blur'\n ]).forEach(event => {\n input.addEventListener(event, () => {\n this.#validator(input)\n });\n });\n });\n }\n\n\tapply(selector) {\n (document.querySelectorAll(selector ?? this.#selector))\n .forEach(this.#validator, this);\n }\n}\n\nexport default st_validator;\n"],
|
|
5
|
-
"mappings": "mbAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,IAAMC,EAAN,KAAmB,CAElBC,GAAa,KACVC,GAAY,KAEZ,YAAYC,EAAO,CAEf,KAAKF,GAAa,SAAUG,EAAO,EAC1B,CAAAD,EAAM,WAAYA,EAAM,UAAUC,CAAK,KACtC,IAAM,CACR,GAAID,EAAM,OAAQ,CACd,IAAIE,EAAa,IAAI,OAAOF,EAAM,MAAM,EAExC,MAAI,CAACA,EAAM,WAAa,CAACA,EAAM,SACpBE,EAAW,KAAKD,EAAM,KAAK,EAElCD,EAAM,WAAaA,EAAM,SAClBC,EAAM,aAAaD,EAAM,SAAS,GAAKE,EAAW,KAAKD,EAAMD,EAAM,QAAQ,CAAC,GAE/EA,EAAM,UAAYE,EAAW,KAAKD,EAAM,aAAaD,EAAM,SAAS,CAAC,EAAI,MAAUA,EAAM,SAAWE,EAAW,KAAKD,EAAMD,EAAM,QAAQ,CAAC,EAAI,GACzJ,KACI,QAAOA,EAAM,UAAYC,EAAM,aAAaD,EAAM,SAAS,EAAI,EAEvE,GAAG,EACCA,EAAM,SAASC,CAAK,EAEpBD,EAAM,WAAWC,CAAK,EAEtBD,EAAM,aAAaA,EAAM,YAAYC,CAAK,CAClD,EAEA,KAAKF,GAAYC,EAAM,SAEnB,OAAOA,EAAM,QAAU,WACvBA,EAAM,OAAO,KAAKD,EAAS,EACtBC,EAAM,SAAW,IACtB,KAAK,MAAM,EAEf,SAAS,iBAAiB,KAAKD,EAAS,EAAE,QAAQE,GAAS,EACtDD,EAAM,QAAU,CACb,QACA,MACJ,GAAG,QAAQG,GAAS,CAChBF,EAAM,iBAAiBE,EAAO,IAAM,CAChC,KAAKL,GAAWG,CAAK,CACzB,CAAC,CACL,CAAC,CACL,CAAC,CACL,CAEH,MAAMG,EAAU,CACR,SAAS,iBAAiBA,GAAY,KAAKL,EAAS,EAChD,QAAQ,KAAKD,GAAY,IAAI,CACtC,CACJ,EAEOF,EAAQC",
|
|
6
|
-
"names": ["index_exports", "__export", "index_default", "st_validator", "#validator", "#selector", "param", "input", "regexp_obj", "event", "selector"]
|
|
7
|
-
}
|
package/src/st_observer/index.js
DELETED
|
@@ -1,426 +0,0 @@
|
|
|
1
|
-
class st_observer {
|
|
2
|
-
static #registry = new Map();
|
|
3
|
-
|
|
4
|
-
constructor(params = {}) {
|
|
5
|
-
const cfg = Object.assign({
|
|
6
|
-
selector: null,
|
|
7
|
-
root: null,
|
|
8
|
-
rootMargin: '0px',
|
|
9
|
-
threshold: 0,
|
|
10
|
-
before_init: () => {},
|
|
11
|
-
on_init: () => {}
|
|
12
|
-
}, params);
|
|
13
|
-
|
|
14
|
-
if (!cfg.selector) throw new Error('st_observer: selector required');
|
|
15
|
-
|
|
16
|
-
const key = (typeof cfg.selector === 'string') ? cfg.selector : Symbol('selector');
|
|
17
|
-
|
|
18
|
-
if (st_observer.#registry.has(key)) {
|
|
19
|
-
this._internal = st_observer.#registry.get(key);
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const internal = {
|
|
24
|
-
key,
|
|
25
|
-
selector: cfg.selector,
|
|
26
|
-
options: { root: cfg.root, rootMargin: cfg.rootMargin, threshold: cfg.threshold },
|
|
27
|
-
observer: null,
|
|
28
|
-
callbacks: new WeakMap(), // element -> callback object (show/hide)
|
|
29
|
-
elements: new Set(),
|
|
30
|
-
_connected: false,
|
|
31
|
-
crossRules: new Map() // Map<targetEl, Array<rule>>
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
cfg.before_init();
|
|
35
|
-
|
|
36
|
-
const createCbObj = () => ({
|
|
37
|
-
on_show: new Set(), once_show: new Set(), while_show: new Set(),
|
|
38
|
-
on_hide: new Set(), once_hide: new Set(), while_hide: new Set(),
|
|
39
|
-
_once_shown: false, _once_hidden: false, _wasIntersecting: false
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
const safeCall = (fn, ...args) => { try { fn(...args); } catch (e) { console.error(e); } };
|
|
43
|
-
|
|
44
|
-
const rectsIntersect = (a, b) => !(a.right <= b.left || a.left >= b.right || a.bottom <= b.top || a.top >= b.bottom);
|
|
45
|
-
|
|
46
|
-
const resolveTargets = (target) => {
|
|
47
|
-
if (!target) {
|
|
48
|
-
if (typeof internal.selector === 'string') return Array.from(document.querySelectorAll(internal.selector));
|
|
49
|
-
if (internal.selector instanceof Element) return [internal.selector];
|
|
50
|
-
if (NodeList.prototype.isPrototypeOf(internal.selector) || Array.isArray(internal.selector)) return Array.from(internal.selector);
|
|
51
|
-
return [];
|
|
52
|
-
}
|
|
53
|
-
if (typeof target === 'string') return Array.from(document.querySelectorAll(target));
|
|
54
|
-
if (target instanceof Element) return [target];
|
|
55
|
-
if (NodeList.prototype.isPrototypeOf(target) || Array.isArray(target)) return Array.from(target);
|
|
56
|
-
return [];
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// cross rule factory
|
|
60
|
-
const createCrossRule = (goals) => ({
|
|
61
|
-
goals: new Set(goals),
|
|
62
|
-
on_cross: new Set(),
|
|
63
|
-
once_cross: new Set(),
|
|
64
|
-
while_cross: new Set(),
|
|
65
|
-
_once_done: new Set(),
|
|
66
|
-
_wasCrossing: new Map()
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
const checkCrossForTarget = (targetEl) => {
|
|
70
|
-
const rules = internal.crossRules.get(targetEl);
|
|
71
|
-
if (!rules || !rules.length) return;
|
|
72
|
-
if (!document.contains(targetEl)) return;
|
|
73
|
-
const rTarget = targetEl.getBoundingClientRect();
|
|
74
|
-
for (const rule of rules) {
|
|
75
|
-
for (const goalEl of rule.goals) {
|
|
76
|
-
if (!document.contains(goalEl)) {
|
|
77
|
-
rule._wasCrossing.set(goalEl, false);
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
const rGoal = goalEl.getBoundingClientRect();
|
|
81
|
-
const crossing = rectsIntersect(rTarget, rGoal);
|
|
82
|
-
|
|
83
|
-
if (crossing) {
|
|
84
|
-
rule.while_cross.forEach(fn => safeCall(fn, {target: targetEl, goal: goalEl}, targetEl, goalEl));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const was = !!rule._wasCrossing.get(goalEl);
|
|
88
|
-
if (!was && crossing) {
|
|
89
|
-
rule.on_cross.forEach(fn => safeCall(fn, {target: targetEl, goal: goalEl}, targetEl, goalEl));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (crossing && !rule._once_done.has(goalEl)) {
|
|
93
|
-
rule.once_cross.forEach(fn => safeCall(fn, {target: targetEl, goal: goalEl}, targetEl, goalEl));
|
|
94
|
-
rule._once_done.add(goalEl);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
rule._wasCrossing.set(goalEl, !!crossing);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const runAllCrossChecks = () => {
|
|
103
|
-
for (const targetEl of Array.from(internal.crossRules.keys())) {
|
|
104
|
-
if (!document.contains(targetEl)) continue;
|
|
105
|
-
try { checkCrossForTarget(targetEl); } catch (e) { console.error(e); }
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const makeIoCallback = () => (entries) => {
|
|
110
|
-
for (const entry of entries) {
|
|
111
|
-
const el = entry.target;
|
|
112
|
-
let cbObj = internal.callbacks.get(el);
|
|
113
|
-
if (!cbObj) {
|
|
114
|
-
cbObj = createCbObj();
|
|
115
|
-
internal.callbacks.set(el, cbObj);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (entry.isIntersecting) {
|
|
119
|
-
cbObj.while_show.forEach(fn => safeCall(fn, entry, el));
|
|
120
|
-
if (!cbObj._wasIntersecting) cbObj.on_show.forEach(fn => safeCall(fn, entry, el));
|
|
121
|
-
if (!cbObj._once_shown) {
|
|
122
|
-
cbObj._once_shown = true;
|
|
123
|
-
cbObj.once_show.forEach(fn => safeCall(fn, entry, el));
|
|
124
|
-
}
|
|
125
|
-
cbObj._wasIntersecting = true;
|
|
126
|
-
} else {
|
|
127
|
-
cbObj.while_hide.forEach(fn => safeCall(fn, entry, el));
|
|
128
|
-
if (cbObj._wasIntersecting) cbObj.on_hide.forEach(fn => safeCall(fn, entry, el));
|
|
129
|
-
if (!cbObj._once_hidden) {
|
|
130
|
-
cbObj._once_hidden = true;
|
|
131
|
-
cbObj.once_hide.forEach(fn => safeCall(fn, entry, el));
|
|
132
|
-
}
|
|
133
|
-
cbObj._wasIntersecting = false;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// after processing intersection entries — check cross rules (so crossing reacts to movement)
|
|
138
|
-
runAllCrossChecks();
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// attach internals
|
|
142
|
-
this._internal = internal;
|
|
143
|
-
this._createCbObj = createCbObj;
|
|
144
|
-
this._resolveTargets = resolveTargets;
|
|
145
|
-
this._createCrossRule = createCrossRule;
|
|
146
|
-
this._checkCrossForTarget = checkCrossForTarget;
|
|
147
|
-
this._runAllCrossChecks = runAllCrossChecks;
|
|
148
|
-
this._makeIoCallback = makeIoCallback;
|
|
149
|
-
|
|
150
|
-
// seed initial elements (selector)
|
|
151
|
-
const initial = resolveTargets();
|
|
152
|
-
for (const el of initial) {
|
|
153
|
-
internal.elements.add(el);
|
|
154
|
-
cfg.on_init(el);
|
|
155
|
-
if (!internal.callbacks.get(el)) internal.callbacks.set(el, createCbObj());
|
|
156
|
-
}
|
|
157
|
-
st_observer.#registry.set(key, internal);
|
|
158
|
-
|
|
159
|
-
// auto-connect
|
|
160
|
-
this.connect();
|
|
161
|
-
|
|
162
|
-
// cfg.on_init();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
enableAutoCrossChecks(options = {}) {
|
|
166
|
-
const cfg = Object.assign({ strategy: 'all', watchScrollAncestors: true, watchResize: true, rAFWhileAnimating: false }, options);
|
|
167
|
-
if (this._autoChecks) {
|
|
168
|
-
// уже включены — обновим конфиг
|
|
169
|
-
this._autoChecks.cfg = cfg;
|
|
170
|
-
return this;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const internal = this._internal;
|
|
174
|
-
const self = this;
|
|
175
|
-
|
|
176
|
-
// throttled runner через rAF
|
|
177
|
-
let scheduled = false;
|
|
178
|
-
const requestRun = () => {
|
|
179
|
-
if (scheduled) return;
|
|
180
|
-
scheduled = true;
|
|
181
|
-
requestAnimationFrame(() => {
|
|
182
|
-
scheduled = false;
|
|
183
|
-
if (cfg.strategy === 'all') {
|
|
184
|
-
self._runAllCrossChecks();
|
|
185
|
-
} else if (cfg.strategy === 'entryOnly') {
|
|
186
|
-
// если entryOnly — у нас нет списка entry здесь, будем просто запускать всё, либо
|
|
187
|
-
// можно хранить lastEntries при IO и проверять только те элементы — см. ниже улучшение.
|
|
188
|
-
self._runAllCrossChecks();
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
// helper: find scrollable ancestors of elements in internal.elements
|
|
194
|
-
const findScrollParents = (el) => {
|
|
195
|
-
const res = new Set();
|
|
196
|
-
let node = el.parentNode;
|
|
197
|
-
while (node && node !== document) {
|
|
198
|
-
if (node instanceof HTMLElement) {
|
|
199
|
-
const style = getComputedStyle(node);
|
|
200
|
-
const overflow = style.overflow + style.overflowX + style.overflowY;
|
|
201
|
-
if (/(auto|scroll|overlay)/.test(overflow)) res.add(node);
|
|
202
|
-
}
|
|
203
|
-
node = node.parentNode;
|
|
204
|
-
}
|
|
205
|
-
// also listen to window
|
|
206
|
-
res.add(window);
|
|
207
|
-
return Array.from(res);
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
// attach listeners
|
|
211
|
-
const onScrollOrResize = () => requestRun();
|
|
212
|
-
const scrollTargets = new Set();
|
|
213
|
-
|
|
214
|
-
if (cfg.watchScrollAncestors) {
|
|
215
|
-
// attach to scroll parents of currently observed elements
|
|
216
|
-
for (const el of Array.from(internal.elements)) {
|
|
217
|
-
const sp = findScrollParents(el);
|
|
218
|
-
sp.forEach(p => {
|
|
219
|
-
if (!scrollTargets.has(p)) {
|
|
220
|
-
p.addEventListener('scroll', onScrollOrResize, { passive: true });
|
|
221
|
-
scrollTargets.add(p);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
} else {
|
|
226
|
-
window.addEventListener('scroll', onScrollOrResize, { passive: true });
|
|
227
|
-
scrollTargets.add(window);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (cfg.watchResize) {
|
|
231
|
-
window.addEventListener('resize', onScrollOrResize, { passive: true });
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// optional RAF loop for animations (start/stop)
|
|
235
|
-
let rafId = null;
|
|
236
|
-
const rafLoop = () => {
|
|
237
|
-
self._runAllCrossChecks();
|
|
238
|
-
rafId = requestAnimationFrame(rafLoop);
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
// store autoChecks state
|
|
242
|
-
this._autoChecks = {
|
|
243
|
-
cfg,
|
|
244
|
-
scrollTargets,
|
|
245
|
-
onScrollOrResize,
|
|
246
|
-
rafId,
|
|
247
|
-
startRaf: () => { if (!rafId) rafLoop(); },
|
|
248
|
-
stopRaf: () => { if (rafId) { cancelAnimationFrame(rafId); rafId = null; } }
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
// if user wants rAFWhileAnimating === true, we do not start it by default;
|
|
252
|
-
// startRaf can be called externally when an animation begins (or we can detect transform changes).
|
|
253
|
-
return this;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
disableAutoCrossChecks() {
|
|
257
|
-
if (!this._autoChecks) return this;
|
|
258
|
-
const ac = this._autoChecks;
|
|
259
|
-
ac.scrollTargets.forEach(p => {
|
|
260
|
-
try { p.removeEventListener('scroll', ac.onScrollOrResize); } catch (e) {}
|
|
261
|
-
});
|
|
262
|
-
try { window.removeEventListener('resize', ac.onScrollOrResize); } catch (e) {}
|
|
263
|
-
ac.stopRaf();
|
|
264
|
-
this._autoChecks = null;
|
|
265
|
-
return this;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/* ---------- show/hide registration ---------- */
|
|
269
|
-
_register(cbSetName, fn, target) {
|
|
270
|
-
if (typeof fn !== 'function') return this;
|
|
271
|
-
const internal = this._internal;
|
|
272
|
-
const targets = this._resolveTargets(target);
|
|
273
|
-
for (const el of targets) {
|
|
274
|
-
let obj = internal.callbacks.get(el);
|
|
275
|
-
if (!obj) {
|
|
276
|
-
obj = this._createCbObj();
|
|
277
|
-
internal.callbacks.set(el, obj);
|
|
278
|
-
}
|
|
279
|
-
obj[cbSetName].add(fn);
|
|
280
|
-
if (!internal.elements.has(el)) {
|
|
281
|
-
internal.elements.add(el);
|
|
282
|
-
if (internal._connected && internal.observer) {
|
|
283
|
-
try { internal.observer.observe(el); } catch (e) {}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return this;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
on_show(callback, target) { return this._register('on_show', callback, target); }
|
|
291
|
-
while_show(callback, target) { return this._register('while_show', callback, target); }
|
|
292
|
-
once_show(callback, target) { return this._register('once_show', callback, target); }
|
|
293
|
-
|
|
294
|
-
on_hide(callback, target) { return this._register('on_hide', callback, target); }
|
|
295
|
-
while_hide(callback, target) { return this._register('while_hide', callback, target); }
|
|
296
|
-
once_hide(callback, target) { return this._register('once_hide', callback, target); }
|
|
297
|
-
|
|
298
|
-
observe(target) {
|
|
299
|
-
const targets = this._resolveTargets(target);
|
|
300
|
-
for (const el of targets) {
|
|
301
|
-
if (!this._internal.callbacks.get(el)) this._internal.callbacks.set(el, this._createCbObj());
|
|
302
|
-
this._internal.elements.add(el);
|
|
303
|
-
if (this._internal._connected && this._internal.observer) {
|
|
304
|
-
try { this._internal.observer.observe(el); } catch (e) {}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
return this;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
unobserve(target) {
|
|
311
|
-
const targets = this._resolveTargets(target);
|
|
312
|
-
for (const el of targets) {
|
|
313
|
-
try { if (this._internal.observer) this._internal.observer.unobserve(el); } catch (e) {}
|
|
314
|
-
this._internal.callbacks.delete(el);
|
|
315
|
-
this._internal.elements.delete(el);
|
|
316
|
-
}
|
|
317
|
-
return this;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
disconnect() {
|
|
321
|
-
try { if (this._internal.observer) this._internal.observer.disconnect(); } catch (e) {}
|
|
322
|
-
this._internal.observer = null;
|
|
323
|
-
this._internal._connected = false;
|
|
324
|
-
return this;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
connect(options = {}) {
|
|
328
|
-
if (this._internal._connected) return this;
|
|
329
|
-
|
|
330
|
-
if (options.root !== undefined) this._internal.options.root = options.root;
|
|
331
|
-
if (options.rootMargin !== undefined) this._internal.options.rootMargin = options.rootMargin;
|
|
332
|
-
if (options.threshold !== undefined) this._internal.options.threshold = options.threshold;
|
|
333
|
-
|
|
334
|
-
this._internal.observer = new IntersectionObserver(this._makeIoCallback(), this._internal.options);
|
|
335
|
-
|
|
336
|
-
if (options.resetFlags) {
|
|
337
|
-
for (const el of Array.from(this._internal.elements)) {
|
|
338
|
-
const obj = this._internal.callbacks.get(el);
|
|
339
|
-
if (obj) { obj._once_shown = false; obj._once_hidden = false; obj._wasIntersecting = false; }
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
for (const el of Array.from(this._internal.elements)) {
|
|
344
|
-
try { this._internal.observer.observe(el); } catch (e) {}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
this._internal._connected = true;
|
|
348
|
-
return this;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
reconnect(options = {}) { return this.connect(options); }
|
|
352
|
-
|
|
353
|
-
resetOnceFlags(target) {
|
|
354
|
-
const targets = target ? this._resolveTargets(target) : Array.from(this._internal.elements);
|
|
355
|
-
for (const el of targets) {
|
|
356
|
-
const obj = this._internal.callbacks.get(el);
|
|
357
|
-
if (obj) { obj._once_shown = false; obj._once_hidden = false; obj._wasIntersecting = false; }
|
|
358
|
-
}
|
|
359
|
-
return this;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
isConnected() { return !!this._internal._connected; }
|
|
363
|
-
getObservedElements() { return Array.from(this._internal.elements); }
|
|
364
|
-
|
|
365
|
-
/* ---------- cross API ---------- */
|
|
366
|
-
_registerCross(cbSetName, callback, target, goal) {
|
|
367
|
-
if (typeof callback !== 'function') return this;
|
|
368
|
-
const internal = this._internal;
|
|
369
|
-
// signature: (callback, goal) -> target omitted
|
|
370
|
-
if (goal === undefined && (typeof target === 'string' || target instanceof Element || NodeList.prototype.isPrototypeOf(target) || Array.isArray(target))) {
|
|
371
|
-
goal = target; target = undefined;
|
|
372
|
-
}
|
|
373
|
-
const targets = this._resolveTargets(target);
|
|
374
|
-
const goals = this._resolveTargets(goal);
|
|
375
|
-
if (!goals.length) { console.warn('st_observer: cross registration no goals'); return this; }
|
|
376
|
-
|
|
377
|
-
for (const tEl of targets) {
|
|
378
|
-
if (!internal.callbacks.get(tEl)) internal.callbacks.set(tEl, this._createCbObj());
|
|
379
|
-
internal.elements.add(tEl);
|
|
380
|
-
|
|
381
|
-
let rules = internal.crossRules.get(tEl);
|
|
382
|
-
if (!rules) { rules = []; internal.crossRules.set(tEl, rules); }
|
|
383
|
-
|
|
384
|
-
let rule = rules.find(r => {
|
|
385
|
-
if (r.goals.size !== goals.length) return false;
|
|
386
|
-
for (const g of goals) if (!r.goals.has(g)) return false;
|
|
387
|
-
return true;
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
if (!rule) {
|
|
391
|
-
rule = this._createCrossRule(goals);
|
|
392
|
-
rules.push(rule);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
rule[cbSetName].add(callback);
|
|
396
|
-
|
|
397
|
-
for (const g of goals) internal.elements.add(g);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// immediate check
|
|
401
|
-
this._runAllCrossChecks();
|
|
402
|
-
return this;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
on_cross(callback, target, goal) { return this._registerCross('on_cross', callback, target, goal); }
|
|
406
|
-
once_cross(callback, target, goal) { return this._registerCross('once_cross', callback, target, goal); }
|
|
407
|
-
while_cross(callback, target, goal) { return this._registerCross('while_cross', callback, target, goal); }
|
|
408
|
-
|
|
409
|
-
resetCrossOnceFlags(target, goal) {
|
|
410
|
-
const internal = this._internal;
|
|
411
|
-
const targets = target ? this._resolveTargets(target) : Array.from(internal.crossRules.keys());
|
|
412
|
-
const goals = goal ? this._resolveTargets(goal) : null;
|
|
413
|
-
|
|
414
|
-
for (const tEl of targets) {
|
|
415
|
-
const rules = internal.crossRules.get(tEl);
|
|
416
|
-
if (!rules) continue;
|
|
417
|
-
for (const rule of rules) {
|
|
418
|
-
if (!goals) rule._once_done.clear();
|
|
419
|
-
else for (const gEl of goals) rule._once_done.delete(gEl);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
export default st_observer;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
class st_validator {
|
|
2
|
-
|
|
3
|
-
#validator = null;
|
|
4
|
-
#selector = null;
|
|
5
|
-
|
|
6
|
-
constructor(param) {
|
|
7
|
-
|
|
8
|
-
this.#validator = function (input) {
|
|
9
|
-
if ((param.validator ? param.validator(input) : true) &&
|
|
10
|
-
((() => {
|
|
11
|
-
if (param.regexp) {
|
|
12
|
-
let regexp_obj = new RegExp(param.regexp);
|
|
13
|
-
|
|
14
|
-
if (!param.attribute && !param.property)
|
|
15
|
-
return regexp_obj.test(input.value);
|
|
16
|
-
|
|
17
|
-
if (param.attribute && param.property)
|
|
18
|
-
return input.hasAttribute(param.attribute) && regexp_obj.test(input[param.property]);
|
|
19
|
-
|
|
20
|
-
return (param.attribute ? regexp_obj.test(input.getAttribute(param.attribute)) : true) && (param.property ? regexp_obj.test(input[param.property]) : true);
|
|
21
|
-
} else {
|
|
22
|
-
return param.attribute ? input.hasAttribute(param.attribute) : true;
|
|
23
|
-
}
|
|
24
|
-
})()))
|
|
25
|
-
param.on_valid(input);
|
|
26
|
-
else
|
|
27
|
-
param.on_invalid(input);
|
|
28
|
-
|
|
29
|
-
if (param.after_check) param.after_check(input);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
this.#selector = param.selector;
|
|
33
|
-
|
|
34
|
-
if (typeof param.onload == 'function')
|
|
35
|
-
param.onload(this.#selector);
|
|
36
|
-
else if (param.onload === true)
|
|
37
|
-
this.apply();
|
|
38
|
-
|
|
39
|
-
document.querySelectorAll(this.#selector).forEach(input => {
|
|
40
|
-
(param.events ?? [
|
|
41
|
-
'input',
|
|
42
|
-
'blur'
|
|
43
|
-
]).forEach(event => {
|
|
44
|
-
input.addEventListener(event, () => {
|
|
45
|
-
this.#validator(input)
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
apply(selector) {
|
|
52
|
-
(document.querySelectorAll(selector ?? this.#selector))
|
|
53
|
-
.forEach(this.#validator, this);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export default st_validator;
|