marshaller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. import './ActionButton.js' // To define action-button
  2. import { ExecutionDialog } from './ExecutionDialog.js'
  3. import { ActionStatusDisplay } from './ActionStatusDisplay.js'
  4. /**
  5. * This is a weird function that just sets some globals.
  6. */
  7. export function initMarshaller () {
  8. window.showSection = showSection
  9. window.showSectionView = showSectionView
  10. window.executionDialog = new ExecutionDialog()
  11. window.logEntries = {}
  12. window.registeredPaths = new Map()
  13. window.breadcrumbNavigation = []
  14. window.currentPath = ''
  15. window.addEventListener('EventExecutionFinished', onExecutionFinished)
  16. window.addEventListener('EventOutputChunk', onOutputChunk)
  17. }
  18. export function marshalDashboardComponentsJsonToHtml (json) {
  19. marshalActionsJsonToHtml(json)
  20. marshalDashboardStructureToHtml(json)
  21. document.getElementById('username').innerText = json.authenticatedUser
  22. document.body.setAttribute('initial-marshal-complete', 'true')
  23. }
  24. function marshalActionsJsonToHtml (json) {
  25. const currentIterationTimestamp = Date.now()
  26. window.actionButtons = {}
  27. for (const jsonButton of json.actions) {
  28. let htmlButton = window.actionButtons[jsonButton.id]
  29. if (typeof htmlButton === 'undefined') {
  30. htmlButton = document.createElement('action-button')
  31. htmlButton.constructFromJson(jsonButton)
  32. window.actionButtons[jsonButton.title] = htmlButton
  33. }
  34. htmlButton.updateFromJson(jsonButton)
  35. htmlButton.updateIterationTimestamp = currentIterationTimestamp
  36. }
  37. // Remove existing, but stale buttons (that were not updated in this round)
  38. for (const existingButton of document.querySelectorAll('action-button')) {
  39. if (existingButton.updateIterationTimestamp !== currentIterationTimestamp) {
  40. existingButton.remove()
  41. }
  42. }
  43. }
  44. function onOutputChunk (evt) {
  45. const chunk = evt.payload
  46. if (chunk.executionTrackingId === window.executionDialog.executionTrackingId) {
  47. window.terminal.write(chunk.output)
  48. window.executionDialog.showOutput()
  49. }
  50. }
  51. function onExecutionFinished (evt) {
  52. const logEntry = evt.payload.logEntry
  53. const actionButton = window.actionButtons[logEntry.actionTitle]
  54. if (actionButton === undefined) {
  55. return
  56. }
  57. switch (actionButton.popupOnStart) {
  58. case 'execution-button':
  59. document.querySelector('execution-button#execution-' + logEntry.executionTrackingId).onExecutionFinished(logEntry)
  60. break
  61. case 'execution-dialog-stdout-only':
  62. case 'execution-dialog':
  63. actionButton.onExecutionFinished(logEntry)
  64. // We don't need to fetch the logEntry for the dialog because we already
  65. // have it, so we open the dialog and it will get updated below.
  66. window.executionDialog.show()
  67. window.executionDialog.executionTrackingId = logEntry.uuid
  68. break
  69. default:
  70. actionButton.onExecutionFinished(logEntry)
  71. break
  72. }
  73. marshalLogsJsonToHtml({
  74. logs: [logEntry]
  75. })
  76. // If the current execution dialog is open, update that too
  77. if (window.executionDialog.dlg.open && window.executionDialog.executionUuid === logEntry.uuid) {
  78. window.executionDialog.renderExecutionResult({
  79. logEntry: logEntry
  80. })
  81. }
  82. }
  83. function convertPathToBreadcrumb (path) {
  84. const parts = path.split('/')
  85. const result = []
  86. for (let i = 0; i < parts.length; i++) {
  87. if (parts[i] === '') {
  88. continue
  89. }
  90. result.push(parts.slice(0, i + 1).join('/'))
  91. }
  92. return result
  93. }
  94. function showExecutionResult (pathName) {
  95. const executionTrackingId = pathName.split('/')[2]
  96. window.executionDialog.fetchExecutionResult(executionTrackingId)
  97. window.executionDialog.show()
  98. }
  99. function showSection (pathName) {
  100. if (pathName.startsWith('/logs/')) {
  101. showExecutionResult(pathName)
  102. pushNewNavigationPath(pathName)
  103. return
  104. }
  105. const path = window.registeredPaths.get(pathName)
  106. if (path === undefined) {
  107. console.warn('Section not found by path: ' + pathName)
  108. showSection('/')
  109. return
  110. }
  111. window.convertPathToBreadcrumb = convertPathToBreadcrumb
  112. window.currentPath = pathName
  113. window.breadcrumbNavigation = convertPathToBreadcrumb(pathName)
  114. for (const section of document.querySelectorAll('section')) {
  115. if (section.title === path.section) {
  116. section.style.display = 'block'
  117. } else {
  118. section.style.display = 'none'
  119. }
  120. }
  121. pushNewNavigationPath(pathName)
  122. setSectionNavigationVisible(false)
  123. showSectionView(path.view)
  124. }
  125. function pushNewNavigationPath (pathName) {
  126. window.history.pushState({
  127. path: pathName
  128. }, null, pathName)
  129. }
  130. function setSectionNavigationVisible (visible) {
  131. const nav = document.querySelector('nav')
  132. const btn = document.getElementById('sidebar-toggler-button')
  133. nav.removeAttribute('hidden')
  134. if (document.body.classList.contains('has-sidebar')) {
  135. if (visible) {
  136. btn.setAttribute('aria-pressed', false)
  137. btn.setAttribute('aria-label', 'Open sidebar navigation')
  138. btn.innerHTML = '&laquo;'
  139. nav.classList.add('shown')
  140. } else {
  141. btn.setAttribute('aria-pressed', true)
  142. btn.setAttribute('aria-label', 'Close sidebar navigation')
  143. btn.innerHTML = '&#9776;'
  144. nav.classList.remove('shown')
  145. }
  146. } else {
  147. btn.disabled = true
  148. }
  149. }
  150. export function setupSectionNavigation (style) {
  151. const nav = document.querySelector('nav')
  152. const btn = document.getElementById('sidebar-toggler-button')
  153. if (style === 'sidebar') {
  154. nav.classList.add('sidebar')
  155. document.body.classList.add('has-sidebar')
  156. btn.onclick = () => {
  157. if (nav.classList.contains('shown')) {
  158. setSectionNavigationVisible(false)
  159. } else {
  160. setSectionNavigationVisible(true)
  161. }
  162. }
  163. } else {
  164. nav.classList.add('topbar')
  165. document.body.classList.add('has-topbar')
  166. }
  167. registerSection('/', 'Actions', null, document.getElementById('showActions'))
  168. registerSection('/diagnostics', 'Diagnostics', null, document.getElementById('showDiagnostics'))
  169. registerSection('/logs', 'Logs', null, document.getElementById('showLogs'))
  170. }
  171. function registerSection (path, section, view, linkElement) {
  172. window.registeredPaths.set(path, {
  173. section: section,
  174. view: view
  175. })
  176. if (linkElement != null) {
  177. addLinkToSection(path, linkElement)
  178. }
  179. }
  180. function addLinkToSection (pathName, element) {
  181. const path = window.registeredPaths.get(pathName)
  182. element.href = 'javascript:void(0)'
  183. element.title = path.section
  184. element.onclick = () => {
  185. showSection(pathName)
  186. }
  187. }
  188. export function refreshDiagnostics () {
  189. document.getElementById('diagnostics-sshfoundkey').innerHTML = window.settings.SshFoundKey
  190. document.getElementById('diagnostics-sshfoundconfig').innerHTML = window.settings.SshFoundConfig
  191. }
  192. function getSystemTitle (title) {
  193. return title.replaceAll(' ', '')
  194. }
  195. function marshalSingleDashboard (dashboard, nav) {
  196. const oldsection = document.querySelector('section[title="' + getSystemTitle(dashboard.title) + '"]')
  197. if (oldsection != null) {
  198. oldsection.remove()
  199. }
  200. const section = document.createElement('section')
  201. section.setAttribute('system-title', getSystemTitle(dashboard.title))
  202. section.title = section.getAttribute('system-title')
  203. const def = createFieldset('default', section)
  204. section.appendChild(def)
  205. document.getElementsByTagName('main')[0].appendChild(section)
  206. marshalContainerContents(dashboard, section, def, dashboard.title)
  207. const oldLi = nav.querySelector('li[title="' + dashboard.title + '"]')
  208. if (oldLi != null) {
  209. oldLi.remove()
  210. }
  211. const navigationA = document.createElement('a')
  212. navigationA.title = dashboard.title
  213. navigationA.innerText = dashboard.title
  214. registerSection('/' + getSystemTitle(section.title), section.title, null, navigationA)
  215. const navigationLi = document.createElement('li')
  216. navigationLi.appendChild(navigationA)
  217. navigationLi.title = dashboard.title
  218. document.getElementById('navigation-links').appendChild(navigationLi)
  219. }
  220. function marshalDashboardStructureToHtml (json) {
  221. const nav = document.getElementById('navigation-links')
  222. for (const dashboard of json.dashboards) {
  223. marshalSingleDashboard(dashboard, nav)
  224. }
  225. const rootGroup = document.querySelector('#root-group')
  226. for (const btn of Object.values(window.actionButtons)) {
  227. if (btn.parentElement === null) {
  228. rootGroup.appendChild(btn)
  229. }
  230. }
  231. if (window.currentPath !== '') {
  232. showSection(window.currentPath)
  233. } else if (window.location.pathname !== '/' && document.body.getAttribute('initial-marshal-complete') === null) {
  234. showSection(window.location.pathname)
  235. } else {
  236. if (rootGroup.querySelectorAll('action-button').length === 0 && json.dashboards.length > 0) {
  237. nav.querySelector('li[title="Actions"]').style.display = 'none'
  238. showSection('/' + getSystemTitle(json.dashboards[0].title))
  239. } else {
  240. showSection('/')
  241. }
  242. }
  243. }
  244. function marshalLink (item, fieldset) {
  245. let btn = window.actionButtons[item.title]
  246. if (typeof btn === 'undefined') {
  247. btn = document.createElement('button')
  248. btn.innerText = 'Action not found: ' + item.title
  249. btn.classList.add('error')
  250. }
  251. if (item.cssClass !== '') {
  252. btn.classList.add(item.cssClass)
  253. }
  254. fieldset.appendChild(btn)
  255. }
  256. function marshalMreOutput (dashboardComponent, fieldset) {
  257. const pre = document.createElement('pre')
  258. pre.classList.add('mre-output')
  259. pre.innerHTML = 'Waiting...'
  260. const executionStatus = {
  261. actionId: dashboardComponent.title
  262. }
  263. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  264. method: 'POST',
  265. headers: {
  266. 'Content-Type': 'application/json'
  267. },
  268. body: JSON.stringify(executionStatus)
  269. }).then((res) => {
  270. if (res.ok) {
  271. return res.json()
  272. } else {
  273. pre.innerHTML = 'error'
  274. throw new Error(res.statusText)
  275. }
  276. }).then((json) => {
  277. updateMre(pre, json.logEntry)
  278. })
  279. const updateMre = (pre, json) => {
  280. pre.innerHTML = json.stdout
  281. }
  282. window.addEventListener('ExecutionFinished', (e) => {
  283. // The dashboard component "title" field is used for lots of things
  284. // and in this context for MreOutput it's just to refer an an actionId.
  285. //
  286. // So this is not a typo.
  287. if (e.payload.actionId === dashboardComponent.title) {
  288. updateMre(pre, e.payload)
  289. }
  290. })
  291. fieldset.appendChild(pre)
  292. }
  293. function marshalContainerContents (json, section, fieldset, parentDashboard) {
  294. for (const item of json.contents) {
  295. switch (item.type) {
  296. case 'fieldset':
  297. marshalFieldset(item, section, parentDashboard)
  298. break
  299. case 'directory': {
  300. const directoryPath = marshalDirectory(item, section)
  301. marshalDirectoryButton(item, fieldset, directoryPath)
  302. }
  303. break
  304. case 'display':
  305. marshalDisplay(item, fieldset)
  306. break
  307. case 'stdout-most-recent-execution':
  308. marshalMreOutput(item, fieldset)
  309. break
  310. case 'link':
  311. marshalLink(item, fieldset)
  312. break
  313. default:
  314. }
  315. }
  316. }
  317. function createFieldset (title, parentDashboard) {
  318. const legend = document.createElement('legend')
  319. legend.innerText = title
  320. const fs = document.createElement('fieldset')
  321. fs.title = title
  322. fs.appendChild(legend)
  323. if (typeof parentDashboard === 'undefined') {
  324. fs.setAttribute('parent-dashboard', '')
  325. } else {
  326. fs.setAttribute('parent-dashboard', parentDashboard)
  327. }
  328. return fs
  329. }
  330. function marshalFieldset (item, section, parentDashboard) {
  331. const fs = createFieldset(item.title, parentDashboard)
  332. marshalContainerContents(item, section, fs, parentDashboard)
  333. section.appendChild(fs)
  334. }
  335. function showSectionView (selected) {
  336. if (selected === '') {
  337. selected = null
  338. }
  339. for (const fieldset of document.querySelectorAll('fieldset')) {
  340. if (selected === null) {
  341. if ((fieldset.id === 'root-group' || fieldset.getAttribute('parent-dashboard') !== '') && fieldset.children.length > 1) {
  342. fieldset.style.display = 'grid'
  343. } else {
  344. fieldset.style.display = 'none'
  345. }
  346. } else {
  347. if (fieldset.title === selected) {
  348. fieldset.style.display = 'grid'
  349. } else {
  350. fieldset.style.display = 'none'
  351. }
  352. }
  353. }
  354. rebuildH1BreadcrumbNavigation(selected)
  355. pushNewNavigationPath(window.currentPath)
  356. }
  357. function rebuildH1BreadcrumbNavigation () {
  358. const title = document.querySelector('h1')
  359. title.innerHTML = ''
  360. const rootLink = document.createElement('a')
  361. rootLink.innerText = window.pageTitle
  362. rootLink.href = 'javascript:void(0)'
  363. rootLink.onclick = () => {
  364. showSection('/')
  365. }
  366. title.appendChild(rootLink)
  367. for (const pathName of window.breadcrumbNavigation) {
  368. const sep = document.createElement('span')
  369. sep.innerHTML = ' &raquo; '
  370. title.append(sep)
  371. const path = window.registeredPaths.get(pathName)
  372. title.appendChild(createNavigationBreadcrumbDisplay(path))
  373. }
  374. document.title = title.innerText
  375. }
  376. function createNavigationBreadcrumbDisplay (path) {
  377. const a = document.createElement('a')
  378. a.href = 'javascript:void(0)'
  379. if (path.view === null) {
  380. a.title = path.section
  381. a.innerText = path.section
  382. } else {
  383. a.innerText = path.view
  384. a.title = path.view
  385. }
  386. a.onclick = () => {
  387. showSectionView(path.view)
  388. }
  389. return a
  390. }
  391. function marshalDisplay (item, fieldset) {
  392. const display = document.createElement('div')
  393. display.innerHTML = item.title
  394. display.classList.add('display')
  395. if (item.cssClass !== '') {
  396. display.classList.add(item.cssClass)
  397. }
  398. fieldset.appendChild(display)
  399. }
  400. function marshalDirectoryButton (item, fieldset, path) {
  401. const directoryButton = document.createElement('button')
  402. directoryButton.innerHTML = '<span class = "icon">' + item.icon + '</span> ' + item.title
  403. directoryButton.onclick = () => {
  404. showSection(path)
  405. }
  406. fieldset.appendChild(directoryButton)
  407. }
  408. function marshalDirectory (item, section) {
  409. const fs = createFieldset(item.title)
  410. fs.style.display = 'none'
  411. const directoryBackButton = document.createElement('button')
  412. directoryBackButton.innerHTML = window.settings.DefaultIconForBack
  413. directoryBackButton.title = 'Go back one directory'
  414. directoryBackButton.onclick = () => {
  415. showSection('/' + section.title)
  416. }
  417. fs.appendChild(directoryBackButton)
  418. marshalContainerContents(item, section, fs)
  419. section.appendChild(fs)
  420. const path = '/' + section.title + '/' + getSystemTitle(item.title)
  421. registerSection(path, section.title, item.title, null)
  422. return path
  423. }
  424. export function marshalLogsJsonToHtml (json) {
  425. for (const logEntry of json.logs) {
  426. const existing = window.logEntries[logEntry.executionTrackingId]
  427. if (existing !== undefined) {
  428. continue
  429. }
  430. window.logEntries[logEntry.executionTrackingId] = logEntry
  431. const tpl = document.getElementById('tplLogRow')
  432. const row = tpl.content.querySelector('tr').cloneNode(true)
  433. row.querySelector('.timestamp').innerText = logEntry.datetimeStarted
  434. row.querySelector('.content').innerText = logEntry.actionTitle
  435. row.querySelector('.icon').innerHTML = logEntry.actionIcon
  436. row.setAttribute('title', logEntry.actionTitle)
  437. const exitCodeDisplay = new ActionStatusDisplay(row.querySelector('.exit-code'))
  438. exitCodeDisplay.update(logEntry)
  439. row.querySelector('.content').onclick = () => {
  440. window.executionDialog.reset()
  441. window.executionDialog.show()
  442. window.executionDialog.renderExecutionResult({
  443. logEntry: window.logEntries[logEntry.executionTrackingId]
  444. })
  445. pushNewNavigationPath('/logs/' + logEntry.executionTrackingId)
  446. }
  447. for (const tag of logEntry.tags) {
  448. const domTag = document.createElement('span')
  449. domTag.classList.add('tag')
  450. domTag.innerText = tag
  451. row.querySelector('.tags').append(domTag)
  452. }
  453. document.querySelector('#logTableBody').prepend(row)
  454. }
  455. }
  456. window.addEventListener('popstate', (e) => {
  457. e.preventDefault()
  458. if (e.state != null && typeof e.state.path !== 'undefined') {
  459. showSection(e.state.path)
  460. }
  461. })
  462. export function refreshServerConnectionLabel () {
  463. if (window.restAvailable) {
  464. document.querySelector('#serverConnectionRest').classList.remove('error')
  465. } else {
  466. document.querySelector('#serverConnectionRest').classList.add('error')
  467. }
  468. if (window.websocketAvailable) {
  469. document.querySelector('#serverConnectionWebSocket').classList.remove('error')
  470. document.querySelector('#serverConnectionWebSocket').innerText = 'WebSocket'
  471. } else {
  472. document.querySelector('#serverConnectionWebSocket').classList.add('error')
  473. document.querySelector('#serverConnectionWebSocket').innerText = 'WebSocket Error'
  474. }
  475. }