marshaller.js 14 KB

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