Improve note creation and deletion error handling and reliability
This commit is contained in:
parent
47d0a48e4f
commit
59b4f70a75
@ -4,177 +4,184 @@
|
|||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
class NotePostNotes extends Notes {
|
class NotePostNotes extends Notes {
|
||||||
constructor(server, username, password) {
|
constructor(server, username, password) {
|
||||||
super();
|
super();
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
|
||||||
|
|
||||||
del(noteid, success, error) {
|
|
||||||
super.del(noteid);
|
|
||||||
var self = this;
|
|
||||||
return $.ajax({
|
|
||||||
url: this.server + "/api/deletenote",
|
|
||||||
dataType: "json",
|
|
||||||
cache: false,
|
|
||||||
method: "POST",
|
|
||||||
data: {
|
|
||||||
id: noteid
|
|
||||||
},
|
|
||||||
beforeSend: function (xhr) {
|
|
||||||
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
|
||||||
},
|
|
||||||
success: function (val) {
|
|
||||||
if (val.status == "OK") {
|
|
||||||
self.notes = val.notes;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof success == 'function') {
|
|
||||||
success();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function () {
|
|
||||||
if (typeof error == 'function') {
|
|
||||||
error();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
add(note, success, error) {
|
|
||||||
note.norealid = true;
|
|
||||||
this.saveNote(note, success, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
getNote(noteid, success, error) {
|
|
||||||
return $.ajax({
|
|
||||||
url: this.server + "/api/getnote",
|
|
||||||
dataType: "json",
|
|
||||||
method: "POST",
|
|
||||||
data: {
|
|
||||||
id: noteid
|
|
||||||
},
|
|
||||||
beforeSend: function (xhr) {
|
|
||||||
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
|
||||||
},
|
|
||||||
success: function (val) {
|
|
||||||
if (val.status == "OK") {
|
|
||||||
if (typeof success == 'function') {
|
|
||||||
success(val.note);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (typeof error == 'function') {
|
|
||||||
error(val.msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function () {
|
|
||||||
if (typeof error == 'function') {
|
|
||||||
error();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
saveNote(note, success, error) {
|
|
||||||
var self = this;
|
|
||||||
var data = {
|
|
||||||
text: note.content,
|
|
||||||
color: note.color,
|
|
||||||
modified: note.modified,
|
|
||||||
favorite: note.favorite ? "1" : "0"
|
|
||||||
}; // Don't send ID if it's a locally-made note
|
|
||||||
|
|
||||||
if (note.norealid != true) {
|
|
||||||
data.id = note.noteid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $.ajax({
|
del(noteid, success, error) {
|
||||||
url: this.server + "/api/savenote",
|
super.del(noteid);
|
||||||
dataType: "json",
|
var self = this;
|
||||||
method: "POST",
|
return $.ajax({
|
||||||
data: data,
|
url: this.server + "/api/deletenote",
|
||||||
beforeSend: function (xhr) {
|
dataType: "json",
|
||||||
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
cache: false,
|
||||||
},
|
method: "POST",
|
||||||
success: function (val) {
|
data: {
|
||||||
if (val.status == "OK") {
|
id: noteid
|
||||||
if (typeof success == 'function') {
|
},
|
||||||
success(val.note);
|
beforeSend: function (xhr) {
|
||||||
}
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
||||||
} else {
|
},
|
||||||
if (typeof error == 'function') {
|
success: function (val) {
|
||||||
error();
|
if (val.status == "OK") {
|
||||||
}
|
self.notes = val.notes;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
error: function () {
|
|
||||||
if (typeof error == 'function') {
|
|
||||||
error();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (typeof success == 'function') {
|
||||||
/**
|
success();
|
||||||
* Sync notes with the NotePost server, resolving conflicts in the process.
|
}
|
||||||
*
|
},
|
||||||
* @param {function} success(notes) called when everything's synced up.
|
error: function () {
|
||||||
* @param {function} error
|
if (typeof error == 'function') {
|
||||||
* @returns {undefined}
|
error();
|
||||||
*/
|
}
|
||||||
sync(success, error) {
|
|
||||||
super.sync();
|
|
||||||
var self = this;
|
|
||||||
$.ajax({
|
|
||||||
url: this.server + "/api/getnotes",
|
|
||||||
dataType: "json",
|
|
||||||
cache: false,
|
|
||||||
method: "POST",
|
|
||||||
beforeSend: function (xhr) {
|
|
||||||
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
|
||||||
},
|
|
||||||
success: function (val) {
|
|
||||||
if (val.status == "OK") {
|
|
||||||
console.log("Comparing notes...");
|
|
||||||
console.log("Local copy:", self.notes);
|
|
||||||
console.log("Remote copy:", val.notes);
|
|
||||||
var delta = getDelta(self.notes, val.notes);
|
|
||||||
console.log("Comparison: ", delta);
|
|
||||||
var notes = delta.noChange;
|
|
||||||
notes = notes.concat(delta.addedRemote);
|
|
||||||
notes = notes.concat(delta.changedRemote); // Sync locally-created or modified notes
|
|
||||||
|
|
||||||
var notesToUpload = delta.addedLocal;
|
|
||||||
notesToUpload = notesToUpload.concat(delta.changedLocal);
|
|
||||||
var addedOrChangedLocallyAjax = [];
|
|
||||||
|
|
||||||
for (var i = 0; i < notesToUpload.length; i++) {
|
|
||||||
addedOrChangedLocallyAjax.push(self.saveNote(self.fix(notesToUpload[i]), function (n) {
|
|
||||||
notes.push(n);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
$.when(addedOrChangedLocallyAjax).then(function () {
|
|
||||||
self.notes = notes;
|
|
||||||
self.fixAll();
|
|
||||||
localStorage.setItem("notes", JSON.stringify(notes));
|
|
||||||
console.log(JSON.parse(localStorage.getItem("notes")));
|
|
||||||
|
|
||||||
if (typeof success == 'function') {
|
|
||||||
success(notes);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
add(note, success, error) {
|
||||||
|
note.norealid = true;
|
||||||
|
this.saveNote(note, success, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
getNote(noteid, success, error) {
|
||||||
|
return $.ajax({
|
||||||
|
url: this.server + "/api/getnote",
|
||||||
|
dataType: "json",
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
id: noteid
|
||||||
|
},
|
||||||
|
beforeSend: function (xhr) {
|
||||||
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
||||||
|
},
|
||||||
|
success: function (val) {
|
||||||
|
if (val.status == "OK") {
|
||||||
|
if (typeof success == 'function') {
|
||||||
|
success(val.note);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error(val.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
saveNote(note, success, error) {
|
||||||
|
var self = this;
|
||||||
|
var data = {
|
||||||
|
text: note.content,
|
||||||
|
color: note.color,
|
||||||
|
modified: note.modified,
|
||||||
|
favorite: note.favorite ? "1" : "0"
|
||||||
|
}; // Don't send ID if it's a locally-made note
|
||||||
|
|
||||||
|
if (note.norealid != true) {
|
||||||
|
data.id = note.noteid;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
error: function () {
|
return $.ajax({
|
||||||
if (typeof error == 'function') {
|
url: this.server + "/api/savenote",
|
||||||
error();
|
dataType: "json",
|
||||||
}
|
method: "POST",
|
||||||
}
|
data: data,
|
||||||
});
|
beforeSend: function (xhr) {
|
||||||
}
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
||||||
|
},
|
||||||
|
success: function (val) {
|
||||||
|
if (val.status == "OK") {
|
||||||
|
if (typeof success == 'function') {
|
||||||
|
success(val.note);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error(val.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync notes with the NotePost server, resolving conflicts in the process.
|
||||||
|
*
|
||||||
|
* @param {function} success(notes) called when everything's synced up.
|
||||||
|
* @param {function} error
|
||||||
|
* @returns {undefined}
|
||||||
|
*/
|
||||||
|
sync(success, error) {
|
||||||
|
super.sync();
|
||||||
|
var self = this;
|
||||||
|
$.ajax({
|
||||||
|
url: this.server + "/api/getnotes",
|
||||||
|
dataType: "json",
|
||||||
|
cache: false,
|
||||||
|
method: "POST",
|
||||||
|
beforeSend: function (xhr) {
|
||||||
|
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
|
||||||
|
},
|
||||||
|
success: function (val) {
|
||||||
|
if (val.status == "OK") {
|
||||||
|
console.log("Comparing notes...");
|
||||||
|
console.log("Local copy:", self.notes);
|
||||||
|
console.log("Remote copy:", val.notes);
|
||||||
|
var delta = getDelta(self.notes, val.notes);
|
||||||
|
console.log("Comparison: ", delta);
|
||||||
|
var notes = delta.noChange;
|
||||||
|
notes = notes.concat(delta.addedRemote);
|
||||||
|
notes = notes.concat(delta.changedRemote); // Sync locally-created or modified notes
|
||||||
|
|
||||||
|
var notesToUpload = delta.addedLocal;
|
||||||
|
notesToUpload = notesToUpload.concat(delta.changedLocal);
|
||||||
|
var addedOrChangedLocallyAjax = [];
|
||||||
|
|
||||||
|
for (var i = 0; i < notesToUpload.length; i++) {
|
||||||
|
addedOrChangedLocallyAjax.push(self.saveNote(self.fix(notesToUpload[i]), function (n) {
|
||||||
|
notes.push(n);
|
||||||
|
}, function (err) {
|
||||||
|
if (typeof err === "string") {
|
||||||
|
app.dialog.alert(err, "Error");
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
$.when(addedOrChangedLocallyAjax).always(function () {
|
||||||
|
self.notes = notes;
|
||||||
|
self.fixAll();
|
||||||
|
localStorage.setItem("notes", JSON.stringify(notes));
|
||||||
|
console.log(JSON.parse(localStorage.getItem("notes")));
|
||||||
|
}).then(function () {
|
||||||
|
if (typeof success == 'function') {
|
||||||
|
success(notes);
|
||||||
|
}
|
||||||
|
}, function () {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error(notes);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -137,6 +137,9 @@ class Notes {
|
|||||||
if (localStorage.getItem("notes") !== null) {
|
if (localStorage.getItem("notes") !== null) {
|
||||||
// There is localstorage
|
// There is localstorage
|
||||||
var storage = JSON.parse(localStorage.getItem("notes"));
|
var storage = JSON.parse(localStorage.getItem("notes"));
|
||||||
|
if (typeof this.notes === 'undefined') {
|
||||||
|
this.notes = [];
|
||||||
|
}
|
||||||
console.log("Memory copy:", this.notes);
|
console.log("Memory copy:", this.notes);
|
||||||
console.log("Storage copy:", storage);
|
console.log("Storage copy:", storage);
|
||||||
var delta = getDelta(this.notes, storage);
|
var delta = getDelta(this.notes, storage);
|
||||||
|
@ -13,10 +13,18 @@ function saveme(callback) {
|
|||||||
closeTimeout: 2000
|
closeTimeout: 2000
|
||||||
}).open();
|
}).open();
|
||||||
$("#orig_content").val(note.content);
|
$("#orig_content").val(note.content);
|
||||||
|
if (typeof callback == "function") {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}, function () {
|
||||||
|
app.toast.create({
|
||||||
|
text: 'Something went wrong, your note might not be synced correctly.',
|
||||||
|
closeTimeout: 10000
|
||||||
|
}).open();
|
||||||
|
if (typeof callback == "function") {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
if (typeof callback == "function") {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sync();
|
sync();
|
||||||
@ -29,6 +37,15 @@ function saveme(callback) {
|
|||||||
NOTES.add(note, function (n) {
|
NOTES.add(note, function (n) {
|
||||||
$("#note_content").data("noteid", n.noteid);
|
$("#note_content").data("noteid", n.noteid);
|
||||||
finishSave(n, callback);
|
finishSave(n, callback);
|
||||||
|
}, function (err) {
|
||||||
|
if (typeof err == "string") {
|
||||||
|
app.dialog.alert(err, "Error");
|
||||||
|
} else {
|
||||||
|
app.dialog.alert("An unknown error occurred.", "Error");
|
||||||
|
}
|
||||||
|
if (typeof callback == "function") {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var note = NOTES.get(noteid);
|
var note = NOTES.get(noteid);
|
||||||
@ -39,17 +56,6 @@ function saveme(callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function exiteditor() {
|
|
||||||
sync();
|
|
||||||
if ($("#note_content").val() == "" || $("#note_content").val() === $("#orig_content").val()) {
|
|
||||||
router.back({force: true, ignoreCache: true, reload: true});
|
|
||||||
} else {
|
|
||||||
saveme(function () {
|
|
||||||
router.back({force: true, ignoreCache: true, reload: true});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
document.getElementById("noteframe").contentWindow.initEditor($("#note_content").val());
|
document.getElementById("noteframe").contentWindow.initEditor($("#note_content").val());
|
||||||
}
|
}
|
||||||
@ -62,6 +68,17 @@ function sync() {
|
|||||||
$("#note_content").val(document.getElementById("noteframe").contentWindow.getMarkdown());
|
$("#note_content").val(document.getElementById("noteframe").contentWindow.getMarkdown());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function exiteditor() {
|
||||||
|
sync();
|
||||||
|
if ($("#note_content").val() == "" || $("#note_content").val() === $("#orig_content").val()) {
|
||||||
|
router.back({force: true, ignoreCache: true, reload: true});
|
||||||
|
} else {
|
||||||
|
saveme(function () {
|
||||||
|
router.back({force: true, ignoreCache: true, reload: true});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$("#noteframe").on("load", function () {
|
$("#noteframe").on("load", function () {
|
||||||
init();
|
init();
|
||||||
});
|
});
|
@ -70,6 +70,7 @@ function deleteNote(id) {
|
|||||||
app.dialog.confirm('Are you sure?', 'Delete Note', function () {
|
app.dialog.confirm('Are you sure?', 'Delete Note', function () {
|
||||||
NOTES.del(id, function () {
|
NOTES.del(id, function () {
|
||||||
window.shuffleInstance.remove(document.getElementById("notecard-" + id));
|
window.shuffleInstance.remove(document.getElementById("notecard-" + id));
|
||||||
|
loadCards();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user