2021-01-22 17:41:11 +00:00
|
|
|
const Folder = require('./models/Folder').default;
|
2020-11-05 16:58:23 +00:00
|
|
|
const Setting = require('./models/Setting').default;
|
|
|
|
const shim = require('./shim').default;
|
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';
|
|
|
|
|
2023-06-01 12:02:36 +01:00
|
|
|
const folderOptions = {
|
|
|
|
|
|
|
|
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() {
|
2020-10-09 18:35:46 +01:00
|
|
|
if (this.scheduleRefreshFoldersIID_) shim.clearTimeout(this.scheduleRefreshFoldersIID_);
|
|
|
|
this.scheduleRefreshFoldersIID_ = shim.setTimeout(() => {
|
2019-03-02 17:35:57 +00:00
|
|
|
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_) {
|
2020-10-09 18:35:46 +01:00
|
|
|
shim.clearTimeout(this.scheduleRefreshFoldersIID_);
|
2020-02-28 05:25:42 +11:00
|
|
|
this.scheduleRefreshFoldersIID_ = null;
|
|
|
|
}
|
2020-02-22 22:25:16 +11:00
|
|
|
return new Promise((resolve) => {
|
2020-10-09 18:35:46 +01:00
|
|
|
const iid = shim.setInterval(() => {
|
2020-02-22 22:25:16 +11:00
|
|
|
if (!FoldersScreenUtils.refreshCalls_.length) {
|
2020-10-09 18:35:46 +01:00
|
|
|
shim.clearInterval(iid);
|
2020-02-22 22:25:16 +11:00
|
|
|
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 };
|