marshaller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. switch (actionButton.popupOnStart) {
  42. case 'execution-button':
  43. document.querySelector('execution-button#execution-' + logEntry.executionTrackingId).onExecutionFinished(logEntry)
  44. break
  45. case 'execution-dialog-stdout-only':
  46. case 'execution-dialog':
  47. actionButton.onExecutionFinished(logEntry)
  48. // We don't need to fetch the logEntry for the dialog because we already
  49. // have it, so we open the dialog and it will get updated below.
  50. window.executionDialog.show()
  51. window.executionDialog.executionUuid = logEntry.uuid
  52. break
  53. default:
  54. actionButton.onExecutionFinished(logEntry)
  55. break
  56. }
  57. marshalLogsJsonToHtml({
  58. logs: [logEntry]
  59. })
  60. // If the current execution dialog is open, update that too
  61. if (window.executionDialog.dlg.open && window.executionDialog.executionUuid === logEntry.uuid) {
  62. window.executionDialog.renderExecutionResult({
  63. logEntry: logEntry
  64. })
  65. }
  66. }
  67. function showSection (title) {
  68. for (const section of document.querySelectorAll('section')) {
  69. if (section.title === title) {
  70. section.style.display = 'block'
  71. } else {
  72. section.style.display = 'none'
  73. }
  74. }
  75. for (const otherName of ['Actions', 'Logs']) {
  76. document.getElementById('show' + otherName).classList.remove('activeSection')
  77. document.getElementById('content' + otherName).hidden = true
  78. }
  79. // document.getElementById('show' + name).classList.add('activeSection')
  80. // document.getElementById('content' + name).hidden = false
  81. document.getElementById('hide-sidebar-checkbox').checked = true
  82. changeDirectory(null)
  83. }
  84. export function setupSectionNavigation (style) {
  85. const nav = document.querySelector('nav')
  86. if (style === 'sidebar') {
  87. nav.classList += 'sidebar'
  88. document.body.classList += 'has-sidebar'
  89. } else {
  90. nav.classList += 'topbar'
  91. document.body.classList += 'has-topbar'
  92. }
  93. nav.hidden = false
  94. document.getElementById('showActions').onclick = () => { showSection('Actions') }
  95. document.getElementById('showLogs').onclick = () => { showSection('Logs') }
  96. }
  97. function marshalDashboardStructureToHtml (json) {
  98. const nav = document.getElementById('navigation-links')
  99. for (const dashboard of json.dashboards) {
  100. const oldsection = document.querySelector('section[title="' + dashboard.title + '"]')
  101. if (oldsection != null) {
  102. oldsection.remove()
  103. }
  104. const section = document.createElement('section')
  105. section.title = dashboard.title
  106. const def = createFieldset('default', section)
  107. section.appendChild(def)
  108. document.getElementsByTagName('main')[0].appendChild(section)
  109. marshalContainerContents(dashboard, section, def, dashboard.title)
  110. const oldLi = nav.querySelector('li[title="' + dashboard.title + '"]')
  111. if (oldLi != null) {
  112. oldLi.remove()
  113. }
  114. const navigationA = document.createElement('a')
  115. navigationA.title = dashboard.title
  116. navigationA.innerText = dashboard.title
  117. navigationA.onclick = () => {
  118. showSection(dashboard.title)
  119. }
  120. const navigationLi = document.createElement('li')
  121. navigationLi.appendChild(navigationA)
  122. navigationLi.title = dashboard.title
  123. document.getElementById('navigation-links').appendChild(navigationLi)
  124. }
  125. if (document.getElementById('root-group').children.length === 0 && json.dashboards.length > 0) {
  126. showSection(json.dashboards[0].title)
  127. } else {
  128. showSection('Actions')
  129. }
  130. const rootGroup = document.querySelector('#root-group')
  131. let hasRootActions = false
  132. for (const btn of Object.values(window.actionButtons)) {
  133. if (btn.parentElement === null) {
  134. rootGroup.appendChild(btn)
  135. hasRootActions = true
  136. }
  137. }
  138. if (!hasRootActions) {
  139. nav.querySelector('li[title="Actions"]').style.display = 'none'
  140. }
  141. }
  142. function marshalLink (item, fieldset) {
  143. let btn = window.actionButtons[item.title]
  144. if (typeof btn === 'undefined') {
  145. btn = document.createElement('button')
  146. btn.innerText = 'Action not found: ' + item.title
  147. btn.classList.add('error')
  148. }
  149. fieldset.appendChild(btn)
  150. }
  151. function marshalContainerContents (json, section, fieldset, parentDashboard) {
  152. for (const item of json.contents) {
  153. switch (item.type) {
  154. case 'fieldset':
  155. marshalFieldset(item, section, parentDashboard)
  156. break
  157. case 'directory':
  158. marshalDirectoryButton(item, fieldset)
  159. marshalDirectory(item, section)
  160. break
  161. case 'display':
  162. marshalDisplay(item, fieldset)
  163. break
  164. case 'link':
  165. marshalLink(item, fieldset)
  166. break
  167. default:
  168. }
  169. }
  170. }
  171. function createFieldset (title, parentDashboard) {
  172. const legend = document.createElement('legend')
  173. legend.innerText = title
  174. const fs = document.createElement('fieldset')
  175. fs.title = title
  176. fs.appendChild(legend)
  177. if (typeof parentDashboard === 'undefined') {
  178. fs.setAttribute('parent-dashboard', '')
  179. } else {
  180. fs.setAttribute('parent-dashboard', parentDashboard)
  181. }
  182. return fs
  183. }
  184. function marshalFieldset (item, section, parentDashboard) {
  185. const fs = createFieldset(item.title, parentDashboard)
  186. marshalContainerContents(item, section, fs)
  187. section.appendChild(fs)
  188. }
  189. function changeDirectory (selected) {
  190. if (selected === '') {
  191. selected = null
  192. }
  193. if (selected === null) {
  194. window.directoryNavigation = []
  195. } else if (selected === '..') {
  196. window.directoryNavigation.pop()
  197. if (window.directoryNavigation.length > 0) {
  198. selected = window.directoryNavigation[window.directoryNavigation.length - 1]
  199. } else {
  200. selected = null
  201. }
  202. } else {
  203. // If the selected item is already in the nav list, pop elements until we get
  204. // "back" to the existing nav item
  205. while (window.directoryNavigation.includes(selected)) {
  206. window.directoryNavigation.pop()
  207. }
  208. window.directoryNavigation.push(selected)
  209. }
  210. for (const fieldset of document.querySelectorAll('fieldset')) {
  211. if (selected === null) {
  212. if ((fieldset.id === 'root-group' || fieldset.getAttribute('parent-dashboard') !== '') && fieldset.children.length > 1) {
  213. fieldset.style.display = 'grid'
  214. } else {
  215. fieldset.style.display = 'none'
  216. }
  217. } else {
  218. if (fieldset.title === selected) {
  219. fieldset.style.display = 'grid'
  220. } else {
  221. fieldset.style.display = 'none'
  222. }
  223. }
  224. }
  225. const title = document.querySelector('h1')
  226. title.innerHTML = ''
  227. const rootLink = createDirectoryBreadcrumb('OliveTin', null)
  228. title.appendChild(rootLink)
  229. for (const dir of window.directoryNavigation) {
  230. const sep = document.createElement('span')
  231. sep.innerHTML = ' » '
  232. title.append(sep)
  233. if (dir === selected) {
  234. title.append(selected)
  235. } else {
  236. title.appendChild(createDirectoryBreadcrumb(dir))
  237. }
  238. }
  239. document.title = title.innerText
  240. if (selected === null) {
  241. window.location.hash = null
  242. window.history.pushState({ dir: null }, null, '#')
  243. } else {
  244. window.location.hash = selected
  245. window.history.pushState({ dir: selected }, null, '#' + selected)
  246. }
  247. }
  248. function createDirectoryBreadcrumb (title, link) {
  249. const a = document.createElement('a')
  250. a.innerText = title
  251. a.title = title
  252. if (typeof link === 'undefined') {
  253. link = title
  254. }
  255. if (link === null) {
  256. a.href = '#'
  257. } else {
  258. a.href = '#' + link
  259. }
  260. a.onclick = () => {
  261. changeDirectory(link)
  262. }
  263. return a
  264. }
  265. function marshalDisplay (item, fieldset) {
  266. const display = document.createElement('div')
  267. display.innerHTML = item.title
  268. fieldset.appendChild(display)
  269. }
  270. function marshalDirectoryButton (item, fieldset) {
  271. const directoryButton = document.createElement('button')
  272. directoryButton.innerHTML = '<span class = "icon">&#128193;</span> ' + item.title
  273. directoryButton.onclick = () => {
  274. changeDirectory(item.title)
  275. }
  276. fieldset.appendChild(directoryButton)
  277. }
  278. function marshalDirectory (item, section) {
  279. const fs = createFieldset(item.title)
  280. fs.style.display = 'none'
  281. const directoryBackButton = document.createElement('button')
  282. directoryBackButton.innerHTML = '&laquo;'
  283. directoryBackButton.title = 'Go back one directory'
  284. directoryBackButton.onclick = () => {
  285. changeDirectory('..')
  286. }
  287. fs.appendChild(directoryBackButton)
  288. marshalContainerContents(item, section, fs)
  289. section.appendChild(fs)
  290. }
  291. export function marshalLogsJsonToHtml (json) {
  292. for (const logEntry of json.logs) {
  293. const existing = window.logEntries[logEntry.executionTrackingId]
  294. if (existing !== undefined) {
  295. continue
  296. }
  297. window.logEntries[logEntry.executionTrackingId] = logEntry
  298. const tpl = document.getElementById('tplLogRow')
  299. const row = tpl.content.querySelector('tr').cloneNode(true)
  300. if (logEntry.stdout.length === 0) {
  301. logEntry.stdout = '(empty)'
  302. }
  303. if (logEntry.stderr.length === 0) {
  304. logEntry.stderr = '(empty)'
  305. }
  306. let logTableExitCode = logEntry.exitCode
  307. if (logEntry.exitCode === 0) {
  308. logTableExitCode = 'OK'
  309. }
  310. if (logEntry.timedOut) {
  311. logTableExitCode += ' (timed out)'
  312. }
  313. row.querySelector('.timestamp').innerText = logEntry.datetimeStarted
  314. row.querySelector('.content').innerText = logEntry.actionTitle
  315. row.querySelector('.icon').innerHTML = logEntry.actionIcon
  316. row.querySelector('pre.stdout').innerText = logEntry.stdout
  317. row.querySelector('pre.stderr').innerText = logEntry.stderr
  318. row.querySelector('.exit-code').innerText = logTableExitCode
  319. row.setAttribute('title', logEntry.actionTitle)
  320. row.querySelector('.content').onclick = () => {
  321. window.executionDialog.reset()
  322. window.executionDialog.show()
  323. window.executionDialog.renderExecutionResult({
  324. logEntry: window.logEntries[logEntry.executionTrackingId]
  325. })
  326. }
  327. for (const tag of logEntry.tags) {
  328. const domTag = document.createElement('span')
  329. domTag.classList.add('tag')
  330. domTag.innerText = tag
  331. row.querySelector('.tags').append(domTag)
  332. }
  333. document.querySelector('#logTableBody').prepend(row)
  334. }
  335. }
  336. window.addEventListener('popstate', (e) => {
  337. e.preventDefault()
  338. if (e.state != null && typeof e.state.dir !== 'undefined') {
  339. changeDirectory(e.state.dir)
  340. }
  341. })