@customviews-js/customviews 1.0.3 → 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.
Files changed (52) hide show
  1. package/README.md +362 -103
  2. package/dist/{custom-views.cjs.js → custom-views.core.cjs.js} +1708 -891
  3. package/dist/custom-views.core.cjs.js.map +1 -0
  4. package/dist/custom-views.core.esm.js +2413 -0
  5. package/dist/custom-views.core.esm.js.map +1 -0
  6. package/dist/custom-views.esm.js +1708 -890
  7. package/dist/custom-views.esm.js.map +1 -1
  8. package/dist/{custom-views.umd.js → custom-views.js} +1707 -890
  9. package/dist/custom-views.js.map +1 -0
  10. package/dist/custom-views.min.js +7 -0
  11. package/dist/custom-views.min.js.map +1 -0
  12. package/dist/types/{models/AssetsManager.d.ts → core/assets-manager.d.ts} +1 -1
  13. package/dist/types/{models/AssetsManager.d.ts.map → core/assets-manager.d.ts.map} +1 -1
  14. package/dist/types/core/core.d.ts +25 -9
  15. package/dist/types/core/core.d.ts.map +1 -1
  16. package/dist/types/core/custom-elements.d.ts +8 -0
  17. package/dist/types/core/custom-elements.d.ts.map +1 -0
  18. package/dist/types/core/render.d.ts +4 -1
  19. package/dist/types/core/render.d.ts.map +1 -1
  20. package/dist/types/core/tab-manager.d.ts +35 -0
  21. package/dist/types/core/tab-manager.d.ts.map +1 -0
  22. package/dist/types/core/toggle-manager.d.ts +28 -0
  23. package/dist/types/core/toggle-manager.d.ts.map +1 -0
  24. package/dist/types/core/url-state-manager.d.ts.map +1 -1
  25. package/dist/types/core/visibility-manager.d.ts +1 -1
  26. package/dist/types/core/widget.d.ts +2 -0
  27. package/dist/types/core/widget.d.ts.map +1 -1
  28. package/dist/types/entry/browser-entry.d.ts +13 -0
  29. package/dist/types/entry/browser-entry.d.ts.map +1 -0
  30. package/dist/types/index.d.ts +11 -21
  31. package/dist/types/index.d.ts.map +1 -1
  32. package/dist/types/lib/custom-views.d.ts +29 -0
  33. package/dist/types/lib/custom-views.d.ts.map +1 -0
  34. package/dist/types/styles/styles.d.ts +2 -0
  35. package/dist/types/styles/styles.d.ts.map +1 -1
  36. package/dist/types/styles/tab-styles.d.ts +5 -0
  37. package/dist/types/styles/tab-styles.d.ts.map +1 -0
  38. package/dist/types/styles/toggle-styles.d.ts +5 -0
  39. package/dist/types/styles/toggle-styles.d.ts.map +1 -0
  40. package/dist/types/styles/widget-styles.d.ts +1 -1
  41. package/dist/types/styles/widget-styles.d.ts.map +1 -1
  42. package/dist/types/types/types.d.ts +85 -0
  43. package/dist/types/types/types.d.ts.map +1 -1
  44. package/dist/types/utils/url-utils.d.ts +8 -0
  45. package/dist/types/utils/url-utils.d.ts.map +1 -0
  46. package/package.json +61 -57
  47. package/dist/custom-views.cjs.js.map +0 -1
  48. package/dist/custom-views.umd.js.map +0 -1
  49. package/dist/custom-views.umd.min.js +0 -7
  50. package/dist/custom-views.umd.min.js.map +0 -1
  51. package/dist/types/models/Config.d.ts +0 -10
  52. package/dist/types/models/Config.d.ts.map +0 -1
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @customviews-js/customviews v1.0.2
2
+ * @customviews-js/customviews v1.1.1
3
3
  * (c) 2025 Chan Ger Teck
4
4
  * Released under the MIT License.
5
5
  */
@@ -9,95 +9,6 @@
9
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CustomViews = {}));
10
10
  })(this, (function (exports) { 'use strict';
11
11
 
12
- /** --- Basic renderers --- */
13
- function renderImage(el, asset) {
14
- if (!asset.src)
15
- return;
16
- el.innerHTML = '';
17
- const img = document.createElement('img');
18
- img.src = asset.src;
19
- img.alt = asset.alt || '';
20
- // Apply custom styling if provided
21
- if (asset.className) {
22
- img.className = asset.className;
23
- }
24
- if (asset.style) {
25
- img.setAttribute('style', asset.style);
26
- }
27
- // Default styles (can be overridden by asset.style)
28
- img.style.maxWidth = img.style.maxWidth || '100%';
29
- img.style.height = img.style.height || 'auto';
30
- img.style.display = img.style.display || 'block';
31
- el.appendChild(img);
32
- }
33
- function renderText(el, asset) {
34
- if (asset.content != null) {
35
- el.textContent = asset.content;
36
- }
37
- // Apply custom styling if provided
38
- if (asset.className) {
39
- el.className = asset.className;
40
- }
41
- if (asset.style) {
42
- el.setAttribute('style', asset.style);
43
- }
44
- }
45
- function renderHtml(el, asset) {
46
- if (asset.content != null) {
47
- el.innerHTML = asset.content;
48
- }
49
- // Apply custom styling if provided
50
- if (asset.className) {
51
- el.className = asset.className;
52
- }
53
- if (asset.style) {
54
- el.setAttribute('style', asset.style);
55
- }
56
- }
57
- /** --- Unified asset renderer --- */
58
- function detectAssetType(asset) {
59
- // If src exists, it's an image
60
- if (asset.src)
61
- return 'image';
62
- // If content contains HTML tags, it's HTML
63
- if (asset.content && /<[^>]+>/.test(asset.content)) {
64
- return 'html';
65
- }
66
- return 'text';
67
- }
68
- function renderAssetInto(el, assetId, assetsManager) {
69
- const asset = assetsManager.get(assetId);
70
- if (!asset)
71
- return;
72
- const type = asset.type || detectAssetType(asset);
73
- switch (type) {
74
- case 'image':
75
- renderImage(el, asset);
76
- break;
77
- case 'text':
78
- renderText(el, asset);
79
- break;
80
- case 'html':
81
- renderHtml(el, asset);
82
- break;
83
- default:
84
- el.innerHTML = asset.content || String(asset);
85
- console.warn('[CustomViews] Unknown asset type:', type);
86
- }
87
- }
88
-
89
- /**
90
- * Configuration for the site, has default state and list of toggles
91
- */
92
- class Config {
93
- defaultState;
94
- allToggles;
95
- constructor(defaultState, allToggles) {
96
- this.defaultState = defaultState;
97
- this.allToggles = allToggles;
98
- }
99
- }
100
-
101
12
  /**
102
13
  * Manages persistence of custom views state using browser localStorage
103
14
  */
@@ -227,6 +138,10 @@
227
138
  const compact = {
228
139
  t: state.toggles
229
140
  };
141
+ // Add tab groups if present
142
+ if (state.tabs && Object.keys(state.tabs).length > 0) {
143
+ compact.g = Object.entries(state.tabs);
144
+ }
230
145
  // Convert to JSON and encode
231
146
  const json = JSON.stringify(compact);
232
147
  let encoded;
@@ -273,9 +188,19 @@
273
188
  if (!compact || typeof compact !== 'object') {
274
189
  throw new Error('Invalid compact state structure');
275
190
  }
276
- return {
191
+ const state = {
277
192
  toggles: Array.isArray(compact.t) ? compact.t : []
278
193
  };
194
+ // Reconstruct tabs from compact format
195
+ if (Array.isArray(compact.g)) {
196
+ state.tabs = {};
197
+ for (const [groupId, tabId] of compact.g) {
198
+ if (typeof groupId === 'string' && typeof tabId === 'string') {
199
+ state.tabs[groupId] = tabId;
200
+ }
201
+ }
202
+ }
203
+ return state;
279
204
  }
280
205
  catch (error) {
281
206
  console.warn('Failed to decode view state:', error);
@@ -330,7 +255,7 @@
330
255
  }
331
256
  /**
332
257
  * Apply simple class-based visibility to a toggle element.
333
- * The element is assumed to have data-customviews-toggle.
258
+ * The element is assumed to have data-cv-toggle or data-customviews-toggle.
334
259
  */
335
260
  applyElementVisibility(el, visible) {
336
261
  if (visible) {
@@ -344,34 +269,581 @@
344
269
  }
345
270
  }
346
271
 
347
- const CORE_STYLES = `
348
- [data-customviews-toggle] {
349
- transition: opacity 150ms ease,
350
- transform 150ms ease,
351
- max-height 200ms ease,
352
- margin 150ms ease;
353
- will-change: opacity, transform, max-height, margin;
354
- }
355
-
356
- .cv-visible {
357
- opacity: 1 !important;
358
- transform: translateY(0) !important;
359
- max-height: var(--cv-max-height, 9999px) !important;
360
- }
361
-
362
- .cv-hidden {
363
- opacity: 0 !important;
364
- transform: translateY(-4px) !important;
365
- pointer-events: none !important;
366
- padding-top: 0 !important;
367
- padding-bottom: 0 !important;
368
- border-top-width: 0 !important;
369
- border-bottom-width: 0 !important;
370
- max-height: 0 !important;
371
- margin-top: 0 !important;
372
- margin-bottom: 0 !important;
373
- overflow: hidden !important;
374
- }
272
+ /** --- Icon utilities --- */
273
+ function ensureFontAwesomeInjected() {
274
+ const isFontAwesomeLoaded = Array.from(document.styleSheets).some(sheet => sheet.href && (sheet.href.includes('font-awesome') || sheet.href.includes('fontawesome')));
275
+ if (isFontAwesomeLoaded)
276
+ return;
277
+ const link = document.createElement('link');
278
+ link.rel = 'stylesheet';
279
+ link.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css';
280
+ link.setAttribute('data-customviews-fontawesome', 'true');
281
+ document.head.appendChild(link);
282
+ }
283
+ function replaceIconShortcodes(text) {
284
+ // Handle Font Awesome shortcodes
285
+ return text.replace(/:fa-([\w-]+):/g, (_, icon) => `<i class="fa fa-${icon}"></i>`);
286
+ }
287
+ /** --- Basic renderers --- */
288
+ function renderImage(el, asset) {
289
+ if (!asset.src)
290
+ return;
291
+ el.innerHTML = '';
292
+ const img = document.createElement('img');
293
+ img.src = asset.src;
294
+ img.alt = asset.alt || '';
295
+ // Apply custom styling if provided
296
+ if (asset.className) {
297
+ img.className = asset.className;
298
+ }
299
+ if (asset.style) {
300
+ img.setAttribute('style', asset.style);
301
+ }
302
+ // Default styles (can be overridden by asset.style)
303
+ img.style.maxWidth = img.style.maxWidth || '100%';
304
+ img.style.height = img.style.height || 'auto';
305
+ img.style.display = img.style.display || 'block';
306
+ el.appendChild(img);
307
+ }
308
+ function renderText(el, asset) {
309
+ if (asset.content != null) {
310
+ el.textContent = asset.content;
311
+ }
312
+ // Apply custom styling if provided
313
+ if (asset.className) {
314
+ el.className = asset.className;
315
+ }
316
+ if (asset.style) {
317
+ el.setAttribute('style', asset.style);
318
+ }
319
+ }
320
+ function renderHtml(el, asset) {
321
+ if (asset.content != null) {
322
+ el.innerHTML = asset.content;
323
+ }
324
+ // Apply custom styling if provided
325
+ if (asset.className) {
326
+ el.className = asset.className;
327
+ }
328
+ if (asset.style) {
329
+ el.setAttribute('style', asset.style);
330
+ }
331
+ }
332
+ /** --- Unified asset renderer --- */
333
+ function detectAssetType(asset) {
334
+ // If src exists, it's an image
335
+ if (asset.src)
336
+ return 'image';
337
+ // If content contains HTML tags, it's HTML
338
+ if (asset.content && /<[^>]+>/.test(asset.content)) {
339
+ return 'html';
340
+ }
341
+ return 'text';
342
+ }
343
+ function renderAssetInto(el, assetId, assetsManager) {
344
+ const asset = assetsManager.get(assetId);
345
+ if (!asset)
346
+ return;
347
+ const type = asset.type || detectAssetType(asset);
348
+ switch (type) {
349
+ case 'image':
350
+ renderImage(el, asset);
351
+ break;
352
+ case 'text':
353
+ renderText(el, asset);
354
+ break;
355
+ case 'html':
356
+ renderHtml(el, asset);
357
+ break;
358
+ default:
359
+ el.innerHTML = asset.content || String(asset);
360
+ console.warn('[CustomViews] Unknown asset type:', type);
361
+ }
362
+ }
363
+
364
+ // Constants for selectors
365
+ const TABGROUP_SELECTOR = 'cv-tabgroup';
366
+ const TAB_SELECTOR = 'cv-tab';
367
+ const NAV_AUTO_SELECTOR = 'cv-tabgroup[nav="auto"], cv-tabgroup:not([nav])';
368
+ const NAV_CONTAINER_CLASS = 'cv-tabs-nav';
369
+ /**
370
+ * TabManager handles discovery, visibility, and navigation for tab groups and tabs
371
+ */
372
+ class TabManager {
373
+ /**
374
+ * Apply tab selections to all tab groups in the DOM
375
+ */
376
+ static applySelections(rootEl, tabs, cfgGroups) {
377
+ // Find all cv-tabgroup elements
378
+ const tabGroups = rootEl.querySelectorAll(TABGROUP_SELECTOR);
379
+ tabGroups.forEach((groupEl) => {
380
+ const groupId = groupEl.getAttribute('id');
381
+ if (!groupId)
382
+ return;
383
+ // Determine the active tab for this group
384
+ const activeTabId = this.resolveActiveTab(groupId, tabs, cfgGroups, groupEl);
385
+ // Apply visibility to direct child cv-tab elements only (not nested ones)
386
+ const tabElements = Array.from(groupEl.children).filter((child) => child.tagName.toLowerCase() === TAB_SELECTOR);
387
+ tabElements.forEach((tabEl) => {
388
+ const tabId = tabEl.getAttribute('id');
389
+ if (!tabId)
390
+ return;
391
+ const isActive = tabId === activeTabId;
392
+ this.applyTabVisibility(tabEl, isActive);
393
+ });
394
+ });
395
+ }
396
+ /**
397
+ * Resolve the active tab for a group based on state, config, and DOM
398
+ */
399
+ static resolveActiveTab(groupId, tabs, cfgGroups, groupEl) {
400
+ // 1. Check state
401
+ if (tabs[groupId]) {
402
+ return tabs[groupId];
403
+ }
404
+ // 2. Check config default
405
+ if (cfgGroups) {
406
+ const groupCfg = cfgGroups.find(g => g.id === groupId);
407
+ if (groupCfg) {
408
+ if (groupCfg.default) {
409
+ return groupCfg.default;
410
+ }
411
+ // Fallback to first tab in config
412
+ const firstConfigTab = groupCfg.tabs[0];
413
+ if (firstConfigTab) {
414
+ return firstConfigTab.id;
415
+ }
416
+ }
417
+ }
418
+ // 3. Fallback to first direct cv-tab child in DOM
419
+ const firstTab = Array.from(groupEl.children).find((child) => child.tagName.toLowerCase() === TAB_SELECTOR);
420
+ if (firstTab) {
421
+ return firstTab.getAttribute('id');
422
+ }
423
+ return null;
424
+ }
425
+ /**
426
+ * Apply visibility classes to a tab element
427
+ */
428
+ static applyTabVisibility(tabEl, isActive) {
429
+ if (isActive) {
430
+ tabEl.classList.remove('cv-hidden');
431
+ tabEl.classList.add('cv-visible');
432
+ }
433
+ else {
434
+ tabEl.classList.add('cv-hidden');
435
+ tabEl.classList.remove('cv-visible');
436
+ }
437
+ }
438
+ /**
439
+ * Build navigation for tab groups with nav="auto" (one-time setup)
440
+ */
441
+ static buildNavs(rootEl, cfgGroups, onTabClick) {
442
+ // Find all cv-tabgroup elements with nav="auto" or no nav attribute
443
+ const tabGroups = rootEl.querySelectorAll(NAV_AUTO_SELECTOR);
444
+ // Check if any tab headers contain Font Awesome shortcodes
445
+ // Inject Font Awesome CSS only if needed
446
+ let hasFontAwesomeShortcodes = false;
447
+ tabGroups.forEach((groupEl) => {
448
+ const groupId = groupEl.getAttribute('id');
449
+ if (!groupId)
450
+ return;
451
+ const tabElements = Array.from(groupEl.children).filter((child) => child.tagName.toLowerCase() === 'cv-tab');
452
+ tabElements.forEach((tabEl) => {
453
+ const tabId = tabEl.getAttribute('id');
454
+ if (!tabId)
455
+ return;
456
+ const header = tabEl.getAttribute('header') || this.getTabLabel(tabId, groupId, cfgGroups) || tabId;
457
+ if (/:fa-[\w-]+:/.test(header)) {
458
+ hasFontAwesomeShortcodes = true;
459
+ }
460
+ });
461
+ });
462
+ // Inject Font Awesome only if shortcodes are found
463
+ if (hasFontAwesomeShortcodes) {
464
+ ensureFontAwesomeInjected();
465
+ }
466
+ tabGroups.forEach((groupEl) => {
467
+ const groupId = groupEl.getAttribute('id');
468
+ if (!groupId)
469
+ return;
470
+ // Check if nav already exists - if so, skip building
471
+ let navContainer = groupEl.querySelector(`.${NAV_CONTAINER_CLASS}`);
472
+ if (navContainer)
473
+ return; // Already built
474
+ // Get only direct child tabs (not nested ones)
475
+ const tabElements = Array.from(groupEl.children).filter((child) => child.tagName.toLowerCase() === TAB_SELECTOR);
476
+ if (tabElements.length === 0)
477
+ return;
478
+ // Create nav container
479
+ navContainer = document.createElement('ul');
480
+ navContainer.className = `${NAV_CONTAINER_CLASS} nav-tabs`;
481
+ navContainer.setAttribute('role', 'tablist');
482
+ groupEl.insertBefore(navContainer, groupEl.firstChild);
483
+ // Build nav items
484
+ tabElements.forEach((tabEl) => {
485
+ const tabId = tabEl.getAttribute('id');
486
+ if (!tabId)
487
+ return;
488
+ const header = tabEl.getAttribute('header') || this.getTabLabel(tabId, groupId, cfgGroups) || tabId;
489
+ const listItem = document.createElement('li');
490
+ listItem.className = 'nav-item';
491
+ const navLink = document.createElement('a');
492
+ navLink.className = 'nav-link';
493
+ // Replace icon shortcodes in header
494
+ navLink.innerHTML = replaceIconShortcodes(header);
495
+ navLink.href = '#';
496
+ navLink.setAttribute('data-tab-id', tabId);
497
+ navLink.setAttribute('data-group-id', groupId);
498
+ navLink.setAttribute('role', 'tab');
499
+ // Check if this tab is currently active
500
+ const isActive = tabEl.classList.contains('cv-visible');
501
+ if (isActive) {
502
+ navLink.classList.add('active');
503
+ navLink.setAttribute('aria-selected', 'true');
504
+ }
505
+ else {
506
+ navLink.setAttribute('aria-selected', 'false');
507
+ }
508
+ // Add click handler
509
+ if (onTabClick) {
510
+ navLink.addEventListener('click', (e) => {
511
+ e.preventDefault();
512
+ onTabClick(groupId, tabId);
513
+ });
514
+ }
515
+ listItem.appendChild(navLink);
516
+ navContainer.appendChild(listItem);
517
+ });
518
+ // Add bottom border line at the end of the tab group
519
+ const bottomBorder = document.createElement('div');
520
+ bottomBorder.className = 'cv-tabgroup-bottom-border';
521
+ groupEl.appendChild(bottomBorder);
522
+ });
523
+ }
524
+ /**
525
+ * Get tab label from config
526
+ */
527
+ static getTabLabel(tabId, groupId, cfgGroups) {
528
+ if (!cfgGroups)
529
+ return null;
530
+ const groupCfg = cfgGroups.find(g => g.id === groupId);
531
+ if (!groupCfg)
532
+ return null;
533
+ const tabCfg = groupCfg.tabs.find(t => t.id === tabId);
534
+ return tabCfg?.label || null;
535
+ }
536
+ /**
537
+ * Update active state in navs after selection change (single group)
538
+ */
539
+ static updateNavActiveState(rootEl, groupId, activeTabId) {
540
+ const tabGroups = rootEl.querySelectorAll(`${TABGROUP_SELECTOR}[id="${groupId}"]`);
541
+ tabGroups.forEach((groupEl) => {
542
+ const navLinks = groupEl.querySelectorAll('.nav-link');
543
+ navLinks.forEach((link) => {
544
+ const tabId = link.getAttribute('data-tab-id');
545
+ if (tabId === activeTabId) {
546
+ link.classList.add('active');
547
+ link.setAttribute('aria-selected', 'true');
548
+ }
549
+ else {
550
+ link.classList.remove('active');
551
+ link.setAttribute('aria-selected', 'false');
552
+ }
553
+ });
554
+ });
555
+ }
556
+ /**
557
+ * Update active states for all tab groups based on current state
558
+ */
559
+ static updateAllNavActiveStates(rootEl, tabs, cfgGroups) {
560
+ const tabGroups = rootEl.querySelectorAll(TABGROUP_SELECTOR);
561
+ tabGroups.forEach((groupEl) => {
562
+ const groupId = groupEl.getAttribute('id');
563
+ if (!groupId)
564
+ return;
565
+ // Determine the active tab for this group
566
+ const activeTabId = this.resolveActiveTab(groupId, tabs, cfgGroups, groupEl);
567
+ if (!activeTabId)
568
+ return;
569
+ // Update nav links for this group
570
+ const navLinks = groupEl.querySelectorAll('.nav-link');
571
+ navLinks.forEach((link) => {
572
+ const tabId = link.getAttribute('data-tab-id');
573
+ if (tabId === activeTabId) {
574
+ link.classList.add('active');
575
+ link.setAttribute('aria-selected', 'true');
576
+ }
577
+ else {
578
+ link.classList.remove('active');
579
+ link.setAttribute('aria-selected', 'false');
580
+ }
581
+ });
582
+ });
583
+ }
584
+ }
585
+
586
+ class AssetsManager {
587
+ assets;
588
+ baseURL;
589
+ constructor(assets, baseURL = '') {
590
+ this.assets = assets;
591
+ this.baseURL = baseURL;
592
+ if (!this.validate()) {
593
+ console.warn('Invalid assets:', this.assets);
594
+ }
595
+ }
596
+ // Check each asset has content or src
597
+ validate() {
598
+ return Object.values(this.assets).every(a => a.src || a.content);
599
+ }
600
+ get(assetId) {
601
+ const asset = this.assets[assetId];
602
+ if (!asset)
603
+ return undefined;
604
+ // If there's a baseURL and the asset has a src property, prepend the baseURL
605
+ if (this.baseURL && asset.src) {
606
+ // Create a shallow copy to avoid mutating the original asset
607
+ return {
608
+ ...asset,
609
+ src: this.prependBaseURL(asset.src)
610
+ };
611
+ }
612
+ return asset;
613
+ }
614
+ prependBaseURL(path) {
615
+ // Don't prepend if the path is already absolute (starts with http:// or https://)
616
+ if (path.startsWith('http://') || path.startsWith('https://')) {
617
+ return path;
618
+ }
619
+ // Ensure baseURL doesn't end with / and path starts with /
620
+ const cleanBaseURL = this.baseURL.endsWith('/') ? this.baseURL.slice(0, -1) : this.baseURL;
621
+ const cleanPath = path.startsWith('/') ? path : '/' + path;
622
+ return cleanBaseURL + cleanPath;
623
+ }
624
+ loadFromJSON(json) {
625
+ this.assets = json;
626
+ }
627
+ loadAdditionalAssets(additionalAssets) {
628
+ this.assets = { ...this.assets, ...additionalAssets };
629
+ }
630
+ }
631
+
632
+ // Constants for selectors
633
+ const TOGGLE_DATA_SELECTOR = "[data-cv-toggle], [data-customviews-toggle]";
634
+ const TOGGLE_ELEMENT_SELECTOR = "cv-toggle";
635
+ const TOGGLE_SELECTOR = `${TOGGLE_DATA_SELECTOR}, ${TOGGLE_ELEMENT_SELECTOR}`;
636
+ /**
637
+ * ToggleManager handles discovery, visibility, and asset rendering for toggle elements
638
+ */
639
+ class ToggleManager {
640
+ /**
641
+ * Apply toggle visibility to all toggle elements in the DOM
642
+ */
643
+ static applyToggles(rootEl, activeToggles) {
644
+ rootEl.querySelectorAll(TOGGLE_SELECTOR).forEach(el => {
645
+ const categories = this.getToggleCategories(el);
646
+ const shouldShow = categories.some(cat => activeToggles.includes(cat));
647
+ this.applyToggleVisibility(el, shouldShow);
648
+ });
649
+ }
650
+ /**
651
+ * Render assets into toggle elements that are currently visible
652
+ */
653
+ static renderAssets(rootEl, activeToggles, assetsManager) {
654
+ rootEl.querySelectorAll(TOGGLE_SELECTOR).forEach(el => {
655
+ const categories = this.getToggleCategories(el);
656
+ const toggleId = this.getToggleId(el);
657
+ if (toggleId && categories.some(cat => activeToggles.includes(cat))) {
658
+ renderAssetInto(el, toggleId, assetsManager);
659
+ }
660
+ });
661
+ }
662
+ /**
663
+ * Get toggle categories from an element (supports both data attributes and cv-toggle elements)
664
+ */
665
+ static getToggleCategories(el) {
666
+ if (el.tagName.toLowerCase() === 'cv-toggle') {
667
+ const category = el.getAttribute('category');
668
+ return (category || '').split(/\s+/).filter(Boolean);
669
+ }
670
+ else {
671
+ const data = el.dataset.cvToggle || el.dataset.customviewsToggle;
672
+ return (data || '').split(/\s+/).filter(Boolean);
673
+ }
674
+ }
675
+ /**
676
+ * Get toggle ID from an element
677
+ */
678
+ static getToggleId(el) {
679
+ return el.dataset.cvId || el.dataset.customviewsId || el.getAttribute('data-cv-id') || el.getAttribute('data-customviews-id') || undefined;
680
+ }
681
+ /**
682
+ * Apply simple class-based visibility to a toggle element
683
+ */
684
+ static applyToggleVisibility(el, visible) {
685
+ if (visible) {
686
+ el.classList.remove('cv-hidden');
687
+ el.classList.add('cv-visible');
688
+ }
689
+ else {
690
+ el.classList.add('cv-hidden');
691
+ el.classList.remove('cv-visible');
692
+ }
693
+ }
694
+ }
695
+
696
+ /**
697
+ * Styles for toggle visibility and animations
698
+ */
699
+ const TOGGLE_STYLES = `
700
+ /* Core toggle visibility transitions */
701
+ [data-cv-toggle], [data-customviews-toggle], cv-toggle {
702
+ transition: opacity 150ms ease,
703
+ transform 150ms ease,
704
+ max-height 200ms ease,
705
+ margin 150ms ease;
706
+ will-change: opacity, transform, max-height, margin;
707
+ }
708
+
709
+ .cv-visible {
710
+ opacity: 1 !important;
711
+ transform: translateY(0) !important;
712
+ max-height: var(--cv-max-height, 9999px) !important;
713
+ }
714
+
715
+ .cv-hidden {
716
+ opacity: 0 !important;
717
+ transform: translateY(-4px) !important;
718
+ pointer-events: none !important;
719
+ padding-top: 0 !important;
720
+ padding-bottom: 0 !important;
721
+ border-top-width: 0 !important;
722
+ border-bottom-width: 0 !important;
723
+ max-height: 0 !important;
724
+ margin-top: 0 !important;
725
+ margin-bottom: 0 !important;
726
+ overflow: hidden !important;
727
+ }
728
+ `;
729
+
730
+ /**
731
+ * Styles for tab groups and tab navigation
732
+ */
733
+ const TAB_STYLES = `
734
+ /* Tab navigation styles - Bootstrap-style tabs matching MarkBind */
735
+ .cv-tabs-nav {
736
+ display: flex;
737
+ flex-wrap: wrap;
738
+ padding-left: 0;
739
+ margin-top: 0.5rem;
740
+ margin-bottom: 1rem;
741
+ list-style: none;
742
+ border-bottom: 1px solid #dee2e6;
743
+ }
744
+
745
+ .cv-tabs-nav .nav-item {
746
+ margin-bottom: -1px;
747
+ list-style: none;
748
+ display: inline-block;
749
+ }
750
+
751
+ .cv-tabs-nav .nav-link {
752
+ display: block;
753
+ padding: 0.5rem 1rem;
754
+ color: #495057;
755
+ text-decoration: none;
756
+ background-color: transparent;
757
+ border: 1px solid transparent;
758
+ border-top-left-radius: 0.25rem;
759
+ border-top-right-radius: 0.25rem;
760
+ transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out;
761
+ cursor: pointer;
762
+ }
763
+
764
+ .cv-tabs-nav .nav-link:hover,
765
+ .cv-tabs-nav .nav-link:focus {
766
+ border-color: #e9ecef #e9ecef #dee2e6;
767
+ isolation: isolate;
768
+ }
769
+
770
+ .cv-tabs-nav .nav-link.active {
771
+ color: #495057;
772
+ background-color: #fff;
773
+ border-color: #dee2e6 #dee2e6 #fff;
774
+ }
775
+
776
+ .cv-tabs-nav .nav-link:focus {
777
+ outline: 0;
778
+ box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
779
+ }
780
+
781
+ /* Legacy button-based nav (deprecated, kept for compatibility) */
782
+ .cv-tabs-nav-item {
783
+ background: none;
784
+ border: none;
785
+ border-bottom: 2px solid transparent;
786
+ padding: 0.5rem 1rem;
787
+ cursor: pointer;
788
+ font-size: 1rem;
789
+ color: #6c757d;
790
+ transition: color 150ms ease, border-color 150ms ease;
791
+ }
792
+
793
+ .cv-tabs-nav-item:hover {
794
+ color: #495057;
795
+ border-bottom-color: #dee2e6;
796
+ }
797
+
798
+ .cv-tabs-nav-item.active {
799
+ color: #007bff;
800
+ border-bottom-color: #007bff;
801
+ font-weight: 500;
802
+ }
803
+
804
+ .cv-tabs-nav-item:focus {
805
+ outline: 2px solid #007bff;
806
+ outline-offset: 2px;
807
+ }
808
+
809
+ /* Tab panel base styles */
810
+ cv-tab {
811
+ display: block;
812
+ }
813
+
814
+ /* Override visibility for tab panels - use display instead of collapse animation */
815
+ cv-tab.cv-hidden {
816
+ display: none !important;
817
+ }
818
+
819
+ cv-tab.cv-visible {
820
+ display: block !important;
821
+ }
822
+
823
+ cv-tabgroup {
824
+ display: block;
825
+ margin-bottom: 1.5rem;
826
+ }
827
+
828
+ /* Bottom border line for tab groups */
829
+ .cv-tabgroup-bottom-border {
830
+ border-bottom: 1px solid #dee2e6;
831
+ margin-top: 1rem;
832
+ }
833
+
834
+ /* Tab content wrapper */
835
+ .cv-tab-content {
836
+ padding: 1rem 0;
837
+ }
838
+ `;
839
+
840
+ /**
841
+ * Combined core styles for toggles and tabs
842
+ */
843
+ const CORE_STYLES = `
844
+ ${TOGGLE_STYLES}
845
+
846
+ ${TAB_STYLES}
375
847
  `;
376
848
  /**
377
849
  * Add styles for hiding and showing toggles animations and transitions to the document head
@@ -392,36 +864,85 @@
392
864
  assetsManager;
393
865
  persistenceManager;
394
866
  visibilityManager;
395
- stateFromUrl = null;
396
- localConfig;
867
+ config;
397
868
  stateChangeListeners = [];
869
+ showUrlEnabled;
870
+ lastAppliedState = null;
398
871
  constructor(opt) {
399
872
  this.assetsManager = opt.assetsManager;
400
- this.localConfig = opt.config;
873
+ this.config = opt.config;
401
874
  this.rootEl = opt.rootEl || document.body;
402
875
  this.persistenceManager = new PersistenceManager();
403
876
  this.visibilityManager = new VisibilityManager();
877
+ this.showUrlEnabled = opt.showUrl ?? true;
878
+ this.lastAppliedState = this.cloneState(this.config?.defaultState);
879
+ }
880
+ getConfig() {
881
+ return this.config;
404
882
  }
405
- getLocalConfig() {
406
- return this.localConfig;
883
+ /**
884
+ * Get tab groups from config
885
+ */
886
+ getTabGroups() {
887
+ return this.config.tabGroups;
888
+ }
889
+ /**
890
+ * Get currently active tabs (from URL > persisted (localStorage) > defaults)
891
+ */
892
+ getCurrentActiveTabs() {
893
+ if (this.lastAppliedState?.tabs) {
894
+ return { ...this.lastAppliedState.tabs };
895
+ }
896
+ const persistedState = this.persistenceManager.getPersistedState();
897
+ if (persistedState?.tabs) {
898
+ return { ...persistedState.tabs };
899
+ }
900
+ return this.config?.defaultState?.tabs ? { ...this.config.defaultState.tabs } : {};
901
+ }
902
+ /**
903
+ * Set active tab for a group and apply state
904
+ */
905
+ setActiveTab(groupId, tabId) {
906
+ // Get current state
907
+ const currentToggles = this.getCurrentActiveToggles();
908
+ const currentTabs = this.getCurrentActiveTabs();
909
+ // Merge new tab selection
910
+ const newTabs = { ...currentTabs, [groupId]: tabId };
911
+ // Create new state
912
+ const newState = {
913
+ toggles: currentToggles,
914
+ tabs: newTabs
915
+ };
916
+ // Apply the state
917
+ this.applyState(newState);
918
+ // Emit custom event
919
+ const event = new CustomEvent('customviews:tab-change', {
920
+ detail: { groupId, tabId },
921
+ bubbles: true
922
+ });
923
+ document.dispatchEvent(event);
407
924
  }
408
925
  // Inject styles, setup listeners and call rendering logic
409
926
  async init() {
410
927
  injectCoreStyles();
928
+ // Build navigation once (with click handlers)
929
+ TabManager.buildNavs(this.rootEl, this.config.tabGroups, (groupId, tabId) => {
930
+ this.setActiveTab(groupId, tabId);
931
+ });
411
932
  // For session history, clicks on back/forward button
412
933
  window.addEventListener("popstate", () => {
413
- this.loadAndRenderState();
934
+ this.loadAndCallApplyState();
414
935
  });
415
- this.loadAndRenderState();
936
+ this.loadAndCallApplyState();
416
937
  }
417
938
  // Priority: URL state > persisted state > default
418
939
  // Also filters using the visibility manager to persist selection
419
940
  // across back/forward button clicks
420
- async loadAndRenderState() {
941
+ async loadAndCallApplyState() {
421
942
  // 1. URL State
422
- this.stateFromUrl = URLStateManager.parseURL();
423
- if (this.stateFromUrl) {
424
- this.applyState(this.stateFromUrl);
943
+ const urlState = URLStateManager.parseURL();
944
+ if (urlState) {
945
+ this.applyState(urlState);
425
946
  return;
426
947
  }
427
948
  // 2. Persisted State
@@ -431,38 +952,35 @@
431
952
  return;
432
953
  }
433
954
  // 3. Local Config Fallback
434
- this.renderState(this.localConfig.defaultState);
955
+ this.renderState(this.config.defaultState);
435
956
  }
436
957
  /**
437
- * Apply a custom state, saves to localStorage and updates the URL
438
- */
958
+ * Apply a custom state, saves to localStorage and updates the URL
959
+ */
439
960
  applyState(state) {
440
- this.renderState(state);
441
- this.persistenceManager.persistState(state);
442
- this.stateFromUrl = state;
443
- URLStateManager.updateURL(state);
961
+ const snapshot = this.cloneState(state);
962
+ this.renderState(snapshot);
963
+ this.persistenceManager.persistState(snapshot);
964
+ if (this.showUrlEnabled) {
965
+ URLStateManager.updateURL(snapshot);
966
+ }
967
+ else {
968
+ URLStateManager.clearURL();
969
+ }
444
970
  }
445
971
  /** Render all toggles for the current state */
446
972
  renderState(state) {
973
+ this.lastAppliedState = this.cloneState(state);
447
974
  const toggles = state.toggles || [];
448
975
  const finalToggles = this.visibilityManager.filterVisibleToggles(toggles);
449
- // Toggles hide or show relevant toggles
450
- this.rootEl.querySelectorAll("[data-customviews-toggle]").forEach(el => {
451
- const category = el.dataset.customviewsToggle;
452
- const shouldShow = !!category && finalToggles.includes(category);
453
- this.visibilityManager.applyElementVisibility(el, shouldShow);
454
- });
455
- // Render toggles
456
- for (const category of finalToggles) {
457
- this.rootEl.querySelectorAll(`[data-customviews-toggle="${category}"]`).forEach(el => {
458
- // if it has an id, then we render the asset into it
459
- // if it has no id, then we assume it's a container
460
- const toggleId = el.dataset.customviewsId;
461
- if (toggleId) {
462
- renderAssetInto(el, toggleId, this.assetsManager);
463
- }
464
- });
465
- }
976
+ // Apply toggle visibility
977
+ ToggleManager.applyToggles(this.rootEl, finalToggles);
978
+ // Render assets into toggles
979
+ ToggleManager.renderAssets(this.rootEl, finalToggles, this.assetsManager);
980
+ // Apply tab selections
981
+ TabManager.applySelections(this.rootEl, state.tabs || {}, this.config.tabGroups);
982
+ // Update nav active states (without rebuilding)
983
+ TabManager.updateAllNavActiveStates(this.rootEl, state.tabs || {}, this.config.tabGroups);
466
984
  // Notify state change listeners (like widgets)
467
985
  this.notifyStateChangeListeners();
468
986
  }
@@ -470,10 +988,9 @@
470
988
  * Reset to default state
471
989
  */
472
990
  resetToDefault() {
473
- this.stateFromUrl = null;
474
991
  this.persistenceManager.clearAll();
475
- if (this.localConfig) {
476
- this.renderState(this.localConfig.defaultState);
992
+ if (this.config) {
993
+ this.renderState(this.config.defaultState);
477
994
  }
478
995
  else {
479
996
  console.warn("No configuration loaded, cannot reset to default state");
@@ -485,15 +1002,12 @@
485
1002
  * Get the currently active toggles regardless of whether they come from custom state or default configuration
486
1003
  */
487
1004
  getCurrentActiveToggles() {
488
- // If we have a custom state, return its toggles
489
- if (this.stateFromUrl) {
490
- return this.stateFromUrl.toggles || [];
1005
+ if (this.lastAppliedState) {
1006
+ return this.lastAppliedState.toggles || [];
491
1007
  }
492
- // Otherwise, if we have local config, return its default state toggles
493
- if (this.localConfig) {
494
- return this.localConfig.defaultState.toggles || [];
1008
+ if (this.config) {
1009
+ return this.config.defaultState.toggles || [];
495
1010
  }
496
- // No configuration or state
497
1011
  return [];
498
1012
  }
499
1013
  /**
@@ -501,15 +1015,35 @@
501
1015
  */
502
1016
  clearPersistence() {
503
1017
  this.persistenceManager.clearAll();
504
- this.stateFromUrl = null;
505
- if (this.localConfig) {
506
- this.renderState(this.localConfig.defaultState);
1018
+ if (this.config) {
1019
+ this.renderState(this.config.defaultState);
507
1020
  }
508
1021
  else {
509
1022
  console.warn("No configuration loaded, cannot reset to default state");
510
1023
  }
511
1024
  URLStateManager.clearURL();
512
1025
  }
1026
+ setOption(flag, value) {
1027
+ switch (flag) {
1028
+ case 'showUrl': {
1029
+ const nextValue = Boolean(value);
1030
+ if (this.showUrlEnabled === nextValue) {
1031
+ return;
1032
+ }
1033
+ this.showUrlEnabled = nextValue;
1034
+ if (nextValue) {
1035
+ const stateForUrl = this.getTrackedStateSnapshot();
1036
+ URLStateManager.updateURL(stateForUrl);
1037
+ }
1038
+ else {
1039
+ URLStateManager.clearURL();
1040
+ }
1041
+ break;
1042
+ }
1043
+ default:
1044
+ console.warn(`[CustomViews] Unknown option '${flag}' passed to setOption`);
1045
+ }
1046
+ }
513
1047
  // === STATE CHANGE LISTENER METHODS ===
514
1048
  /**
515
1049
  * Add a listener that will be called whenever the state changes
@@ -539,51 +1073,136 @@
539
1073
  }
540
1074
  });
541
1075
  }
1076
+ cloneState(state) {
1077
+ const toggles = state?.toggles ? [...state.toggles] : [];
1078
+ const tabs = state?.tabs ? { ...state.tabs } : undefined;
1079
+ return tabs ? { toggles, tabs } : { toggles };
1080
+ }
1081
+ getTrackedStateSnapshot() {
1082
+ if (this.lastAppliedState) {
1083
+ return this.cloneState(this.lastAppliedState);
1084
+ }
1085
+ if (this.config) {
1086
+ return this.cloneState(this.config.defaultState);
1087
+ }
1088
+ return { toggles: [] };
1089
+ }
542
1090
  }
543
1091
 
544
- class AssetsManager {
545
- assets;
546
- baseURL;
547
- constructor(assets, baseURL = '') {
548
- this.assets = assets;
549
- this.baseURL = baseURL;
550
- if (!this.validate()) {
551
- console.warn('Invalid assets:', this.assets);
552
- }
1092
+ /**
1093
+ * Helper function to prepend baseUrl to a path
1094
+ * @param path The path to prepend the baseUrl to
1095
+ * @param baseUrl The base URL to prepend
1096
+ * @returns The full URL with baseUrl prepended if applicable
1097
+ */
1098
+ function prependBaseUrl(path, baseUrl) {
1099
+ if (!baseUrl)
1100
+ return path;
1101
+ // Don't prepend if the path is already absolute (starts with http:// or https://)
1102
+ if (path.startsWith('http://') || path.startsWith('https://')) {
1103
+ return path;
1104
+ }
1105
+ // Ensure baseUrl doesn't end with / and path starts with /
1106
+ const cleanbaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
1107
+ const cleanPath = path.startsWith('/') ? path : '/' + path;
1108
+ return cleanbaseUrl + cleanPath;
1109
+ }
1110
+
1111
+ /**
1112
+ * Custom Elements for Tab Groups and Tabs
1113
+ */
1114
+ /**
1115
+ * <cv-tab> element - represents a single tab panel
1116
+ */
1117
+ class CVTab extends HTMLElement {
1118
+ connectedCallback() {
1119
+ // Element is managed by TabManager
553
1120
  }
554
- // Check each asset has content or src
555
- validate() {
556
- return Object.values(this.assets).every(a => a.src || a.content);
1121
+ }
1122
+ /**
1123
+ * <cv-tabgroup> element - represents a group of tabs
1124
+ */
1125
+ class CVTabgroup extends HTMLElement {
1126
+ connectedCallback() {
1127
+ // Element is managed by TabManager
1128
+ // Emit ready event after a brief delay to ensure children are parsed
1129
+ setTimeout(() => {
1130
+ const event = new CustomEvent('cv:tabgroup-ready', {
1131
+ bubbles: true,
1132
+ detail: { groupId: this.getAttribute('id') }
1133
+ });
1134
+ this.dispatchEvent(event);
1135
+ }, 0);
557
1136
  }
558
- get(assetId) {
559
- const asset = this.assets[assetId];
560
- if (!asset)
561
- return undefined;
562
- // If there's a baseURL and the asset has a src property, prepend the baseURL
563
- if (this.baseURL && asset.src) {
564
- // Create a shallow copy to avoid mutating the original asset
565
- return {
566
- ...asset,
567
- src: this.prependBaseURL(asset.src)
568
- };
569
- }
570
- return asset;
1137
+ }
1138
+ /**
1139
+ * <cv-toggle> element - represents a toggleable content block
1140
+ */
1141
+ class CVToggle extends HTMLElement {
1142
+ connectedCallback() {
1143
+ // Element is managed by Core
571
1144
  }
572
- prependBaseURL(path) {
573
- // Don't prepend if the path is already absolute (starts with http:// or https://)
574
- if (path.startsWith('http://') || path.startsWith('https://')) {
575
- return path;
576
- }
577
- // Ensure baseURL doesn't end with / and path starts with /
578
- const cleanBaseURL = this.baseURL.endsWith('/') ? this.baseURL.slice(0, -1) : this.baseURL;
579
- const cleanPath = path.startsWith('/') ? path : '/' + path;
580
- return cleanBaseURL + cleanPath;
1145
+ }
1146
+ /**
1147
+ * Register custom elements
1148
+ */
1149
+ function registerCustomElements() {
1150
+ // Only register if not already defined
1151
+ if (!customElements.get('cv-tab')) {
1152
+ customElements.define('cv-tab', CVTab);
581
1153
  }
582
- loadFromJSON(json) {
583
- this.assets = json;
1154
+ if (!customElements.get('cv-tabgroup')) {
1155
+ customElements.define('cv-tabgroup', CVTabgroup);
584
1156
  }
585
- loadAdditionalAssets(additionalAssets) {
586
- this.assets = { ...this.assets, ...additionalAssets };
1157
+ if (!customElements.get('cv-toggle')) {
1158
+ customElements.define('cv-toggle', CVToggle);
1159
+ }
1160
+ }
1161
+
1162
+ /**
1163
+ * Main CustomViews class for initializing and managing custom views
1164
+ */
1165
+ class CustomViews {
1166
+ /**
1167
+ * Entry Point to use CustomViews
1168
+ * @param opts Initialization options including config object and assets path
1169
+ * @returns Promise resolving to the CustomViewsCore instance or null if initialization fails
1170
+ */
1171
+ static async init(opts) {
1172
+ // Register custom elements
1173
+ registerCustomElements();
1174
+ // Load assets JSON if provided
1175
+ let assetsManager;
1176
+ const baseURL = opts.baseURL || '';
1177
+ if (opts.assetsJsonPath) {
1178
+ const assetsPath = prependBaseUrl(opts.assetsJsonPath, baseURL);
1179
+ const assetsJson = await (await fetch(assetsPath)).json();
1180
+ assetsManager = new AssetsManager(assetsJson, baseURL);
1181
+ }
1182
+ else {
1183
+ assetsManager = new AssetsManager({}, baseURL);
1184
+ }
1185
+ // Use provided config or create a minimal default one
1186
+ let config;
1187
+ if (opts.config) {
1188
+ config = opts.config;
1189
+ }
1190
+ else {
1191
+ console.error("No config provided, using minimal default config");
1192
+ // Create a minimal default config
1193
+ config = { allToggles: [], defaultState: { toggles: [] } };
1194
+ }
1195
+ const coreOptions = {
1196
+ assetsManager,
1197
+ config: config,
1198
+ rootEl: opts.rootEl,
1199
+ };
1200
+ if (opts.showUrl !== undefined) {
1201
+ coreOptions.showUrl = opts.showUrl;
1202
+ }
1203
+ const core = new CustomViewsCore(coreOptions);
1204
+ core.init();
1205
+ return core;
587
1206
  }
588
1207
  }
589
1208
 
@@ -594,586 +1213,644 @@
594
1213
  * Note: Styles are kept as a TypeScript string for compatibility with the build system.
595
1214
  * This approach ensures the styles are properly bundled and don't require separate CSS file handling.
596
1215
  */
597
- const WIDGET_STYLES = `
598
- /* Rounded rectangle widget icon styles */
599
- .cv-widget-icon {
600
- position: fixed;
601
- background: white;
602
- color: black;
603
- display: flex;
604
- align-items: center;
605
- justify-content: center;
606
- font-size: 18px;
607
- font-weight: bold;
608
- cursor: pointer;
609
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
610
- z-index: 9998;
611
- transition: all 0.3s ease;
612
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
613
- }
614
-
615
- .cv-widget-icon:hover {
616
- background: white;
617
- color: black;
618
- }
619
-
620
- /* Top-right: rounded end on left, sticks out leftward on hover */
621
- .cv-widget-top-right {
622
- top: 20px;
623
- right: 0;
624
- border-radius: 18px 0 0 18px;
625
- padding-left: 8px;
626
- justify-content: flex-start;
627
- }
628
-
629
- /* Top-left: rounded end on right, sticks out rightward on hover */
630
- .cv-widget-top-left {
631
- top: 20px;
632
- left: 0;
633
- border-radius: 0 18px 18px 0;
634
- padding-right: 8px;
635
- justify-content: flex-end;
636
- }
637
-
638
- /* Bottom-right: rounded end on left, sticks out leftward on hover */
639
- .cv-widget-bottom-right {
640
- bottom: 20px;
641
- right: 0;
642
- border-radius: 18px 0 0 18px;
643
- padding-left: 8px;
644
- justify-content: flex-start;
645
- }
646
-
647
- /* Bottom-left: rounded end on right, sticks out rightward on hover */
648
- .cv-widget-bottom-left {
649
- bottom: 20px;
650
- left: 0;
651
- border-radius: 0 18px 18px 0;
652
- padding-right: 8px;
653
- justify-content: flex-end;
654
- }
655
-
656
- /* Middle-left: rounded end on right, sticks out rightward on hover */
657
- .cv-widget-middle-left {
658
- top: 50%;
659
- left: 0;
660
- transform: translateY(-50%);
661
- border-radius: 0 18px 18px 0;
662
- padding-right: 8px;
663
- justify-content: flex-end;
664
- }
665
-
666
- /* Middle-right: rounded end on left, sticks out leftward on hover */
667
- .cv-widget-middle-right {
668
- top: 50%;
669
- right: 0;
670
- transform: translateY(-50%);
671
- border-radius: 18px 0 0 18px;
672
- padding-left: 8px;
673
- justify-content: flex-start;
674
- }
675
-
676
- .cv-widget-top-right,
677
- .cv-widget-middle-right,
678
- .cv-widget-bottom-right,
679
- .cv-widget-top-left,
680
- .cv-widget-middle-left,
681
- .cv-widget-bottom-left {
682
- height: 36px;
683
- width: 36px;
684
- }
685
-
686
- .cv-widget-middle-right:hover,
687
- .cv-widget-top-right:hover,
688
- .cv-widget-bottom-right:hover,
689
- .cv-widget-top-left:hover,
690
- .cv-widget-middle-left:hover,
691
- .cv-widget-bottom-left:hover {
692
- width: 55px;
693
- }
694
-
695
- /* Modal content styles */
696
- .cv-widget-section {
697
- margin-bottom: 16px;
698
- }
699
-
700
- .cv-widget-section:last-child {
701
- margin-bottom: 0;
702
- }
703
-
704
- .cv-widget-section label {
705
- display: block;
706
- margin-bottom: 4px;
707
- font-weight: 500;
708
- color: #555;
709
- }
710
-
711
- .cv-widget-profile-select,
712
- .cv-widget-state-select {
713
- width: 100%;
714
- padding: 8px 12px;
715
- border: 1px solid #ddd;
716
- border-radius: 4px;
717
- background: white;
718
- font-size: 14px;
719
- }
720
-
721
- .cv-widget-profile-select:focus,
722
- .cv-widget-state-select:focus {
723
- outline: none;
724
- border-color: #007bff;
725
- box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
726
- }
727
-
728
- .cv-widget-profile-select:disabled,
729
- .cv-widget-state-select:disabled {
730
- background: #f8f9fa;
731
- color: #6c757d;
732
- cursor: not-allowed;
733
- }
734
-
735
- .cv-widget-current {
736
- margin: 16px 0;
737
- padding: 12px;
738
- background: #f8f9fa;
739
- border-radius: 4px;
740
- border-left: 4px solid #007bff;
741
- }
742
-
743
- .cv-widget-current label {
744
- font-size: 12px;
745
- text-transform: uppercase;
746
- letter-spacing: 0.5px;
747
- color: #666;
748
- margin-bottom: 4px;
749
- }
750
-
751
- .cv-widget-current-view {
752
- font-weight: 500;
753
- color: #333;
754
- }
755
-
756
- .cv-widget-reset {
757
- width: 100%;
758
- padding: 8px 16px;
759
- background: #dc3545;
760
- color: white;
761
- border: none;
762
- border-radius: 4px;
763
- cursor: pointer;
764
- font-size: 14px;
765
- font-weight: 500;
766
- }
767
-
768
- .cv-widget-reset:hover {
769
- background: #c82333;
770
- }
771
-
772
- .cv-widget-reset:active {
773
- background: #bd2130;
774
- }
775
-
776
- /* Responsive design for mobile */
777
- @media (max-width: 768px) {
778
- .cv-widget-top-right,
779
- .cv-widget-top-left {
780
- top: 10px;
781
- }
782
-
783
- .cv-widget-bottom-right,
784
- .cv-widget-bottom-left {
785
- bottom: 10px;
786
- }
787
-
788
- /* All widgets stay flush with screen edges */
789
- .cv-widget-top-right,
790
- .cv-widget-bottom-right,
791
- .cv-widget-middle-right {
792
- right: 0;
793
- }
794
-
795
- .cv-widget-top-left,
796
- .cv-widget-bottom-left,
797
- .cv-widget-middle-left {
798
- left: 0;
799
- }
800
-
801
- /* Slightly smaller on mobile */
802
- .cv-widget-icon {
803
- width: 60px;
804
- height: 32px;
805
- }
806
-
807
- .cv-widget-icon:hover {
808
- width: 75px;
809
- }
810
- }
811
-
812
- /* Modal styles */
813
- .cv-widget-modal-overlay {
814
- position: fixed;
815
- top: 0;
816
- left: 0;
817
- right: 0;
818
- bottom: 0;
819
- background: rgba(0, 0, 0, 0.5);
820
- display: flex;
821
- align-items: center;
822
- justify-content: center;
823
- z-index: 10002;
824
- animation: fadeIn 0.2s ease;
825
- }
826
-
827
- @keyframes fadeIn {
828
- from { opacity: 0; }
829
- to { opacity: 1; }
830
- }
831
-
832
- .cv-widget-modal {
833
- background: white;
834
- border-radius: 8px;
835
- box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
836
- max-width: 400px;
837
- width: 90vw;
838
- max-height: 80vh;
839
- overflow-y: auto;
840
- animation: slideIn 0.2s ease;
841
- }
842
-
843
- @keyframes slideIn {
844
- from {
845
- opacity: 0;
846
- transform: scale(0.9) translateY(-20px);
847
- }
848
- to {
849
- opacity: 1;
850
- transform: scale(1) translateY(0);
851
- }
852
- }
853
-
854
- .cv-widget-modal-header {
855
- display: flex;
856
- justify-content: space-between;
857
- align-items: center;
858
- padding: 16px 20px;
859
- border-bottom: 1px solid #e9ecef;
860
- background: #f8f9fa;
861
- border-radius: 8px 8px 0 0;
862
- }
863
-
864
- .cv-widget-modal-header h3 {
865
- margin: 0;
866
- font-size: 18px;
867
- font-weight: 600;
868
- color: #333;
869
- }
870
-
871
- .cv-widget-modal-close {
872
- background: none;
873
- border: none;
874
- font-size: 24px;
875
- cursor: pointer;
876
- padding: 0;
877
- width: 32px;
878
- height: 32px;
879
- display: flex;
880
- align-items: center;
881
- justify-content: center;
882
- border-radius: 4px;
883
- color: #666;
884
- }
885
-
886
- .cv-widget-modal-close:hover {
887
- background: #e9ecef;
888
- }
889
-
890
- .cv-widget-modal-content {
891
- padding: 20px;
892
- }
893
-
894
- .cv-widget-modal-actions {
895
- margin-top: 20px;
896
- padding-top: 16px;
897
- border-top: 1px solid #e9ecef;
898
- }
899
-
900
- .cv-widget-restore {
901
- width: 100%;
902
- padding: 10px 16px;
903
- background: #28a745;
904
- color: white;
905
- border: none;
906
- border-radius: 4px;
907
- cursor: pointer;
908
- font-size: 14px;
909
- font-weight: 500;
910
- }
911
-
912
- .cv-widget-restore:hover {
913
- background: #218838;
914
- }
915
-
916
- .cv-widget-create-state {
917
- width: 100%;
918
- padding: 10px 16px;
919
- background: #007bff;
920
- color: white;
921
- border: none;
922
- border-radius: 4px;
923
- cursor: pointer;
924
- font-size: 14px;
925
- font-weight: 500;
926
- margin-bottom: 10px;
927
- }
928
-
929
- .cv-widget-create-state:hover {
930
- background: #0056b3;
931
- }
932
-
933
- /* Dark theme modal styles */
934
- .cv-widget-theme-dark .cv-widget-modal {
935
- background: #2d3748;
936
- color: #e2e8f0;
937
- }
938
-
939
- .cv-widget-theme-dark .cv-widget-modal-header {
940
- background: #1a202c;
941
- border-color: #4a5568;
942
- }
943
-
944
- .cv-widget-theme-dark .cv-widget-modal-header h3 {
945
- color: #e2e8f0;
946
- }
947
-
948
- .cv-widget-theme-dark .cv-widget-modal-close {
949
- color: #a0aec0;
950
- }
951
-
952
- .cv-widget-theme-dark .cv-widget-modal-close:hover {
953
- background: #4a5568;
954
- }
955
-
956
- .cv-widget-theme-dark .cv-widget-modal-actions {
957
- border-color: #4a5568;
958
- }
959
-
960
- /* Custom state creator styles */
961
- .cv-custom-state-modal {
962
- max-width: 500px;
963
- }
964
-
965
- .cv-custom-state-form h4 {
966
- margin: 20px 0 10px 0;
967
- font-size: 16px;
968
- font-weight: 600;
969
- color: #333;
970
- border-bottom: 1px solid #e9ecef;
971
- padding-bottom: 5px;
972
- }
973
-
974
- .cv-custom-state-section {
975
- margin-bottom: 16px;
976
- }
977
-
978
- .cv-custom-state-section label {
979
- display: block;
980
- margin-bottom: 4px;
981
- font-weight: 500;
982
- color: #555;
983
- }
984
-
985
- .cv-custom-state-input {
986
- width: 100%;
987
- padding: 8px 12px;
988
- border: 1px solid #ddd;
989
- border-radius: 4px;
990
- background: white;
991
- font-size: 14px;
992
- }
993
-
994
- .cv-custom-state-input:focus {
995
- outline: none;
996
- border-color: #007bff;
997
- box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
998
- }
999
-
1000
- .cv-custom-toggles {
1001
- display: grid;
1002
- grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
1003
- gap: 10px;
1004
- }
1005
-
1006
- .cv-custom-state-toggle {
1007
- display: flex;
1008
- align-items: center;
1009
- }
1010
-
1011
- .cv-custom-state-toggle label {
1012
- display: flex;
1013
- align-items: center;
1014
- cursor: pointer;
1015
- font-weight: normal;
1016
- margin: 0;
1017
- }
1018
-
1019
- .cv-custom-toggle-checkbox {
1020
- margin-right: 8px;
1021
- width: auto;
1022
- }
1023
-
1024
- .cv-custom-state-actions {
1025
- display: flex;
1026
- gap: 10px;
1027
- margin-top: 20px;
1028
- padding-top: 16px;
1029
- border-top: 1px solid #e9ecef;
1030
- }
1031
-
1032
- .cv-custom-state-cancel,
1033
- .cv-custom-state-copy-url {
1034
- flex: 1;
1035
- padding: 10px 16px;
1036
- border: none;
1037
- border-radius: 4px;
1038
- cursor: pointer;
1039
- font-size: 14px;
1040
- font-weight: 500;
1041
- }
1042
-
1043
- .cv-custom-state-reset {
1044
- flex: 1;
1045
- padding: 10px 16px;
1046
- border: none;
1047
- border-radius: 4px;
1048
- cursor: pointer;
1049
- font-size: 14px;
1050
- font-weight: 500;
1051
- background: #dc3545;
1052
- color: white;
1053
- }
1054
-
1055
- .cv-custom-state-reset:hover {
1056
- background: #c82333;
1057
- }
1058
-
1059
- .cv-custom-state-cancel {
1060
- background: #6c757d;
1061
- color: white;
1062
- }
1063
-
1064
- .cv-custom-state-cancel:hover {
1065
- background: #5a6268;
1066
- }
1067
-
1068
- .cv-custom-state-copy-url {
1069
- background: #28a745;
1070
- color: white;
1071
- }
1072
-
1073
- .cv-custom-state-copy-url:hover {
1074
- background: #218838;
1075
- }
1076
-
1077
- /* Dark theme custom state styles */
1078
- .cv-widget-theme-dark .cv-custom-state-form h4 {
1079
- color: #e2e8f0;
1080
- border-color: #4a5568;
1081
- }
1082
-
1083
- .cv-widget-theme-dark .cv-custom-state-section label {
1084
- color: #a0aec0;
1085
- }
1086
-
1087
- .cv-widget-theme-dark .cv-custom-state-input {
1088
- background: #1a202c;
1089
- border-color: #4a5568;
1090
- color: #e2e8f0;
1091
- }
1092
-
1093
- .cv-widget-theme-dark .cv-custom-state-actions {
1094
- border-color: #4a5568;
1095
- }
1096
-
1097
- /* Welcome modal styles */
1098
- .cv-welcome-modal {
1099
- max-width: 500px;
1100
- }
1101
-
1102
- .cv-welcome-content {
1103
- text-align: center;
1104
- }
1105
-
1106
- .cv-welcome-content p {
1107
- font-size: 15px;
1108
- line-height: 1.6;
1109
- color: #555;
1110
- margin-bottom: 24px;
1111
- }
1112
-
1113
- .cv-welcome-widget-preview {
1114
- display: flex;
1115
- flex-direction: column;
1116
- align-items: center;
1117
- gap: 12px;
1118
- padding: 20px;
1119
- background: #f8f9fa;
1120
- border-radius: 8px;
1121
- margin-bottom: 24px;
1122
- }
1123
-
1124
- .cv-welcome-widget-icon {
1125
- width: 36px;
1126
- height: 36px;
1127
- background: white;
1128
- color: black;
1129
- border-radius: 0 18px 18px 0;
1130
- display: flex;
1131
- align-items: center;
1132
- justify-content: center;
1133
- font-size: 18px;
1134
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
1135
- }
1136
-
1137
- .cv-welcome-widget-label {
1138
- font-size: 14px;
1139
- color: #666;
1140
- margin: 0;
1141
- font-weight: 500;
1142
- }
1143
-
1144
- .cv-welcome-got-it {
1145
- width: 100%;
1146
- padding: 12px 24px;
1147
- background: #007bff;
1148
- color: white;
1149
- border: none;
1150
- border-radius: 4px;
1151
- cursor: pointer;
1152
- font-size: 16px;
1153
- font-weight: 600;
1154
- transition: background 0.2s ease;
1155
- }
1156
-
1157
- .cv-welcome-got-it:hover {
1158
- background: #0056b3;
1159
- }
1160
-
1161
- .cv-welcome-got-it:active {
1162
- background: #004494;
1163
- }
1164
-
1165
- /* Dark theme welcome modal styles */
1166
- .cv-widget-theme-dark .cv-welcome-content p {
1167
- color: #cbd5e0;
1168
- }
1169
-
1170
- .cv-widget-theme-dark .cv-welcome-widget-preview {
1171
- background: #1a202c;
1172
- }
1173
-
1174
- .cv-widget-theme-dark .cv-welcome-widget-label {
1175
- color: #a0aec0;
1176
- }
1216
+ const WIDGET_STYLES = `
1217
+ /* Rounded rectangle widget icon styles */
1218
+ .cv-widget-icon {
1219
+ position: fixed;
1220
+ /* Slightly transparent by default so the widget is subtle at the page edge */
1221
+ background: rgba(255, 255, 255, 0.92);
1222
+ color: rgba(0, 0, 0, 0.9);
1223
+ opacity: 0.6;
1224
+ display: flex;
1225
+ align-items: center;
1226
+ justify-content: center;
1227
+ font-size: 18px;
1228
+ font-weight: bold;
1229
+ cursor: pointer;
1230
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
1231
+ z-index: 9998;
1232
+ transition: all 0.3s ease;
1233
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
1234
+ }
1235
+
1236
+ .cv-widget-icon:hover {
1237
+ /* Become fully opaque on hover to improve readability */
1238
+ background: rgba(255, 255, 255, 1);
1239
+ color: rgba(0, 0, 0, 1);
1240
+ opacity: 1;
1241
+ }
1242
+
1243
+ /* Top-right: rounded end on left, sticks out leftward on hover */
1244
+ .cv-widget-top-right {
1245
+ top: 20px;
1246
+ right: 0;
1247
+ border-radius: 18px 0 0 18px;
1248
+ padding-left: 8px;
1249
+ justify-content: flex-start;
1250
+ }
1251
+
1252
+ /* Top-left: rounded end on right, sticks out rightward on hover */
1253
+ .cv-widget-top-left {
1254
+ top: 20px;
1255
+ left: 0;
1256
+ border-radius: 0 18px 18px 0;
1257
+ padding-right: 8px;
1258
+ justify-content: flex-end;
1259
+ }
1260
+
1261
+ /* Bottom-right: rounded end on left, sticks out leftward on hover */
1262
+ .cv-widget-bottom-right {
1263
+ bottom: 20px;
1264
+ right: 0;
1265
+ border-radius: 18px 0 0 18px;
1266
+ padding-left: 8px;
1267
+ justify-content: flex-start;
1268
+ }
1269
+
1270
+ /* Bottom-left: rounded end on right, sticks out rightward on hover */
1271
+ .cv-widget-bottom-left {
1272
+ bottom: 20px;
1273
+ left: 0;
1274
+ border-radius: 0 18px 18px 0;
1275
+ padding-right: 8px;
1276
+ justify-content: flex-end;
1277
+ }
1278
+
1279
+ /* Middle-left: rounded end on right, sticks out rightward on hover */
1280
+ .cv-widget-middle-left {
1281
+ top: 50%;
1282
+ left: 0;
1283
+ transform: translateY(-50%);
1284
+ border-radius: 0 18px 18px 0;
1285
+ padding-right: 8px;
1286
+ justify-content: flex-end;
1287
+ }
1288
+
1289
+ /* Middle-right: rounded end on left, sticks out leftward on hover */
1290
+ .cv-widget-middle-right {
1291
+ top: 50%;
1292
+ right: 0;
1293
+ transform: translateY(-50%);
1294
+ border-radius: 18px 0 0 18px;
1295
+ padding-left: 8px;
1296
+ justify-content: flex-start;
1297
+ }
1298
+
1299
+ .cv-widget-top-right,
1300
+ .cv-widget-middle-right,
1301
+ .cv-widget-bottom-right,
1302
+ .cv-widget-top-left,
1303
+ .cv-widget-middle-left,
1304
+ .cv-widget-bottom-left {
1305
+ height: 36px;
1306
+ width: 36px;
1307
+ }
1308
+
1309
+ .cv-widget-middle-right:hover,
1310
+ .cv-widget-top-right:hover,
1311
+ .cv-widget-bottom-right:hover,
1312
+ .cv-widget-top-left:hover,
1313
+ .cv-widget-middle-left:hover,
1314
+ .cv-widget-bottom-left:hover {
1315
+ width: 55px;
1316
+ }
1317
+
1318
+ /* Modal content styles */
1319
+ .cv-widget-section {
1320
+ margin-bottom: 16px;
1321
+ }
1322
+
1323
+ .cv-widget-section:last-child {
1324
+ margin-bottom: 0;
1325
+ }
1326
+
1327
+ .cv-widget-section label {
1328
+ display: block;
1329
+ margin-bottom: 4px;
1330
+ font-weight: 500;
1331
+ color: #555;
1332
+ }
1333
+
1334
+ .cv-widget-profile-select,
1335
+ .cv-widget-state-select {
1336
+ width: 100%;
1337
+ padding: 8px 12px;
1338
+ border: 1px solid #ddd;
1339
+ border-radius: 4px;
1340
+ background: white;
1341
+ font-size: 14px;
1342
+ }
1343
+
1344
+ .cv-widget-profile-select:focus,
1345
+ .cv-widget-state-select:focus {
1346
+ outline: none;
1347
+ border-color: #007bff;
1348
+ box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
1349
+ }
1350
+
1351
+ .cv-widget-profile-select:disabled,
1352
+ .cv-widget-state-select:disabled {
1353
+ background: #f8f9fa;
1354
+ color: #6c757d;
1355
+ cursor: not-allowed;
1356
+ }
1357
+
1358
+ .cv-widget-current {
1359
+ margin: 16px 0;
1360
+ padding: 12px;
1361
+ background: #f8f9fa;
1362
+ border-radius: 4px;
1363
+ border-left: 4px solid #007bff;
1364
+ }
1365
+
1366
+ .cv-widget-current label {
1367
+ font-size: 12px;
1368
+ text-transform: uppercase;
1369
+ letter-spacing: 0.5px;
1370
+ color: #666;
1371
+ margin-bottom: 4px;
1372
+ }
1373
+
1374
+ .cv-widget-current-view {
1375
+ font-weight: 500;
1376
+ color: #333;
1377
+ }
1378
+
1379
+ .cv-widget-reset {
1380
+ width: 100%;
1381
+ padding: 8px 16px;
1382
+ background: #dc3545;
1383
+ color: white;
1384
+ border: none;
1385
+ border-radius: 4px;
1386
+ cursor: pointer;
1387
+ font-size: 14px;
1388
+ font-weight: 500;
1389
+ }
1390
+
1391
+ .cv-widget-reset:hover {
1392
+ background: #c82333;
1393
+ }
1394
+
1395
+ .cv-widget-reset:active {
1396
+ background: #bd2130;
1397
+ }
1398
+
1399
+ /* Responsive design for mobile */
1400
+ @media (max-width: 768px) {
1401
+ .cv-widget-top-right,
1402
+ .cv-widget-top-left {
1403
+ top: 10px;
1404
+ }
1405
+
1406
+ .cv-widget-bottom-right,
1407
+ .cv-widget-bottom-left {
1408
+ bottom: 10px;
1409
+ }
1410
+
1411
+ /* All widgets stay flush with screen edges */
1412
+ .cv-widget-top-right,
1413
+ .cv-widget-bottom-right,
1414
+ .cv-widget-middle-right {
1415
+ right: 0;
1416
+ }
1417
+
1418
+ .cv-widget-top-left,
1419
+ .cv-widget-bottom-left,
1420
+ .cv-widget-middle-left {
1421
+ left: 0;
1422
+ }
1423
+
1424
+ /* Slightly smaller on mobile */
1425
+ .cv-widget-icon {
1426
+ width: 60px;
1427
+ height: 32px;
1428
+ }
1429
+
1430
+ .cv-widget-icon:hover {
1431
+ width: 75px;
1432
+ }
1433
+ }
1434
+
1435
+ /* Modal styles */
1436
+ .cv-widget-modal-overlay {
1437
+ position: fixed;
1438
+ top: 0;
1439
+ left: 0;
1440
+ right: 0;
1441
+ bottom: 0;
1442
+ background: rgba(0, 0, 0, 0.5);
1443
+ display: flex;
1444
+ align-items: center;
1445
+ justify-content: center;
1446
+ z-index: 10002;
1447
+ animation: fadeIn 0.2s ease;
1448
+ }
1449
+
1450
+ @keyframes fadeIn {
1451
+ from { opacity: 0; }
1452
+ to { opacity: 1; }
1453
+ }
1454
+
1455
+ .cv-widget-modal {
1456
+ background: white;
1457
+ border-radius: 8px;
1458
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
1459
+ max-width: 400px;
1460
+ width: 90vw;
1461
+ max-height: 80vh;
1462
+ overflow-y: auto;
1463
+ animation: slideIn 0.2s ease;
1464
+ }
1465
+
1466
+ @keyframes slideIn {
1467
+ from {
1468
+ opacity: 0;
1469
+ transform: scale(0.9) translateY(-20px);
1470
+ }
1471
+ to {
1472
+ opacity: 1;
1473
+ transform: scale(1) translateY(0);
1474
+ }
1475
+ }
1476
+
1477
+ .cv-widget-modal-header {
1478
+ display: flex;
1479
+ justify-content: space-between;
1480
+ align-items: center;
1481
+ padding: 16px 20px;
1482
+ border-bottom: 1px solid #e9ecef;
1483
+ background: #f8f9fa;
1484
+ border-radius: 8px 8px 0 0;
1485
+ }
1486
+
1487
+ .cv-widget-modal-header h3 {
1488
+ margin: 0;
1489
+ font-size: 18px;
1490
+ font-weight: 600;
1491
+ color: #333;
1492
+ }
1493
+
1494
+ .cv-widget-modal-close {
1495
+ background: none;
1496
+ border: none;
1497
+ font-size: 20px;
1498
+ cursor: pointer;
1499
+ padding: 0;
1500
+ width: 32px;
1501
+ height: 32px;
1502
+ display: flex;
1503
+ align-items: center;
1504
+ justify-content: center;
1505
+ border-radius: 4px;
1506
+ color: #666;
1507
+ line-height: 1;
1508
+ transition: all 0.2s ease;
1509
+ }
1510
+
1511
+ .cv-widget-modal-close:hover {
1512
+ background: #e9ecef;
1513
+ color: #333;
1514
+ }
1515
+
1516
+ .cv-widget-modal-content {
1517
+ padding: 20px;
1518
+ }
1519
+
1520
+ .cv-widget-modal-actions {
1521
+ margin-top: 20px;
1522
+ padding-top: 16px;
1523
+ border-top: 1px solid #e9ecef;
1524
+ }
1525
+
1526
+ .cv-widget-restore {
1527
+ width: 100%;
1528
+ padding: 10px 16px;
1529
+ background: #28a745;
1530
+ color: white;
1531
+ border: none;
1532
+ border-radius: 4px;
1533
+ cursor: pointer;
1534
+ font-size: 14px;
1535
+ font-weight: 500;
1536
+ }
1537
+
1538
+ .cv-widget-restore:hover {
1539
+ background: #218838;
1540
+ }
1541
+
1542
+ .cv-widget-create-state {
1543
+ width: 100%;
1544
+ padding: 10px 16px;
1545
+ background: #007bff;
1546
+ color: white;
1547
+ border: none;
1548
+ border-radius: 4px;
1549
+ cursor: pointer;
1550
+ font-size: 14px;
1551
+ font-weight: 500;
1552
+ margin-bottom: 10px;
1553
+ }
1554
+
1555
+ .cv-widget-create-state:hover {
1556
+ background: #0056b3;
1557
+ }
1558
+
1559
+ /* Dark theme modal styles */
1560
+ .cv-widget-theme-dark .cv-widget-modal {
1561
+ background: #2d3748;
1562
+ color: #e2e8f0;
1563
+ }
1564
+
1565
+ .cv-widget-theme-dark .cv-widget-modal-header {
1566
+ background: #1a202c;
1567
+ border-color: #4a5568;
1568
+ }
1569
+
1570
+ .cv-widget-theme-dark .cv-widget-modal-header h3 {
1571
+ color: #e2e8f0;
1572
+ }
1573
+
1574
+ .cv-widget-theme-dark .cv-widget-modal-close {
1575
+ color: #a0aec0;
1576
+ }
1577
+
1578
+ .cv-widget-theme-dark .cv-widget-modal-close:hover {
1579
+ background: #4a5568;
1580
+ color: #e2e8f0;
1581
+ }
1582
+
1583
+ .cv-widget-theme-dark .cv-widget-modal-actions {
1584
+ border-color: #4a5568;
1585
+ }
1586
+
1587
+ /* Custom state creator styles */
1588
+ .cv-custom-state-modal {
1589
+ max-width: 500px;
1590
+ }
1591
+
1592
+ .cv-custom-state-form h4 {
1593
+ margin: 20px 0 10px 0;
1594
+ font-size: 16px;
1595
+ font-weight: 600;
1596
+ color: #333;
1597
+ border-bottom: 1px solid #e9ecef;
1598
+ padding-bottom: 5px;
1599
+ }
1600
+
1601
+ .cv-custom-state-form p {
1602
+ font-size: 15px;
1603
+ line-height: 1.6;
1604
+ color: #555;
1605
+ margin-bottom: 24px;
1606
+ text-align: justify;
1607
+ }
1608
+
1609
+ .cv-custom-state-section {
1610
+ margin-bottom: 16px;
1611
+ }
1612
+
1613
+ .cv-custom-state-section label {
1614
+ display: block;
1615
+ margin-bottom: 4px;
1616
+ font-weight: 500;
1617
+ color: #555;
1618
+ }
1619
+
1620
+ .cv-custom-state-input {
1621
+ width: 100%;
1622
+ padding: 8px 12px;
1623
+ border: 1px solid #ddd;
1624
+ border-radius: 4px;
1625
+ background: white;
1626
+ font-size: 14px;
1627
+ }
1628
+
1629
+ .cv-custom-state-input:focus {
1630
+ outline: none;
1631
+ border-color: #007bff;
1632
+ box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
1633
+ }
1634
+
1635
+ .cv-custom-toggles {
1636
+ display: grid;
1637
+ grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
1638
+ gap: 10px;
1639
+ }
1640
+
1641
+ .cv-custom-state-toggle {
1642
+ display: flex;
1643
+ align-items: center;
1644
+ }
1645
+
1646
+ .cv-custom-state-toggle label {
1647
+ display: flex;
1648
+ align-items: center;
1649
+ cursor: pointer;
1650
+ font-weight: normal;
1651
+ margin: 0;
1652
+ }
1653
+
1654
+ .cv-custom-toggle-checkbox {
1655
+ margin-right: 8px;
1656
+ width: auto;
1657
+ }
1658
+
1659
+ .cv-tab-groups {
1660
+ margin-top: 20px;
1661
+ }
1662
+
1663
+ .cv-tab-group-control {
1664
+ margin-bottom: 15px;
1665
+ }
1666
+
1667
+ .cv-tab-group-control label {
1668
+ display: block;
1669
+ margin-bottom: 5px;
1670
+ font-weight: 500;
1671
+ font-size: 14px;
1672
+ }
1673
+
1674
+ .cv-tab-group-select {
1675
+ width: 100%;
1676
+ padding: 8px 12px;
1677
+ border: 1px solid #ced4da;
1678
+ border-radius: 4px;
1679
+ font-size: 14px;
1680
+ background-color: white;
1681
+ cursor: pointer;
1682
+ }
1683
+
1684
+ .cv-tab-group-select:focus {
1685
+ outline: none;
1686
+ border-color: #007bff;
1687
+ box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
1688
+ }
1689
+
1690
+ .cv-widget-theme-dark .cv-tab-group-select {
1691
+ background-color: #2d3748;
1692
+ border-color: #4a5568;
1693
+ color: #e2e8f0;
1694
+ }
1695
+
1696
+ .cv-custom-state-actions {
1697
+ display: flex;
1698
+ gap: 10px;
1699
+ margin-top: 20px;
1700
+ padding-top: 16px;
1701
+ border-top: 1px solid #e9ecef;
1702
+ }
1703
+
1704
+ .cv-custom-state-cancel,
1705
+ .cv-custom-state-copy-url {
1706
+ flex: 1;
1707
+ padding: 10px 16px;
1708
+ border: none;
1709
+ border-radius: 4px;
1710
+ cursor: pointer;
1711
+ font-size: 14px;
1712
+ font-weight: 500;
1713
+ }
1714
+
1715
+ .cv-custom-state-reset {
1716
+ flex: 1;
1717
+ padding: 10px 16px;
1718
+ border: none;
1719
+ border-radius: 4px;
1720
+ cursor: pointer;
1721
+ font-size: 14px;
1722
+ font-weight: 500;
1723
+ background: #dc3545;
1724
+ color: white;
1725
+ }
1726
+
1727
+ .cv-custom-state-reset:hover {
1728
+ background: #c82333;
1729
+ }
1730
+
1731
+ .cv-custom-state-cancel {
1732
+ background: #6c757d;
1733
+ color: white;
1734
+ }
1735
+
1736
+ .cv-custom-state-cancel:hover {
1737
+ background: #5a6268;
1738
+ }
1739
+
1740
+ .cv-custom-state-copy-url {
1741
+ background: #28a745;
1742
+ color: white;
1743
+ }
1744
+
1745
+ .cv-custom-state-copy-url:hover {
1746
+ background: #218838;
1747
+ }
1748
+
1749
+ /* Dark theme custom state styles */
1750
+ .cv-widget-theme-dark .cv-custom-state-form h4 {
1751
+ color: #e2e8f0;
1752
+ border-color: #4a5568;
1753
+ }
1754
+
1755
+ .cv-widget-theme-dark .cv-custom-state-form p {
1756
+ color: #cbd5e0;
1757
+ }
1758
+
1759
+ .cv-widget-theme-dark .cv-custom-state-section label {
1760
+ color: #a0aec0;
1761
+ }
1762
+
1763
+ .cv-widget-theme-dark .cv-custom-state-input {
1764
+ background: #1a202c;
1765
+ border-color: #4a5568;
1766
+ color: #e2e8f0;
1767
+ }
1768
+
1769
+ .cv-widget-theme-dark .cv-custom-state-actions {
1770
+ border-color: #4a5568;
1771
+ }
1772
+
1773
+ /* Welcome modal styles */
1774
+ .cv-welcome-modal {
1775
+ max-width: 500px;
1776
+ }
1777
+
1778
+ .cv-welcome-content {
1779
+ text-align: center;
1780
+ }
1781
+
1782
+ .cv-welcome-content p {
1783
+ font-size: 15px;
1784
+ line-height: 1.6;
1785
+ color: #555;
1786
+ margin-bottom: 24px;
1787
+ text-align: justify;
1788
+ }
1789
+
1790
+ .cv-welcome-widget-preview {
1791
+ display: flex;
1792
+ flex-direction: column;
1793
+ align-items: center;
1794
+ gap: 12px;
1795
+ padding: 20px;
1796
+ background: #f8f9fa;
1797
+ border-radius: 8px;
1798
+ margin-bottom: 24px;
1799
+ }
1800
+
1801
+ .cv-welcome-widget-icon {
1802
+ width: 36px;
1803
+ height: 36px;
1804
+ background: white;
1805
+ color: black;
1806
+ border-radius: 0 18px 18px 0;
1807
+ display: flex;
1808
+ align-items: center;
1809
+ justify-content: center;
1810
+ font-size: 18px;
1811
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
1812
+ }
1813
+
1814
+ .cv-welcome-widget-label {
1815
+ font-size: 14px;
1816
+ color: #666;
1817
+ margin: 0;
1818
+ font-weight: 500;
1819
+ }
1820
+
1821
+ .cv-welcome-got-it {
1822
+ width: 100%;
1823
+ padding: 12px 24px;
1824
+ background: #007bff;
1825
+ color: white;
1826
+ border: none;
1827
+ border-radius: 4px;
1828
+ cursor: pointer;
1829
+ font-size: 16px;
1830
+ font-weight: 600;
1831
+ transition: background 0.2s ease;
1832
+ }
1833
+
1834
+ .cv-welcome-got-it:hover {
1835
+ background: #0056b3;
1836
+ }
1837
+
1838
+ .cv-welcome-got-it:active {
1839
+ background: #004494;
1840
+ }
1841
+
1842
+ /* Dark theme welcome modal styles */
1843
+ .cv-widget-theme-dark .cv-welcome-content p {
1844
+ color: #cbd5e0;
1845
+ }
1846
+
1847
+ .cv-widget-theme-dark .cv-welcome-widget-preview {
1848
+ background: #1a202c;
1849
+ }
1850
+
1851
+ .cv-widget-theme-dark .cv-welcome-widget-label {
1852
+ color: #a0aec0;
1853
+ }
1177
1854
  `;
1178
1855
  /**
1179
1856
  * Inject widget styles into the document head
@@ -1208,8 +1885,9 @@
1208
1885
  title: options.title || 'Custom Views',
1209
1886
  description: options.description || 'Toggle different content sections to customize your view. Changes are applied instantly and the URL will be updated for sharing.',
1210
1887
  showWelcome: options.showWelcome ?? false,
1211
- welcomeTitle: options.welcomeTitle || 'Welcome to Custom Views!',
1212
- welcomeMessage: options.welcomeMessage || 'This website uses Custom Views to let you personalize your experience. Use the widget on the side (⚙) to show or hide different content sections based on your preferences. Your selections will be saved and can be shared via URL.'
1888
+ welcomeTitle: options.welcomeTitle || 'This website uses Custom Views',
1889
+ welcomeMessage: options.welcomeMessage || 'This site uses Custom Views to let you personalize your experience. Use the widget on the side (⚙) to show or hide different content sections based on your preferences. Your selections will be saved and can be shared via URL.',
1890
+ showTabGroups: options.showTabGroups ?? true
1213
1891
  };
1214
1892
  // No external state manager to initialize
1215
1893
  }
@@ -1274,8 +1952,8 @@
1274
1952
  */
1275
1953
  openStateModal() {
1276
1954
  // Get toggles from current configuration and open the modal regardless of count
1277
- const localConfig = this.core.getLocalConfig();
1278
- const toggles = localConfig?.allToggles || [];
1955
+ const config = this.core.getConfig();
1956
+ const toggles = config?.allToggles || [];
1279
1957
  this.createCustomStateModal(toggles);
1280
1958
  }
1281
1959
  /**
@@ -1288,37 +1966,61 @@
1288
1966
  this.modal.className = 'cv-widget-modal-overlay';
1289
1967
  this.applyThemeToModal();
1290
1968
  const toggleControls = toggles.length
1291
- ? toggles.map(toggle => `
1292
- <div class="cv-custom-state-toggle">
1293
- <label>
1294
- <input type="checkbox" class="cv-custom-toggle-checkbox" data-toggle="${toggle}" />
1295
- ${this.formatToggleName(toggle)}
1296
- </label>
1297
- </div>
1969
+ ? toggles.map(toggle => `
1970
+ <div class="cv-custom-state-toggle">
1971
+ <label>
1972
+ <input type="checkbox" class="cv-custom-toggle-checkbox" data-toggle="${toggle}" />
1973
+ ${this.formatToggleName(toggle)}
1974
+ </label>
1975
+ </div>
1298
1976
  `).join('')
1299
1977
  : `<p class="cv-no-toggles">No configurable sections available.</p>`;
1300
- this.modal.innerHTML = `
1301
- <div class="cv-widget-modal cv-custom-state-modal">
1302
- <div class="cv-widget-modal-header">
1303
- <h3>Customize View</h3>
1304
- <button class="cv-widget-modal-close" aria-label="Close modal">X</button>
1305
- </div>
1306
- <div class="cv-widget-modal-content">
1307
- <div class="cv-custom-state-form">
1308
- <p>${this.options.description}</p>
1309
-
1310
- <h4>Content Sections</h4>
1311
- <div class="cv-custom-toggles">
1312
- ${toggleControls}
1313
- </div>
1314
-
1315
- <div class="cv-custom-state-actions">
1316
- ${this.options.showReset ? `<button class="cv-custom-state-reset">Reset to Default</button>` : ''}
1317
- <button class="cv-custom-state-copy-url">Copy Shareable URL</button>
1318
- </div>
1319
- </div>
1320
- </div>
1321
- </div>
1978
+ // Get tab groups
1979
+ const tabGroups = this.core.getTabGroups();
1980
+ let tabGroupsHTML = '';
1981
+ if (this.options.showTabGroups && tabGroups && tabGroups.length > 0) {
1982
+ const tabGroupControls = tabGroups.map(group => {
1983
+ const options = group.tabs.map(tab => `<option value="${tab.id}">${tab.label || tab.id}</option>`).join('');
1984
+ return `
1985
+ <div class="cv-tab-group-control">
1986
+ <label for="tab-group-${group.id}">${group.label || group.id}</label>
1987
+ <select id="tab-group-${group.id}" class="cv-tab-group-select" data-group-id="${group.id}">
1988
+ ${options}
1989
+ </select>
1990
+ </div>
1991
+ `;
1992
+ }).join('');
1993
+ tabGroupsHTML = `
1994
+ <h4>Tab Groups</h4>
1995
+ <div class="cv-tab-groups">
1996
+ ${tabGroupControls}
1997
+ </div>
1998
+ `;
1999
+ }
2000
+ this.modal.innerHTML = `
2001
+ <div class="cv-widget-modal cv-custom-state-modal">
2002
+ <div class="cv-widget-modal-header">
2003
+ <h3>Customize View</h3>
2004
+ <button class="cv-widget-modal-close" aria-label="Close modal">×</button>
2005
+ </div>
2006
+ <div class="cv-widget-modal-content">
2007
+ <div class="cv-custom-state-form">
2008
+ <p>${this.options.description}</p>
2009
+
2010
+ <h4>Content Sections</h4>
2011
+ <div class="cv-custom-toggles">
2012
+ ${toggleControls}
2013
+ </div>
2014
+
2015
+ ${tabGroupsHTML}
2016
+
2017
+ <div class="cv-custom-state-actions">
2018
+ ${this.options.showReset ? `<button class="cv-custom-state-reset">Reset to Default</button>` : ''}
2019
+ <button class="cv-custom-state-copy-url">Copy Shareable URL</button>
2020
+ </div>
2021
+ </div>
2022
+ </div>
2023
+ </div>
1322
2024
  `;
1323
2025
  document.body.appendChild(this.modal);
1324
2026
  this.attachStateModalEventListeners();
@@ -1361,6 +2063,17 @@
1361
2063
  this.core.applyState(state);
1362
2064
  });
1363
2065
  });
2066
+ // Listen to tab group selects
2067
+ const tabGroupSelects = this.modal.querySelectorAll('.cv-tab-group-select');
2068
+ tabGroupSelects.forEach(select => {
2069
+ select.addEventListener('change', () => {
2070
+ const groupId = select.dataset.groupId;
2071
+ const tabId = select.value;
2072
+ if (groupId && tabId) {
2073
+ this.core.setActiveTab(groupId, tabId);
2074
+ }
2075
+ });
2076
+ });
1364
2077
  // Overlay click to close
1365
2078
  this.modal.addEventListener('click', (e) => {
1366
2079
  if (e.target === this.modal) {
@@ -1405,7 +2118,16 @@
1405
2118
  toggles.push(toggle);
1406
2119
  }
1407
2120
  });
1408
- return { toggles };
2121
+ // Collect tab selections
2122
+ const tabGroupSelects = this.modal.querySelectorAll('.cv-tab-group-select');
2123
+ const tabs = {};
2124
+ tabGroupSelects.forEach(select => {
2125
+ const groupId = select.dataset.groupId;
2126
+ if (groupId) {
2127
+ tabs[groupId] = select.value;
2128
+ }
2129
+ });
2130
+ return Object.keys(tabs).length > 0 ? { toggles, tabs } : { toggles };
1409
2131
  }
1410
2132
  /**
1411
2133
  * Copy shareable URL to clipboard
@@ -1441,6 +2163,15 @@
1441
2163
  }
1442
2164
  }
1443
2165
  });
2166
+ // Load tab group selections
2167
+ const activeTabs = this.core.getCurrentActiveTabs();
2168
+ const tabGroupSelects = this.modal.querySelectorAll('.cv-tab-group-select');
2169
+ tabGroupSelects.forEach(select => {
2170
+ const groupId = select.dataset.groupId;
2171
+ if (groupId && activeTabs[groupId]) {
2172
+ select.value = activeTabs[groupId];
2173
+ }
2174
+ });
1444
2175
  }
1445
2176
  /**
1446
2177
  * Format toggle name for display
@@ -1474,25 +2205,25 @@
1474
2205
  this.modal = document.createElement('div');
1475
2206
  this.modal.className = 'cv-widget-modal-overlay cv-welcome-modal-overlay';
1476
2207
  this.applyThemeToModal();
1477
- this.modal.innerHTML = `
1478
- <div class="cv-widget-modal cv-welcome-modal">
1479
- <div class="cv-widget-modal-header">
1480
- <h3>${this.options.welcomeTitle}</h3>
1481
- <button class="cv-widget-modal-close" aria-label="Close modal">×</button>
1482
- </div>
1483
- <div class="cv-widget-modal-content">
1484
- <div class="cv-welcome-content">
1485
- <p>${this.options.welcomeMessage}</p>
1486
-
1487
- <div class="cv-welcome-widget-preview">
1488
- <div class="cv-welcome-widget-icon">⚙</div>
1489
- <p class="cv-welcome-widget-label">Look for this widget on the side of the screen</p>
1490
- </div>
1491
-
1492
- <button class="cv-welcome-got-it">Got it!</button>
1493
- </div>
1494
- </div>
1495
- </div>
2208
+ this.modal.innerHTML = `
2209
+ <div class="cv-widget-modal cv-welcome-modal">
2210
+ <div class="cv-widget-modal-header">
2211
+ <h3>${this.options.welcomeTitle}</h3>
2212
+ <button class="cv-widget-modal-close" aria-label="Close modal">×</button>
2213
+ </div>
2214
+ <div class="cv-widget-modal-content">
2215
+ <div class="cv-welcome-content">
2216
+ <p>${this.options.welcomeMessage}</p>
2217
+
2218
+ <div class="cv-welcome-widget-preview">
2219
+ <div class="cv-welcome-widget-icon">⚙</div>
2220
+ <p class="cv-welcome-widget-label">Look for this widget on the side of the screen</p>
2221
+ </div>
2222
+
2223
+ <button class="cv-welcome-got-it">Got it!</button>
2224
+ </div>
2225
+ </div>
2226
+ </div>
1496
2227
  `;
1497
2228
  document.body.appendChild(this.modal);
1498
2229
  this.attachWelcomeModalEventListeners();
@@ -1534,76 +2265,162 @@
1534
2265
  }
1535
2266
  }
1536
2267
 
1537
- class CustomViews {
1538
- // Helper function to prepend baseURL to a path
1539
- static prependBaseURL(path, baseURL) {
1540
- if (!baseURL)
1541
- return path;
1542
- // Don't prepend if the path is already absolute (starts with http:// or https://)
1543
- if (path.startsWith('http://') || path.startsWith('https://')) {
1544
- return path;
1545
- }
1546
- // Ensure baseURL doesn't end with / and path starts with /
1547
- const cleanBaseURL = baseURL.endsWith('/') ? baseURL.slice(0, -1) : baseURL;
1548
- const cleanPath = path.startsWith('/') ? path : '/' + path;
1549
- return cleanBaseURL + cleanPath;
2268
+ /**
2269
+ * Initialize CustomViews from script tag attributes and config file
2270
+ * This function handles the automatic initialization of CustomViews when included via script tag
2271
+ *
2272
+ * Data attributes supported:
2273
+ * - data-base-url: Base URL for the site (e.g., "/customviews" for subdirectory deployments)
2274
+ * - data-config-path: Path to config file (default: "/customviews.config.json")
2275
+ *
2276
+ * The function fetches the config file and uses it directly to initialize CustomViews.
2277
+ * Widget visibility is controlled via the config file (widget.enabled property).
2278
+ */
2279
+ function initializeFromScript() {
2280
+ // Only run in browser environment
2281
+ if (typeof window === 'undefined')
2282
+ return;
2283
+ // Use the typed global `window` (see src/types/global.d.ts)
2284
+ // Idempotency guard: if already initialized, skip setting up listener again.
2285
+ if (window.__customViewsInitialized) {
2286
+ // Informational for developers; harmless in production.
2287
+ console.info('[CustomViews] Auto-init skipped: already initialized.');
2288
+ return;
1550
2289
  }
1551
- // Entry Point to use CustomViews
1552
- static async initFromJson(opts) {
1553
- // Load assets JSON if provided
1554
- let assetsManager;
1555
- const baseURL = opts.baseURL || '';
1556
- if (opts.assetsJsonPath) {
1557
- const assetsPath = this.prependBaseURL(opts.assetsJsonPath, baseURL);
1558
- const assetsJson = await (await fetch(assetsPath)).json();
1559
- assetsManager = new AssetsManager(assetsJson, baseURL);
1560
- }
1561
- else {
1562
- assetsManager = new AssetsManager({}, baseURL);
1563
- }
1564
- // Load config JSON if provided, else just log error and don't load the custom views
1565
- let localConfig;
1566
- if (opts.config) {
1567
- localConfig = opts.config;
2290
+ document.addEventListener('DOMContentLoaded', async function () {
2291
+ // Prevent concurrent initialization runs (race conditions when script is loaded twice)
2292
+ if (window.__customViewsInitInProgress || window.__customViewsInitialized) {
2293
+ return;
1568
2294
  }
1569
- else {
1570
- if (!opts.configPath) {
1571
- console.error("No config path provided, skipping custom views");
1572
- return null;
2295
+ window.__customViewsInitInProgress = true;
2296
+ try {
2297
+ // Find the script tag
2298
+ let scriptTag = document.currentScript;
2299
+ // Fallback if currentScript is not available (executed after page load)
2300
+ if (!scriptTag) {
2301
+ // Try to find the script tag by looking for our script
2302
+ const scripts = document.querySelectorAll('script[src*="custom-views"]');
2303
+ if (scripts.length > 0) {
2304
+ // Find the most specific match (to avoid matching other custom-views scripts)
2305
+ for (let i = 0; i < scripts.length; i++) {
2306
+ const script = scripts[i];
2307
+ const src = script.getAttribute('src') || '';
2308
+ // Look for .min.js or .js at the end
2309
+ if (src.match(/custom-views(\.min)?\.js($|\?)/)) {
2310
+ scriptTag = script;
2311
+ break;
2312
+ }
2313
+ }
2314
+ // If no specific match found, use the first one
2315
+ if (!scriptTag) {
2316
+ scriptTag = scripts[0];
2317
+ }
2318
+ }
1573
2319
  }
2320
+ // Read data attributes from script tag
2321
+ let baseURL = '';
2322
+ let configPath = '/customviews.config.json';
2323
+ if (scriptTag) {
2324
+ baseURL = scriptTag.getAttribute('data-base-url') || '';
2325
+ configPath = scriptTag.getAttribute('data-config-path') || configPath;
2326
+ }
2327
+ // Fetch config file
2328
+ let configFile;
1574
2329
  try {
1575
- const configPath = this.prependBaseURL(opts.configPath, baseURL);
1576
- localConfig = await (await fetch(configPath)).json();
2330
+ const fullConfigPath = prependBaseUrl(configPath, baseURL);
2331
+ console.log(`[CustomViews] Loading config from: ${fullConfigPath}`);
2332
+ const response = await fetch(fullConfigPath);
2333
+ if (!response.ok) {
2334
+ console.warn(`[CustomViews] Config file not found at ${fullConfigPath}. Using defaults.`);
2335
+ // Provide minimal default config structure
2336
+ configFile = {
2337
+ config: {
2338
+ allToggles: [],
2339
+ defaultState: { toggles: [] }
2340
+ },
2341
+ widget: {
2342
+ enabled: true
2343
+ }
2344
+ };
2345
+ }
2346
+ else {
2347
+ configFile = await response.json();
2348
+ console.log('[CustomViews] Config loaded successfully');
2349
+ }
1577
2350
  }
1578
2351
  catch (error) {
1579
- console.error("Error loading config:", error);
1580
- return null;
2352
+ console.error('[CustomViews] Error loading config file:', error);
2353
+ return; // Abort initialization
2354
+ }
2355
+ // Determine effective baseURL (data attribute takes precedence over config)
2356
+ const effectiveBaseURL = baseURL || configFile.baseURL || '';
2357
+ const options = {
2358
+ config: configFile.config,
2359
+ assetsJsonPath: configFile.assetsJsonPath,
2360
+ baseURL: effectiveBaseURL,
2361
+ };
2362
+ if (configFile.showUrl !== undefined) {
2363
+ options.showUrl = configFile.showUrl;
2364
+ }
2365
+ // Initialize CustomViews core
2366
+ const core = await CustomViews.init(options);
2367
+ if (!core) {
2368
+ console.error('[CustomViews] Failed to initialize core.');
2369
+ return; // Abort widget creation
1581
2370
  }
2371
+ // Store instance
2372
+ window.customViewsInstance = { core };
2373
+ // Initialize widget if enabled in config
2374
+ let widget;
2375
+ if (configFile.widget?.enabled !== false) {
2376
+ widget = new CustomViewsWidget({
2377
+ core,
2378
+ ...configFile.widget
2379
+ });
2380
+ widget.render();
2381
+ // Store widget instance
2382
+ window.customViewsInstance.widget = widget;
2383
+ console.log('[CustomViews] Widget initialized and rendered');
2384
+ }
2385
+ else {
2386
+ console.log('[CustomViews] Widget disabled in config - skipping initialization');
2387
+ }
2388
+ // Dispatch ready event
2389
+ const readyEvent = new CustomEvent('customviews:ready', {
2390
+ detail: {
2391
+ core,
2392
+ widget
2393
+ }
2394
+ });
2395
+ document.dispatchEvent(readyEvent);
2396
+ // Mark initialized and clear in-progress flag
2397
+ window.__customViewsInitialized = true;
2398
+ window.__customViewsInitInProgress = false;
1582
2399
  }
1583
- const coreOptions = {
1584
- assetsManager,
1585
- config: localConfig,
1586
- rootEl: opts.rootEl,
1587
- };
1588
- const core = new CustomViewsCore(coreOptions);
1589
- core.init();
1590
- return core;
1591
- }
2400
+ catch (error) {
2401
+ // Clear in-progress flag so a future attempt can retry
2402
+ window.__customViewsInitInProgress = false;
2403
+ console.error('[CustomViews] Auto-initialization error:', error);
2404
+ }
2405
+ });
1592
2406
  }
2407
+
2408
+ // Import from new modules
2409
+ // Set up globals and auto-initialization
1593
2410
  if (typeof window !== "undefined") {
1594
- // @ts-ignore
2411
+ // Expose to window to enable usage (e.g. const app = new window.CustomViews(...))
1595
2412
  window.CustomViews = CustomViews;
1596
- // @ts-ignore
1597
2413
  window.CustomViewsWidget = CustomViewsWidget;
2414
+ // Run auto-initialization
2415
+ initializeFromScript();
1598
2416
  }
1599
2417
 
1600
2418
  exports.AssetsManager = AssetsManager;
1601
2419
  exports.CustomViews = CustomViews;
1602
2420
  exports.CustomViewsCore = CustomViewsCore;
1603
2421
  exports.CustomViewsWidget = CustomViewsWidget;
1604
- exports.LocalConfig = Config;
1605
2422
  exports.PersistenceManager = PersistenceManager;
1606
2423
  exports.URLStateManager = URLStateManager;
1607
2424
 
1608
2425
  }));
1609
- //# sourceMappingURL=custom-views.umd.js.map
2426
+ //# sourceMappingURL=custom-views.js.map