be4311c07e14feb728abc6425ee606ffaa611a58
markd
  Fri Jan 22 06:46:58 2021 -0800
merge with master

diff --git src/hg/hgGateway/hgGateway.c src/hg/hgGateway/hgGateway.c
index 968ad8f..3b02cf2 100644
--- src/hg/hgGateway/hgGateway.c
+++ src/hg/hgGateway/hgGateway.c
@@ -197,34 +197,45 @@
 // cart's db was already set by magic handling of hub CGI variables sent along
 // with this command.
 char *db = cartString(cart, "db");
 if (isEmpty(db))
     jsonWriteStringf(cj->jw, "error", "No db for hubUrl '%s'", hubUrl);
 else
     writeFindPositionInfo(cj->jw, db, taxId, hubUrl, hDefaultPos(db));
 }
 
 static void setDb(struct cartJson *cj, struct hash *paramHash)
 /* Set taxId and genome according to db and return the info we'll need to fill in
  * the findPosition section. */
 {
 char *db = cartJsonRequiredParam(paramHash, "db", cj->jw, "setDb");
 char *hubUrl = cartJsonOptionalParam(paramHash, "hubUrl");
+// we want to go back to the most recent position on this db
+// so push "lastDbPos" into the cart so cartGetPosition() can find it
+char *maybePosition = cartJsonOptionalParam(paramHash, "position");
+if (maybePosition)
+    cartSetString(cart, "position", maybePosition);
 int taxId = hTaxId(db);
-writeFindPositionInfo(cj->jw, db, taxId, hubUrl, hDefaultPos(db));
+
+// look up the old position the user was browsing (if they came here from
+// a hub connection for instance) and start them there, otherwise use
+// the assembly default position
+char *maybeLastPos = cartGetPosition(cart, db, NULL);
+char *pos = maybeLastPos != NULL ? maybeLastPos : hDefaultPos(db);
+writeFindPositionInfo(cj->jw, db, taxId, hubUrl, pos);
 cartSetString(cart, "db", db);
-cartSetString(cart, "position", hDefaultPos(db));
+cartSetString(cart, "position", pos);
 }
 
 static void getUiState(struct cartJson *cj, struct hash *paramHash)
 /* Write out JSON for hgGateway.js's uiState object using current cart settings. */
 {
 char *db = cartUsualString(cj->cart, "db", hDefaultDb());
 char *position = cartUsualString(cart, "position", hDefaultPos(db));
 char *hubUrl = NULL;
 if (trackHubDatabase(db))
     {
     struct trackHub *hub = hubConnectGetHubForDb(db);
     hubUrl = hub->url;
     }
 writeFindPositionInfo(cj->jw, db, hTaxId(db), hubUrl, position);
 // If cart already has a pix setting, pass that along; otherwise the JS will