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

116 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-05-15 21:10:00 +02:00
import { BaseModel } from 'src/base-model.js';
import { Log } from 'src/log.js';
2017-06-15 01:14:15 +02:00
import { promiseChain } from 'src/promise-utils.js';
2017-05-18 22:31:40 +02:00
import { Note } from 'src/models/note.js';
2017-06-19 20:58:49 +02:00
import { Setting } from 'src/models/setting.js';
2017-06-04 15:30:12 +02:00
import { _ } from 'src/locale.js';
2017-06-12 23:56:27 +02:00
import moment from 'moment';
2017-06-15 20:18:48 +02:00
import { BaseItem } from 'src/models/base-item.js';
2017-05-15 21:10:00 +02:00
2017-06-15 20:18:48 +02:00
class Folder extends BaseItem {
2017-05-15 21:10:00 +02:00
static tableName() {
return 'folders';
}
2017-06-19 00:06:10 +02:00
static serialize(folder) {
return super.serialize(folder, 'folder', ['id', 'created_time', 'updated_time', 'type_']);
2017-05-15 21:10:00 +02:00
}
2017-05-18 21:58:01 +02:00
static itemType() {
2017-06-19 21:26:27 +02:00
return BaseModel.MODEL_TYPE_FOLDER;
2017-05-18 21:58:01 +02:00
}
static trackChanges() {
return true;
}
2017-06-20 00:18:24 +02:00
static trackDeleted() {
return true;
}
2017-06-11 23:11:14 +02:00
2017-05-15 21:10:00 +02:00
static newFolder() {
return {
id: null,
title: '',
}
}
2017-06-20 00:18:24 +02:00
static syncedNoteIds() {
2017-06-20 21:18:19 +02:00
return this.db().selectAll('SELECT id FROM notes WHERE is_conflict = 0 AND sync_time > 0').then((rows) => {
2017-06-20 00:18:24 +02:00
let output = [];
for (let i = 0; i < rows.length; i++) {
output.push(rows[i].id);
}
return output;
});
}
static noteIds(parentId) {
2017-06-20 21:18:19 +02:00
return this.db().selectAll('SELECT id FROM notes WHERE is_conflict = 0 AND parent_id = ?', [parentId]).then((rows) => {
2017-05-18 22:31:40 +02:00
let output = [];
2017-06-11 23:11:14 +02:00
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
2017-05-18 22:31:40 +02:00
output.push(row.id);
}
return output;
});
}
static delete(folderId, options = null) {
2017-06-04 15:30:12 +02:00
return this.load(folderId).then((folder) => {
2017-06-20 00:18:24 +02:00
if (!folder) throw new Error('Trying to delete non-existing folder: ' + folderId);
2017-06-04 15:30:12 +02:00
if (!!folder.is_default) {
throw new Error(_('Cannot delete the default list'));
}
}).then(() => {
return this.noteIds(folderId);
}).then((ids) => {
2017-05-18 22:31:40 +02:00
let chain = [];
for (let i = 0; i < ids.length; i++) {
chain.push(() => {
return Note.delete(ids[i]);
});
}
return promiseChain(chain);
}).then(() => {
return super.delete(folderId, options);
}).then(() => {
this.dispatch({
type: 'FOLDER_DELETE',
folderId: folderId,
});
});
}
2017-06-11 23:11:14 +02:00
static loadNoteByField(folderId, field, value) {
2017-06-20 21:18:19 +02:00
return this.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 0 AND `parent_id` = ? AND `' + field + '` = ?', [folderId, value]);
2017-06-11 23:11:14 +02:00
}
2017-06-18 01:49:52 +02:00
static async all(includeNotes = false) {
2017-06-18 22:19:13 +02:00
let folders = await Folder.modelSelectAll('SELECT * FROM folders');
2017-06-18 01:49:52 +02:00
if (!includeNotes) return folders;
2017-06-20 21:18:19 +02:00
let notes = await Note.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 0');
2017-06-18 22:19:13 +02:00
return folders.concat(notes);
2017-06-19 20:58:49 +02:00
}
2017-05-18 22:31:40 +02:00
static save(o, options = null) {
2017-06-11 23:11:14 +02:00
return Folder.loadByField('title', o.title).then((existingFolder) => {
if (existingFolder && existingFolder.id != o.id) throw new Error(_('A folder with title "%s" already exists', o.title));
return super.save(o, options).then((folder) => {
this.dispatch({
type: 'FOLDERS_UPDATE_ONE',
folder: folder,
});
return folder;
2017-05-18 22:31:40 +02:00
});
});
}
2017-05-15 21:10:00 +02:00
}
export { Folder };