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

114 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-11 23:11:14 +02:00
import { folderItemFilename } from 'src/string-utils.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-11 23:11:14 +02:00
2017-05-15 21:10:00 +02:00
static newFolder() {
return {
id: null,
title: '',
}
}
2017-05-18 22:31:40 +02:00
static noteIds(id) {
2017-06-11 23:11:14 +02:00
return this.db().selectAll('SELECT id FROM notes WHERE parent_id = ?', [id]).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) => {
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-17 20:12:09 +02:00
return this.modelSelectAll('SELECT * FROM notes WHERE `parent_id` = ? AND `' + field + '` = ?', [folderId, value]);
//return this.db().selectOne('SELECT * FROM notes WHERE `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-18 22:19:13 +02:00
let notes = await Note.modelSelectAll('SELECT * FROM notes');
return folders.concat(notes);
2017-06-19 20:58:49 +02:00
}
static conflictFolder() {
let folderId = Setting.value('sync.conflictFolderId');
if (!folderId) {
return Folder.save({ title: _('Conflicts') }).then((folder) => {
Setting.setValue('sync.conflictFolderId', folder.id);
return folder;
});
}
2017-06-18 01:49:52 +02:00
2017-06-19 20:58:49 +02:00
return Folder.load(folderId);
2017-05-15 21:10:00 +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 };