marshaller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 marshalContainerContents (json, section, fieldset, parentDashboard) {
  151. for (const item of json.contents) {
  152. switch (item.type) {
  153. case 'fieldset':
  154. marshalFieldset(item, section, parentDashboard)
  155. break
  156. case 'directory':
  157. marshalDirectoryButton(item, fieldset)
  158. marshalDirectory(item, section)
  159. break
  160. case 'display':
  161. marshalDisplay(item, fieldset)
  162. break
  163. case 'link':
  164. marshalLink(item, fieldset)
  165. break
  166. default:
  167. }
  168. }
  169. }
  170. function createFieldset (title, parentDashboard) {
  171. const legend = document.createElement('legend')
  172. legend.innerText = title
  173. const fs = document.createElement('fieldset')
  174. fs.title = title
  175. fs.appendChild(legend)
  176. if (typeof parentDashboard === 'undefined') {
  177. fs.setAttribute('parent-dashboard', '')
  178. } else {
  179. fs.setAttribute('parent-dashboard', parentDashboard)
  180. }
  181. return fs
  182. }
  183. function marshalFieldset (item, section, parentDashboard) {
  184. const fs = createFieldset(item.title, parentDashboard)
  185. marshalContainerContents(item, section, fs)
  186. section.appendChild(fs)
  187. }
  188. function changeDirectory (selected) {
  189. if (selected === '') {
  190. selected = null
  191. }
  192. if (selected === null) {
  193. window.directoryNavigation = []
  194. } else if (selected === '..') {
  195. window.directoryNavigation.pop()
  196. if (window.directoryNavigation.length > 0) {
  197. selected = window.directoryNavigation[window.directoryNavigation.length - 1]
  198. } else {
  199. selected = null
  200. }
  201. } else {
  202. // If the selected item is already in the nav list, pop elements until we get
  203. // "back" to the existing nav item
  204. while (window.directoryNavigation.includes(selected)) {
  205. window.directoryNavigation.pop()
  206. }
  207. window.directoryNavigation.push(selected)
  208. }
  209. for (const fieldset of document.querySelectorAll('fieldset')) {
  210. if (selected === null) {
  211. if ((fieldset.id === 'root-group' || fieldset.getAttribute('parent-dashboard') !== '') && fieldset.children.length > 1) {
  212. fieldset.style.display = 'grid'
  213. } else {
  214. fieldset.style.display = 'none'
  215. }
  216. } else {
  217. if (fieldset.title === selected) {
  218. fieldset.style.display = 'grid'
  219. } else {
  220. fieldset.style.display = 'none'
  221. }
  222. }
  223. }
  224. const title = document.querySelector('h1')
  225. title.innerHTML = ''
  226. const rootLink = createDirectoryBreadcrumb(window.pageTitle, null)
  227. title.appendChild(rootLink)
  228. for (const dir of window.directoryNavigation) {
  229. const sep = document.createElement('span')
  230. sep.innerHTML = ' » '
  231. title.append(sep)
  232. if (dir === selected) {
  233. title.append(selected)
  234. } else {
  235. title.appendChild(createDirectoryBreadcrumb(dir))
  236. }
  237. }
  238. document.title = title.innerText
  239. if (selected === null) {
  240. window.location.hash = null
  241. window.history.pushState({ dir: null }, null, '#')
  242. } else {
  243. window.location.hash = selected
  244. window.history.pushState({ dir: selected }, null, '#' + selected)
  245. }
  246. }
  247. function createDirectoryBreadcrumb (title, link) {
  248. const a = document.createElement('a')
  249. a.innerText = title
  250. a.title = title
  251. if (typeof link === 'undefined') {
  252. link = title
  253. }
  254. if (link === null) {
  255. a.href = '#'
  256. } else {
  257. a.href = '#' + link
  258. }
  259. a.onclick = () => {
  260. changeDirectory(link)
  261. }
  262. return a
  263. }
  264. function marshalDisplay (item, fieldset) {
  265. const display = document.createElement('div')
  266. display.innerHTML = item.title
  267. fieldset.appendChild(display)
  268. }
  269. function marshalDirectoryButton (item, fieldset) {
  270. const directoryButton = document.createElement('button')
  271. directoryButton.innerHTML = '<span class = "icon">&#128193;</span> ' + item.title
  272. directoryButton.onclick = () => {
  273. changeDirectory(item.title)
  274. }
  275. fieldset.appendChild(directoryButton)
  276. }
  277. function marshalDirectory (item, section) {
  278. const fs = createFieldset(item.title)
  279. fs.style.display = 'none'
  280. const directoryBackButton = document.createElement('button')
  281. directoryBackButton.innerHTML = '&laquo;'
  282. directoryBackButton.title = 'Go back one directory'
  283. directoryBackButton.onclick = () => {
  284. changeDirectory('..')
  285. }
  286. fs.appendChild(directoryBackButton)
  287. marshalContainerContents(item, section, fs)
  288. section.appendChild(fs)
  289. }
  290. export function marshalLogsJsonToHtml (json) {
  291. for (const logEntry of json.logs) {
  292. const existing = window.logEntries[logEntry.executionTrackingId]
  293. if (existing !== undefined) {
  294. continue
  295. }
  296. window.logEntries[logEntry.executionTrackingId] = logEntry
  297. const tpl = document.getElementById('tplLogRow')
  298. const row = tpl.content.querySelector('tr').cloneNode(true)
  299. if (logEntry.stdout.length === 0) {
  300. logEntry.stdout = '(empty)'
  301. }
  302. if (logEntry.stderr.length === 0) {
  303. logEntry.stderr = '(empty)'
  304. }
  305. let logTableExitCode = logEntry.exitCode
  306. if (logEntry.exitCode === 0) {
  307. logTableExitCode = 'OK'
  308. }
  309. if (logEntry.timedOut) {
  310. logTableExitCode += ' (timed out)'
  311. }
  312. row.querySelector('.timestamp').innerText = logEntry.datetimeStarted
  313. row.querySelector('.content').innerText = logEntry.actionTitle
  314. row.querySelector('.icon').innerHTML = logEntry.actionIcon
  315. row.querySelector('pre.stdout').innerText = logEntry.stdout
  316. row.querySelector('pre.stderr').innerText = logEntry.stderr
  317. row.querySelector('.exit-code').innerText = logTableExitCode
  318. row.setAttribute('title', logEntry.actionTitle)
  319. row.querySelector('.content').onclick = () => {
  320. window.executionDialog.reset()
  321. window.executionDialog.show()
  322. window.executionDialog.renderExecutionResult({
  323. logEntry: window.logEntries[logEntry.executionTrackingId]
  324. })
  325. }
  326. for (const tag of logEntry.tags) {
  327. const domTag = document.createElement('span')
  328. domTag.classList.add('tag')
  329. domTag.innerText = tag
  330. row.querySelector('.tags').append(domTag)
  331. }
  332. document.querySelector('#logTableBody').prepend(row)
  333. }
  334. }
  335. window.addEventListener('popstate', (e) => {
  336. e.preventDefault()
  337. if (e.state != null && typeof e.state.dir !== 'undefined') {
  338. changeDirectory(e.state.dir)
  339. }
  340. })