Browse Source

Added parsing hash parameters "from", "length", "to"

This is done to support BREW linking to this
2xB 2 years ago
parent
commit
83314ff5c8
2 changed files with 179 additions and 23 deletions
  1. 161 21
      viewer/archive/archiveviewer.js
  2. 18 2
      viewer/archive/new_monitor.html

+ 161 - 21
viewer/archive/archiveviewer.js

@@ -33,22 +33,53 @@ function replace_urls(text, url_before, url_after) {
     return text
 }
 
+function date_to_iso(value) {
+    console.log(value)
+    if ((value + "").includes("-")) {
+        return value
+    }
+    
+    var date = new Date(parseInt(value)*1000)
+    return date.toISOString().substring(0,16)
+}
+
+function date_to_unix(value) {
+    return parseInt(new Date(value + ":00+00:00").getTime() / 1000)
+}
+
 class ArchiveView {
-    constructor(dateselector, commitsSelect, content, token, url, project_id, branch, page_path, index) {
+    constructor(mindate, selecttype, dateselector, range_from, range_to, rangespan, commitsSelect, content, token, url, project_id, branch, page_path, index) {
         this.token = token
         this.url = url
         this.project_id = project_id
         this.branch = branch
         
+        // Alternative, doesn't allow selecting today in first two hours of day:
+        // var maxdate = new Date().toISOString().split("T")[0]
+        var maxdate = new Date().toLocaleDateString('en-ca')
         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.max = maxdate
         this.dateselector.addEventListener("input",((event) => this.on_date_selected(event)))
         
+        this.range_from = range_from
+        this.range_from.value = ""
+        this.range_from.min = mindate + "T00:00"
+        this.range_from.max = maxdate + "T23:59"
+        this.range_from.addEventListener("input",(() => this.on_range_from_selected()))
+        
+        this.range_to = range_to
+        this.range_to.value = ""
+        this.range_to.max = maxdate + "T23:59"
+        this.range_to.addEventListener("input",(() => this.on_range_to_selected()))
+        
+        this.rangespan = rangespan
+        
+        this.selecttype = selecttype
+        this.selecttype.addEventListener("input",(() => this.on_selecttype_selected()))
+        
         this.commitsSelect = commitsSelect
-        this.commitsSelect.addEventListener("input",((event) => this.on_commit_selected(event)))
+        this.commitsSelect.addEventListener("input",(() => this.on_commit_selected()))
         
         this.content = content
         
@@ -82,41 +113,132 @@ class ArchiveView {
         </html>
         `], {type: "text/html"})
         this.content.src = URL.createObjectURL(blobContent)
+        
+        window.addEventListener('hashchange', (event) => this.on_hash_changed(event))
+        
+        // Updates
+        this.on_hash_changed(null)
+        this.on_selecttype_selected() // Update visibilities
+        this.load_pages() // Update version list
     }
     
-    add_commits(commits) {
+    on_hash_changed(event) {
+        var remaining_hash = window.location.hash.substring(1).split("&")
+        
+        var date_from = null
+        var length = null
+        var to = null
+        
+        var provided_full_range = false;
+        while (remaining_hash.length > 0) {
+            var current_part = remaining_hash.pop()
+            
+            var [key, value] = current_part.split("=")
+            
+            if (key == "from") {
+                date_from = value
+            }
+            else if (key == "length") {
+                length = value
+            }
+            else if (key == "to") {
+                to = value
+            }
+        }
+        
+        if (date_from != null) {
+            this.selecttype.value = "range"
+            var from_iso = date_to_iso(date_from)
+            console.log(date_from, from_iso)
+            this.range_from.value = from_iso
+            this.on_range_from_selected()
+            
+            if (length != null) {
+                // Only works when given UNIX timestamps
+                console.log(from_iso)
+                console.log(from_iso,date_to_iso(date_to_unix(from_iso)))
+                this.range_to.value = date_to_iso(date_to_unix(from_iso)+parseInt(length))
+                provided_full_range = true
+            }
+        }
+        
+        if (to != null) {
+            if (date_from != nullptr) {
+                provided_full_range = true
+            }
+            
+            this.selecttype.value = "range"
+            this.on_selecttype_selected() // Update visibilities
+            this.range_to.value = date_to_iso(to)
+        }
+        
+        if (provided_full_range) {
+            this.load_pages(true)
+        }
+    }
+    
+    add_commits(commits, open_first) {
+        var to_open = open_first
         commits.map(commit => {
             var elem = document.createElement("option")
             elem.innerHTML = commit.message
             elem.value = commit.sha
             this.commitsSelect.appendChild(elem)
+            
+            if (to_open) {
+                this.commitsSelect.value = elem.value
+                this.on_commit_selected()
+                to_open = false
+            }
         })
     }
     
+    on_selecttype_selected() {
+        this.dateselector.value = ""
+        
+        var is_date = this.selecttype.value == "date"
+        
+        this.dateselector.hidden = !is_date
+        this.rangespan.hidden = is_date
+    }
+    
     on_date_selected(event) {
         if (!event.target.value)
             return
-        var page = 1
-        var date = event.target.value
+        var date_from = event.target.value
+        
+        var date_to_obj = new Date(date_from)
+        date_to_obj.setDate(date_to_obj.getDate() + 1)
+          // Works also across months, years
+        var date_to = date_to_obj.toLocaleDateString('en-ca')
         
-        // Clean commits list
-        this.commitsSelect.innerHTML = ""
-        this.commitsSelect.appendChild(document.createElement("option"))
+        this.range_from.value = date_from + "T00:00"
+        this.range_to.value = date_to + "T00:00"
         
-        this.load_pages_recursive(page, date)
+        this.load_pages()
     }
     
-    on_commit_selected(event) {
-        var sha = event.target.value
+    on_range_from_selected() {
+        var val = this.range_from.value
+        this.range_to.min = val
+        this.range_to.disabled = (val == "")
+        
+        this.load_pages()
+    }
+    
+    on_range_to_selected() {
+        this.load_pages()
+    }
+    
+    on_commit_selected() {
+        var sha = this.commitsSelect.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(this.index) + 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)
@@ -124,8 +246,25 @@ class ArchiveView {
 
     }
     
-    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;
+    load_pages(open_first=false) {
+        var date_from = this.range_from.value
+        var date_to = this.range_to.value
+        
+        this.load_pages_recursive(1, date_from, date_to, open_first)
+    }
+    
+    load_pages_recursive(page, date_from, date_to, open_first) {
+        if (page == 1) {
+            // Clean commits list
+            this.commitsSelect.innerHTML = ""
+            this.commitsSelect.appendChild(document.createElement("option"))
+        }
+        
+        if (date_from == "" || date_to == "") {
+            return
+        }
+        
+        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;
         
         var length_promise = fetch(commits_url)
         .then(response => response.json())
@@ -140,16 +279,17 @@ class ArchiveView {
             )
         ).then((commits) => {
                 // Skip if new date is selected
-                if (date != this.dateselector.value) {
+                if (date_from != this.range_from.value 
+                    || date_to != this.range_to.value) {
                     return 0;
                 }
                 
-                this.add_commits(commits)
+                this.add_commits(commits, open_first)
                 return commits.length
             }
         ).then((length) => {
             if (length > 0) {
-                this.load_pages_recursive(page+1, date)
+                this.load_pages_recursive(page+1, date_from, date_to, false)
             }
         })
     }

+ 18 - 2
viewer/archive/new_monitor.html

@@ -11,8 +11,19 @@
     </header>
     <div class="archive main">
         <span>
-          Date (2021-12-21 to today): <input type="date" min="2021-12-21" id="dateselector"/> | Version:
-          <select class="archive commits" id="commitSelect"></select>
+          <select id="selecttype">
+            <option value="date">Date</option>
+            <option value="range">Range</option>
+          </select>
+          (2021-12-21 to today): <input type="date" id="dateselector"/>
+          <span id="rangespan">
+            <input type="datetime-local" id="range_from"/>
+            &ndash;
+            <input type="datetime-local" id="range_to" disabled />
+          </span>
+          | Version:
+          <select class="archive commits" id="commitSelect"></select> <br/>
+          <i>Note: Large time ranges can slow the version selection down.</i>
         </span>
         <iframe class="archive content" height="100%" id="content"></iframe>
     </div>
@@ -25,7 +36,12 @@
     </footer>
     <script>
     new ArchiveView(
+        mindate = "2021-12-21",
+        selecttype = document.getElementById("selecttype"),
         dateselector = document.getElementById("dateselector"),
+        range_from = document.getElementById("range_from"),
+        range_to = document.getElementById("range_to"),
+        range_to = document.getElementById("rangespan"),
         commitsSelect = document.getElementById("commitSelect"),
         content = document.getElementById("content"),
         token = "MwoSWmkk76mcyxSGR2Ci",