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

154 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-06-24 20:06:28 +02:00
import { BaseModel } from 'lib/base-model.js';
import { Log } from 'lib/log.js';
import { promiseChain } from 'lib/promise-utils.js';
2017-07-15 17:35:40 +02:00
import { time } from 'lib/time-utils.js';
2017-06-24 20:06:28 +02:00
import { Note } from 'lib/models/note.js';
import { Setting } from 'lib/models/setting.js';
import { Database } from 'lib/database.js';
2017-06-24 20:06:28 +02:00
import { _ } from 'lib/locale.js';
2017-06-12 23:56:27 +02:00
import moment from 'moment';
2017-06-24 20:06:28 +02:00
import { BaseItem } from 'lib/models/base-item.js';
2017-06-29 22:52:52 +02:00
import lodash from 'lodash';
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-07-02 17:46:03 +02:00
static async serialize(folder) {
2017-06-29 22:52:52 +02:00
let fieldNames = this.fieldNames();
fieldNames.push('type_');
lodash.pull(fieldNames, 'parent_id');
2017-06-29 22:52:52 +02:00
return super.serialize(folder, 'folder', fieldNames);
2017-05-15 21:10:00 +02:00
}
2017-07-03 21:50:45 +02:00
static modelType() {
return BaseModel.TYPE_FOLDER;
2017-05-18 21:58:01 +02:00
}
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 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;
});
}
2017-07-12 22:39:47 +02:00
static async noteCount(parentId) {
let r = await this.db().selectOne('SELECT count(*) as total FROM notes WHERE is_conflict = 0 AND parent_id = ?', [parentId]);
return r ? r.total : 0;
}
static markNotesAsConflict(parentId) {
let query = Database.updateQuery('notes', { is_conflict: 1 }, { parent_id: parentId });
return this.db().exec(query);
}
2017-06-25 09:52:25 +02:00
static async delete(folderId, options = null) {
if (!options) options = {};
if (!('deleteChildren' in options)) options.deleteChildren = true;
2017-06-25 09:52:25 +02:00
let folder = await Folder.load(folderId);
if (!folder) return; // noop
if (options.deleteChildren) {
let noteIds = await Folder.noteIds(folderId);
for (let i = 0; i < noteIds.length; i++) {
await Note.delete(noteIds[i]);
}
2017-06-25 09:52:25 +02:00
}
2017-06-25 17:17:40 +02:00
await super.delete(folderId, options);
2017-06-25 09:52:25 +02:00
this.dispatch({
type: 'FOLDER_DELETE',
folderId: folderId,
});
}
2017-07-15 17:35:40 +02:00
static conflictFolderTitle() {
return _('Conflicts');
}
2017-06-18 01:49:52 +02:00
2017-07-15 17:35:40 +02:00
static conflictFolderId() {
return 'c04f1c7c04f1c7c04f1c7c04f1c7c04f';
}
static conflictFolder() {
return {
type_: this.TYPE_FOLDER,
id: this.conflictFolderId(),
title: this.conflictFolderTitle(),
updated_time: time.unixMs(),
};
}
2017-06-25 11:00:54 +02:00
2017-07-15 17:35:40 +02:00
static async all(options = null) {
let output = await super.all(options);
if (options && options.includeConflictFolder) {
let conflictCount = await Note.conflictedCount();
if (conflictCount) output.push(this.conflictFolder());
}
return output;
}
2017-06-25 11:00:54 +02:00
2017-07-15 17:35:40 +02:00
static load(id) {
if (id == this.conflictFolderId()) return this.conflictFolder();
return super.load(id);
2017-06-19 20:58:49 +02:00
}
2017-06-27 22:16:03 +02:00
static defaultFolder() {
2017-06-25 09:52:25 +02:00
return this.modelSelectOne('SELECT * FROM folders ORDER BY created_time DESC LIMIT 1');
2017-06-25 01:19:11 +02:00
}
2017-07-15 17:35:40 +02:00
// These "duplicateCheck" and "reservedTitleCheck" should only be done when a user is
// manually creating a folder. They shouldn't be done for example when the folders
2017-07-17 21:19:01 +02:00
// are being synced to avoid any strange side-effects. Technically it's possible to
// have folders and notes with duplicate titles (or no title), or with reserved words.
2017-07-03 20:58:01 +02:00
static async save(o, options = null) {
2017-07-17 21:19:01 +02:00
if (!options) options = {};
if (options.userSideValidation === true) {
if (!('duplicateCheck' in options)) options.duplicateCheck = true;
if (!('reservedTitleCheck' in options)) options.reservedTitleCheck = true;
if (!('stripLeftSlashes' in options)) options.stripLeftSlashes = true;
}
if (options.stripLeftSlashes === true && o.title) {
while (o.title.length && (o.title[0] == '/' || o.title[0] == "\\")) {
o.title = o.title.substr(1);
}
}
if (options.duplicateCheck === true && o.title) {
2017-07-03 20:58:01 +02:00
let existingFolder = await Folder.loadByTitle(o.title);
2017-07-15 18:25:33 +02:00
if (existingFolder && existingFolder.id != o.id) throw new Error(_('A notebook with this title already exists: "%s"', o.title));
2017-07-03 20:58:01 +02:00
}
2017-07-17 21:19:01 +02:00
if (options.reservedTitleCheck === true && o.title) {
2017-07-15 17:35:40 +02:00
if (o.title == Folder.conflictFolderTitle()) throw new Error(_('Notebooks cannot be named "%s", which is a reserved title.', o.title));
}
return super.save(o, options).then((folder) => {
this.dispatch({
type: 'FOLDERS_UPDATE_ONE',
folder: folder,
2017-05-18 22:31:40 +02:00
});
return folder;
2017-05-18 22:31:40 +02:00
});
}
2017-05-15 21:10:00 +02:00
}
export { Folder };