marshaller.js 16 KB

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