2018-01-24 09:35:09 +08:00
|
|
|
const _ = require('underscore');
|
|
|
|
const _str = require('underscore.string');
|
|
|
|
const { OutlineViewItem } = require('mailspring-component-kit');
|
|
|
|
const {
|
|
|
|
MailboxPerspective,
|
|
|
|
FocusedPerspectiveStore,
|
|
|
|
SyncbackCategoryTask,
|
|
|
|
DestroyCategoryTask,
|
|
|
|
CategoryStore,
|
|
|
|
Actions,
|
|
|
|
RegExpUtils,
|
|
|
|
} = require('mailspring-exports');
|
|
|
|
|
|
|
|
const SidebarActions = require('./sidebar-actions');
|
|
|
|
|
|
|
|
const idForCategories = categories => _.pluck(categories, 'id').join('-');
|
|
|
|
|
|
|
|
const countForItem = function(perspective) {
|
|
|
|
const unreadCountEnabled = AppEnv.config.get('core.workspace.showUnreadForAllCategories');
|
|
|
|
if (perspective.isInbox() || unreadCountEnabled) {
|
|
|
|
return perspective.unreadCount();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isItemSelected = perspective => FocusedPerspectiveStore.current().isEqual(perspective);
|
|
|
|
|
|
|
|
const isItemCollapsed = function(id) {
|
|
|
|
if (AppEnv.savedState.sidebarKeysCollapsed[id] !== undefined) {
|
|
|
|
return AppEnv.savedState.sidebarKeysCollapsed[id];
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleItemCollapsed = function(item) {
|
|
|
|
if (!(item.children.length > 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SidebarActions.setKeyCollapsed(item.id, !isItemCollapsed(item.id));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDeleteItem = function(item) {
|
|
|
|
// TODO Delete multiple categories at once
|
|
|
|
if (item.deleted === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const category = item.perspective.category();
|
|
|
|
if (!category) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Actions.queueTask(
|
|
|
|
new DestroyCategoryTask({
|
|
|
|
path: category.path,
|
|
|
|
accountId: category.accountId,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onEditItem = function(item, value) {
|
|
|
|
let newDisplayName;
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (item.deleted === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const category = item.perspective.category();
|
|
|
|
if (!category) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const re = RegExpUtils.subcategorySplitRegex();
|
|
|
|
let match = re.exec(category.displayName);
|
|
|
|
let lastMatch = match;
|
|
|
|
while (match) {
|
|
|
|
lastMatch = match;
|
|
|
|
match = re.exec(category.displayName);
|
|
|
|
}
|
|
|
|
if (lastMatch) {
|
|
|
|
newDisplayName = category.displayName.slice(0, lastMatch.index + 1) + value;
|
|
|
|
} else {
|
|
|
|
newDisplayName = value;
|
|
|
|
}
|
|
|
|
if (newDisplayName === category.displayName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Actions.queueTask(
|
|
|
|
SyncbackCategoryTask.forRenaming({
|
|
|
|
accountId: category.accountId,
|
|
|
|
path: category.path,
|
|
|
|
newName: newDisplayName,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SidebarItem {
|
2018-01-25 06:13:08 +08:00
|
|
|
static forPerspective(id, perspective, opts = {}) {
|
|
|
|
let counterStyle;
|
2018-01-24 09:35:09 +08:00
|
|
|
if (perspective.isInbox()) {
|
|
|
|
counterStyle = OutlineViewItem.CounterStyles.Alt;
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:13:08 +08:00
|
|
|
const collapsed = isItemCollapsed(id);
|
|
|
|
|
2018-01-24 09:35:09 +08:00
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
name: perspective.name,
|
|
|
|
contextMenuLabel: perspective.name,
|
|
|
|
count: countForItem(perspective),
|
|
|
|
iconName: perspective.iconName,
|
|
|
|
children: [],
|
|
|
|
perspective,
|
|
|
|
selected: isItemSelected(perspective),
|
2018-01-25 06:13:08 +08:00
|
|
|
collapsed: collapsed != null ? collapsed : true,
|
2018-01-24 09:35:09 +08:00
|
|
|
counterStyle,
|
|
|
|
onDelete: opts.deletable ? onDeleteItem : undefined,
|
|
|
|
onEdited: opts.editable ? onEditItem : undefined,
|
|
|
|
onCollapseToggled: toggleItemCollapsed,
|
|
|
|
|
|
|
|
onDrop(item, event) {
|
|
|
|
const jsonString = event.dataTransfer.getData('nylas-threads-data');
|
|
|
|
let jsonData = null;
|
|
|
|
try {
|
|
|
|
jsonData = JSON.parse(jsonString);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`JSON parse error: ${err}`);
|
|
|
|
}
|
|
|
|
if (!jsonData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
item.perspective.receiveThreadIds(jsonData.threadIds);
|
|
|
|
},
|
|
|
|
|
|
|
|
shouldAcceptDrop(item, event) {
|
|
|
|
const target = item.perspective;
|
|
|
|
const current = FocusedPerspectiveStore.current();
|
|
|
|
if (!event.dataTransfer.types.includes('nylas-threads-data')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (target.isEqual(current)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't inspect the drag payload until drop, so we use a dataTransfer
|
|
|
|
// type to encode the account IDs of threads currently being dragged.
|
|
|
|
const accountsType = event.dataTransfer.types.find(t => t.startsWith('nylas-accounts='));
|
|
|
|
const accountIds = (accountsType || '').replace('nylas-accounts=', '').split(',');
|
2018-01-29 23:58:25 +08:00
|
|
|
return target.canReceiveThreadsFromAccountIds(accountIds);
|
2018-01-24 09:35:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
onSelect(item) {
|
|
|
|
Actions.focusMailboxPerspective(item.perspective);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opts
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:13:08 +08:00
|
|
|
static forCategories(categories = [], opts = {}) {
|
2018-01-24 09:35:09 +08:00
|
|
|
const id = idForCategories(categories);
|
|
|
|
const contextMenuLabel = _str.capitalize(
|
|
|
|
categories[0] != null ? categories[0].displayType() : undefined
|
|
|
|
);
|
|
|
|
const perspective = MailboxPerspective.forCategories(categories);
|
|
|
|
|
|
|
|
if (opts.deletable == null) {
|
|
|
|
opts.deletable = true;
|
|
|
|
}
|
|
|
|
if (opts.editable == null) {
|
|
|
|
opts.editable = true;
|
|
|
|
}
|
|
|
|
opts.contextMenuLabel = contextMenuLabel;
|
|
|
|
return this.forPerspective(id, perspective, opts);
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:13:08 +08:00
|
|
|
static forStarred(accountIds, opts = {}) {
|
2018-01-24 09:35:09 +08:00
|
|
|
const perspective = MailboxPerspective.forStarred(accountIds);
|
|
|
|
let id = 'Starred';
|
|
|
|
if (opts.name) {
|
|
|
|
id += `-${opts.name}`;
|
|
|
|
}
|
|
|
|
return this.forPerspective(id, perspective, opts);
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:13:08 +08:00
|
|
|
static forUnread(accountIds, opts = {}) {
|
2018-01-24 09:35:09 +08:00
|
|
|
let categories = accountIds.map(accId => {
|
|
|
|
return CategoryStore.getCategoryByRole(accId, 'inbox');
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE: It's possible for an account to not yet have an `inbox`
|
|
|
|
// category. Since the `SidebarStore` triggers on `AccountStore`
|
|
|
|
// changes, it'll trigger the exact moment an account is added to the
|
|
|
|
// config. However, the API has not yet come back with the list of
|
|
|
|
// `categories` for that account.
|
|
|
|
categories = _.compact(categories);
|
|
|
|
|
|
|
|
const perspective = MailboxPerspective.forUnread(categories);
|
|
|
|
let id = 'Unread';
|
|
|
|
if (opts.name) {
|
|
|
|
id += `-${opts.name}`;
|
|
|
|
}
|
|
|
|
return this.forPerspective(id, perspective, opts);
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:13:08 +08:00
|
|
|
static forDrafts(accountIds, opts = {}) {
|
2018-01-24 09:35:09 +08:00
|
|
|
const perspective = MailboxPerspective.forDrafts(accountIds);
|
|
|
|
const id = `Drafts-${opts.name}`;
|
|
|
|
return this.forPerspective(id, perspective, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SidebarItem;
|