marshaller.js 17 KB

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