archiveviewer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Git HTML Archive Viewer
  3. *
  4. * Benedikt Bieringer <benedikt.b@wwu.de>
  5. * https://nuserv.uni-muenster.de:8443/bbieringer/statusarchiveviewer
  6. */
  7. class ArchiveCommit {
  8. constructor(message, time, sha) {
  9. this.message = message
  10. this.time = time
  11. this.sha = sha
  12. }
  13. }
  14. function replace_urls(text, url_before, url_after) {
  15. for(const key of ["src=\""]) {
  16. segments = text.split(key)
  17. result_text = segments[0]
  18. for(const segment of segments.slice(1)) {
  19. var index = segment.indexOf('"')
  20. var first = segment.substring(0, index)
  21. var second = segment.substring(index)
  22. var new_url = url_before + encodeURIComponent(first) + url_after
  23. result_text += key + new_url + second
  24. }
  25. text = result_text
  26. }
  27. return text
  28. }
  29. function hide_iframes(text) {
  30. return text.replaceAll("<iframe", "<div hidden ").replaceAll("/iframe>", "/div>")
  31. }
  32. function date_to_iso(value) {
  33. if ((value + "").includes("-")) {
  34. return value
  35. }
  36. var date = new Date(parseInt(value)*1000)
  37. return date.toISOString().substring(0,16)
  38. }
  39. function date_to_unix(value) {
  40. return parseInt(new Date(value + ":00+00:00").getTime() / 1000)
  41. }
  42. class ArchiveView {
  43. constructor(mindate, maindiv, token, url, project_id, branch, page_path, index) {
  44. this.token = token
  45. this.url = url
  46. this.project_id = project_id
  47. this.branch = branch
  48. this.next_commit = ""
  49. this.current_commit = ""
  50. this.last_from = ""
  51. this.last_to = ""
  52. this.maindiv = maindiv
  53. var outer_span = document.createElement("span")
  54. this.maindiv.appendChild(outer_span)
  55. // ---
  56. this.selecttype = document.createElement("select")
  57. outer_span.appendChild(this.selecttype)
  58. var option_date = document.createElement("option")
  59. option_date.value = "date"
  60. option_date.innerHTML = "Date"
  61. this.selecttype.appendChild(option_date)
  62. var option_range = document.createElement("option")
  63. option_range.value = "range"
  64. option_range.innerHTML = "Range"
  65. this.selecttype.appendChild(option_range)
  66. this.selecttype.addEventListener("input",(() => this.update_hash()))
  67. // ---
  68. outer_span.append(" (" + mindate + " to today): ")
  69. this.dateselector = document.createElement("input")
  70. this.dateselector.type = "date"
  71. // Alternative, doesn't allow selecting today in first two hours of day:
  72. // var maxdate = new Date().toISOString().split("T")[0]
  73. var maxdate = new Date().toLocaleDateString('en-ca')
  74. this.dateselector.value = ""
  75. this.dateselector.max = maxdate
  76. this.dateselector.addEventListener("input",(() => this.update_hash()))
  77. outer_span.appendChild(this.dateselector)
  78. // ---
  79. this.rangespan = document.createElement("span")
  80. outer_span.appendChild(this.rangespan)
  81. this.range_from = document.createElement("input")
  82. this.range_from.type = "datetime-local"
  83. this.range_from.value = ""
  84. this.range_from.min = mindate + "T00:00"
  85. this.range_from.max = maxdate + "T23:59"
  86. this.range_from.addEventListener("input",(() => this.update_hash()))
  87. this.rangespan.appendChild(this.range_from)
  88. this.rangespan.insertAdjacentHTML('beforeend', " &ndash; ")
  89. this.range_to = document.createElement("input")
  90. this.range_to.type = "datetime-local"
  91. this.range_to.disabled = true
  92. this.range_to.value = ""
  93. this.range_to.max = maxdate + "T23:59"
  94. this.range_to.addEventListener("input",(() => this.update_hash()))
  95. this.rangespan.appendChild(this.range_to)
  96. // ---
  97. outer_span.append(" | Version: ")
  98. this.commitsSelect = document.createElement("select")
  99. this.commitsSelect.className = "archive commits"
  100. this.commitsSelect.addEventListener("input",(() => this.on_commit_selected()))
  101. outer_span.appendChild(this.commitsSelect)
  102. outer_span.insertAdjacentHTML('beforeend', "<br/><i>Note: Large time ranges can slow the version selection down.</i>")
  103. // ---
  104. this.content = document.createElement("iframe")
  105. this.content.className = "archive content"
  106. this.content.height = "100%"
  107. this.maindiv.appendChild(this.content)
  108. this.page_path = page_path
  109. this.index = index
  110. this.defaultcontent = new Blob([`
  111. <!doctype html><html><head>
  112. <style>
  113. body {
  114. min-height: 100vh;
  115. display: flex;
  116. align-items: center;
  117. justify-content: space-around;
  118. }
  119. h1,h2 {
  120. display: flex;
  121. align-items: center;
  122. justify-content: center;
  123. }
  124. </style>
  125. </head>
  126. <body style="background:#aaa; color:#fff;">
  127. <div>
  128. <h1>Welcome to the archive viewer!</h1>
  129. <h2>Select a date and version to start.</h2>
  130. </div>
  131. </body>
  132. </html>
  133. `], {type: "text/html"})
  134. this.content.src = URL.createObjectURL(this.defaultcontent)
  135. window.addEventListener('hashchange', (event) => this.on_hash_changed(event))
  136. // Updates
  137. this.on_hash_changed(null)
  138. this.update_hash() // Update visibilities
  139. this.load_pages() // Update version list
  140. }
  141. update_hash() {
  142. var old_hash = window.location.hash
  143. var new_hash = ""
  144. if (this.selecttype.value == "date") {
  145. new_hash = "#date=" + this.dateselector.value + "&commit=" + this.next_commit
  146. }
  147. else {
  148. new_hash = "#from=" + this.range_from.value + "&to=" + this.range_to.value + "&commit=" + this.next_commit
  149. }
  150. if (new_hash == old_hash) {
  151. return false
  152. }
  153. window.location.hash = new_hash
  154. return true
  155. }
  156. on_hash_changed(event) {
  157. var remaining_hash = window.location.hash.substring(1).split("&")
  158. var date_from = null
  159. var length = null
  160. var date_to = null
  161. var date = null
  162. while (remaining_hash.length > 0) {
  163. var current_part = remaining_hash.pop()
  164. var [key, value] = current_part.split("=")
  165. if (key == "from") {
  166. date_from = ""
  167. if (value) {
  168. date_from = date_to_iso(value)
  169. }
  170. }
  171. else if (key == "length") {
  172. length = value
  173. }
  174. else if (key == "to") {
  175. date_to = ""
  176. if (value) {
  177. date_to = date_to_iso(value)
  178. }
  179. }
  180. else if (key == "date"){
  181. date = ""
  182. if (value) {
  183. date = date_to_iso(value)
  184. }
  185. }
  186. else if (key == "commit"){
  187. this.next_commit = value
  188. }
  189. }
  190. if (date) {
  191. this.dateselector.value = date
  192. this.selecttype.value = "date"
  193. // Convert date to range so one only needs to work with ranges
  194. if (this.dateselector.value) {
  195. var date_from_tmp = this.dateselector.value
  196. date_from = date_from_tmp + "T00:00"
  197. var date_to_obj = new Date(date_from_tmp)
  198. date_to_obj.setDate(date_to_obj.getDate() + 1)
  199. // Works also across months, years
  200. date_to = date_to_obj.toJSON().slice(0, 10) + "T00:00"
  201. }
  202. }
  203. if (date_from) {
  204. this.range_from.value = date_from
  205. this.range_to.min = date_from
  206. this.range_to.disabled = (date_from == "")
  207. if (!date) {
  208. this.selecttype.value = "range"
  209. }
  210. }
  211. if (date_to) {
  212. this.range_to.value = date_to
  213. }
  214. if (length) {
  215. this.range_to.value = date_to_iso(date_to_unix(date_from)+parseInt(length))
  216. }
  217. if (this.update_hash()) {
  218. return // Triggered another hash change
  219. }
  220. // Visibilities
  221. var is_date = this.selecttype.value == "date"
  222. this.dateselector.hidden = !is_date
  223. this.rangespan.hidden = is_date
  224. if (!is_date) {
  225. this.dateselector.value = ""
  226. }
  227. if (!date_from) {
  228. return
  229. }
  230. if (!date_to) {
  231. return
  232. }
  233. if (this.commitsSelect.length && date_from == this.last_from && date_to == this.last_to) {
  234. this.commitsSelect.value = this.next_commit
  235. this.on_commit_selected()
  236. return // List didn't change
  237. }
  238. this.last_from = date_from
  239. this.last_to = date_to
  240. if (this.next_commit) {
  241. this.load_pages(false)
  242. return
  243. }
  244. this.load_pages(true)
  245. }
  246. add_commits(commits, open_first) {
  247. var to_open = open_first
  248. commits.map(commit => {
  249. var elem = document.createElement("option")
  250. elem.innerHTML = commit.message
  251. elem.value = commit.sha
  252. this.commitsSelect.appendChild(elem)
  253. if (to_open) {
  254. this.commitsSelect.value = elem.value
  255. this.on_commit_selected()
  256. to_open = false
  257. }
  258. })
  259. }
  260. on_commit_selected() {
  261. if (!this.commitsSelect.value) {
  262. this.content.contentWindow.location.replace(URL.createObjectURL(this.defaultcontent))
  263. this.next_commit = ""
  264. this.update_hash()
  265. return
  266. }
  267. this.next_commit = this.commitsSelect.value
  268. this.update_hash()
  269. if (this.next_commit == this.current_commit) {
  270. return // Nothing changed
  271. }
  272. this.current_commit = this.next_commit
  273. var url_before = 'https://' + this.url + '/api/v4/projects/' + this.project_id + '/repository/files/' + encodeURIComponent(this.page_path + "/")
  274. var url_after = '/raw?ref=' + this.next_commit + '&private_token=' + this.token
  275. var url = url_before + encodeURIComponent(this.index) + url_after
  276. fetch(url)
  277. .then(response => response.text())
  278. .then(text => {
  279. var cleaned_text = hide_iframes(replace_urls(text, url_before, url_after))
  280. const blobContent = new Blob([cleaned_text], {type: "text/html"})
  281. this.content.contentWindow.location.replace(URL.createObjectURL(blobContent)) // Don't affect browser history by content change (as opposed to this.content.src = ...)
  282. })
  283. }
  284. load_pages(open_first=false) {
  285. var date_from = this.range_from.value
  286. var date_to = this.range_to.value
  287. this.load_pages_recursive(1, date_from, date_to, open_first)
  288. }
  289. load_pages_recursive(page, date_from, date_to, open_first) {
  290. if (page == 1) {
  291. // Clean commits list
  292. this.commitsSelect.innerHTML = ""
  293. this.commitsSelect.appendChild(document.createElement("option"))
  294. }
  295. if (date_from == "" || date_to == "") {
  296. return
  297. }
  298. var commits_url = 'https://' + this.url + '/api/v4/projects/' + this.project_id + '/repository/commits?ref_name=' + this.branch + '&private_token=' + this.token + "&since="+date_from+"&until="+date_to+"&per_page=100&page=" + page;
  299. var length_promise = fetch(commits_url)
  300. .then(response => response.json())
  301. .then(
  302. // Translate response to commits
  303. json => json.map(
  304. gitlab_commit => new ArchiveCommit(
  305. gitlab_commit.message,
  306. gitlab_commit.created_at,
  307. gitlab_commit.id
  308. )
  309. )
  310. ).then((commits) => {
  311. // Skip if new date is selected
  312. if (date_from != this.range_from.value
  313. || date_to != this.range_to.value) {
  314. return 0;
  315. }
  316. this.add_commits(commits, open_first)
  317. return commits.length
  318. }
  319. ).then((length) => {
  320. if (length > 0) {
  321. this.load_pages_recursive(page+1, date_from, date_to, false)
  322. }
  323. else {
  324. // Loading done, update commit
  325. this.commitsSelect.value = this.next_commit
  326. this.on_commit_selected()
  327. }
  328. })
  329. }
  330. }