|
6 | 6 | if (window.__knowrithmWidgetLoaded) return; |
7 | 7 | window.__knowrithmWidgetLoaded = true; |
8 | 8 |
|
| 9 | + var WIDGET_STYLE_ID = 'knowrithm-widget-style'; |
| 10 | + var WIDGET_STYLE_HREF = '/styles/chat-widget-custom.css'; |
| 11 | + var NAV_ELEMENTS = [ |
| 12 | + { id: 'navbar', minWidth: 0, display: 'block', zIndex: 90 }, |
| 13 | + { id: 'sidebar', minWidth: 1024, display: 'block', zIndex: 80 }, |
| 14 | + { id: 'content-side-layout', minWidth: 1280, display: 'flex', zIndex: 70 }, |
| 15 | + { id: 'table-of-contents-layout', minWidth: 1280, display: 'flex', zIndex: 70 }, |
| 16 | + { id: 'table-of-contents', minWidth: 1280, display: 'block', zIndex: 70 } |
| 17 | + ]; |
| 18 | + var navVisibilityGuardAttached = false; |
| 19 | + var navMutationObserver = null; |
| 20 | + var navElementObservers = {}; |
| 21 | + |
| 22 | + function observeNavElement(ruleId, element) { |
| 23 | + if (typeof MutationObserver === 'undefined' || !element) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + var existing = navElementObservers[ruleId]; |
| 28 | + if (existing && existing.element === element) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (existing && existing.observer) { |
| 33 | + existing.observer.disconnect(); |
| 34 | + } |
| 35 | + |
| 36 | + var observer = new MutationObserver(function () { |
| 37 | + applyAllNavigationRules(); |
| 38 | + }); |
| 39 | + |
| 40 | + observer.observe(element, { attributes: true, attributeFilter: ['class', 'style', 'hidden'] }); |
| 41 | + navElementObservers[ruleId] = { observer: observer, element: element }; |
| 42 | + } |
| 43 | + |
| 44 | + function applyNavigationRule(rule) { |
| 45 | + var element = document.getElementById(rule.id); |
| 46 | + if (!element) return; |
| 47 | + |
| 48 | + var meetsBreakpoint = true; |
| 49 | + if (rule.minWidth) { |
| 50 | + if (typeof window.matchMedia === 'function') { |
| 51 | + meetsBreakpoint = window.matchMedia('(min-width: ' + rule.minWidth + 'px)').matches; |
| 52 | + } else { |
| 53 | + meetsBreakpoint = window.innerWidth >= rule.minWidth; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (meetsBreakpoint) { |
| 58 | + var displayValue = rule.display || 'block'; |
| 59 | + element.style.setProperty('display', displayValue, 'important'); |
| 60 | + element.style.setProperty('visibility', 'visible', 'important'); |
| 61 | + element.style.setProperty('opacity', '1', 'important'); |
| 62 | + element.style.setProperty('pointer-events', 'auto', 'important'); |
| 63 | + if (typeof rule.zIndex === 'number') { |
| 64 | + element.style.setProperty('z-index', String(rule.zIndex), 'important'); |
| 65 | + } |
| 66 | + if (element.hasAttribute('hidden')) { |
| 67 | + element.removeAttribute('hidden'); |
| 68 | + } |
| 69 | + } else { |
| 70 | + ['display', 'visibility', 'opacity', 'pointer-events', 'z-index'].forEach(function (prop) { |
| 71 | + element.style.removeProperty(prop); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + observeNavElement(rule.id, element); |
| 76 | + } |
| 77 | + |
| 78 | + function applyAllNavigationRules() { |
| 79 | + NAV_ELEMENTS.forEach(applyNavigationRule); |
| 80 | + } |
| 81 | + |
| 82 | + function maintainNavigationVisibility() { |
| 83 | + if (typeof window === 'undefined' || typeof document === 'undefined') return; |
| 84 | + |
| 85 | + applyAllNavigationRules(); |
| 86 | + |
| 87 | + if (navVisibilityGuardAttached) { |
| 88 | + return; |
| 89 | + } |
| 90 | + navVisibilityGuardAttached = true; |
| 91 | + |
| 92 | + var queries = NAV_ELEMENTS |
| 93 | + .filter(function (rule) { return rule.minWidth; }) |
| 94 | + .map(function (rule) { return '(min-width: ' + rule.minWidth + 'px)'; }) |
| 95 | + .filter(function (query, index, self) { return self.indexOf(query) === index; }); |
| 96 | + |
| 97 | + function handleViewportChange() { |
| 98 | + applyAllNavigationRules(); |
| 99 | + } |
| 100 | + |
| 101 | + if (queries.length && typeof window.matchMedia === 'function') { |
| 102 | + queries.forEach(function (query) { |
| 103 | + var mediaQuery = window.matchMedia(query); |
| 104 | + if (typeof mediaQuery.addEventListener === 'function') { |
| 105 | + mediaQuery.addEventListener('change', handleViewportChange); |
| 106 | + } else if (typeof mediaQuery.addListener === 'function') { |
| 107 | + mediaQuery.addListener(handleViewportChange); |
| 108 | + } |
| 109 | + }); |
| 110 | + } else { |
| 111 | + window.addEventListener('resize', handleViewportChange, { passive: true }); |
| 112 | + } |
| 113 | + |
| 114 | + if (typeof MutationObserver !== 'undefined') { |
| 115 | + NAV_ELEMENTS.forEach(function (rule) { |
| 116 | + var target = document.getElementById(rule.id); |
| 117 | + if (target) { |
| 118 | + var observer = new MutationObserver(handleViewportChange); |
| 119 | + observer.observe(target, { attributes: true, attributeFilter: ['class', 'style'] }); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + if (!navMutationObserver) { |
| 124 | + navMutationObserver = new MutationObserver(function () { |
| 125 | + applyAllNavigationRules(); |
| 126 | + }); |
| 127 | + |
| 128 | + if (document.body) { |
| 129 | + navMutationObserver.observe(document.body, { childList: true, subtree: true }); |
| 130 | + } else { |
| 131 | + document.addEventListener('DOMContentLoaded', function () { |
| 132 | + if (!navMutationObserver) return; |
| 133 | + navMutationObserver.observe(document.body, { childList: true, subtree: true }); |
| 134 | + }); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
9 | 140 | // Wait for DOM to be ready |
10 | 141 | function initWidget() { |
| 142 | + maintainNavigationVisibility(); |
| 143 | + |
| 144 | + // Ensure the scoped widget stylesheet is loaded once |
| 145 | + if (!document.getElementById(WIDGET_STYLE_ID)) { |
| 146 | + var link = document.createElement('link'); |
| 147 | + link.id = WIDGET_STYLE_ID; |
| 148 | + link.rel = 'stylesheet'; |
| 149 | + link.href = WIDGET_STYLE_HREF; |
| 150 | + link.type = 'text/css'; |
| 151 | + link.media = 'all'; |
| 152 | + link.onerror = function (error) { |
| 153 | + console.error('Failed to load Knowrithm widget styles:', error); |
| 154 | + }; |
| 155 | + |
| 156 | + (document.head || document.body).appendChild(link); |
| 157 | + } |
| 158 | + |
11 | 159 | // Configure the widget BEFORE loading the script |
12 | 160 | window.AIChatWidget = { |
13 | 161 | agentId: "2b041b45-a585-47e9-abaf-ebaad766bce9", |
|
40 | 188 | }, |
41 | 189 | assets: { |
42 | 190 | scriptUrl: "https://app.knowrithm.org/api/widget.js", |
43 | | - styleUrl: "https://minio.knowrithm.org/knowrithm-bucket/chat-widget.css" |
| 191 | + styleUrl: WIDGET_STYLE_HREF |
44 | 192 | } |
45 | 193 | }; |
46 | 194 |
|
|
0 commit comments