a7a5949636596841e4dcef2e7682e976de84addf
chmalee
  Wed Jan 8 12:24:26 2025 -0800
Make back button work when doing an upload, clicking to view a file and then immediately hitting web browser back button, refs #31058

diff --git src/hg/js/hgMyData.js src/hg/js/hgMyData.js
index 3342e9c..53c271f 100644
--- src/hg/js/hgMyData.js
+++ src/hg/js/hgMyData.js
@@ -396,30 +396,31 @@
         if (!(req.fullPath in filesHash)) {
             if ($.fn.dataTable.isDataTable("#filesTable")) {
                 let table = $("#filesTable").DataTable();
                 let rowObj = table.row.add(req);
                 rowVis[req.fullPath] = true;
                 table.search.fixed("defaultView", function(searchStr, data, rowIx) {
                     return rowVis[data.fileName] || rowVis[data.fullPath];
                 }).draw();
                 indentActionButton(rowObj);
                 let newRow = rowObj.node();
                 $(newRow).css('color','red').animate({color: 'black'}, 1500);
             } else {
                 showExistingFiles([req]);
             }
             filesHash[req.fullPath] = req;
+            uiState.fileList.push(req);
         }
     }
 
     function doRowSelect(ev, table, indexes) {
         let row = table.row(indexes);
         let rowTr = row.node();
         let rowCheckbox = rowTr.childNodes[0].firstChild;
         if (rowTr.classList.contains("parentRow")) {
             // we need to manually check the children
             table.rows((idx,rowData) => rowData.fullPath.startsWith(row.data().fullPath) && rowData.parentDir === row.data().fileName).every(function(rowIdx, tableLoop, rowLoop) {
                 if (ev.type === "select") {
                     this.select();
                 } else {
                     this.deselect();
                 }
@@ -603,64 +604,54 @@
         table.on("select", function(e, dt, type, indexes) {
             doRowSelect(e, dt, indexes);
         });
         table.on("deselect", function(e, dt, type, indexes) {
             doRowSelect(e, dt, indexes);
         });
         _.each(d, function(f) {
             filesHash[f.fullPath] = f;
         });
         return table;
     }
 
     function init() {
         cart.setCgi('hgMyData');
         cart.debug(debugCartJson);
-
-        if (typeof cartJson !== "undefined") {
-            if (typeof cartJson.warning !== "undefined") {
-                alert("Warning: " + cartJson.warning);
-            }
-            var urlParts = {};
-            if (debugCartJson) {
-                console.log('from server:\n', cartJson);
-            }
-            _.assign(uiState,cartJson);
-            saveHistory(cartJson, urlParts, true);
-        } else {
-            // no cartJson object means we are coming to the page for the first time:
         // TODO: write functions for
         //     creating default trackDbs
         //     editing trackDbs
         let fileDiv = document.getElementById('filesDiv');
-            if (typeof userFiles !== 'undefined' && Object.keys(userFiles).length > 0) {
+        // get the state from the history stack if it exists
+        if (history.state) {
+            uiState = history.state;
+        } else if (typeof userFiles !== 'undefined' && Object.keys(userFiles).length > 0) {
             uiState.fileList = userFiles.fileList;
             uiState.hubList = userFiles.hubList;
             uiState.userUrl = userFiles.userUrl;
         }
         // first add the top level directories/files
         let table = showExistingFiles(uiState.fileList);
         // TODO: add event handlers for editing defaults, grouping into hub
-        }
         $("#newTrackHubDialog").dialog({
             modal: true,
             autoOpen: false,
             title: "Create new track hub",
             closeOnEscape: true,
             minWidth: 400,
             minHeight: 120
         });
+
         // create a custom uppy plugin to batch change the type and db fields
         class BatchChangePlugin extends Uppy.BasePlugin {
             constructor(uppy, opts) {
                 super(uppy, opts);
                 this.id = "BatchChangePlugin";
                 this.type = "progressindicator";
                 this.opts = opts;
             }
 
             createOptsForSelect(select, opts) {
                 opts.forEach( (opt) => {
                     let option = document.createElement("option");
                     option.value = opt.value;
                     option.label = opt.label;
                     option.id = opt.id;
@@ -915,20 +906,24 @@
             };
             parentDirObj = {
                 "uploadTime": now.toLocaleString(),
                 "lastModified": d.toLocaleString(),
                 "fileName": metadata.parentDir,
                 "fileSize": 0,
                 "fileType": "dir",
                 "genome": metadata.genome,
                 "parentDir": "",
                 "fullPath": metadata.parentDir + "/",
             };
             addNewUploadedFileToTable(parentDirObj);
             addNewUploadedFileToTable(hubTxtObj);
             addNewUploadedFileToTable(newReqObj);
         });
+        uppy.on('complete', (result) => {
+            history.replaceState(uiState, "", document.location.href);
+            console.log("replace history with uiState");
+        });
     }
     return { init: init,
              uiState: uiState,
            };
 }());