2017-11-05 02:49:23 +02:00
|
|
|
const { reg } = require('lib/registry.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
|
|
const Note = require('lib/models/Note.js');
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
const shared = {};
|
|
|
|
|
|
|
|
shared.noteExists = async function(noteId) {
|
|
|
|
const existingNote = await Note.load(noteId);
|
|
|
|
return !!existingNote;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared.saveNoteButton_press = async function(comp) {
|
|
|
|
let note = Object.assign({}, comp.state.note);
|
|
|
|
|
2017-12-01 02:00:18 +02:00
|
|
|
// Note has been deleted while user was modifying it. In that case, we
|
2017-11-05 02:49:23 +02:00
|
|
|
// just save a new note by clearing the note ID.
|
|
|
|
if (note.id && !(await shared.noteExists(note.id))) delete note.id;
|
|
|
|
|
|
|
|
if (!note.parent_id) {
|
|
|
|
let folder = await Folder.defaultFolder();
|
2018-01-11 22:05:34 +02:00
|
|
|
if (!folder) return;
|
2017-11-05 02:49:23 +02:00
|
|
|
note.parent_id = folder.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
let isNew = !note.id;
|
|
|
|
|
2018-01-12 21:58:01 +02:00
|
|
|
let options = { userSideValidation: true };
|
2017-11-30 02:25:52 +02:00
|
|
|
if (!isNew) {
|
2017-12-14 22:21:36 +02:00
|
|
|
options.fields = BaseModel.diffObjectsFields(comp.state.lastSavedNote, note);
|
2017-11-30 02:25:52 +02:00
|
|
|
}
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2018-01-30 23:36:54 +02:00
|
|
|
|
2018-01-12 21:58:01 +02:00
|
|
|
const hasAutoTitle = comp.state.newAndNoTitleChangeNoteId || (isNew && !note.title);
|
|
|
|
if (hasAutoTitle) {
|
|
|
|
note.title = Note.defaultTitle(note);
|
|
|
|
if (options.fields && options.fields.indexOf('title') < 0) options.fields.push('title');
|
|
|
|
}
|
|
|
|
|
|
|
|
const savedNote = ('fields' in options) && !options.fields.length ? Object.assign({}, note) : await Note.save(note, options);
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2017-12-01 02:00:18 +02:00
|
|
|
const stateNote = comp.state.note;
|
2018-01-12 21:58:01 +02:00
|
|
|
|
|
|
|
// Note was reloaded while being saved.
|
|
|
|
if (!isNew && (!stateNote || stateNote.id !== savedNote.id)) return;
|
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
// Re-assign any property that might have changed during saving (updated_time, etc.)
|
|
|
|
note = Object.assign(note, savedNote);
|
|
|
|
|
2018-01-12 21:58:01 +02:00
|
|
|
if (stateNote.id === note.id) {
|
2017-12-01 02:00:18 +02:00
|
|
|
// But we preserve the current title and body because
|
|
|
|
// the user might have changed them between the time
|
|
|
|
// saveNoteButton_press was called and the note was
|
2017-12-07 23:32:22 +02:00
|
|
|
// saved (it's done asynchronously).
|
|
|
|
//
|
|
|
|
// If the title was auto-assigned above, we don't restore
|
|
|
|
// it from the state because it will be empty there.
|
2018-01-12 21:58:01 +02:00
|
|
|
if (!hasAutoTitle) note.title = stateNote.title;
|
2017-12-01 02:00:18 +02:00
|
|
|
note.body = stateNote.body;
|
|
|
|
}
|
|
|
|
|
2018-01-12 21:58:01 +02:00
|
|
|
let newState = {
|
2017-11-05 02:49:23 +02:00
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
2018-01-12 21:58:01 +02:00
|
|
|
};
|
|
|
|
|
2018-01-30 23:36:54 +02:00
|
|
|
if (isNew && hasAutoTitle) newState.newAndNoTitleChangeNoteId = note.id;
|
2018-01-12 21:58:01 +02:00
|
|
|
|
|
|
|
comp.setState(newState);
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2018-01-28 20:42:43 +02:00
|
|
|
if (isNew) {
|
|
|
|
Note.updateGeolocation(note.id).then((geoNote) => {
|
|
|
|
const stateNote = comp.state.note;
|
|
|
|
if (stateNote.id !== geoNote.id) return; // Another note has been loaded while geoloc was being retrieved
|
|
|
|
|
|
|
|
// Geo-location for this note has been saved to the database however the properties
|
|
|
|
// are is not in the state so set them now.
|
|
|
|
|
|
|
|
const geoInfo = {
|
|
|
|
longitude: geoNote.longitude,
|
|
|
|
latitude: geoNote.latitude,
|
|
|
|
altitude: geoNote.altitude,
|
|
|
|
}
|
|
|
|
|
|
|
|
const modNote = Object.assign({}, stateNote, geoInfo);
|
|
|
|
const modLastSavedNote = Object.assign({}, comp.state.lastSavedNote, geoInfo);
|
|
|
|
|
|
|
|
comp.setState({ note: modNote, lastSavedNote: modLastSavedNote });
|
|
|
|
comp.refreshNoteMetadata();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-05 20:36:27 +02:00
|
|
|
comp.refreshNoteMetadata();
|
2018-01-12 21:58:01 +02:00
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
// Clear the newNote item now that the note has been saved, and
|
|
|
|
// make sure that the note we're editing is selected.
|
|
|
|
comp.props.dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: savedNote.id,
|
|
|
|
});
|
|
|
|
}
|
2017-11-05 02:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared.saveOneProperty = async function(comp, name, value) {
|
|
|
|
let note = Object.assign({}, comp.state.note);
|
|
|
|
|
|
|
|
// Note has been deleted while user was modifying it. In that, we
|
|
|
|
// just save a new note by clearing the note ID.
|
|
|
|
if (note.id && !(await shared.noteExists(note.id))) delete note.id;
|
|
|
|
|
2017-12-01 02:00:18 +02:00
|
|
|
// reg.logger().info('Saving note property: ', note.id, name, value);
|
2017-11-05 02:49:23 +02:00
|
|
|
|
|
|
|
if (note.id) {
|
|
|
|
let toSave = { id: note.id };
|
|
|
|
toSave[name] = value;
|
|
|
|
toSave = await Note.save(toSave);
|
|
|
|
note[name] = toSave[name];
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
note[name] = value;
|
|
|
|
comp.setState({ note: note });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shared.noteComponent_change = function(comp, propName, propValue) {
|
2018-01-12 21:58:01 +02:00
|
|
|
let newState = {}
|
|
|
|
|
2017-11-05 02:49:23 +02:00
|
|
|
let note = Object.assign({}, comp.state.note);
|
|
|
|
note[propName] = propValue;
|
2018-01-12 21:58:01 +02:00
|
|
|
newState.note = note;
|
|
|
|
|
|
|
|
comp.setState(newState);
|
2017-11-05 02:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared.refreshNoteMetadata = async function(comp, force = null) {
|
|
|
|
if (force !== true && !comp.state.showNoteMetadata) return;
|
|
|
|
|
|
|
|
let noteMetadata = await Note.serializeAllProps(comp.state.note);
|
|
|
|
comp.setState({ noteMetadata: noteMetadata });
|
|
|
|
}
|
|
|
|
|
|
|
|
shared.isModified = function(comp) {
|
|
|
|
if (!comp.state.note || !comp.state.lastSavedNote) return false;
|
2018-01-12 21:58:01 +02:00
|
|
|
let diff = BaseModel.diffObjects(comp.state.lastSavedNote, comp.state.note);
|
2017-11-05 02:49:23 +02:00
|
|
|
delete diff.type_;
|
|
|
|
return !!Object.getOwnPropertyNames(diff).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared.initState = async function(comp) {
|
|
|
|
let note = null;
|
|
|
|
let mode = 'view';
|
|
|
|
if (!comp.props.noteId) {
|
|
|
|
note = comp.props.itemType == 'todo' ? Note.newTodo(comp.props.folderId) : Note.new(comp.props.folderId);
|
|
|
|
mode = 'edit';
|
|
|
|
} else {
|
|
|
|
note = await Note.load(comp.props.noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const folder = Folder.byId(comp.props.folders, note.parent_id);
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
lastSavedNote: Object.assign({}, note),
|
|
|
|
note: note,
|
|
|
|
mode: mode,
|
|
|
|
folder: folder,
|
|
|
|
isLoading: false,
|
|
|
|
});
|
2017-11-07 01:56:33 +02:00
|
|
|
|
|
|
|
comp.lastLoadedNoteId_ = note ? note.id : null;
|
2017-11-05 02:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared.showMetadata_onPress = function(comp) {
|
|
|
|
comp.setState({ showNoteMetadata: !comp.state.showNoteMetadata });
|
2017-11-05 20:36:27 +02:00
|
|
|
comp.refreshNoteMetadata(true);
|
2017-11-05 02:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared.toggleIsTodo_onPress = function(comp) {
|
|
|
|
let newNote = Note.toggleIsTodo(comp.state.note);
|
|
|
|
let newState = { note: newNote };
|
|
|
|
comp.setState(newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = shared;
|