@cat-of-summer/st-script 1.0.9 → 1.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.
@@ -10,9 +10,27 @@ export default class Modal {
10
10
  #params = {};
11
11
  #methods = {};
12
12
  #state = 'hidden';
13
+ #pending_show = null;
14
+ // Куда идёт окно. Меняется синхронно, а state — только в requestAnimationFrame,
15
+ // поэтому show() сразу после hide() видит закрытие по #target.
16
+ #target = 'hidden';
13
17
 
14
18
  static find = find;
15
19
 
20
+ // Открытые окна в порядке открытия: Esc закрывает только верхнее.
21
+ static #open = [];
22
+ static #esc_bound = false;
23
+
24
+ static #bind_esc() {
25
+ if (Modal.#esc_bound) return;
26
+ Modal.#esc_bound = true;
27
+ document.addEventListener('keydown', e => {
28
+ if (e.code !== 'Escape' && e.keyCode !== 27) return;
29
+ let top = Modal.#open[Modal.#open.length - 1];
30
+ if (top?.params.close_by_esc) top.hide(e);
31
+ });
32
+ }
33
+
16
34
  get state() {return this.#state;}
17
35
  get params() {return this.#params;}
18
36
 
@@ -110,8 +128,8 @@ export default class Modal {
110
128
  left: '0',
111
129
  right: '0',
112
130
  bottom: '0',
113
- backgroundColor: `rgba(0, 0, 0, ${this.#params.overlay_shading})`,
114
- backdropFilter: `blur(${this.#params.overlay_blur})`,
131
+ backgroundColor: `var(--modal-overlay-color, rgba(0, 0, 0, ${this.#params.overlay_shading}))`,
132
+ backdropFilter: `var(--modal-overlay-filter, blur(${this.#params.overlay_blur}))`,
115
133
  zIndex: ++this.#params.zIndex,
116
134
  transition: 'inherit',
117
135
  });
@@ -178,8 +196,10 @@ export default class Modal {
178
196
  own(this.content, this);
179
197
  this.content.style.transition = 'inherit';
180
198
 
181
- this.content.querySelectorAll(`[action="close"]`).forEach(close_button => {
182
- close_button.addEventListener('click', () => this.hide());
199
+ // Делегирование: кнопки, появившиеся или пересозданные позже (App, innerHTML), тоже работают.
200
+ this.content.addEventListener('click', e => {
201
+ let close_button = e.target.closest?.('[action="close"]');
202
+ if (close_button && this.content.contains(close_button)) this.hide();
183
203
  });
184
204
 
185
205
  let toggle = (params) => {
@@ -202,6 +222,13 @@ export default class Modal {
202
222
 
203
223
  params.after_func(params.data);
204
224
 
225
+ // show(), пришедший во время hiding, выполняется после полного закрытия.
226
+ if (params.hide && this.#pending_show) {
227
+ let pending = this.#pending_show;
228
+ this.#pending_show = null;
229
+ this.show(pending.data);
230
+ }
231
+
205
232
  }, this.#params.duration * 1000);
206
233
  });
207
234
  });
@@ -209,10 +236,19 @@ export default class Modal {
209
236
  }
210
237
 
211
238
  this.show = (data = null) => {
239
+ if (this.#target == 'hidden' && this.#state != 'hidden' && !this.#params.allow_interrupt) {
240
+ this.#pending_show = { data };
241
+ return;
242
+ }
243
+
212
244
  if (
213
245
  this.#state == 'hidden' ||
214
246
  (this.#params.allow_interrupt && this.#state == 'hiding')
215
247
  ) {
248
+ Modal.#open = Modal.#open.filter(modal => modal !== this);
249
+ Modal.#open.push(this);
250
+ this.#target = 'shown';
251
+
216
252
  toggle({
217
253
  before_func: this.before_show,
218
254
  after_func: this.on_show,
@@ -257,10 +293,15 @@ export default class Modal {
257
293
  };
258
294
 
259
295
  this.hide = (data = null) => {
296
+ this.#pending_show = null;
297
+
260
298
  if (
261
299
  this.#state == 'shown' ||
262
300
  (this.#params.allow_interrupt && this.#state == 'showing')
263
301
  ) {
302
+ Modal.#open = Modal.#open.filter(modal => modal !== this);
303
+ this.#target = 'hidden';
304
+
264
305
  toggle({
265
306
  before_func: this.before_hide,
266
307
  after_func: this.on_hide,
@@ -286,9 +327,7 @@ export default class Modal {
286
327
  };
287
328
 
288
329
  if (this.#params.close_by_esc)
289
- document.addEventListener('keydown', e => {
290
- if ((e.code === 'Escape' || e.keyCode === 27)) this.hide(e);
291
- });
330
+ Modal.#bind_esc();
292
331
 
293
332
  if (this.#params.trigger)
294
333
  document.querySelectorAll(this.#params.trigger).forEach(trigger => {
@@ -29,8 +29,14 @@ export default class Route {
29
29
  match_all: true,
30
30
  strict_mode: true,
31
31
 
32
+ // static — один разбор адреса при загрузке; history и hash — SPA:
33
+ // navigate(), переходы назад/вперёд, перехват ссылок.
34
+ mode: 'static',
35
+ intercept_links: true,
36
+
32
37
  before_init: () => {},
33
38
  on_init: () => {},
39
+ not_found: () => {},
34
40
 
35
41
  ...params
36
42
  };
@@ -52,9 +58,75 @@ export default class Route {
52
58
  else
53
59
  queueMicrotask(() => this.#dispatch());
54
60
 
61
+ if (this.#spa()) {
62
+ window.addEventListener(this.#params.mode === 'hash' ? 'hashchange' : 'popstate', () => this.#dispatch());
63
+
64
+ if (this.#params.intercept_links)
65
+ document.addEventListener('click', e => this.#intercept(e));
66
+ }
67
+
55
68
  this.on_init(this.#params);
56
69
  }
57
70
 
71
+ #spa() {
72
+ return this.#params.mode === 'history' || this.#params.mode === 'hash';
73
+ }
74
+
75
+ navigate(url, { replace = false } = {}) {
76
+ if (!this.#spa()) {
77
+ location[replace ? 'replace' : 'assign'](url);
78
+ return;
79
+ }
80
+
81
+ if (this.#params.mode === 'hash') {
82
+ let hash = '#' + String(url).replace(/^#/, '');
83
+ if (hash === location.hash) return this.#dispatch();
84
+ if (replace)
85
+ history.replaceState(history.state, '', hash);
86
+ else
87
+ history.pushState(history.state, '', hash);
88
+ } else
89
+ history[replace ? 'replaceState' : 'pushState'](null, '', url);
90
+
91
+ this.#dispatch();
92
+ }
93
+
94
+ #intercept(e) {
95
+ if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
96
+
97
+ let link = e.target.closest?.('a[href]');
98
+ if (!link || link.target && link.target !== '_self' || link.hasAttribute('download') || /\bexternal\b/.test(link.rel)) return;
99
+
100
+ let raw = link.getAttribute('href');
101
+ if (this.#params.mode === 'hash') {
102
+ if (!raw.startsWith('#')) return;
103
+ e.preventDefault();
104
+ this.navigate(raw);
105
+ return;
106
+ }
107
+
108
+ let url = new URL(link.href, location.href);
109
+ if (url.origin !== location.origin) return;
110
+ if (!url.pathname.startsWith('/' + this.#trim(this.#params.point))) return;
111
+ if (url.pathname === location.pathname && url.search === location.search && url.hash) return;
112
+
113
+ e.preventDefault();
114
+ this.navigate(url.pathname + url.search + url.hash);
115
+ }
116
+
117
+ // Текущий адрес: в hash-режиме путь и query живут после #.
118
+ #read() {
119
+ if (this.#params.mode !== 'hash')
120
+ return { path: location.pathname, search: location.search };
121
+
122
+ let hash = location.hash.slice(1) || '/';
123
+ let index = hash.indexOf('?');
124
+
125
+ return index === -1
126
+ ? { path: hash, search: '' }
127
+ : { path: hash.slice(0, index) || '/', search: hash.slice(index) };
128
+ }
129
+
58
130
  #current() {
59
131
  return this.#stack[this.#stack.length - 1];
60
132
  }
@@ -153,12 +225,17 @@ export default class Route {
153
225
  }
154
226
 
155
227
  #dispatch() {
156
- if (this.#dispatched) return;
228
+ if (this.#dispatched && !this.#spa()) return;
157
229
  this.#dispatched = true;
158
230
 
159
- this.#path = location.pathname;
231
+ let { path, search } = this.#read();
232
+
233
+ this.#path = path;
160
234
  this.#url = location.href;
161
- this.query = Object.fromEntries(new URLSearchParams(location.search));
235
+ this.query = Object.fromEntries(new URLSearchParams(search));
236
+ this.params = {};
237
+
238
+ let matched = false;
162
239
 
163
240
  this.#emit(this.#signal(), { name: this.#params.name, path: this.#path, query: this.query, url: this.#url });
164
241
 
@@ -174,6 +251,7 @@ export default class Route {
174
251
  if (route.middlewares.some(mid => !mid.call(this, ...values)))
175
252
  continue;
176
253
 
254
+ matched = true;
177
255
  route.closure.call(this, ...values);
178
256
 
179
257
  let detail = {
@@ -193,6 +271,11 @@ export default class Route {
193
271
 
194
272
  if (!this.#params.match_all) break;
195
273
  }
274
+
275
+ if (!matched) {
276
+ this.not_found(this.#path);
277
+ this.#emit(this.#matchEvent('404'), { name: this.#params.name, path: this.#path, query: this.query, url: this.#url });
278
+ }
196
279
  }
197
280
 
198
281
  get routes() { return this.#routes; }
@@ -280,16 +280,17 @@ export default class Uploader {
280
280
  });
281
281
  });
282
282
 
283
- target.querySelectorAll('*[add-button]').forEach(b => b.addEventListener('click', () => {
284
- let hidden = createFileInput();
283
+ let picker = createFileInput();
284
+ picker.removeAttribute('name');
285
285
 
286
- hidden.addEventListener('change', () => {
287
- handleFiles(hidden.files);
288
- });
286
+ picker.addEventListener('change', () => {
287
+ handleFiles(picker.files);
288
+ picker.value = '';
289
+ });
290
+
291
+ target.appendChild(picker);
289
292
 
290
- document.body.appendChild(hidden);
291
- hidden.click();
292
- }));
293
+ target.querySelectorAll('*[add-button]').forEach(b => b.addEventListener('click', () => picker.click()));
293
294
 
294
295
  target.on_init(params);
295
296
  });