|
15 | 15 | }; |
16 | 16 |
|
17 | 17 | const initializeKnowl = (knowl) => { |
18 | | - if (getComputedStyle(knowl)?.display === '') { |
19 | | - setTimeout(() => initializeKnowl(knowl), 100); |
20 | | - return; |
21 | | - } |
22 | | - |
23 | | - knowl.dataset.bsToggle = 'collapse'; |
24 | | - if (!knowl.knowlContainer) { |
25 | | - knowl.knowlContainer = document.createElement('div'); |
26 | | - knowl.knowlContainer.id = `knowl-uid-${knowlUID++}`; |
27 | | - knowl.knowlContainer.classList.add('collapse'); |
| 18 | + knowl.dataset.bsToggle = 'modal'; |
| 19 | + if (!knowl.knowlModal) { |
| 20 | + knowl.knowlModal = document.createElement('div'); |
| 21 | + knowl.knowlModal.id = `knowl-uid-${knowlUID++}`; |
| 22 | + knowl.knowlModal.classList.add('modal', 'fade'); |
| 23 | + knowl.knowlModal.tabIndex = -1; |
| 24 | + knowl.knowlModal.setAttribute('aria-labelledby', `${knowl.knowlModal.id}-title`); |
| 25 | + knowl.knowlModal.setAttribute('aria-hidden', 'true'); |
28 | 26 |
|
29 | | - const knowlOutput = document.createElement('div'); |
30 | | - knowlOutput.classList.add('knowl-output'); |
| 27 | + const knowlDialog = document.createElement('div'); |
| 28 | + knowlDialog.classList.add('modal-dialog', 'modal-dialog-centered', 'modal-dialog-scrollable'); |
| 29 | + knowl.knowlModal.append(knowlDialog); |
31 | 30 |
|
32 | 31 | const knowlContent = document.createElement('div'); |
33 | | - knowlContent.classList.add('knowl-content'); |
34 | | - knowlOutput.append(knowlContent); |
| 32 | + knowlContent.classList.add('modal-content'); |
| 33 | + knowlDialog.append(knowlContent); |
| 34 | + |
| 35 | + const knowlHeader = document.createElement('div'); |
| 36 | + knowlHeader.classList.add('modal-header'); |
| 37 | + |
| 38 | + const knowlTitle = document.createElement('h1'); |
| 39 | + knowlTitle.classList.add('modal-title', 'fs-5'); |
| 40 | + knowlTitle.id = `${knowl.knowlModal.id}-title`; |
| 41 | + knowlTitle.textContent = knowl.dataset.knowlTitle || knowl.textContent; |
| 42 | + |
| 43 | + const closeButton = document.createElement('button'); |
| 44 | + closeButton.type = 'button'; |
| 45 | + closeButton.classList.add('btn-close'); |
| 46 | + closeButton.dataset.bsDismiss = 'modal'; |
| 47 | + closeButton.setAttribute('aria-label', 'Close'); |
| 48 | + |
| 49 | + knowlHeader.append(knowlTitle, closeButton); |
| 50 | + |
| 51 | + const knowlBody = document.createElement('div'); |
| 52 | + knowlBody.classList.add('modal-body'); |
| 53 | + |
| 54 | + knowlContent.append(knowlHeader, knowlBody); |
35 | 55 |
|
36 | 56 | if (knowl.dataset.knowlUrl) { |
37 | 57 | const knowlFooter = document.createElement('div'); |
38 | | - knowlFooter.classList.add('knowl-footer'); |
| 58 | + knowlFooter.classList.add('modal-footer', 'knowl-footer', 'justify-content-center', 'p-1'); |
39 | 59 | knowlFooter.textContent = knowl.dataset.knowlUrl; |
40 | | - knowlOutput.append(knowlFooter); |
| 60 | + knowlContent.append(knowlFooter); |
41 | 61 | } |
42 | 62 |
|
43 | | - knowl.knowlContainer.appendChild(knowlOutput); |
44 | | - |
45 | | - knowl.knowlContainer.addEventListener('show.bs.collapse', () => knowl.classList.add('active')); |
46 | | - knowl.knowlContainer.addEventListener('hide.bs.collapse', () => knowl.classList.remove('active')); |
47 | | - |
48 | | - // If the knowl is inside a table row, then insert a new row into the table after that one to contain |
49 | | - // the knowl content. If the knowl is inside a list element, then insert the content after the list |
50 | | - // element. Otherwise insert the content either before the first sibling that follows it that is |
51 | | - // display block, or append it to the first ancestor that is display block. |
52 | | - let insertElt = knowl.closest('tr'); |
53 | | - if (insertElt) { |
54 | | - const row = document.createElement('tr'); |
55 | | - const td = document.createElement('td'); |
56 | | - td.colSpan = insertElt.childElementCount; |
57 | | - td.appendChild(knowl.knowlContainer); |
58 | | - row.appendChild(td); |
59 | | - insertElt.after(row); |
60 | | - } else { |
61 | | - insertElt = knowl.closest('li'); |
62 | | - if (insertElt) { |
63 | | - const newDiv = document.createElement('div'); |
64 | | - newDiv.append(knowl.knowlContainer); |
65 | | - insertElt.append(newDiv); |
66 | | - } else { |
67 | | - let append = false; |
68 | | - insertElt = knowl; |
69 | | - do { |
70 | | - const lastElt = insertElt; |
71 | | - insertElt = lastElt.nextElementSibling; |
72 | | - if (!insertElt) { |
73 | | - insertElt = lastElt.parentNode; |
74 | | - append = true; |
75 | | - } |
76 | | - } while (getComputedStyle(insertElt)?.getPropertyValue('display') !== 'block'); |
| 63 | + // Insert the knowl modal into the end of the closest div ancestor. |
| 64 | + // If no such element is found, there is not much else that can be done, so just bail. |
| 65 | + const insertElt = knowl.closest('div'); |
| 66 | + if (insertElt) insertElt.append(knowl.knowlModal); |
| 67 | + else return; |
77 | 68 |
|
78 | | - if (append) insertElt.append(knowl.knowlContainer); |
79 | | - else insertElt.before(knowl.knowlContainer); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - knowl.dataset.bsTarget = `#${knowl.knowlContainer.id}`; |
| 69 | + knowl.dataset.bsTarget = `#${knowl.knowlModal.id}`; |
84 | 70 |
|
85 | 71 | if (knowl.dataset.knowlContents) { |
86 | 72 | // Inline html |
87 | | - if (knowl.dataset.base64 == '1') { |
88 | | - if (window.Base64) setInnerHTML(knowlContent, Base64.decode(knowl.dataset.knowlContents)); |
89 | | - else { |
90 | | - setInnerHTML(knowlContent, 'ERROR: Base64 decoding not available'); |
91 | | - knowlContent.classList.add('knowl-error'); |
92 | | - } |
93 | | - } else { |
94 | | - setInnerHTML(knowlContent, knowl.dataset.knowlContents); |
95 | | - } |
| 73 | + setInnerHTML(knowlBody, knowl.dataset.knowlContents); |
| 74 | + |
96 | 75 | // If we are using MathJax, then render math content. |
97 | 76 | if (window.MathJax) { |
98 | | - MathJax.startup.promise = MathJax.startup.promise.then(() => |
99 | | - MathJax.typesetPromise([knowlContent]) |
100 | | - ); |
| 77 | + MathJax.startup.promise = MathJax.startup.promise.then(() => MathJax.typesetPromise([knowlBody])); |
101 | 78 | } |
102 | 79 | } else if (knowl.dataset.knowlUrl) { |
103 | 80 | // Retrieve url content. |
104 | 81 | fetch(knowl.dataset.knowlUrl) |
105 | 82 | .then((response) => (response.ok ? response.text() : response)) |
106 | 83 | .then((data) => { |
107 | 84 | if (typeof data == 'object') { |
108 | | - knowlContent.textContent = `ERROR: ${data.status} ${data.statusText}`; |
109 | | - knowlContent.classList.add('knowl-error'); |
| 85 | + knowlBody.textContent = `ERROR: ${data.status} ${data.statusText}`; |
| 86 | + knowlBody.classList.add('knowl-error'); |
110 | 87 | } else { |
111 | | - setInnerHTML(knowlContent, data); |
| 88 | + setInnerHTML(knowlBody, data); |
112 | 89 | } |
113 | 90 | // If we are using MathJax, then render math content. |
114 | 91 | if (window.MathJax) { |
115 | 92 | MathJax.startup.promise = MathJax.startup.promise.then(() => |
116 | | - MathJax.typesetPromise([knowlContent]) |
| 93 | + MathJax.typesetPromise([knowlBody]) |
117 | 94 | ); |
118 | 95 | } |
| 96 | + }) |
| 97 | + .catch((err) => { |
| 98 | + knowlBody.textContent = `ERROR: ${err}`; |
| 99 | + knowlBody.classList.add('knowl-error'); |
119 | 100 | }); |
120 | 101 | } else { |
121 | | - knowlContent.textContent = 'ERROR: knowl content not provided.'; |
122 | | - knowlContent.classList.add('knowl-error'); |
| 102 | + knowlBody.textContent = 'ERROR: knowl content not provided.'; |
| 103 | + knowlBody.classList.add('knowl-error'); |
123 | 104 | } |
124 | 105 | } |
125 | 106 | }; |
|
0 commit comments