2017-12-14 18:12:14 +00:00
|
|
|
const Folder = require('lib/models/Folder.js');
|
2019-03-02 17:35:57 +00:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2017-07-15 16:54:19 +01:00
|
|
|
|
|
|
|
class FoldersScreenUtils {
|
2019-04-20 19:29:23 +01:00
|
|
|
static async allForDisplay(options = {}) {
|
2019-03-02 17:35:57 +00:00
|
|
|
const orderDir = Setting.value('folders.sortOrder.reverse') ? 'DESC' : 'ASC';
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const folderOptions = Object.assign(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
caseInsensitive: true,
|
|
|
|
order: [
|
|
|
|
{
|
|
|
|
by: 'title',
|
|
|
|
dir: orderDir,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
options
|
|
|
|
);
|
2019-04-20 19:29:23 +01:00
|
|
|
|
|
|
|
let folders = await Folder.all(folderOptions);
|
2017-10-07 23:17:10 +01:00
|
|
|
|
2019-03-02 17:35:57 +00:00
|
|
|
if (Setting.value('folders.sortOrder.field') === 'last_note_user_updated_time') {
|
|
|
|
folders = await Folder.orderByLastModified(folders, orderDir);
|
|
|
|
}
|
|
|
|
|
2019-11-10 22:14:56 -08:00
|
|
|
if (Setting.value('showNoteCounts')) {
|
2020-01-18 13:46:04 +00:00
|
|
|
await Folder.addNoteCounts(folders,
|
|
|
|
Setting.value('showCompletedTodos'));
|
2019-11-10 22:14:56 -08:00
|
|
|
}
|
|
|
|
|
2019-04-20 19:29:23 +01:00
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async refreshFolders() {
|
2020-02-22 22:25:16 +11:00
|
|
|
FoldersScreenUtils.refreshCalls_.push(true);
|
|
|
|
try {
|
|
|
|
const folders = await this.allForDisplay({ includeConflictFolder: true });
|
2019-04-20 19:29:23 +01:00
|
|
|
|
2020-02-22 22:25:16 +11:00
|
|
|
this.dispatch({
|
|
|
|
type: 'FOLDER_UPDATE_ALL',
|
|
|
|
items: folders,
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
FoldersScreenUtils.refreshCalls_.pop();
|
|
|
|
}
|
2017-07-15 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 17:35:57 +00:00
|
|
|
static scheduleRefreshFolders() {
|
|
|
|
if (this.scheduleRefreshFoldersIID_) clearTimeout(this.scheduleRefreshFoldersIID_);
|
|
|
|
this.scheduleRefreshFoldersIID_ = setTimeout(() => {
|
|
|
|
this.scheduleRefreshFoldersIID_ = null;
|
|
|
|
this.refreshFolders();
|
|
|
|
}, 1000);
|
|
|
|
}
|
2020-02-22 22:25:16 +11:00
|
|
|
|
|
|
|
static async cancelTimers() {
|
2020-02-28 05:25:42 +11:00
|
|
|
if (this.scheduleRefreshFoldersIID_) {
|
|
|
|
clearTimeout(this.scheduleRefreshFoldersIID_);
|
|
|
|
this.scheduleRefreshFoldersIID_ = null;
|
|
|
|
}
|
2020-02-22 22:25:16 +11:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const iid = setInterval(() => {
|
|
|
|
if (!FoldersScreenUtils.refreshCalls_.length) {
|
|
|
|
clearInterval(iid);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}
|
2017-07-15 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 22:25:16 +11:00
|
|
|
FoldersScreenUtils.refreshCalls_ = [];
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { FoldersScreenUtils };
|