ui-optago 1.0.4 → 1.1.1

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/ui-optago.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /* ============================================================================
2
- UI ÓPTAGO — Design System v1.0.4 (JavaScript)
2
+ UI ÓPTAGO: Design System v1.1.0 (JavaScript)
3
3
  Sem dependências de build. Carregue no <head> SEM defer para evitar
4
4
  flash de tema errado (FOUC):
5
5
 
@@ -42,11 +42,11 @@
42
42
 
43
43
  // Fonte única da versão exibida no badge do header e no rodapé
44
44
  // (elementos com o atributo data-op-version). Atualize só aqui.
45
- var VERSION = '1.0.4';
45
+ var VERSION = '1.1.1';
46
46
  OptagoUI.version = VERSION;
47
47
 
48
48
  /* --------------------------------------------------------------------------
49
- 1. TEMA — aplicado imediatamente (antes do primeiro paint)
49
+ 1. TEMA: aplicado imediatamente (antes do primeiro paint)
50
50
  -------------------------------------------------------------------------- */
51
51
  var THEME_KEY = 'op-theme';
52
52
 
@@ -138,7 +138,7 @@
138
138
  };
139
139
 
140
140
  /* --------------------------------------------------------------------------
141
- 4. TOASTS — OptagoUI.toast('Mensagem', 'success'|'error'|'warning'|'info')
141
+ 4. TOASTS: OptagoUI.toast('Mensagem', 'success'|'error'|'warning'|'info')
142
142
  -------------------------------------------------------------------------- */
143
143
  var TOAST_ICONS = {
144
144
  success: 'ph-check-circle',
@@ -166,11 +166,17 @@
166
166
  toast.innerHTML =
167
167
  '<div class="op-toast-icon"><i class="ph ' + (TOAST_ICONS[type] || TOAST_ICONS.info) + '"></i></div>' +
168
168
  '<span></span>' +
169
- '<button class="op-toast-close" aria-label="Fechar"><i class="ph ph-x"></i></button>';
169
+ '<button class="op-toast-close" aria-label="Fechar"><i class="ph ph-x"></i></button>' +
170
+ '<div class="op-toast-progress"><div class="op-toast-progress-bar"></div></div>';
170
171
  toast.querySelector('span').textContent = message;
171
172
  container.appendChild(toast);
172
173
 
174
+ var bar = toast.querySelector('.op-toast-progress-bar');
173
175
  var removed = false;
176
+ var timer = null;
177
+ var remaining = duration;
178
+ var startedAt = 0;
179
+
174
180
  var dismiss = function () {
175
181
  if (removed) return;
176
182
  removed = true;
@@ -178,13 +184,43 @@
178
184
  setTimeout(function () { toast.remove(); }, 320);
179
185
  };
180
186
 
187
+ // Anima a barra de largura atual até 0 ao longo de `ms`; usar
188
+ // transition (não @keyframes) permite congelar no valor exato em pause().
189
+ var run = function (ms) {
190
+ startedAt = Date.now();
191
+ timer = setTimeout(dismiss, ms);
192
+ requestAnimationFrame(function () {
193
+ bar.style.transitionDuration = ms + 'ms';
194
+ bar.style.width = '0%';
195
+ });
196
+ };
197
+
198
+ var pause = function () {
199
+ if (removed || !timer) return;
200
+ clearTimeout(timer);
201
+ timer = null;
202
+ remaining -= Date.now() - startedAt;
203
+ // Congela a barra no ponto exato em px (transition-duration 0 = sem salto).
204
+ bar.style.width = getComputedStyle(bar).width;
205
+ bar.style.transitionDuration = '0ms';
206
+ };
207
+
208
+ var resume = function () {
209
+ if (removed || timer || remaining <= 0) return;
210
+ run(remaining);
211
+ };
212
+
213
+ toast.addEventListener('mouseenter', pause);
214
+ toast.addEventListener('mouseleave', resume);
215
+ toast.addEventListener('focusin', pause);
216
+ toast.addEventListener('focusout', resume);
181
217
  toast.querySelector('.op-toast-close').addEventListener('click', dismiss);
182
218
 
183
219
  requestAnimationFrame(function () {
184
220
  requestAnimationFrame(function () { toast.classList.add('is-visible'); });
185
221
  });
186
222
 
187
- setTimeout(dismiss, duration);
223
+ run(duration);
188
224
 
189
225
  return toast;
190
226
  };
@@ -209,7 +245,7 @@
209
245
  };
210
246
 
211
247
  /* --------------------------------------------------------------------------
212
- 5b. TABULATOR — tabela avançada com tema Óptago
248
+ 5b. TABULATOR: tabela avançada com tema Óptago
213
249
  Uso: OptagoUI.tabulator('#minhaTabela', { data: [...], columns: [...] })
214
250
  Carrega a biblioteca (CDN) sob demanda; o CSS base do Tabulator é
215
251
  inserido ANTES do ui-optago.css para que o tema personalizado
@@ -257,7 +293,7 @@
257
293
  };
258
294
 
259
295
  /* --------------------------------------------------------------------------
260
- 5c. TELA DE CARREGAMENTO — overlay em tela cheia
296
+ 5c. TELA DE CARREGAMENTO: overlay em tela cheia
261
297
  Uso: OptagoUI.showLoading('Salvando...') ... OptagoUI.hideLoading()
262
298
  ou data-op-loading-form no <form> para acionar sozinho no submit.
263
299
  -------------------------------------------------------------------------- */
@@ -304,7 +340,7 @@
304
340
  });
305
341
 
306
342
  /* --------------------------------------------------------------------------
307
- 6. HELPERS DO SHOWCASE — copiar HTML / ver código
343
+ 6. HELPERS DO SHOWCASE: copiar HTML / ver código
308
344
  -------------------------------------------------------------------------- */
309
345
  function dedentHTML(el) {
310
346
  var html = el.innerHTML.replace(/^\n+|\s+$/g, '');
@@ -457,6 +493,75 @@
457
493
  }
458
494
  return;
459
495
  }
496
+
497
+ // Combobox: clique numa opção seleciona; clique fora fecha.
498
+ t = e.target.closest('[data-op-combobox-option]');
499
+ if (t && !t.hasAttribute('hidden')) { selectComboboxOption(t); return; }
500
+ if (!e.target.closest('[data-op-combobox]')) {
501
+ document.querySelectorAll('.op-combobox.is-open').forEach(closeCombobox);
502
+ }
503
+ });
504
+
505
+ /* --------------------------------------------------------------------------
506
+ 7b. COMBOBOX: input com filtro de opções (autocomplete)
507
+ -------------------------------------------------------------------------- */
508
+ function comboboxOptions(box) {
509
+ var menu = box.querySelector('[data-op-combobox-menu]');
510
+ return menu ? Array.prototype.slice.call(menu.querySelectorAll('[data-op-combobox-option]')) : [];
511
+ }
512
+
513
+ function visibleComboboxOptions(box) {
514
+ return comboboxOptions(box).filter(function (o) { return !o.hasAttribute('hidden'); });
515
+ }
516
+
517
+ function openCombobox(box) {
518
+ box.classList.add('is-open');
519
+ }
520
+
521
+ function closeCombobox(box) {
522
+ box.classList.remove('is-open');
523
+ comboboxOptions(box).forEach(function (o) { o.classList.remove('is-active'); });
524
+ }
525
+
526
+ function selectComboboxOption(option) {
527
+ var box = option.closest('[data-op-combobox]');
528
+ var input = box.querySelector('[data-op-combobox-input]');
529
+ comboboxOptions(box).forEach(function (o) { o.classList.remove('is-selected'); });
530
+ option.classList.add('is-selected');
531
+ if (input) {
532
+ input.value = option.textContent.trim();
533
+ box.dataset.value = option.getAttribute('data-value') || '';
534
+ input.dispatchEvent(new Event('change', { bubbles: true }));
535
+ }
536
+ closeCombobox(box);
537
+ }
538
+
539
+ function filterCombobox(box) {
540
+ var input = box.querySelector('[data-op-combobox-input]');
541
+ var empty = box.querySelector('.op-combobox-empty');
542
+ var query = (input.value || '').trim().toLowerCase();
543
+ var anyVisible = false;
544
+ comboboxOptions(box).forEach(function (o) {
545
+ var match = o.textContent.toLowerCase().indexOf(query) !== -1;
546
+ o.toggleAttribute('hidden', !match);
547
+ o.classList.remove('is-active');
548
+ if (match) anyVisible = true;
549
+ });
550
+ if (empty) empty.hidden = anyVisible;
551
+ }
552
+
553
+ document.addEventListener('input', function (e) {
554
+ var input = e.target.closest('[data-op-combobox-input]');
555
+ if (!input) return;
556
+ var box = input.closest('[data-op-combobox]');
557
+ filterCombobox(box);
558
+ openCombobox(box);
559
+ });
560
+
561
+ document.addEventListener('focusin', function (e) {
562
+ var input = e.target.closest('[data-op-combobox-input]');
563
+ if (!input) return;
564
+ openCombobox(input.closest('[data-op-combobox]'));
460
565
  });
461
566
 
462
567
  // Tooltip da sidebar colapsada: o overflow-y da nav cortaria um tooltip
@@ -481,12 +586,37 @@
481
586
  OptagoUI.showLoading(form.getAttribute('data-op-loading-message'));
482
587
  });
483
588
 
484
- // ESC fecha modais e dropdowns
589
+ // ESC fecha modais, dropdowns e comboboxes
485
590
  document.addEventListener('keydown', function (e) {
486
- if (e.key !== 'Escape') return;
487
- document.querySelectorAll('.op-modal.is-open').forEach(function (m) { OptagoUI.closeModal(m); });
488
- document.querySelectorAll('.op-dropdown.is-open').forEach(function (d) { d.classList.remove('is-open'); });
489
- OptagoUI.closeSidebarMobile();
591
+ if (e.key === 'Escape') {
592
+ document.querySelectorAll('.op-modal.is-open').forEach(function (m) { OptagoUI.closeModal(m); });
593
+ document.querySelectorAll('.op-dropdown.is-open').forEach(function (d) { d.classList.remove('is-open'); });
594
+ document.querySelectorAll('.op-combobox.is-open').forEach(closeCombobox);
595
+ OptagoUI.closeSidebarMobile();
596
+ return;
597
+ }
598
+
599
+ // Navegação por teclado dentro de um combobox aberto
600
+ var comboInput = e.target.closest('[data-op-combobox-input]');
601
+ if (!comboInput) return;
602
+ var box = comboInput.closest('[data-op-combobox]');
603
+ if (!box.classList.contains('is-open')) return;
604
+
605
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
606
+ e.preventDefault();
607
+ var opts = visibleComboboxOptions(box);
608
+ if (!opts.length) return;
609
+ var current = opts.findIndex(function (o) { return o.classList.contains('is-active'); });
610
+ var next = e.key === 'ArrowDown' ? current + 1 : current - 1;
611
+ if (next < 0) next = opts.length - 1;
612
+ if (next >= opts.length) next = 0;
613
+ opts.forEach(function (o) { o.classList.remove('is-active'); });
614
+ opts[next].classList.add('is-active');
615
+ opts[next].scrollIntoView({ block: 'nearest' });
616
+ } else if (e.key === 'Enter') {
617
+ var active = visibleComboboxOptions(box).filter(function (o) { return o.classList.contains('is-active'); })[0];
618
+ if (active) { e.preventDefault(); selectComboboxOption(active); }
619
+ }
490
620
  });
491
621
 
492
622
  /* --------------------------------------------------------------------------
@@ -499,7 +629,7 @@
499
629
  });
500
630
 
501
631
  // Toasts vindos do servidor (flash messages), sem precisar de <script>
502
- // inline — importante para funcionar com Content-Security-Policy estrito.
632
+ // inline: importante para funcionar com Content-Security-Policy estrito.
503
633
  // Uso: <div hidden data-op-toast='[{"type":"success","message":"Oi!"}]'></div>
504
634
  document.querySelectorAll('[data-op-toast]').forEach(function (el) {
505
635
  try {