|
@@ -0,0 +1,115 @@
|
|
|
+class ArchiveCommit {
|
|
|
+ constructor(message, time, sha) {
|
|
|
+ this.message = message
|
|
|
+ this.time = time
|
|
|
+ this.sha = sha
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function replace_urls(text, url_before, url_after) {
|
|
|
+ for(const key of ["src=\""]) {
|
|
|
+ segments = text.split(key)
|
|
|
+ result_text = segments[0]
|
|
|
+ for(const segment of segments.slice(1)) {
|
|
|
+ var index = segment.indexOf('"')
|
|
|
+ var first = segment.substring(0, index)
|
|
|
+ var second = segment.substring(index)
|
|
|
+
|
|
|
+ var new_url = url_before + encodeURIComponent(first) + url_after
|
|
|
+
|
|
|
+ result_text += key + new_url + second
|
|
|
+ }
|
|
|
+
|
|
|
+ text = result_text
|
|
|
+ }
|
|
|
+
|
|
|
+ return text
|
|
|
+}
|
|
|
+
|
|
|
+class ArchiveView {
|
|
|
+ constructor(dateselector, commitsSelect, content, token, url, project_id, branch, page_path) {
|
|
|
+ this.token = token
|
|
|
+ this.url = url
|
|
|
+ this.project_id = project_id
|
|
|
+ this.branch = branch
|
|
|
+
|
|
|
+ this.dateselector = dateselector
|
|
|
+ this.dateselector.value = ""
|
|
|
+ this.dateselector.max = new Date().toLocaleDateString('en-ca')
|
|
|
+ // Alternative, doesn't allow selecting today in first two hours of day:
|
|
|
+ // this.dateselector.max = new Date().toISOString().split("T")[0]
|
|
|
+ this.dateselector.addEventListener("input",((event) => this.on_date_selected(event)))
|
|
|
+
|
|
|
+ this.commitsSelect = commitsSelect
|
|
|
+ this.commitsSelect.addEventListener("input",((event) => this.on_commit_selected(event)))
|
|
|
+
|
|
|
+ this.content = content
|
|
|
+
|
|
|
+ this.page_path = page_path
|
|
|
+ }
|
|
|
+
|
|
|
+ add_commits(commits) {
|
|
|
+ commits.map(commit => {
|
|
|
+ var elem = document.createElement("option")
|
|
|
+ elem.innerHTML = commit.message
|
|
|
+ elem.value = commit.sha
|
|
|
+ this.commitsSelect.appendChild(elem)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ on_date_selected(event) {
|
|
|
+ if (!event.target.value)
|
|
|
+ return
|
|
|
+ var page = 1
|
|
|
+ var date = event.target.value
|
|
|
+
|
|
|
+ // Clean commits list
|
|
|
+ this.commitsSelect.innerHTML = ""
|
|
|
+ this.commitsSelect.appendChild(document.createElement("option"))
|
|
|
+
|
|
|
+ this.load_pages_recursive(page, date)
|
|
|
+ }
|
|
|
+
|
|
|
+ on_commit_selected(event) {
|
|
|
+ var sha = event.target.value
|
|
|
+ var url_before = 'https://' + this.url + '/api/v4/projects/' + this.project_id + '/repository/files/' + encodeURIComponent(this.page_path + "/")
|
|
|
+ var url_after = '/raw?ref=' + sha + '&private_token=' + this.token
|
|
|
+ var url = url_before + encodeURIComponent("index.html") + url_after
|
|
|
+ fetch(url)
|
|
|
+ .then(response => response.text())
|
|
|
+ .then(text => {
|
|
|
+ console.log(text)
|
|
|
+ var cleaned_text = replace_urls(text, url_before, url_after)
|
|
|
+ console.log(cleaned_text)
|
|
|
+
|
|
|
+ const blobContent = new Blob([cleaned_text], {type: "text/html"})
|
|
|
+ this.content.src = URL.createObjectURL(blobContent)
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ load_pages_recursive(page, date) {
|
|
|
+ var commits_url = 'https://' + this.url + '/api/v4/projects/' + this.project_id + '/repository/commits?ref_name=' + this.branch + '&private_token=' + this.token + "&since="+date+"T00:00&until="+date+"T24:00&per_page=100&page=" + page;
|
|
|
+
|
|
|
+ var length_promise = fetch(commits_url)
|
|
|
+ .then(response => response.json())
|
|
|
+ .then(
|
|
|
+ // Translate response to commits
|
|
|
+ json => json.map(
|
|
|
+ gitlab_commit => new ArchiveCommit(
|
|
|
+ gitlab_commit.message,
|
|
|
+ gitlab_commit.created_at,
|
|
|
+ gitlab_commit.id
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ).then((commits) => {
|
|
|
+ this.add_commits(commits)
|
|
|
+ return commits.length
|
|
|
+ }
|
|
|
+ ).then((length) => {
|
|
|
+ if (length > 0) {
|
|
|
+ this.load_pages_recursive(page+1, date)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|