|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Notes {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.notes = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
get(noteid) {
|
|
|
|
this.loadAll();
|
|
|
|
for (var n in this.notes) {
|
|
|
|
if (this.notes[n].getID() == noteid) {
|
|
|
|
return this.notes[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAll() {
|
|
|
|
this.loadAll();
|
|
|
|
return this.notes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a Note into this Notes object, and save to localStorage.
|
|
|
|
* @param {type} note
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
set(note) {
|
|
|
|
for (var n in this.notes) {
|
|
|
|
if (this.notes[n].getID() == note.getID()) {
|
|
|
|
this.notes[n] = note;
|
|
|
|
this.saveAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.notes.push(note);
|
|
|
|
this.saveAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the notes from localStorage.
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
loadAll() {
|
|
|
|
var thisnotes = [];
|
|
|
|
var notes = JSON.parse(localStorage.getItem("notes"));
|
|
|
|
console.log("notes JSON:", notes);
|
|
|
|
if (notes == null) {
|
|
|
|
notes = [];
|
|
|
|
}
|
|
|
|
for (var n in notes) {
|
|
|
|
var note = Note.loadNote(notes[n].noteid);
|
|
|
|
if (note != null) {
|
|
|
|
thisnotes.push(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.notes = thisnotes;
|
|
|
|
console.log("Loading all notes: ", this.notes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save all notes to localStorage.
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
saveAll() {
|
|
|
|
console.log("Saving all notes: ", this.notes);
|
|
|
|
localStorage.setItem("notes", "[]");
|
|
|
|
for (var n in this.notes) {
|
|
|
|
this.notes[n].saveNote();
|
|
|
|
}
|
|
|
|
this.loadAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync all notes to the remote and callback with the latest array of notes
|
|
|
|
* @param {function} callback
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
syncAll(success, error) {
|
|
|
|
this.loadAll();
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
APICLIENT.post(
|
|
|
|
"getnotes",
|
|
|
|
{},
|
|
|
|
function (val) {
|
|
|
|
if (val.status == "OK") {
|
|
|
|
var localnotes = self.notes;
|
|
|
|
var remotenotes = [];
|
|
|
|
|
|
|
|
for (var n in val.notes) {
|
|
|
|
var no = new Note(val.notes[n].content, val.notes[n].color, val.notes[n].noteid);
|
|
|
|
no.setModified(val.notes[n].modified);
|
|
|
|
no.setFavorite(val.notes[n].favorite);
|
|
|
|
remotenotes.push(no);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasNoteID(array, id) {
|
|
|
|
for (var n in array) {
|
|
|
|
if (array[n].getID() == id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNoteByID(array, id) {
|
|
|
|
for (var n in array) {
|
|
|
|
if (array[n].getID() == id) {
|
|
|
|
return array[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("localnotes:", localnotes);
|
|
|
|
console.log("remotenotes:", remotenotes);
|
|
|
|
|
|
|
|
// List of notes to delete on the server
|
|
|
|
var notesToDeleteRemote = [];
|
|
|
|
// List of notes to add on the server
|
|
|
|
var notesToAddRemote = [];
|
|
|
|
// List of notes to save/update on the server
|
|
|
|
var notesToSaveRemote = [];
|
|
|
|
// List of notes to save/update locally
|
|
|
|
var notesToSaveLocal = [];
|
|
|
|
|
|
|
|
// List of notes after syncing, should match on both sides
|
|
|
|
var notesListFinal = [];
|
|
|
|
|
|
|
|
for (var i in localnotes) {
|
|
|
|
var note = localnotes[i];
|
|
|
|
switch (note.getSyncStatus()) {
|
|
|
|
case "LOCAL_DELETED":
|
|
|
|
case "DELETED":
|
|
|
|
if (hasNoteID(remotenotes, note.getID())) {
|
|
|
|
notesToDeleteRemote.push(note);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "LOCAL_MODIFIED":
|
|
|
|
default:
|
|
|
|
if (hasNoteID(remotenotes, note.getID())) {
|
|
|
|
// The note exists remotely too
|
|
|
|
var remnote = getNoteByID(remotenotes, note.getID());
|
|
|
|
if (remnote.getModified() > note.getModified()) {
|
|
|
|
// Remote note is newer, keep it
|
|
|
|
notesToSaveLocal.push(remnote);
|
|
|
|
} else if (remnote.getModified() < note.getModified()) {
|
|
|
|
// Local note is newer, push it
|
|
|
|
console.log("remote note that's being overwritten: ", remnote);
|
|
|
|
console.log("local note that's overwriting it: ", note);
|
|
|
|
notesToSaveRemote.push(note);
|
|
|
|
} else {
|
|
|
|
if (note.compareTo(remnote) != true) {
|
|
|
|
// They aren't the same, let's trust the server
|
|
|
|
notesToSaveLocal.push(remnote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (note.getSyncStatus() != "NONE") {
|
|
|
|
notesToAddRemote.push(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i in remotenotes) {
|
|
|
|
var note = remotenotes[i];
|
|
|
|
if (!hasNoteID(localnotes, note.getID())) {
|
|
|
|
// The note is only on the remote, save it locally
|
|
|
|
notesToSaveLocal.push(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log("notesToDeleteRemote", notesToDeleteRemote);
|
|
|
|
console.log("notesToAddRemote", notesToAddRemote);
|
|
|
|
console.log("notesToSaveRemote", notesToSaveRemote);
|
|
|
|
console.log("notesToSaveLocal", notesToSaveLocal);
|
|
|
|
|
|
|
|
|
|
|
|
// Save notes locally
|
|
|
|
for (var i in notesToSaveLocal) {
|
|
|
|
notesListFinal.push(notesToSaveLocal[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("final before ajax: ", notesListFinal);
|
|
|
|
|
|
|
|
var ajaxOps = [];
|
|
|
|
|
|
|
|
// Delete notes on server
|
|
|
|
for (var i in notesToDeleteRemote) {
|
|
|
|
ajaxOps.push(notesToDeleteRemote[i].deleteOnNotePost(function (n) {
|
|
|
|
console.log("NotePost sync delete: ", n);
|
|
|
|
}, function (err) {
|
|
|
|
if (typeof err === "string") {
|
|
|
|
app.dialog.alert(err, "Error");
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add notes on server
|
|
|
|
for (var i in notesToAddRemote) {
|
|
|
|
ajaxOps.push(notesToAddRemote[i].saveToNotePost(function (n) {
|
|
|
|
notesListFinal.push(n);
|
|
|
|
console.log("NotePost sync add: ", n);
|
|
|
|
}, function (err) {
|
|
|
|
notesListFinal.push(notesToAddRemote[i]);
|
|
|
|
if (typeof err === "string") {
|
|
|
|
app.dialog.alert(err, "Error");
|
|
|
|
}
|
|
|
|
}, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save notes on server
|
|
|
|
for (var i in notesToSaveRemote) {
|
|
|
|
ajaxOps.push(notesToSaveRemote[i].saveToNotePost(function (n) {
|
|
|
|
notesListFinal.push(n);
|
|
|
|
console.log("NotePost sync save: ", n);
|
|
|
|
}, function (err) {
|
|
|
|
notesListFinal.push(notesToSaveRemote[i]);
|
|
|
|
if (typeof err === "string") {
|
|
|
|
app.dialog.alert(err, "Error");
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
$.when(...ajaxOps).always(function () {
|
|
|
|
// success
|
|
|
|
console.log("final after sync: ", notesListFinal);
|
|
|
|
self.notes = notesListFinal;
|
|
|
|
self.saveAll();
|
|
|
|
self.loadAll();
|
|
|
|
if (typeof success == 'function') {
|
|
|
|
success(self.notes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
if (typeof error == 'function') {
|
|
|
|
error(self.notes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|