You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
NotePostApp/www/js/NotePostNotes.class.js

114 lines
3.1 KiB
JavaScript

/*
* 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 NotePostNotes extends Notes {
constructor(server, username, password) {
super();
this.server = server;
this.username = username;
this.password = password;
}
del(noteid, callback) {
super.del(noteid);
var self = this;
$.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 callback == 'function') {
callback();
}
}
});
}
add(note, callback) {
note.norealid = true;
super.add(note, callback);
}
fix(note) {
super.fix(note);
note.id = note.noteid;
this.set(note);
}
load(callback) {
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") {
self.notes = val.notes;
}
if (typeof callback == 'function') {
callback();
}
}
});
}
saveNote(note, callback) {
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.id;
}
$.ajax({
url: this.server + "/api/savenote",
dataType: "json",
method: "POST",
data: data,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(self.username + ":" + self.password));
}
}).always(function () {
if (typeof callback == 'function') {
callback();
}
});
}
save(callback) {
this.fixAll();
super.save();
var ajaxcalls = [];
for (var i = 0; i < this.notes.length; i++) {
ajaxcalls.push(this.saveNote(this.notes[i]));
}
$.when(ajaxcalls).always(function () {
if (typeof callback == 'function') {
callback();
}
});
}
}