marshaller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. for (const otherName of ['Actions', 'Logs']) {
  79. document.getElementById('show' + otherName).classList.remove('activeSection')
  80. document.getElementById('content' + otherName).hidden = true
  81. }
  82. // document.getElementById('show' + name).classList.add('activeSection')
  83. // document.getElementById('content' + name).hidden = false
  84. document.getElementById('hide-sidebar-checkbox').checked = true
  85. changeDirectory(null)
  86. }
  87. export function setupSectionNavigation (style) {
  88. const nav = document.querySelector('nav')
  89. if (style === 'sidebar') {
  90. nav.classList += 'sidebar'
  91. document.body.classList += 'has-sidebar'
  92. } else {
  93. nav.classList += 'topbar'
  94. document.body.classList += 'has-topbar'
  95. }
  96. nav.hidden = false
  97. document.getElementById('showActions').onclick = () => { showSection('Actions') }
  98. document.getElementById('showLogs').onclick = () => { showSection('Logs') }
  99. }
  100. function marshalDashboardStructureToHtml (json) {
  101. const nav = document.getElementById('navigation-links')
  102. for (const dashboard of json.dashboards) {
  103. const oldsection = document.querySelector('section[title="' + dashboard.title + '"]')
  104. if (oldsection != null) {
  105. oldsection.remove()
  106. }
  107. const section = document.createElement('section')
  108. section.title = dashboard.title
  109. const def = createFieldset('default', section)
  110. section.appendChild(def)
  111. document.getElementsByTagName('main')[0].appendChild(section)
  112. marshalContainerContents(dashboard, section, def, dashboard.title)
  113. const oldLi = nav.querySelector('li[title="' + dashboard.title + '"]')
  114. if (oldLi != null) {
  115. oldLi.remove()
  116. }
  117. const navigationA = document.createElement('a')
  118. navigationA.title = dashboard.title
  119. navigationA.innerText = dashboard.title
  120. navigationA.onclick = () => {
  121. showSection(dashboard.title)
  122. }
  123. const navigationLi = document.createElement('li')
  124. navigationLi.appendChild(navigationA)
  125. navigationLi.title = dashboard.title
  126. document.getElementById('navigation-links').appendChild(navigationLi)
  127. }
  128. const rootGroup = document.querySelector('#root-group')
  129. for (const btn of Object.values(window.actionButtons)) {
  130. if (btn.parentElement === null) {
  131. rootGroup.appendChild(btn)
  132. }
  133. }
  134. if (rootGroup.querySelectorAll('action-button').length === 0 && json.dashboards.length > 0) {
  135. nav.querySelector('li[title="Actions"]').style.display = 'none'
  136. showSection(json.dashboards[0].title)
  137. } else {
  138. showSection('Actions')
  139. }
  140. }
  141. function marshalLink (item, fieldset) {
  142. let btn = window.actionButtons[item.title]
  143. if (typeof btn === 'undefined') {
  144. btn = document.createElement('button')
  145. btn.innerText = 'Action not found: ' + item.title
  146. btn.classList.add('error')
  147. }
  148. fieldset.appendChild(btn)
  149. }
  150. function marshalMreOutput (dashboardComponent, fieldset) {
  151. const pre = document.createElement('pre')
  152. pre.classList.add('mre-output')
  153. pre.innerHTML = 'Waiting...'
  154. const executionStatus = {
  155. actionId: dashboardComponent.title
  156. }
  157. window.fetch(window.restBaseUrl + 'ExecutionStatus', {
  158. method: 'POST',
  159. headers: {
  160. 'Content-Type': 'application/json'
  161. },
  162. body: JSON.stringify(executionStatus)
  163. }).then((res) => {
  164. if (res.ok) {
  165. return res.json()
  166. } else {
  167. pre.innerHTML = 'error'
  168. throw new Error(res.statusText)
  169. }
  170. }).then((json) => {
  171. updateMre(pre, json.logEntry)
  172. })
  173. const updateMre = (pre, json) => {
  174. pre.innerHTML = json.stdout
  175. }
  176. window.addEventListener('ExecutionFinished', (e) => {
  177. // The dashboard component "title" field is used for lots of things
  178. // and in this context for MreOutput it's just to refer an an actionId.
  179. //
  180. // So this is not a typo.
  181. if (e.payload.actionId === dashboardComponent.title) {
  182. updateMre(pre, e.payload)
  183. }
  184. })
  185. fieldset.appendChild(pre)
  186. }
  187. function marshalContainerContents (json, section, fieldset, parentDashboard) {
  188. for (const item of json.contents) {
  189. switch (item.type) {
  190. case 'fieldset':
  191. marshalFieldset(item, section, parentDashboard)
  192. break
  193. case 'directory':
  194. marshalDirectoryButton(item, fieldset)
  195. marshalDirectory(item, section)
  196. break
  197. case 'display':
  198. marshalDisplay(item, fieldset)
  199. break
  200. case 'stdout-most-recent-execution':
  201. marshalMreOutput(item, fieldset)
  202. break
  203. case 'link':
  204. marshalLink(item, fieldset)
  205. break
  206. default:
  207. }
  208. }
  209. }
  210. function createFieldset (title, parentDashboard) {
  211. const legend = document.createElement('legend')
  212. legend.innerText = title
  213. const fs = document.createElement('fieldset')
  214. fs.title = title
  215. fs.appendChild(legend)
  216. if (typeof parentDashboard === 'undefined') {
  217. fs.setAttribute('parent-dashboard', '')
  218. } else {
  219. fs.setAttribute('parent-dashboard', parentDashboard)
  220. }
  221. return fs
  222. }
  223. function marshalFieldset (item, section, parentDashboard) {
  224. const fs = createFieldset(item.title, parentDashboard)
  225. marshalContainerContents(item, section, fs)
  226. section.appendChild(fs)
  227. }
  228. function changeDirectory (selected) {
  229. if (selected === '') {
  230. selected = null
  231. }
  232. if (selected === null) {
  233. window.directoryNavigation = []
  234. } else if (selected === '..') {
  235. window.directoryNavigation.pop()
  236. if (window.directoryNavigation.length > 0) {
  237. selected = window.directoryNavigation[window.directoryNavigation.length - 1]
  238. } else {
  239. selected = null
  240. }
  241. } else {
  242. // If the selected item is already in the nav list, pop elements until we get
  243. // "back" to the existing nav item
  244. while (window.directoryNavigation.includes(selected)) {
  245. window.directoryNavigation.pop()
  246. }
  247. window.directoryNavigation.push(selected)
  248. }
  249. for (const fieldset of document.querySelectorAll('fieldset')) {
  250. if (selected === null) {
  251. if ((fieldset.id === 'root-group' || fieldset.getAttribute('parent-dashboard') !== '') && fieldset.children.length > 1) {
  252. fieldset.style.display = 'grid'
  253. } else {
  254. fieldset.style.display = 'none'
  255. }
  256. } else {
  257. if (fieldset.title === selected) {
  258. fieldset.style.display = 'grid'
  259. } else {
  260. fieldset.style.display = 'none'
  261. }
  262. }
  263. }
  264. const title = document.querySelector('h1')
  265. title.innerHTML = ''
  266. const rootLink = createDirectoryBreadcrumb(window.pageTitle, null)
  267. title.appendChild(rootLink)
  268. for (const dir of window.directoryNavigation) {
  269. const sep = document.createElement('span')
  270. sep.innerHTML = ' » '
  271. title.append(sep)
  272. if (dir === selected) {
  273. title.append(selected)
  274. } else {
  275. title.appendChild(createDirectoryBreadcrumb(dir))
  276. }
  277. }
  278. document.title = title.innerText
  279. if (selected === null) {
  280. window.location.hash = null
  281. window.history.pushState({ dir: null }, null, '#')
  282. } else {
  283. window.location.hash = selected
  284. window.history.pushState({ dir: selected }, null, '#' + selected)
  285. }
  286. }
  287. function createDirectoryBreadcrumb (title, link) {
  288. const a = document.createElement('a')
  289. a.innerText = title
  290. a.title = title
  291. if (typeof link === 'undefined') {
  292. link = title
  293. }
  294. if (link === null) {
  295. a.href = '#'
  296. } else {
  297. a.href = '#' + link
  298. }
  299. a.onclick = () => {
  300. changeDirectory(link)
  301. }
  302. return a
  303. }
  304. function marshalDisplay (item, fieldset) {
  305. const display = document.createElement('div')
  306. display.innerHTML = item.title
  307. fieldset.appendChild(display)
  308. }
  309. function marshalDirectoryButton (item, fieldset) {
  310. const directoryButton = document.createElement('button')
  311. directoryButton.innerHTML = '<span class = "icon">&#128193;</span> ' + item.title
  312. directoryButton.onclick = () => {
  313. changeDirectory(item.title)
  314. }
  315. fieldset.appendChild(directoryButton)
  316. }
  317. function marshalDirectory (item, section) {
  318. const fs = createFieldset(item.title)
  319. fs.style.display = 'none'
  320. const directoryBackButton = document.createElement('button')
  321. directoryBackButton.innerHTML = '&laquo;'
  322. directoryBackButton.title = 'Go back one directory'
  323. directoryBackButton.onclick = () => {
  324. changeDirectory('..')
  325. }
  326. fs.appendChild(directoryBackButton)
  327. marshalContainerContents(item, section, fs)
  328. section.appendChild(fs)
  329. }
  330. export function marshalLogsJsonToHtml (json) {
  331. for (const logEntry of json.logs) {
  332. const existing = window.logEntries[logEntry.executionTrackingId]
  333. if (existing !== undefined) {
  334. continue
  335. }
  336. window.logEntries[logEntry.executionTrackingId] = logEntry
  337. const tpl = document.getElementById('tplLogRow')
  338. const row = tpl.content.querySelector('tr').cloneNode(true)
  339. if (logEntry.stdout.length === 0) {
  340. logEntry.stdout = '(empty)'
  341. }
  342. if (logEntry.stderr.length === 0) {
  343. logEntry.stderr = '(empty)'
  344. }
  345. let logTableExitCode = logEntry.exitCode
  346. if (logEntry.exitCode === 0) {
  347. logTableExitCode = 'OK'
  348. }
  349. if (logEntry.timedOut) {
  350. logTableExitCode += ' (timed out)'
  351. }
  352. row.querySelector('.timestamp').innerText = logEntry.datetimeStarted
  353. row.querySelector('.content').innerText = logEntry.actionTitle
  354. row.querySelector('.icon').innerHTML = logEntry.actionIcon
  355. row.querySelector('pre.stdout').innerText = logEntry.stdout
  356. row.querySelector('pre.stderr').innerText = logEntry.stderr
  357. row.querySelector('.exit-code').innerText = logTableExitCode
  358. row.setAttribute('title', logEntry.actionTitle)
  359. row.querySelector('.content').onclick = () => {
  360. window.executionDialog.reset()
  361. window.executionDialog.show()
  362. window.executionDialog.renderExecutionResult({
  363. logEntry: window.logEntries[logEntry.executionTrackingId]
  364. })
  365. }
  366. for (const tag of logEntry.tags) {
  367. const domTag = document.createElement('span')
  368. domTag.classList.add('tag')
  369. domTag.innerText = tag
  370. row.querySelector('.tags').append(domTag)
  371. }
  372. document.querySelector('#logTableBody').prepend(row)
  373. }
  374. }
  375. window.addEventListener('popstate', (e) => {
  376. e.preventDefault()
  377. if (e.state != null && typeof e.state.dir !== 'undefined') {
  378. changeDirectory(e.state.dir)
  379. }
  380. })