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