1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-20 11:28:40 +02:00
joplin/lib/models/note.js

140 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-06-24 19:06:28 +01:00
import { BaseModel } from 'lib/base-model.js';
import { Log } from 'lib/log.js';
import { Folder } from 'lib/models/folder.js';
import { GeolocationReact } from 'lib/geolocation-react.js';
import { BaseItem } from 'lib/models/base-item.js';
2017-06-11 22:11:14 +01:00
import moment from 'moment';
2017-05-10 19:21:09 +00:00
2017-06-15 19:18:48 +01:00
class Note extends BaseItem {
2017-05-10 19:21:09 +00:00
2017-05-10 19:51:43 +00:00
static tableName() {
return 'notes';
}
2017-06-18 23:06:10 +01:00
static serialize(note, type = null, shownKeys = null) {
return super.serialize(note, 'note', ["author", "longitude", "latitude", "is_todo", "todo_due", "todo_completed", 'created_time', 'updated_time', 'id', 'parent_id', 'type_']);
2017-05-12 19:54:06 +00:00
}
2017-05-18 19:58:01 +00:00
static itemType() {
2017-06-19 20:26:27 +01:00
return BaseModel.MODEL_TYPE_NOTE;
2017-05-18 19:58:01 +00:00
}
static trackChanges() {
return true;
}
2017-06-19 23:18:24 +01:00
static trackDeleted() {
return true;
}
2017-05-20 00:16:50 +02:00
static new(parentId = '') {
let output = super.new();
output.parent_id = parentId;
return output;
2017-05-10 19:51:43 +00:00
}
2017-05-24 20:51:50 +00:00
static newTodo(parentId = '') {
let output = this.new(parentId);
output.is_todo = true;
return output;
}
2017-06-25 13:49:46 +01:00
static previewFields() {
return ['id', 'title', 'body', 'is_todo', 'todo_completed', 'parent_id', 'updated_time'];
}
static previewFieldsSql() {
2017-06-25 13:49:46 +01:00
return this.db().escapeFields(this.previewFields()).join(',');
}
2017-06-27 19:48:01 +00:00
static loadFolderNoteByField(folderId, field, value) {
return this.modelSelectOne('SELECT * FROM notes WHERE is_conflict = 0 AND `parent_id` = ? AND `' + field + '` = ?', [folderId, value]);
}
2017-06-25 10:00:54 +01:00
static previews(parentId, options = null) {
if (!options) options = {};
if (!options.orderBy) options.orderBy = 'updated_time';
if (!options.orderByDir) options.orderByDir = 'DESC';
let sql = 'SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE is_conflict = 0 AND parent_id = ?';
2017-06-25 13:49:46 +01:00
let params = [parentId];
2017-06-25 10:00:54 +01:00
if (options.itemTypes && options.itemTypes.length) {
if (options.itemTypes.indexOf('note') >= 0 && options.itemTypes.indexOf('todo') >= 0) {
// Fetch everything
} else if (options.itemTypes.indexOf('note') >= 0) {
sql += ' AND is_todo = 0';
} else if (options.itemTypes.indexOf('todo') >= 0) {
sql += ' AND is_todo = 1';
}
}
2017-06-25 13:49:46 +01:00
if (options.titlePattern) {
let pattern = options.titlePattern.replace(/\*/g, '%');
sql += ' AND title LIKE ?';
params.push(pattern);
}
let query = this.applySqlOptions(options, sql, params);
2017-06-25 10:00:54 +01:00
return this.modelSelectAll(query.sql, query.params);
2017-05-11 20:14:01 +00:00
}
static preview(noteId) {
2017-06-23 22:32:24 +01:00
return this.modelSelectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE is_conflict = 0 AND id = ?', [noteId]);
2017-06-20 19:18:19 +00:00
}
2017-06-20 19:25:01 +00:00
static conflictedNotes() {
2017-06-20 19:18:19 +00:00
return this.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 1');
}
2017-05-23 19:01:37 +00:00
static updateGeolocation(noteId) {
Log.info('Updating lat/long of note ' + noteId);
2017-05-15 19:46:34 +00:00
2017-05-23 19:01:37 +00:00
let geoData = null;
2017-06-24 19:06:28 +01:00
return GeolocationReact.currentPosition().then((data) => {
2017-05-23 19:01:37 +00:00
Log.info('Got lat/long');
geoData = data;
return Note.load(noteId);
}).then((note) => {
2017-05-23 19:58:12 +00:00
if (!note) return; // Race condition - note has been deleted in the meantime
2017-05-23 19:01:37 +00:00
note.longitude = geoData.coords.longitude;
note.latitude = geoData.coords.latitude;
note.altitude = geoData.coords.altitude;
return Note.save(note);
}).catch((error) => {
Log.info('Cannot get location:', error);
});
2017-05-15 19:46:34 +00:00
}
2017-06-24 18:40:03 +01:00
static filter(note) {
if (!note) return note;
2017-06-27 00:20:01 +01:00
let output = super.filter(note);
2017-06-24 18:40:03 +01:00
if ('longitude' in output) output.longitude = Number(!output.longitude ? 0 : output.longitude).toFixed(8);
if ('latitude' in output) output.latitude = Number(!output.latitude ? 0 : output.latitude).toFixed(8);
if ('altitude' in output) output.altitude = Number(!output.altitude ? 0 : output.altitude).toFixed(4);
return output;
}
2017-06-25 16:17:40 +01:00
// static all(options = null) {
// let q = this.applySqlOptions(options, 'SELECT * FROM notes WHERE is_conflict = 0 AND parent_id = ?', [parentId]);
// return this.modelSelectAll(q.sql, q.params);
// }
2017-06-18 00:49:52 +01:00
2017-05-22 20:22:50 +00:00
static save(o, options = null) {
return super.save(o, options).then((result) => {
// 'result' could be a partial one at this point (if, for example, only one property of it was saved)
// so call this.preview() so that the right fields are populated.
2017-06-14 20:59:46 +01:00
return this.load(result.id);
}).then((note) => {
2017-05-22 20:22:50 +00:00
this.dispatch({
type: 'NOTES_UPDATE_ONE',
note: note,
});
return note;
});
}
2017-05-10 19:21:09 +00:00
}
export { Note };