marshaller.js 12 KB

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