marshaller.js 14 KB

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