1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/src/models/note.js

88 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-05-10 21:21:09 +02:00
import { BaseModel } from 'src/base-model.js';
2017-05-11 22:14:01 +02:00
import { Log } from 'src/log.js';
2017-06-11 23:11:14 +02:00
import { Folder } from 'src/models/folder.js';
2017-05-22 22:22:50 +02:00
import { Geolocation } from 'src/geolocation.js';
2017-06-11 23:11:14 +02:00
import { folderItemFilename } from 'src/string-utils.js'
2017-06-15 20:18:48 +02:00
import { BaseItem } from 'src/models/base-item.js';
2017-06-11 23:11:14 +02:00
import moment from 'moment';
2017-05-10 21:21:09 +02:00
2017-06-15 20:18:48 +02:00
class Note extends BaseItem {
2017-05-10 21:21:09 +02:00
2017-05-10 21:51:43 +02:00
static tableName() {
return 'notes';
}
2017-06-15 20:18:48 +02:00
static toFriendlyString(note, type = null, shownKeys = null) {
2017-06-17 20:40:08 +02:00
return super.toFriendlyString(note, 'note', ["author", "longitude", "latitude", "is_todo", "todo_due", "todo_completed", 'created_time', 'updated_time', 'id', 'parent_id', 'type_']);
2017-05-12 21:54:06 +02:00
}
2017-05-18 21:58:01 +02:00
static itemType() {
return BaseModel.ITEM_TYPE_NOTE;
}
static trackChanges() {
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 21:51:43 +02:00
}
2017-05-24 22:51:50 +02:00
static newTodo(parentId = '') {
let output = this.new(parentId);
output.is_todo = true;
return output;
}
static previewFieldsSql() {
return '`id`, `title`, `body`, `is_todo`, `todo_completed`, `parent_id`, `updated_time`'
}
2017-05-15 21:46:34 +02:00
static previews(parentId) {
2017-06-17 20:12:09 +02:00
return this.modelSelectAll('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE parent_id = ?', [parentId]);
//return this.db().selectAll('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE parent_id = ?', [parentId]);
2017-05-11 22:14:01 +02:00
}
static preview(noteId) {
2017-06-17 20:12:09 +02:00
return this.modelSelectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE id = ?', [noteId]);
//return this.db().selectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE id = ?', [noteId]);
}
2017-05-23 21:01:37 +02:00
static updateGeolocation(noteId) {
Log.info('Updating lat/long of note ' + noteId);
2017-05-15 21:46:34 +02:00
2017-05-23 21:01:37 +02:00
let geoData = null;
return Geolocation.currentPosition().then((data) => {
Log.info('Got lat/long');
geoData = data;
return Note.load(noteId);
}).then((note) => {
2017-05-23 21:58:12 +02:00
if (!note) return; // Race condition - note has been deleted in the meantime
2017-05-23 21:01:37 +02: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 21:46:34 +02:00
}
2017-05-22 22:22:50 +02: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 21:59:46 +02:00
return this.load(result.id);
}).then((note) => {
2017-05-22 22:22:50 +02:00
this.dispatch({
type: 'NOTES_UPDATE_ONE',
note: note,
});
return note;
});
}
2017-05-10 21:21:09 +02:00
}
export { Note };