marshaller.js 16 KB

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