2020-07-12 19:09:07 +02:00
|
|
|
const BaseItem = require('lib/models/BaseItem');
|
2018-05-09 11:49:31 +02:00
|
|
|
const BaseModel = require('lib/BaseModel');
|
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
|
|
|
|
2020-07-12 19:09:07 +02:00
|
|
|
function itemHasChildren_(items, itemId) {
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
|
|
|
if (item.parent_id === itemId) return true;
|
2018-05-06 13:11:59 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-12 19:09:07 +02:00
|
|
|
function itemIsVisible(items, itemId, collapsedItemIds) {
|
|
|
|
if (!collapsedItemIds || !collapsedItemIds.length) return true;
|
2018-05-09 11:49:31 +02:00
|
|
|
|
|
|
|
while (true) {
|
2020-07-12 19:09:07 +02:00
|
|
|
const item = BaseModel.byId(items, itemId);
|
|
|
|
if (!item) throw new Error(`No item with id ${itemId}`);
|
|
|
|
if (!item.parent_id) return true;
|
|
|
|
if (collapsedItemIds.indexOf(item.parent_id) >= 0) return false;
|
|
|
|
itemId = item.parent_id;
|
2018-05-09 11:49:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 19:09:07 +02:00
|
|
|
function renderItemsRecursive_(props, renderItem, items, parentId, depth, order, itemType) {
|
|
|
|
let itemsKey = '';
|
|
|
|
let notesParentType = '';
|
|
|
|
let collapsedItemsKey = '';
|
|
|
|
let selectedItemKey = '';
|
|
|
|
if (itemType === BaseModel.TYPE_FOLDER) {
|
|
|
|
itemsKey = 'folders';
|
|
|
|
notesParentType = 'Folder';
|
|
|
|
collapsedItemsKey = 'collapsedFolderIds';
|
|
|
|
selectedItemKey = 'selectedFolderId';
|
|
|
|
} else if (itemType === BaseModel.TYPE_TAG) {
|
|
|
|
itemsKey = 'tags';
|
|
|
|
notesParentType = 'Tag';
|
|
|
|
collapsedItemsKey = 'collapsedTagIds';
|
|
|
|
selectedItemKey = 'selectedTagId';
|
|
|
|
}
|
|
|
|
|
|
|
|
const propItems = props[itemsKey];
|
|
|
|
for (let i = 0; i < propItems.length; i++) {
|
|
|
|
const item = propItems[i];
|
|
|
|
if (!BaseItem.getClassByItemType(itemType).idsEqual(item.parent_id, parentId)) continue;
|
|
|
|
if (!itemIsVisible(props[itemsKey], item.id, props[collapsedItemsKey])) continue;
|
|
|
|
const hasChildren = itemHasChildren_(propItems, item.id);
|
|
|
|
order.push(item.id);
|
|
|
|
items.push(renderItem(item, props[selectedItemKey] == item.id && props.notesParentType == notesParentType, hasChildren, depth));
|
2019-01-26 20:04:32 +02:00
|
|
|
if (hasChildren) {
|
2020-07-12 19:09:07 +02:00
|
|
|
const result = renderItemsRecursive_(props, renderItem, items, item.id, depth + 1, order, itemType);
|
2019-01-26 20:04:32 +02:00
|
|
|
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) {
|
2020-07-12 19:09:07 +02:00
|
|
|
return renderItemsRecursive_(props, renderItem, [], '', 0, [], BaseModel.TYPE_FOLDER);
|
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) {
|
2020-07-12 19:09:07 +02:00
|
|
|
return renderItemsRecursive_(props, renderItem, [], '', 0, [], BaseModel.TYPE_TAG);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-06 01:55:01 +02:00
|
|
|
|
2019-01-26 20:04:32 +02:00
|
|
|
// shared.renderSearches = function(props, renderItem) {
|
|
|
|
// let searches = props.searches.slice();
|
|
|
|
// let searchItems = [];
|
|
|
|
// const order = [];
|
|
|
|
// for (let i = 0; i < searches.length; i++) {
|
|
|
|
// const search = searches[i];
|
|
|
|
// order.push(search.id);
|
|
|
|
// searchItems.push(renderItem(search, props.selectedSearchId == search.id && props.notesParentType == 'Search'));
|
|
|
|
// }
|
|
|
|
// return {
|
|
|
|
// items: searchItems,
|
|
|
|
// order: order,
|
|
|
|
// };
|
|
|
|
// }
|
2017-11-17 20:57:27 +02:00
|
|
|
|
2017-11-06 23:11:15 +02:00
|
|
|
shared.synchronize_press = async function(comp) {
|
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
|
|
|
|
const action = comp.props.syncStarted ? 'cancel' : 'start';
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
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;
|