|
|
|
@ -4,177 +4,184 @@
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
class NotePostNotes extends Notes {
|
|
|
|
|
constructor(server, username, password) {
|
|
|
|
|
super();
|
|
|
|
|
this.server = server;
|
|
|
|
|
this.username = username;
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
constructor(server, username, password) {
|
|
|
|
|
super();
|
|
|
|
|
this.server = server;
|
|
|
|
|
this.username = username;
|
|
|
|
|
this.password = password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(note, success, error) {
|
|
|
|
|
note.norealid = true;
|
|
|
|
|
this.saveNote(note, success, error);
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (typeof success == 'function') {
|
|
|
|
|
success();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
add(note, success, error) {
|
|
|
|
|
note.norealid = true;
|
|
|
|
|
this.saveNote(note, success, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (note.norealid != true) {
|
|
|
|
|
data.id = note.noteid;
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $.ajax({
|
|
|
|
|
url: this.server + "/api/savenote",
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
return $.ajax({
|
|
|
|
|
url: this.server + "/api/savenote",
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var notesToUpload = delta.addedLocal;
|
|
|
|
|
notesToUpload = notesToUpload.concat(delta.changedLocal);
|
|
|
|
|
var addedOrChangedLocallyAjax = [];
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < notesToUpload.length; i++) {
|
|
|
|
|
addedOrChangedLocallyAjax.push(self.saveNote(self.fix(notesToUpload[i]), function (n) {
|
|
|
|
|
notes.push(n);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
var notesToUpload = delta.addedLocal;
|
|
|
|
|
notesToUpload = notesToUpload.concat(delta.changedLocal);
|
|
|
|
|
var addedOrChangedLocallyAjax = [];
|
|
|
|
|
|
|
|
|
|
$.when(addedOrChangedLocallyAjax).then(function () {
|
|
|
|
|
self.notes = notes;
|
|
|
|
|
self.fixAll();
|
|
|
|
|
localStorage.setItem("notes", JSON.stringify(notes));
|
|
|
|
|
console.log(JSON.parse(localStorage.getItem("notes")));
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof success == 'function') {
|
|
|
|
|
success(notes);
|
|
|
|
|
$.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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
error: function () {
|
|
|
|
|
if (typeof error == 'function') {
|
|
|
|
|
error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|