Fix navigating back to notebooks screen

This commit is contained in:
Trevor Slocum 2019-04-25 05:15:16 -07:00
parent 81023930fd
commit 76f6c7893d
1 changed files with 48 additions and 37 deletions

View File

@ -19,6 +19,8 @@ var NoteCheckbox = 0;
var NoteCheckingItem = false;
var NoteHideChecked = true;
var reconnectTimeout;
var db;
var regexpListItem = /^([\s]+)?[\-*#](?![^\S\r\n]+[\-*#])(([^\S\r\n]+\[[ xX]])|[^\S\r\n])/g;
var markdownRenderer = new marked.Renderer();
@ -124,6 +126,7 @@ function ResumeStick() {
}
function ConnectStick() {
reconnectTimeout = null;
if (webSocketReady() || ReconnectDelay === -1) {
return;
}
@ -142,6 +145,14 @@ function ConnectStick() {
wsurl += "//" + loc.host + pathname + "w/" + AuthKey + "/" + NotebooksModified;
socket = new WebSocket(wsurl);
socket.onerror = function (e) {
console.log(e);
};
socket.onopen = function (e) {
if (reconnectTimeout != null) {
clearTimeout(reconnectTimeout);
}
};
socket.onmessage = function (e) {
if (ReconnectDelay > 0) {
ReconnectDelay = 0;
@ -192,10 +203,9 @@ function ConnectStick() {
var mustNavigate = false;
if (NewNoteMode !== "") {
mustNavigate = true;
if (NewNoteMode === "view" && NoteMode === "edit") {
mustRender = true;
} else {
mustNavigate = true;
}
NoteMode = NewNoteMode;
@ -207,16 +217,6 @@ function ConnectStick() {
NewViewNote = "";
}
if (mustNavigate) {
if (ViewNotebook === "") {
window.location.hash = '#';
} else {
window.location.hash = '#' + NoteMode + '/' + ViewNotebook + (ViewNote !== '' ? ('/' + ViewNote) : '');
}
return;
}
if (ViewNotebook === "" && ViewNote === "" && Object.keys(data.notebooks).length > 0) {
mustRender = true;
}
@ -224,6 +224,20 @@ function ConnectStick() {
if (mustRender) {
render();
}
if (mustNavigate) {
if (ViewNote === "") {
if (history.length > 2) {
history.go(-(history.length - 2));
} else {
window.location.hash = '#';
}
} else {
window.location.hash = '#' + NoteMode + '/' + ViewNotebook + (ViewNote !== '' ? ('/' + ViewNote) : '');
}
return;
}
}
} catch (e) {
console.log(e);
@ -231,22 +245,19 @@ function ConnectStick() {
NoteCheckingItem = false;
};
socket.onerror = function (e) {
console.log(e);
};
socket.onclose = function (e) {
if (ReconnectDelay < 0) {
if (ReconnectDelay < 0 || reconnectTimeout != null) {
return;
}
var waitTime = ReconnectDelay;
console.log("Reconnecting in " + ReconnectDelay + " seconds...");
reconnectTimeout = setTimeout(ConnectStick, waitTime * 1000)
ReconnectDelay += (ReconnectDelay * 2) + 1;
if (ReconnectDelay > 10) {
ReconnectDelay = 10;
}
setTimeout(ConnectStick, waitTime * 1000)
};
}
@ -560,6 +571,15 @@ function bindEditor() {
});
}
function setAuthKey(newAuthKey) {
AuthKey = newAuthKey;
db.transaction('rw', db.credentials, async () => {
db.credentials.clear();
db.credentials.add({authkey: AuthKey});
});
}
function processHash() {
if (window.location.hash.substring(0, 7) !== '#login/' && !webSocketReady()) {
waitForSocketConnection(socket, function () {
@ -574,26 +594,17 @@ function processHash() {
if (window.location.hash.substring(0, 7) === '#login/') {
var newAuthKey = window.location.hash.substring(7);
if (newAuthKey !== AuthKey) {
AuthKey = newAuthKey;
db.transaction('rw', db.credentials, async () => {
db.credentials.clear();
db.credentials.add({authkey: AuthKey});
}).then(function () {
Notebooks = {};
NotebooksModified = 0;
NoteMode = "view";
ViewNotebook = "";
ViewNote = "";
render();
PauseStick();
ResumeStick();
setAuthKey(newAuthKey);
history.replaceState({}, "stick", "#");
processHash();
});
} else {
history.replaceState({}, "stick", "#");
processHash();
// Reset state
Notebooks = {};
NotebooksModified = 0;
NoteMode = "view";
ViewNotebook = "";
ViewNote = "";
render();
PauseStick();
ResumeStick();
}
} else if (window.location.hash.substring(0, 6) === '#view/' || window.location.hash.substring(0, 6) === '#edit/') {
var v = window.location.hash.substring(6).split('/');