2018-05-06 13:11:59 +02:00
|
|
|
const ArrayUtils = require('lib/ArrayUtils');
|
2018-05-09 10:53:47 +02:00
|
|
|
const Folder = require('lib/models/Folder');
|
2018-05-06 13:11:59 +02:00
|
|
|
|
2017-11-06 01:55:01 +02:00
|
|
|
let shared = {};
|
|
|
|
|
2018-05-06 13:11:59 +02:00
|
|
|
function folderHasChildren_(folders, folderId) {
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
let folder = folders[i];
|
|
|
|
if (folder.parent_id === folderId) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderFoldersRecursive_(props, renderItem, items, parentId, depth) {
|
|
|
|
const folders = props.folders;
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
let folder = folders[i];
|
2018-05-09 10:53:47 +02:00
|
|
|
if (!Folder.idsEqual(folder.parent_id, parentId)) continue;
|
2018-05-06 13:11:59 +02:00
|
|
|
const hasChildren = folderHasChildren_(folders, folder.id);
|
|
|
|
items.push(renderItem(folder, props.selectedFolderId == folder.id && props.notesParentType == 'Folder', hasChildren, depth));
|
|
|
|
if (hasChildren) items = renderFoldersRecursive_(props, renderItem, items, folder.id, depth + 1);
|
2017-11-06 01:55:01 +02:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:11:59 +02:00
|
|
|
shared.renderFolders = function(props, renderItem) {
|
|
|
|
return renderFoldersRecursive_(props, renderItem, [], '', 0);
|
|
|
|
}
|
|
|
|
|
2017-11-06 01:55:01 +02:00
|
|
|
shared.renderTags = function(props, renderItem) {
|
|
|
|
let tags = props.tags.slice();
|
|
|
|
tags.sort((a, b) => { return a.title < b.title ? -1 : +1; });
|
|
|
|
let tagItems = [];
|
|
|
|
for (let i = 0; i < tags.length; i++) {
|
|
|
|
const tag = tags[i];
|
|
|
|
tagItems.push(renderItem(tag, props.selectedTagId == tag.id && props.notesParentType == 'Tag'));
|
|
|
|
}
|
|
|
|
return tagItems;
|
|
|
|
}
|
|
|
|
|
2017-11-17 20:57:27 +02:00
|
|
|
shared.renderSearches = function(props, renderItem) {
|
|
|
|
let searches = props.searches.slice();
|
|
|
|
let searchItems = [];
|
|
|
|
for (let i = 0; i < searches.length; i++) {
|
|
|
|
const search = searches[i];
|
|
|
|
searchItems.push(renderItem(search, props.selectedSearchId == search.id && props.notesParentType == 'Search'));
|
|
|
|
}
|
|
|
|
return searchItems;
|
|
|
|
}
|
|
|
|
|
2017-11-06 23:11:15 +02:00
|
|
|
shared.synchronize_press = async function(comp) {
|
2017-12-14 20:12:14 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2017-11-06 23:11:15 +02:00
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
|
|
|
|
const action = comp.props.syncStarted ? 'cancel' : 'start';
|
|
|
|
|
2018-03-26 19:33:55 +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';
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.logger().info('Not authentified with sync target - please check your credential.');
|
|
|
|
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) {
|
|
|
|
reg.logger().info('Could not acquire synchroniser:');
|
|
|
|
reg.logger().info(error);
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == 'cancel') {
|
|
|
|
sync.cancel();
|
|
|
|
return 'cancel';
|
|
|
|
} else {
|
|
|
|
reg.scheduleSync(0);
|
|
|
|
return 'sync';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 01:55:01 +02:00
|
|
|
module.exports = shared;
|