marshaller.js 13 KB

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