marshaller.js 15 KB

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