@cat-of-summer/st-script 1.0.2

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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +130 -0
  3. package/dist/app/form.esm.min.js +1 -0
  4. package/dist/app/form.min.js +2 -0
  5. package/dist/app.esm.min.js +1 -0
  6. package/dist/app.min.js +2 -0
  7. package/dist/cookie.esm.min.js +1 -0
  8. package/dist/cookie.min.js +2 -0
  9. package/dist/core.esm.min.js +1 -0
  10. package/dist/core.min.js +2 -0
  11. package/dist/index.esm.min.js +1 -0
  12. package/dist/index.min.js +2 -0
  13. package/dist/loader.esm.min.js +1 -0
  14. package/dist/loader.min.js +2 -0
  15. package/dist/modal.esm.min.js +1 -0
  16. package/dist/modal.min.js +2 -0
  17. package/dist/route.esm.min.js +1 -0
  18. package/dist/route.min.js +2 -0
  19. package/dist/select.esm.min.js +1 -0
  20. package/dist/select.min.js +2 -0
  21. package/dist/st_button_widget.esm.min.js +1 -0
  22. package/dist/st_button_widget.min.js +2 -0
  23. package/dist/st_links_widget.esm.min.js +1 -0
  24. package/dist/st_links_widget.min.js +2 -0
  25. package/dist/st_mask.esm.min.js +1 -0
  26. package/dist/st_mask.min.js +2 -0
  27. package/dist/st_observer.esm.min.js +1 -0
  28. package/dist/st_observer.min.js +2 -0
  29. package/dist/st_typograf.esm.min.js +1 -0
  30. package/dist/st_typograf.min.js +2 -0
  31. package/dist/st_validator.esm.min.js +1 -0
  32. package/dist/st_validator.min.js +2 -0
  33. package/dist/toggle.esm.min.js +1 -0
  34. package/dist/toggle.min.js +2 -0
  35. package/dist/uploader.esm.min.js +1 -0
  36. package/dist/uploader.min.js +2 -0
  37. package/package.json +35 -0
  38. package/src/_traits/hasInstanceSymbol.js +29 -0
  39. package/src/app/form/index.js +357 -0
  40. package/src/app/index.js +1040 -0
  41. package/src/cookie/index.js +141 -0
  42. package/src/core/index.js +159 -0
  43. package/src/index.js +16 -0
  44. package/src/loader/index.js +169 -0
  45. package/src/modal/index.js +297 -0
  46. package/src/route/index.js +208 -0
  47. package/src/select/index.js +210 -0
  48. package/src/st_button_widget/README.txt +62 -0
  49. package/src/st_button_widget/index.js +322 -0
  50. package/src/st_links_widget/README.txt +54 -0
  51. package/src/st_links_widget/index.js +294 -0
  52. package/src/st_mask/index.js +183 -0
  53. package/src/st_observer/index.js +426 -0
  54. package/src/st_typograf/index.js +200 -0
  55. package/src/st_validator/index.js +57 -0
  56. package/src/toggle/index.js +181 -0
  57. package/src/uploader/index.js +297 -0
@@ -0,0 +1,297 @@
1
+ import { element, find, own } from '../_traits/hasInstanceSymbol.js';
2
+
3
+ export default class Modal {
4
+ modal;
5
+ overlay;
6
+ area;
7
+ container;
8
+ content;
9
+
10
+ #params = {};
11
+ #methods = {};
12
+ #state = 'hidden';
13
+
14
+ static find = find;
15
+
16
+ get state() {return this.#state;}
17
+ get params() {return this.#params;}
18
+
19
+ clone(params) {
20
+ let content = this.content.cloneNode(true);
21
+ content.id = params.id ?? (content.id || 'modal').replace(/#+$/, '') + (params.suffix ?? '_copy');
22
+
23
+ return new Modal({
24
+ ...this.#params,
25
+ ...this.#methods,
26
+ content,
27
+ ...params,
28
+ parent: this
29
+ });
30
+ }
31
+
32
+ constructor(params) {
33
+ params = {
34
+ zIndex: 1000,
35
+ duration: 0,
36
+
37
+ container: 'body',
38
+
39
+ overlay: true,
40
+ overlay_shading: 0.5,
41
+ overlay_blur: '5px',
42
+ overlay_scroll_lock: true,
43
+
44
+ content: `<div></div>`,
45
+ location: 'center center',
46
+ trigger: null,
47
+
48
+ close_by_overlay: true,
49
+ close_by_esc: true,
50
+ auto_close: -1,
51
+
52
+ allow_interrupt: false,
53
+
54
+ before_show: () => {},
55
+ on_show: () => {},
56
+ before_hide: () => {},
57
+ on_hide: () => {},
58
+ before_init: () => {},
59
+ on_init: () => {},
60
+
61
+ ...params
62
+ };
63
+
64
+ let timeout;
65
+ let body_inline_styles;
66
+ let default_scroll_behavior;
67
+
68
+ for (let [key, value] of Object.entries(params))
69
+ if (typeof value == "function") {
70
+ this[key] = value.bind(this);
71
+
72
+ this.#methods[key] = value;
73
+ } else
74
+ this.#params[key] = value;
75
+
76
+ this.before_init(this.#params);
77
+
78
+ this.modal = document.createElement('modal');
79
+ own(this.modal, this);
80
+
81
+ Object.assign(this.modal.style, {
82
+ position: 'fixed',
83
+ top: '0',
84
+ left: '0',
85
+ right: '0',
86
+ bottom: '0',
87
+ zIndex: this.#params.zIndex,
88
+ transition: `all ${this.#params.duration}s`,
89
+ pointerEvents: 'none',
90
+ display: 'none',
91
+ overflow: 'hidden',
92
+ });
93
+
94
+ this.modal.setAttribute('state', 'hidden');
95
+
96
+ try {
97
+ element(this.#params.container).append(this.modal);
98
+ } catch {
99
+ throw new Error("Неудачная попытка вставить модальное окно в указанный контейнер");
100
+ }
101
+
102
+ if (this.#params.overlay) {
103
+ this.overlay = document.createElement('modal-overlay');
104
+ own(this.overlay, this);
105
+
106
+ Object.assign(this.overlay.style, {
107
+ position: 'absolute',
108
+ top: '0',
109
+ left: '0',
110
+ right: '0',
111
+ bottom: '0',
112
+ backgroundColor: `rgba(0, 0, 0, ${this.#params.overlay_shading})`,
113
+ backdropFilter: `blur(${this.#params.overlay_blur})`,
114
+ zIndex: ++this.#params.zIndex,
115
+ transition: 'inherit',
116
+ });
117
+
118
+ this.modal.append(this.overlay);
119
+ }
120
+
121
+ let loc = (this.#params.location || '').toLowerCase().split(/\s+/);
122
+
123
+ this.area = document.createElement('modal-area');
124
+ own(this.area, this);
125
+
126
+ Object.assign(this.area.style, {
127
+ position: 'absolute',
128
+ top: '0',
129
+ left: '0',
130
+ right: '0',
131
+ bottom: '0',
132
+ overflowY: 'auto',
133
+ overflowX: 'clip',
134
+ display: 'flex',
135
+ alignItems: 'flex-start',
136
+ justifyContent: loc.includes('left') ? 'flex-start' : loc.includes('right') ? 'flex-end' : 'center',
137
+ zIndex: ++this.#params.zIndex,
138
+ pointerEvents: 'all',
139
+ transition: 'inherit'
140
+ });
141
+
142
+ this.modal.append(this.area);
143
+
144
+ if (this.#params.close_by_overlay)
145
+ this.area.addEventListener('click', (e) => {
146
+ if (!this.container.contains(e.target)) this.hide(e);
147
+ });
148
+
149
+ this.container = document.createElement('modal-container');
150
+ own(this.container, this);
151
+
152
+ Object.assign(this.container.style, {
153
+ position: 'relative',
154
+ flexShrink: '0',
155
+ width: 'max-content',
156
+ height: 'max-content',
157
+ maxWidth: '100vw',
158
+ zIndex: ++this.#params.zIndex,
159
+ transition: 'inherit',
160
+ pointerEvents: 'all',
161
+ marginTop: loc.includes('top') ? '0' : 'auto',
162
+ marginBottom: loc.includes('bottom') ? '0' : 'auto',
163
+ marginLeft: loc.includes('left') ? '0' : 'auto',
164
+ marginRight: loc.includes('right') ? '0' : 'auto',
165
+ });
166
+
167
+ this.area.append(this.container);
168
+
169
+ try {
170
+ this.content = element(this.#params.content) ??
171
+ (new DOMParser()).parseFromString(this.#params.content, 'text/html').body.firstElementChild;
172
+ } catch {
173
+ throw new Error("Неудачная попытка вставить переданный контент в модальное окно");
174
+ }
175
+
176
+ this.container.append(this.content);
177
+ own(this.content, this);
178
+ this.content.style.transition = 'inherit';
179
+
180
+ this.content.querySelectorAll(`[action="close"]`).forEach(close_button => {
181
+ close_button.addEventListener('click', () => this.hide());
182
+ });
183
+
184
+ let toggle = (params) => {
185
+ params.before_func(params.data);
186
+
187
+ clearTimeout(timeout);
188
+
189
+ this.modal.style.display = 'flex';
190
+
191
+ requestAnimationFrame(() => {
192
+ this.#state = params.process;
193
+ this.modal.setAttribute('state', this.#state);
194
+
195
+ requestAnimationFrame(() => {
196
+ timeout = setTimeout(() => {
197
+ this.#state = params.final;
198
+ this.modal.setAttribute('state', this.#state);
199
+
200
+ if (params.hide) this.modal.style.display = 'none';
201
+
202
+ params.after_func(params.data);
203
+
204
+ }, this.#params.duration * 1000);
205
+ });
206
+ });
207
+
208
+ }
209
+
210
+ this.show = (data = null) => {
211
+ if (
212
+ this.#state == 'hidden' ||
213
+ (this.#params.allow_interrupt && this.#state == 'hiding')
214
+ ) {
215
+ toggle({
216
+ before_func: this.before_show,
217
+ after_func: this.on_show,
218
+ process: 'showing',
219
+ final: 'shown',
220
+ hide: false,
221
+ data
222
+ });
223
+
224
+ if (this.#params.overlay_scroll_lock && this.overlay) {
225
+ default_scroll_behavior = document.documentElement.style.scrollBehavior;
226
+
227
+ body_inline_styles = document.body.style;
228
+ body_inline_styles = {
229
+ position: body_inline_styles.position,
230
+ top: body_inline_styles.top,
231
+ left: body_inline_styles.left,
232
+ width: body_inline_styles.width,
233
+ scroll_Y: window.scrollY || document.documentElement.scrollTop,
234
+ scroll_X: window.scrollX || document.documentElement.scrollLeft
235
+ };
236
+
237
+ document.documentElement.style.scrollBehavior = 'unset';
238
+ Object.assign(document.body.style, {
239
+ position: 'fixed',
240
+ width: '100vw',
241
+ top: `-${body_inline_styles.scroll_Y}px`,
242
+ left:`-${body_inline_styles.scroll_X}px`,
243
+ });
244
+ }
245
+
246
+ if (this.#params.auto_close > 0) {
247
+ clearTimeout(timeout);
248
+
249
+ timeout = setTimeout(() => {
250
+ this.hide();
251
+ }, this.#params.auto_close * 1000);
252
+ }
253
+ }
254
+ };
255
+
256
+ this.hide = (data = null) => {
257
+ if (
258
+ this.#state == 'shown' ||
259
+ (this.#params.allow_interrupt && this.#state == 'showing')
260
+ ) {
261
+ toggle({
262
+ before_func: this.before_hide,
263
+ after_func: this.on_hide,
264
+ process: 'hiding',
265
+ final: 'hidden',
266
+ hide: true,
267
+ data
268
+ });
269
+
270
+ clearTimeout(timeout);
271
+
272
+ timeout = setTimeout(() => {
273
+ if (body_inline_styles) {
274
+ Object.assign(document.body.style, body_inline_styles);
275
+ window.scrollTo(body_inline_styles.scroll_X, body_inline_styles.scroll_Y);
276
+ }
277
+
278
+ if (default_scroll_behavior != null)
279
+ document.documentElement.style.scrollBehavior = default_scroll_behavior;
280
+
281
+ }, this.#params.duration * 1000);
282
+ }
283
+ };
284
+
285
+ if (this.#params.close_by_esc)
286
+ document.addEventListener('keydown', e => {
287
+ if ((e.code === 'Escape' || e.keyCode === 27)) this.hide(e);
288
+ });
289
+
290
+ if (this.#params.trigger)
291
+ document.querySelectorAll(this.#params.trigger).forEach(trigger => {
292
+ trigger.addEventListener('click', () => this.show(trigger));
293
+ });
294
+
295
+ this.on_init(this.#params);
296
+ }
297
+ }
@@ -0,0 +1,208 @@
1
+ export default class Route {
2
+
3
+ #params = {};
4
+ #methods = {};
5
+
6
+ #routes = [];
7
+ #stack = [];
8
+ #dispatched = false;
9
+
10
+ params = {};
11
+ query = {};
12
+
13
+ #path = '';
14
+ #url = '';
15
+
16
+ clone(params) {
17
+ return new Route({
18
+ ...this.#params,
19
+ ...this.#methods,
20
+ ...params,
21
+ parent: this
22
+ });
23
+ }
24
+
25
+ constructor(params) {
26
+ params = {
27
+ point: '/',
28
+ name: '',
29
+ match_all: true,
30
+ strict_mode: true,
31
+
32
+ before_init: () => {},
33
+ on_init: () => {},
34
+
35
+ ...params
36
+ };
37
+
38
+ for (let [key, value] of Object.entries(params))
39
+ if (typeof value == "function") {
40
+ this[key] = value.bind(this);
41
+
42
+ this.#methods[key] = value;
43
+ } else
44
+ this.#params[key] = value;
45
+
46
+ this.before_init(this.#params);
47
+
48
+ this.#stack.push({ prefix: this.#params.point, middlewares: [] });
49
+
50
+ if (document.readyState === 'loading')
51
+ document.addEventListener('DOMContentLoaded', () => this.#dispatch());
52
+ else
53
+ queueMicrotask(() => this.#dispatch());
54
+
55
+ this.on_init(this.#params);
56
+ }
57
+
58
+ #current() {
59
+ return this.#stack[this.#stack.length - 1];
60
+ }
61
+
62
+ prefix(uri) {
63
+ let parent = this.#current();
64
+
65
+ this.#stack.push({
66
+ prefix: '/' + this.#trim(parent.prefix) + '/' + this.#trim(uri) + '/',
67
+ middlewares: [...parent.middlewares],
68
+ });
69
+
70
+ return this;
71
+ }
72
+
73
+ group(closure) {
74
+ closure();
75
+
76
+ if (this.#stack.length > 1) this.#stack.pop();
77
+ }
78
+
79
+ middleware(mids) {
80
+ let parent = this.#current();
81
+
82
+ parent.middlewares = parent.middlewares.concat(Array.isArray(mids) ? mids : [mids]);
83
+
84
+ return this;
85
+ }
86
+
87
+ get(uri, closure, params = {}) {
88
+ return this.#add_route(uri, closure, params);
89
+ }
90
+
91
+ #trim(uri) {
92
+ return String(uri).replace(/^\/+|\/+$/g, '');
93
+ }
94
+
95
+ #add_route(uri, closure, params = {}) {
96
+ if (typeof closure != 'function')
97
+ throw new Error('Route: обработчик должен быть замыканием');
98
+
99
+ if (typeof params == 'string') params = { alias: params };
100
+
101
+ let base = this.#current();
102
+ let strict = params.strict_mode ?? this.#params.strict_mode;
103
+
104
+ let full = strict
105
+ ? ('/' + this.#trim(base.prefix) + '/' + this.#trim(uri) + '/').replace(/\/{2,}/g, '/')
106
+ : (this.#trim(base.prefix) + '/' + this.#trim(uri)).replace(/\/{2,}/g, '/');
107
+
108
+ for (let route of this.#routes)
109
+ if (route.pattern === full)
110
+ throw new Error(`Route: дубликат маршрута: ${full}`);
111
+
112
+ let { regex, names } = this.#compile(full);
113
+
114
+ this.#routes.push({
115
+ pattern: full,
116
+ uri,
117
+ alias: params.alias ?? null,
118
+ closure,
119
+ middlewares: base.middlewares,
120
+ names,
121
+ regex,
122
+ strict_mode: strict,
123
+ });
124
+
125
+ return this;
126
+ }
127
+
128
+ #compile(pattern) {
129
+ let names = [];
130
+ let escape = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
131
+
132
+ let body = pattern.replace(/\{(\w+)\}|\*|[^{*]+/g, (token, name) => {
133
+ if (name !== undefined) { names.push(name); return '([^/]+)'; }
134
+ if (token === '*') { names.push('*'); return '(.*)'; }
135
+ return escape(token);
136
+ });
137
+
138
+ let source = '^' + body.replace(/\/+$/, '') + '/?$';
139
+
140
+ return { regex: new RegExp(source), names };
141
+ }
142
+
143
+ #signal() {
144
+ return this.#params.name ? 'route:' + this.#params.name : 'route';
145
+ }
146
+
147
+ #matchEvent(tail) {
148
+ return 'route:' + this.#params.name + ':' + tail;
149
+ }
150
+
151
+ #emit(name, detail) {
152
+ document.dispatchEvent(new CustomEvent(name, { detail }));
153
+ }
154
+
155
+ #dispatch() {
156
+ if (this.#dispatched) return;
157
+ this.#dispatched = true;
158
+
159
+ this.#path = location.pathname;
160
+ this.#url = location.href;
161
+ this.query = Object.fromEntries(new URLSearchParams(location.search));
162
+
163
+ this.#emit(this.#signal(), { name: this.#params.name, path: this.#path, query: this.query, url: this.#url });
164
+
165
+ for (let route of this.#routes) {
166
+ let m = route.regex.exec(this.#path);
167
+ if (!m) continue;
168
+
169
+ let values = m.slice(1).map(Route.#decode);
170
+
171
+ this.params = {};
172
+ route.names.forEach((name, i) => { this.params[name] = values[i]; });
173
+
174
+ if (route.middlewares.some(mid => !mid.call(this, ...values)))
175
+ continue;
176
+
177
+ route.closure.call(this, ...values);
178
+
179
+ let detail = {
180
+ name: this.#params.name,
181
+ pattern: route.pattern,
182
+ uri: route.uri,
183
+ alias: route.alias,
184
+ params: this.params,
185
+ values,
186
+ query: this.query,
187
+ path: this.#path,
188
+ url: this.#url,
189
+ };
190
+
191
+ if (route.alias) this.#emit(this.#matchEvent(route.alias), detail);
192
+ if (route.uri) this.#emit(this.#matchEvent(route.uri), detail);
193
+
194
+ if (!this.#params.match_all) break;
195
+ }
196
+ }
197
+
198
+ get routes() { return this.#routes; }
199
+
200
+ get path() { return this.#path; }
201
+
202
+ get url() { return this.#url; }
203
+
204
+ static #decode(v) {
205
+ if (v == null) return v;
206
+ try { return decodeURIComponent(v); } catch { return v; }
207
+ }
208
+ }