marshaller.js 14 KB

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