|
|
|
/*
|
|
|
|
* 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 Note {
|
|
|
|
|
|
|
|
constructor(content, color, noteid) {
|
|
|
|
if (typeof content != 'string') {
|
|
|
|
content = "";
|
|
|
|
}
|
|
|
|
if (typeof color != 'string') {
|
|
|
|
color = "FFF59D";
|
|
|
|
}
|
|
|
|
if (typeof noteid != 'number') {
|
|
|
|
noteid = null;
|
|
|
|
}
|
|
|
|
this.noteid = noteid;
|
|
|
|
this.color = color;
|
|
|
|
this.content = content;
|
|
|
|
this.title = "";
|
|
|
|
this.modified = null;
|
|
|
|
this.favorite = false;
|
|
|
|
this.status = "NONE";
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadNote(noteid) {
|
|
|
|
var notes = JSON.parse(localStorage.getItem("notes"));
|
|
|
|
if (notes == null) {
|
|
|
|
console.log("localStorage.notes is null");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (var n in notes) {
|
|
|
|
if (notes[n].noteid == noteid) {
|
|
|
|
var note = new Note(notes[n].content, notes[n].color, notes[n].noteid);
|
|
|
|
note.setModified(notes[n].modified);
|
|
|
|
note.setFavorite(notes[n].favorite == true);
|
|
|
|
note.setSyncStatus(notes[n].syncstatus);
|
|
|
|
note.setTitle();
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveNote() {
|
|
|
|
var noteid = this.getID();
|
|
|
|
console.log("Saving note: ", this);
|
|
|
|
var syncstatus = this.getSyncStatus();
|
|
|
|
if (noteid == null) {
|
|
|
|
noteid = Math.floor(Math.random() * (9999999999 - 1000000000) + 1000000000);
|
|
|
|
syncstatus = "LOCAL_ONLY";
|
|
|
|
}
|
|
|
|
var jsonobj = {
|
|
|
|
noteid: noteid,
|
|
|
|
color: this.getColor(),
|
|
|
|
content: this.getText(),
|
|
|
|
modified: this.getModified(),
|
|
|
|
favorite: this.getFavorite(),
|
|
|
|
syncstatus: syncstatus
|
|
|
|
};
|
|
|
|
|
|
|
|
var notes = JSON.parse(localStorage.getItem("notes"));
|
|
|
|
if (notes == null) {
|
|
|
|
console.log("localStorage.notes is null, using empty array");
|
|
|
|
notes = [];
|
|
|
|
}
|
|
|
|
for (var n in notes) {
|
|
|
|
if (notes[n].noteid == noteid) {
|
|
|
|
if (notes[n].syncstatus == "LOCAL_DELETED") {
|
|
|
|
// If this note was previously saved as deleted, don't save again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
notes[n] = jsonobj;
|
|
|
|
console.log(notes);
|
|
|
|
localStorage.setItem("notes", JSON.stringify(notes));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notes.push(jsonobj);
|
|
|
|
console.log(notes);
|
|
|
|
localStorage.setItem("notes", JSON.stringify(notes));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the note to NotePost.
|
|
|
|
*/
|
|
|
|
saveToNotePost(success, error, add) {
|
|
|
|
var self = this;
|
|
|
|
var data = {
|
|
|
|
text: this.getText(),
|
|
|
|
color: this.getColor(),
|
|
|
|
modified: this.getModified(),
|
|
|
|
favorite: this.getFavorite() ? "1" : "0",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof add != 'boolean' || add != true) {
|
|
|
|
data.id = this.getID();
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("uploading: ", data);
|
|
|
|
|
|
|
|
return APICLIENT.post("savenote", data, function (val) {
|
|
|
|
if (val.status == "OK") {
|
|
|
|
self.noteid = val.note.noteid;
|
|
|
|
self.setText(val.note.content);
|
|
|
|
self.setColor(val.note.color);
|
|
|
|
self.setModified(val.note.modified);
|
|
|
|
self.setFavorite(val.note.favorite);
|
|
|
|
self.setSyncStatus("SYNCED");
|
|
|
|
if (typeof success == 'function') {
|
|
|
|
success(self);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeof error == 'function') {
|
|
|
|
error(val.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function () {
|
|
|
|
if (typeof error == 'function') {
|
|
|
|
error();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete this note on the server.
|
|
|
|
*/
|
|
|
|
deleteOnNotePost(success, error) {
|
|
|
|
var self = this;
|
|
|
|
return APICLIENT.post("deletenote", {id: this.getID()}, function (val) {
|
|
|
|
if (val.status == "OK") {
|
|
|
|
if (typeof success == 'function') {
|
|
|
|
success(self);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeof error == 'function') {
|
|
|
|
error(val.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function () {
|
|
|
|
if (typeof error == 'function') {
|
|
|
|
error();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteme() {
|
|
|
|
this.setSyncStatus("LOCAL_DELETED");
|
|
|
|
this.saveNote();
|
|
|
|
}
|
|
|
|
|
|
|
|
getSyncStatus() {
|
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
getID() {
|
|
|
|
return this.noteid;
|
|
|
|
}
|
|
|
|
|
|
|
|
getText() {
|
|
|
|
return this.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
getHTML() {
|
|
|
|
return marked(this.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
getColor() {
|
|
|
|
this.setColor(this.color);
|
|
|
|
return this.color;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTitle() {
|
|
|
|
this.setTitle(this.title);
|
|
|
|
return this.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
getModified() {
|
|
|
|
return this.modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFavorite() {
|
|
|
|
return this.favorite == true;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTextColor() {
|
|
|
|
var color = this.getColor();
|
|
|
|
var r = parseInt(color.substring(0, 2), 16);
|
|
|
|
var g = parseInt(color.substring(2, 4), 16);
|
|
|
|
var b = parseInt(color.substring(4, 6), 16);
|
|
|
|
var contrast = Math.sqrt(r * r * 0.241 + g * g * 0.691 + b * b * 0.068);
|
|
|
|
|
|
|
|
if (contrast > 130) {
|
|
|
|
return "000000";
|
|
|
|
} else {
|
|
|
|
return "FFFFFF";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSyncStatus(status) {
|
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
setText(markdown) {
|
|
|
|
this.content = markdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
setColor(color) {
|
|
|
|
if (typeof color !== 'string') {
|
|
|
|
color = "FFF59D";
|
|
|
|
}
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTitle(title) {
|
|
|
|
if (typeof title != 'string' || title.trim() == "") {
|
|
|
|
if (typeof this.content != 'string' || this.content.trim() == "") {
|
|
|
|
title = "New note";
|
|
|
|
} else {
|
|
|
|
title = this.content.split('\n')[0];
|
|
|
|
title = title.replace(/[\*#_`]/, "");
|
|
|
|
title = title.replace(/^- \[[x ]\] /i, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.title = title.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
setModified(timestamp) {
|
|
|
|
if (typeof timestamp == 'undefined' || timestamp == null) {
|
|
|
|
this.setModified(Math.round(new Date().getTime() / 1000));
|
|
|
|
} else {
|
|
|
|
this.modified = timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setFavorite(favorite) {
|
|
|
|
this.favorite = favorite == true;
|
|
|
|
}
|
|
|
|
|
|
|
|
toChecklist() {
|
|
|
|
var text = this.getText().split('\n');
|
|
|
|
for (var i = 0; i < text.length; i++) {
|
|
|
|
if (text[i].match(/^[^\s\=\#\-<](.+)/)) {
|
|
|
|
// if (text.length > i && text[i + 1].match(/^[\=-](.+)/)) {
|
|
|
|
// // Don't do it if the next line makes this one a heading
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
text[i] = "- [ ] " + text[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setText(text.join("\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
fromChecklist() {
|
|
|
|
var text = this.getText();
|
|
|
|
this.setText(text.replace(/^- \[[ x]\] /im, ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleChecklistItem(item) {
|
|
|
|
item = item.trim();
|
|
|
|
var text = this.getText().split("\n");
|
|
|
|
var finalCheckState = false;
|
|
|
|
for (var i in text) {
|
|
|
|
var li = text[i].trim();
|
|
|
|
if (!li.match(/^- \[[x ]\] .*/i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var linecleaned = li.replace(/^- \[[x ]\] /i, '').trim();
|
|
|
|
if (item != linecleaned) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (li.match(/^- \[[x]\] .*/i)) {
|
|
|
|
text[i] = li.replace(/^- \[[x]\] /i, "- [ ] ");
|
|
|
|
finalCheckState = false;
|
|
|
|
} else if (li.match(/^- \[[ ]\] .*/i)) {
|
|
|
|
text[i] = li.replace(/^- \[[ ]\] /i, "- [x] ");
|
|
|
|
finalCheckState = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setText(text.join("\n"));
|
|
|
|
return finalCheckState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the notes appear to be the same to the user.
|
|
|
|
* Does not check "hidden" attributes such as modified time.
|
|
|
|
* @param {Note} othernote
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
compareTo(othernote) {
|
|
|
|
if (this.getText() != othernote.getText()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.getFavorite() != othernote.getFavorite()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.getColor() != othernote.getColor()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|