2021-01-22 19:41:11 +02:00
|
|
|
const Folder = require('../../models/Folder').default;
|
2021-08-16 17:18:32 +02:00
|
|
|
const Setting = require('../../models/Setting').default;
|
2020-11-05 18:58:23 +02:00
|
|
|
const BaseModel = require('../../BaseModel').default;
|
2022-03-02 20:11:14 +02:00
|
|
|
const Tag = require('../../models/Tag').default;
|
2018-05-06 13:11:59 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const shared = {};
|
2017-11-06 01:55:01 +02:00
|
|
|
|
2018-05-06 13:11:59 +02:00
|
|
|
function folderHasChildren_(folders, folderId) {
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder = folders[i];
|
2018-05-06 13:11:59 +02:00
|
|
|
if (folder.parent_id === folderId) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 11:49:31 +02:00
|
|
|
function folderIsVisible(folders, folderId, collapsedFolderIds) {
|
|
|
|
if (!collapsedFolderIds || !collapsedFolderIds.length) return true;
|
|
|
|
|
|
|
|
while (true) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder = BaseModel.byId(folders, folderId);
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!folder) throw new Error(`No folder with id ${folder.id}`);
|
2018-05-09 11:49:31 +02:00
|
|
|
if (!folder.parent_id) return true;
|
|
|
|
if (collapsedFolderIds.indexOf(folder.parent_id) >= 0) return false;
|
|
|
|
folderId = folder.parent_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:04:32 +02:00
|
|
|
function renderFoldersRecursive_(props, renderItem, items, parentId, depth, order) {
|
2018-05-06 13:11:59 +02:00
|
|
|
const folders = props.folders;
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder = folders[i];
|
2018-05-09 10:53:47 +02:00
|
|
|
if (!Folder.idsEqual(folder.parent_id, parentId)) continue;
|
2018-05-09 11:49:31 +02:00
|
|
|
if (!folderIsVisible(props.folders, folder.id, props.collapsedFolderIds)) continue;
|
2018-05-06 13:11:59 +02:00
|
|
|
const hasChildren = folderHasChildren_(folders, folder.id);
|
2019-01-26 20:04:32 +02:00
|
|
|
order.push(folder.id);
|
2018-05-06 13:11:59 +02:00
|
|
|
items.push(renderItem(folder, props.selectedFolderId == folder.id && props.notesParentType == 'Folder', hasChildren, depth));
|
2019-01-26 20:04:32 +02:00
|
|
|
if (hasChildren) {
|
|
|
|
const result = renderFoldersRecursive_(props, renderItem, items, folder.id, depth + 1, order);
|
|
|
|
items = result.items;
|
|
|
|
order = result.order;
|
|
|
|
}
|
2017-11-06 01:55:01 +02:00
|
|
|
}
|
2019-01-26 20:04:32 +02:00
|
|
|
return {
|
|
|
|
items: items,
|
|
|
|
order: order,
|
|
|
|
};
|
2017-11-06 01:55:01 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 13:11:59 +02:00
|
|
|
shared.renderFolders = function(props, renderItem) {
|
2019-01-26 20:04:32 +02:00
|
|
|
return renderFoldersRecursive_(props, renderItem, [], '', 0, []);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-05-06 13:11:59 +02:00
|
|
|
|
2017-11-06 01:55:01 +02:00
|
|
|
shared.renderTags = function(props, renderItem) {
|
2022-03-02 20:11:14 +02:00
|
|
|
const tags = Tag.sortTags(props.tags.slice());
|
2020-03-14 01:46:14 +02:00
|
|
|
const tagItems = [];
|
2019-01-26 20:04:32 +02:00
|
|
|
const order = [];
|
2017-11-06 01:55:01 +02:00
|
|
|
for (let i = 0; i < tags.length; i++) {
|
|
|
|
const tag = tags[i];
|
2019-01-26 20:04:32 +02:00
|
|
|
order.push(tag.id);
|
2017-11-06 01:55:01 +02:00
|
|
|
tagItems.push(renderItem(tag, props.selectedTagId == tag.id && props.notesParentType == 'Tag'));
|
|
|
|
}
|
2019-01-26 20:04:32 +02:00
|
|
|
return {
|
|
|
|
items: tagItems,
|
|
|
|
order: order,
|
|
|
|
};
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-06 01:55:01 +02:00
|
|
|
|
2017-11-06 23:11:15 +02:00
|
|
|
shared.synchronize_press = async function(comp) {
|
2020-11-05 18:58:23 +02:00
|
|
|
const { reg } = require('../../registry.js');
|
2017-11-06 23:11:15 +02:00
|
|
|
|
|
|
|
const action = comp.props.syncStarted ? 'cancel' : 'start';
|
|
|
|
|
2021-08-16 17:18:32 +02:00
|
|
|
if (!Setting.value('sync.target')) {
|
|
|
|
comp.props.dispatch({
|
|
|
|
type: 'SIDE_MENU_CLOSE',
|
|
|
|
});
|
|
|
|
|
|
|
|
comp.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Config',
|
|
|
|
sectionName: 'sync',
|
|
|
|
});
|
|
|
|
|
|
|
|
return 'init';
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
if (!(await reg.syncTarget().isAuthenticated())) {
|
2018-01-25 21:01:14 +02:00
|
|
|
if (reg.syncTarget().authRouteName()) {
|
|
|
|
comp.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: reg.syncTarget().authRouteName(),
|
|
|
|
});
|
|
|
|
return 'auth';
|
|
|
|
}
|
|
|
|
|
2022-01-09 13:24:24 +02:00
|
|
|
reg.logger().error('Not authenticated with sync target - please check your credentials.');
|
2018-01-25 21:01:14 +02:00
|
|
|
return 'error';
|
2017-11-06 23:11:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let sync = null;
|
|
|
|
try {
|
2017-11-24 20:06:30 +02:00
|
|
|
sync = await reg.syncTarget().synchronizer();
|
2017-11-06 23:11:15 +02:00
|
|
|
} catch (error) {
|
2022-01-09 13:24:24 +02:00
|
|
|
reg.logger().error('Could not initialise synchroniser: ');
|
|
|
|
reg.logger().error(error);
|
|
|
|
error.message = `Could not initialise synchroniser: ${error.message}`;
|
|
|
|
comp.props.dispatch({
|
|
|
|
type: 'SYNC_REPORT_UPDATE',
|
|
|
|
report: { errors: [error] },
|
|
|
|
});
|
2017-11-06 23:11:15 +02:00
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == 'cancel') {
|
|
|
|
sync.cancel();
|
|
|
|
return 'cancel';
|
|
|
|
} else {
|
|
|
|
reg.scheduleSync(0);
|
|
|
|
return 'sync';
|
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-06 23:11:15 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = shared;
|