ui-optago 1.1.0 → 1.2.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/ui-optago.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /* ============================================================================
2
- UI ÓPTAGO: Design System v1.1.0 (JavaScript)
2
+ UI ÓPTAGO: Design System v1.2.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
 
@@ -31,6 +31,15 @@
31
31
  partir de flash messages do servidor,
32
32
  sem precisar de <script> inline (CSP-safe)
33
33
 
34
+ Tema compartilhado entre subdomínios: adicione data-op-theme-domain no
35
+ próprio <script src="ui-optago.js"> pra o tema virar cookie em vez de só
36
+ localStorage, e todo app sob esse domínio (ex.: app1.seusite.com,
37
+ app2.seusite.com) passa a enxergar a mesma escolha de tema:
38
+
39
+ <script src="ui-optago.js" data-op-theme-domain=".seusite.com"></script>
40
+
41
+ Sem esse atributo, o tema continua só em localStorage (por app, como antes).
42
+
34
43
  API global: OptagoUI.toast(msg, tipo, ms) · OptagoUI.openModal(sel)
35
44
  OptagoUI.closeModal(sel) · OptagoUI.setTheme('dark'|'light')
36
45
  OptagoUI.showLoading(msg) · OptagoUI.hideLoading()
@@ -42,7 +51,7 @@
42
51
 
43
52
  // Fonte única da versão exibida no badge do header e no rodapé
44
53
  // (elementos com o atributo data-op-version). Atualize só aqui.
45
- var VERSION = '1.1.0';
54
+ var VERSION = '1.2.0';
46
55
  OptagoUI.version = VERSION;
47
56
 
48
57
  /* --------------------------------------------------------------------------
@@ -50,8 +59,26 @@
50
59
  -------------------------------------------------------------------------- */
51
60
  var THEME_KEY = 'op-theme';
52
61
 
62
+ // document.currentScript só é confiável em scripts síncronos (sem defer/
63
+ // async), que é exatamente como esta lib precisa ser carregada mesmo.
64
+ var THEME_DOMAIN = (function () {
65
+ var s = document.currentScript;
66
+ return (s && s.getAttribute('data-op-theme-domain')) || null;
67
+ })();
68
+
69
+ function readCookie(name) {
70
+ var m = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
71
+ return m ? decodeURIComponent(m[1]) : null;
72
+ }
73
+
74
+ function writeCookie(name, value) {
75
+ var attrs = 'path=/; max-age=31536000; SameSite=Lax; domain=' + THEME_DOMAIN;
76
+ if (window.location.protocol === 'https:') attrs += '; Secure';
77
+ document.cookie = name + '=' + encodeURIComponent(value) + '; ' + attrs;
78
+ }
79
+
53
80
  function currentTheme() {
54
- var saved = localStorage.getItem(THEME_KEY);
81
+ var saved = (THEME_DOMAIN && readCookie(THEME_KEY)) || localStorage.getItem(THEME_KEY);
55
82
  if (saved === 'light' || saved === 'dark') return saved;
56
83
  return window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
57
84
  }
@@ -59,6 +86,7 @@
59
86
  OptagoUI.setTheme = function (theme) {
60
87
  document.documentElement.classList.toggle('light', theme === 'light');
61
88
  localStorage.setItem(THEME_KEY, theme);
89
+ if (THEME_DOMAIN) writeCookie(THEME_KEY, theme);
62
90
  document.querySelectorAll('[data-op-theme-toggle]').forEach(function (btn) {
63
91
  var moon = btn.querySelector('.op-icon-moon');
64
92
  var sun = btn.querySelector('.op-icon-sun');
@@ -493,6 +521,75 @@
493
521
  }
494
522
  return;
495
523
  }
524
+
525
+ // Combobox: clique numa opção seleciona; clique fora fecha.
526
+ t = e.target.closest('[data-op-combobox-option]');
527
+ if (t && !t.hasAttribute('hidden')) { selectComboboxOption(t); return; }
528
+ if (!e.target.closest('[data-op-combobox]')) {
529
+ document.querySelectorAll('.op-combobox.is-open').forEach(closeCombobox);
530
+ }
531
+ });
532
+
533
+ /* --------------------------------------------------------------------------
534
+ 7b. COMBOBOX: input com filtro de opções (autocomplete)
535
+ -------------------------------------------------------------------------- */
536
+ function comboboxOptions(box) {
537
+ var menu = box.querySelector('[data-op-combobox-menu]');
538
+ return menu ? Array.prototype.slice.call(menu.querySelectorAll('[data-op-combobox-option]')) : [];
539
+ }
540
+
541
+ function visibleComboboxOptions(box) {
542
+ return comboboxOptions(box).filter(function (o) { return !o.hasAttribute('hidden'); });
543
+ }
544
+
545
+ function openCombobox(box) {
546
+ box.classList.add('is-open');
547
+ }
548
+
549
+ function closeCombobox(box) {
550
+ box.classList.remove('is-open');
551
+ comboboxOptions(box).forEach(function (o) { o.classList.remove('is-active'); });
552
+ }
553
+
554
+ function selectComboboxOption(option) {
555
+ var box = option.closest('[data-op-combobox]');
556
+ var input = box.querySelector('[data-op-combobox-input]');
557
+ comboboxOptions(box).forEach(function (o) { o.classList.remove('is-selected'); });
558
+ option.classList.add('is-selected');
559
+ if (input) {
560
+ input.value = option.textContent.trim();
561
+ box.dataset.value = option.getAttribute('data-value') || '';
562
+ input.dispatchEvent(new Event('change', { bubbles: true }));
563
+ }
564
+ closeCombobox(box);
565
+ }
566
+
567
+ function filterCombobox(box) {
568
+ var input = box.querySelector('[data-op-combobox-input]');
569
+ var empty = box.querySelector('.op-combobox-empty');
570
+ var query = (input.value || '').trim().toLowerCase();
571
+ var anyVisible = false;
572
+ comboboxOptions(box).forEach(function (o) {
573
+ var match = o.textContent.toLowerCase().indexOf(query) !== -1;
574
+ o.toggleAttribute('hidden', !match);
575
+ o.classList.remove('is-active');
576
+ if (match) anyVisible = true;
577
+ });
578
+ if (empty) empty.hidden = anyVisible;
579
+ }
580
+
581
+ document.addEventListener('input', function (e) {
582
+ var input = e.target.closest('[data-op-combobox-input]');
583
+ if (!input) return;
584
+ var box = input.closest('[data-op-combobox]');
585
+ filterCombobox(box);
586
+ openCombobox(box);
587
+ });
588
+
589
+ document.addEventListener('focusin', function (e) {
590
+ var input = e.target.closest('[data-op-combobox-input]');
591
+ if (!input) return;
592
+ openCombobox(input.closest('[data-op-combobox]'));
496
593
  });
497
594
 
498
595
  // Tooltip da sidebar colapsada: o overflow-y da nav cortaria um tooltip
@@ -517,12 +614,37 @@
517
614
  OptagoUI.showLoading(form.getAttribute('data-op-loading-message'));
518
615
  });
519
616
 
520
- // ESC fecha modais e dropdowns
617
+ // ESC fecha modais, dropdowns e comboboxes
521
618
  document.addEventListener('keydown', function (e) {
522
- if (e.key !== 'Escape') return;
523
- document.querySelectorAll('.op-modal.is-open').forEach(function (m) { OptagoUI.closeModal(m); });
524
- document.querySelectorAll('.op-dropdown.is-open').forEach(function (d) { d.classList.remove('is-open'); });
525
- OptagoUI.closeSidebarMobile();
619
+ if (e.key === 'Escape') {
620
+ document.querySelectorAll('.op-modal.is-open').forEach(function (m) { OptagoUI.closeModal(m); });
621
+ document.querySelectorAll('.op-dropdown.is-open').forEach(function (d) { d.classList.remove('is-open'); });
622
+ document.querySelectorAll('.op-combobox.is-open').forEach(closeCombobox);
623
+ OptagoUI.closeSidebarMobile();
624
+ return;
625
+ }
626
+
627
+ // Navegação por teclado dentro de um combobox aberto
628
+ var comboInput = e.target.closest('[data-op-combobox-input]');
629
+ if (!comboInput) return;
630
+ var box = comboInput.closest('[data-op-combobox]');
631
+ if (!box.classList.contains('is-open')) return;
632
+
633
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
634
+ e.preventDefault();
635
+ var opts = visibleComboboxOptions(box);
636
+ if (!opts.length) return;
637
+ var current = opts.findIndex(function (o) { return o.classList.contains('is-active'); });
638
+ var next = e.key === 'ArrowDown' ? current + 1 : current - 1;
639
+ if (next < 0) next = opts.length - 1;
640
+ if (next >= opts.length) next = 0;
641
+ opts.forEach(function (o) { o.classList.remove('is-active'); });
642
+ opts[next].classList.add('is-active');
643
+ opts[next].scrollIntoView({ block: 'nearest' });
644
+ } else if (e.key === 'Enter') {
645
+ var active = visibleComboboxOptions(box).filter(function (o) { return o.classList.contains('is-active'); })[0];
646
+ if (active) { e.preventDefault(); selectComboboxOption(active); }
647
+ }
526
648
  });
527
649
 
528
650
  /* --------------------------------------------------------------------------