marshaller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 showSection (pathName) {
  95. const path = window.registeredPaths.get(pathName)
  96. if (path === undefined) {
  97. console.warn('Section not found by path: ' + pathName)
  98. showSection('/')
  99. return
  100. }
  101. window.convertPathToBreadcrumb = convertPathToBreadcrumb
  102. window.currentPath = pathName
  103. window.breadcrumbNavigation = convertPathToBreadcrumb(pathName)
  104. for (const section of document.querySelectorAll('section')) {
  105. if (section.title === path.section) {
  106. section.style.display = 'block'
  107. } else {
  108. section.style.display = 'none'
  109. }
  110. }
  111. pushNewNavigationPath(pathName)
  112. setSectionNavigationVisible(false)
  113. showSectionView(path.view)
  114. }
  115. function pushNewNavigationPath (pathName) {
  116. window.history.pushState({
  117. path: pathName
  118. }, null, pathName)
  119. }
  120. function setSectionNavigationVisible (visible) {
  121. const nav = document.querySelector('nav')
  122. const btn = document.getElementById('sidebar-toggler-button')
  123. if (document.body.classList.contains('has-sidebar')) {
  124. if (visible) {
  125. btn.setAttribute('aria-pressed', false)
  126. btn.setAttribute('aria-label', 'Open sidebar navigation')
  127. btn.innerHTML = '&laquo;'
  128. nav.classList.add('shown')
  129. nav.style.display = 'flex'
  130. } else {
  131. btn.setAttribute('aria-pressed', true)
  132. btn.setAttribute('aria-label', 'Close sidebar navigation')
  133. btn.innerHTML = '&#9776;'
  134. nav.classList.remove('shown')
  135. setTimeout(() => {
  136. nav.style.display = 'none'
  137. }, 600)
  138. }
  139. } else {
  140. btn.disabled = true
  141. }
  142. }
  143. export function setupSectionNavigation (style) {
  144. const nav = document.querySelector('nav')
  145. const btn = document.getElementById('sidebar-toggler-button')
  146. if (style === 'sidebar') {
  147. nav.classList.add('sidebar')
  148. document.body.classList.add('has-sidebar')
  149. btn.onclick = () => {
  150. if (nav.classList.contains('shown')) {
  151. setSectionNavigationVisible(false)
  152. } else {
  153. setSectionNavigationVisible(true)
  154. }
  155. }
  156. } else {
  157. nav.classList.add('topbar')
  158. document.body.classList.add('has-topbar')
  159. }
  160. registerSection('/', 'Actions', null, document.getElementById('showActions'))
  161. registerSection('/diagnostics', 'Diagnostics', null, document.getElementById('showDiagnostics'))
  162. registerSection('/logs', 'Logs', null, document.getElementById('showLogs'))
  163. }
  164. function registerSection (path, section, view, linkElement) {
  165. window.registeredPaths.set(path, {
  166. section: section,
  167. view: view
  168. })
  169. if (linkElement != null) {
  170. addLinkToSection(path, linkElement)
  171. }
  172. }
  173. function addLinkToSection (pathName, element) {
  174. const path = window.registeredPaths.get(pathName)
  175. element.href = 'javascript:void(0)'
  176. element.title = path.section
  177. element.onclick = () => {
  178. showSection(pathName)
  179. }
  180. }
  181. export function refreshDiagnostics () {
  182. document.getElementById('diagnostics-sshfoundkey').innerHTML = window.settings.SshFoundKey
  183. document.getElementById('diagnostics-sshfoundconfig').innerHTML = window.settings.SshFoundConfig
  184. }
  185. function getSystemTitle (title) {
  186. return title.replace(' ', '')
  187. }
  188. function marshalSingleDashboard (dashboard, nav) {
  189. const oldsection = document.querySelector('section[title="' + getSystemTitle(dashboard.title) + '"]')
  190. if (oldsection != null) {
  191. oldsection.remove()
  192. }
  193. const section = document.createElement('section')
  194. section.setAttribute('system-title', getSystemTitle(dashboard.title))
  195. section.title = section.getAttribute('system-title')
  196. const def = createFieldset('default', section)
  197. section.appendChild(def)
  198. document.getElementsByTagName('main')[0].appendChild(section)
  199. marshalContainerContents(dashboard, section, def, dashboard.title)
  200. const oldLi = nav.querySelector('li[title="' + dashboard.title + '"]')
  201. if (oldLi != null) {
  202. oldLi.remove()
  203. }
  204. const navigationA = document.createElement('a')
  205. navigationA.title = dashboard.title
  206. navigationA.innerText = dashboard.title
  207. registerSection('/' + getSystemTitle(section.title), section.title, null, navigationA)
  208. const navigationLi = document.createElement('li')
  209. navigationLi.appendChild(navigationA)
  210. navigationLi.title = dashboard.title
  211. document.getElementById('navigation-links').appendChild(navigationLi)
  212. }
  213. function marshalDashboardStructureToHtml (json) {
  214. const nav = document.getElementById('navigation-links')
  215. for (const dashboard of json.dashboards) {
  216. marshalSingleDashboard(dashboard, nav)
  217. }
  218. const rootGroup = document.querySelector('#root-group')
  219. for (const btn of Object.values(window.actionButtons)) {
  220. if (btn.parentElement === null) {
  221. rootGroup.appendChild(btn)
  222. }
  223. }
  224. if (window.currentPath !== '') {
  225. showSection(window.currentPath)
  226. } else if (window.location.pathname !== '/' && document.body.getAttribute('initial-marshal-complete') === null) {
  227. showSection(window.location.pathname)
  228. } else {
  229. if (rootGroup.querySelectorAll('action-button').length === 0 && json.dashboards.length > 0) {
  230. nav.querySelector('li[title="Actions"]').style.display = 'none'
  231. showSection('/' + getSystemTitle(json.dashboards[0].title))
  232. } else {
  233. showSection('/')
  234. }
  235. }
  236. }
  237. function marshalLink (item, fieldset) {
  238. let btn = window.actionButtons[item.title]
  239. if (typeof btn === 'undefined') {
  240. btn = document.createElement('button')
  241. btn.innerText = 'Action not found: ' + item.title
  242. btn.classList.add('error')
  243. }
  244. fieldset.appendChild(btn)
  245. }
  246. function marshalMreOutput (dashboardComponent, fieldset) {
  247. const pre = document.createElement('pre')
  248. pre.classList.add('mre-output')
  249. pre.innerHTML = 'Waiting...'
  250. const executionStatus = {
  251. actionId: dashboardComponent.title
  252. }
  253. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  254. method: 'POST',
  255. headers: {
  256. 'Content-Type': 'application/json'
  257. },
  258. body: JSON.stringify(executionStatus)
  259. }).then((res) => {
  260. if (res.ok) {
  261. return res.json()
  262. } else {
  263. pre.innerHTML = 'error'
  264. throw new Error(res.statusText)
  265. }
  266. }).then((json) => {
  267. updateMre(pre, json.logEntry)
  268. })
  269. const updateMre = (pre, json) => {
  270. pre.innerHTML = json.stdout
  271. }
  272. window.addEventListener('ExecutionFinished', (e) => {
  273. // The dashboard component "title" field is used for lots of things
  274. // and in this context for MreOutput it's just to refer an an actionId.
  275. //
  276. // So this is not a typo.
  277. if (e.payload.actionId === dashboardComponent.title) {
  278. updateMre(pre, e.payload)
  279. }
  280. })
  281. fieldset.appendChild(pre)
  282. }
  283. function marshalContainerContents (json, section, fieldset, parentDashboard) {
  284. for (const item of json.contents) {
  285. switch (item.type) {
  286. case 'fieldset':
  287. marshalFieldset(item, section, parentDashboard)
  288. break
  289. case 'directory': {
  290. const directoryPath = marshalDirectory(item, section)
  291. marshalDirectoryButton(item, fieldset, directoryPath)
  292. }
  293. break
  294. case 'display':
  295. marshalDisplay(item, fieldset)
  296. break
  297. case 'stdout-most-recent-execution':
  298. marshalMreOutput(item, fieldset)
  299. break
  300. case 'link':
  301. marshalLink(item, fieldset)
  302. break
  303. default:
  304. }
  305. }
  306. }
  307. function createFieldset (title, parentDashboard) {
  308. const legend = document.createElement('legend')
  309. legend.innerText = title
  310. const fs = document.createElement('fieldset')
  311. fs.title = title
  312. fs.appendChild(legend)
  313. if (typeof parentDashboard === 'undefined') {
  314. fs.setAttribute('parent-dashboard', '')
  315. } else {
  316. fs.setAttribute('parent-dashboard', parentDashboard)
  317. }
  318. return fs
  319. }
  320. function marshalFieldset (item, section, parentDashboard) {
  321. const fs = createFieldset(item.title, parentDashboard)
  322. marshalContainerContents(item, section, fs, parentDashboard)
  323. section.appendChild(fs)
  324. }
  325. function showSectionView (selected) {
  326. if (selected === '') {
  327. selected = null
  328. }
  329. for (const fieldset of document.querySelectorAll('fieldset')) {
  330. if (selected === null) {
  331. if ((fieldset.id === 'root-group' || fieldset.getAttribute('parent-dashboard') !== '') && fieldset.children.length > 1) {
  332. fieldset.style.display = 'grid'
  333. } else {
  334. fieldset.style.display = 'none'
  335. }
  336. } else {
  337. if (fieldset.title === selected) {
  338. fieldset.style.display = 'grid'
  339. } else {
  340. fieldset.style.display = 'none'
  341. }
  342. }
  343. }
  344. rebuildH1BreadcrumbNavigation(selected)
  345. pushNewNavigationPath(window.currentPath)
  346. }
  347. function rebuildH1BreadcrumbNavigation () {
  348. const title = document.querySelector('h1')
  349. title.innerHTML = ''
  350. const rootLink = document.createElement('a')
  351. rootLink.innerText = window.pageTitle
  352. rootLink.href = 'javascript:void(0)'
  353. rootLink.onclick = () => {
  354. showSection('/')
  355. }
  356. title.appendChild(rootLink)
  357. for (const pathName of window.breadcrumbNavigation) {
  358. const sep = document.createElement('span')
  359. sep.innerHTML = ' &raquo; '
  360. title.append(sep)
  361. const path = window.registeredPaths.get(pathName)
  362. title.appendChild(createNavigationBreadcrumbDisplay(path))
  363. }
  364. document.title = title.innerText
  365. }
  366. function createNavigationBreadcrumbDisplay (path) {
  367. const a = document.createElement('a')
  368. a.href = 'javascript:void(0)'
  369. if (path.view === null) {
  370. a.title = path.section
  371. a.innerText = path.section
  372. } else {
  373. a.innerText = path.view
  374. a.title = path.view
  375. }
  376. a.onclick = () => {
  377. showSectionView(path.view)
  378. }
  379. return a
  380. }
  381. function marshalDisplay (item, fieldset) {
  382. const display = document.createElement('div')
  383. display.innerHTML = item.title
  384. display.classList.add('display')
  385. if (item.cssClass !== '') {
  386. display.classList.add(item.cssClass)
  387. }
  388. fieldset.appendChild(display)
  389. }
  390. function marshalDirectoryButton (item, fieldset, path) {
  391. const directoryButton = document.createElement('button')
  392. directoryButton.innerHTML = '<span class = "icon">' + item.icon + '</span> ' + item.title
  393. directoryButton.onclick = () => {
  394. showSection(path)
  395. }
  396. fieldset.appendChild(directoryButton)
  397. }
  398. function marshalDirectory (item, section) {
  399. const fs = createFieldset(item.title)
  400. fs.style.display = 'none'
  401. const directoryBackButton = document.createElement('button')
  402. directoryBackButton.innerHTML = window.settings.DefaultIconForBack
  403. directoryBackButton.title = 'Go back one directory'
  404. directoryBackButton.onclick = () => {
  405. showSection('/' + section.title)
  406. }
  407. fs.appendChild(directoryBackButton)
  408. marshalContainerContents(item, section, fs)
  409. section.appendChild(fs)
  410. const path = '/' + section.title + '/' + getSystemTitle(item.title)
  411. registerSection(path, section.title, item.title, null)
  412. return path
  413. }
  414. export function marshalLogsJsonToHtml (json) {
  415. for (const logEntry of json.logs) {
  416. const existing = window.logEntries[logEntry.executionTrackingId]
  417. if (existing !== undefined) {
  418. continue
  419. }
  420. window.logEntries[logEntry.executionTrackingId] = logEntry
  421. const tpl = document.getElementById('tplLogRow')
  422. const row = tpl.content.querySelector('tr').cloneNode(true)
  423. row.querySelector('.timestamp').innerText = logEntry.datetimeStarted
  424. row.querySelector('.content').innerText = logEntry.actionTitle
  425. row.querySelector('.icon').innerHTML = logEntry.actionIcon
  426. row.setAttribute('title', logEntry.actionTitle)
  427. const exitCodeDisplay = new ActionStatusDisplay(row.querySelector('.exit-code'))
  428. exitCodeDisplay.update(logEntry)
  429. row.querySelector('.content').onclick = () => {
  430. window.executionDialog.reset()
  431. window.executionDialog.show()
  432. window.executionDialog.renderExecutionResult({
  433. logEntry: window.logEntries[logEntry.executionTrackingId]
  434. })
  435. }
  436. for (const tag of logEntry.tags) {
  437. const domTag = document.createElement('span')
  438. domTag.classList.add('tag')
  439. domTag.innerText = tag
  440. row.querySelector('.tags').append(domTag)
  441. }
  442. document.querySelector('#logTableBody').prepend(row)
  443. }
  444. }
  445. window.addEventListener('popstate', (e) => {
  446. e.preventDefault()
  447. if (e.state != null && typeof e.state.path !== 'undefined') {
  448. showSection(e.state.path)
  449. }
  450. })
  451. export function refreshServerConnectionLabel () {
  452. if (window.restAvailable) {
  453. document.querySelector('#serverConnectionRest').classList.remove('error')
  454. } else {
  455. document.querySelector('#serverConnectionRest').classList.add('error')
  456. }
  457. if (window.websocketAvailable) {
  458. document.querySelector('#serverConnectionWebSocket').classList.remove('error')
  459. document.querySelector('#serverConnectionWebSocket').innerText = 'WebSocket'
  460. } else {
  461. document.querySelector('#serverConnectionWebSocket').classList.add('error')
  462. document.querySelector('#serverConnectionWebSocket').innerText = 'WebSocket Error'
  463. }
  464. }