2017-06-24 19:06:28 +01:00
|
|
|
import { BaseModel } from 'lib/base-model.js';
|
|
|
|
import { Log } from 'lib/log.js';
|
|
|
|
import { promiseChain } from 'lib/promise-utils.js';
|
|
|
|
import { Note } from 'lib/models/note.js';
|
|
|
|
import { Setting } from 'lib/models/setting.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
2017-06-12 22:56:27 +01:00
|
|
|
import moment from 'moment';
|
2017-06-24 19:06:28 +01:00
|
|
|
import { BaseItem } from 'lib/models/base-item.js';
|
2017-06-29 21:52:52 +01:00
|
|
|
import lodash from 'lodash';
|
2017-05-15 19:10:00 +00:00
|
|
|
|
2017-06-15 19:18:48 +01:00
|
|
|
class Folder extends BaseItem {
|
2017-05-15 19:10:00 +00:00
|
|
|
|
|
|
|
static tableName() {
|
|
|
|
return 'folders';
|
|
|
|
}
|
|
|
|
|
2017-07-02 16:46:03 +01:00
|
|
|
static async serialize(folder) {
|
2017-06-29 21:52:52 +01:00
|
|
|
let fieldNames = this.fieldNames();
|
|
|
|
fieldNames.push('type_');
|
|
|
|
lodash.pull(fieldNames, 'parent_id', 'sync_time');
|
|
|
|
return super.serialize(folder, 'folder', fieldNames);
|
2017-05-15 19:10:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:50:45 +01:00
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_FOLDER;
|
2017-05-18 19:58:01 +00:00
|
|
|
}
|
2017-06-11 22:11:14 +01:00
|
|
|
|
2017-05-15 19:10:00 +00:00
|
|
|
static newFolder() {
|
|
|
|
return {
|
|
|
|
id: null,
|
|
|
|
title: '',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 23:18:24 +01:00
|
|
|
static noteIds(parentId) {
|
2017-06-20 19:18:19 +00: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 22:11:14 +01: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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-25 08:52:25 +01:00
|
|
|
static async delete(folderId, options = null) {
|
|
|
|
let folder = await Folder.load(folderId);
|
|
|
|
if (!folder) throw new Error('Trying to delete non-existing notebook: ' + folderId);
|
2017-07-01 16:07:17 +01:00
|
|
|
|
2017-06-25 08:52:25 +01:00
|
|
|
let noteIds = await Folder.noteIds(folderId);
|
|
|
|
for (let i = 0; i < noteIds.length; i++) {
|
|
|
|
await Note.delete(noteIds[i]);
|
|
|
|
}
|
|
|
|
|
2017-06-25 16:17:40 +01:00
|
|
|
await super.delete(folderId, options);
|
2017-06-25 08:52:25 +01:00
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'FOLDER_DELETE',
|
|
|
|
folderId: folderId,
|
2017-05-16 20:42:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-25 10:00:54 +01:00
|
|
|
static async all(options = null) {
|
|
|
|
if (!options) options = {};
|
2017-06-18 00:49:52 +01:00
|
|
|
|
2017-06-25 10:00:54 +01:00
|
|
|
let folders = await super.all(options);
|
|
|
|
if (!options.includeNotes) return folders;
|
|
|
|
|
|
|
|
if (options.limit) options.limit -= folders.length;
|
|
|
|
|
|
|
|
let notes = await Note.all(options);
|
2017-06-18 21:19:13 +01:00
|
|
|
return folders.concat(notes);
|
2017-06-19 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 20:16:03 +00:00
|
|
|
static defaultFolder() {
|
2017-06-25 08:52:25 +01:00
|
|
|
return this.modelSelectOne('SELECT * FROM folders ORDER BY created_time DESC LIMIT 1');
|
2017-06-25 00:19:11 +01:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:58:01 +00:00
|
|
|
static async save(o, options = null) {
|
|
|
|
if (options && options.duplicateCheck === true && o.title) {
|
|
|
|
let existingFolder = await Folder.loadByTitle(o.title);
|
|
|
|
if (existingFolder) throw new Error(_('A notebook with this title already exists: "%s"', o.title));
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:03:14 +00:00
|
|
|
return super.save(o, options).then((folder) => {
|
|
|
|
this.dispatch({
|
|
|
|
type: 'FOLDERS_UPDATE_ONE',
|
|
|
|
folder: folder,
|
2017-05-18 22:31:40 +02:00
|
|
|
});
|
2017-07-03 18:03:14 +00:00
|
|
|
return folder;
|
2017-05-18 22:31:40 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export { Folder };
|