2016-02-20 07:35:48 +08:00
|
|
|
import _ from 'underscore'
|
|
|
|
import {
|
|
|
|
Thread,
|
|
|
|
Actions,
|
|
|
|
Message,
|
|
|
|
TaskFactory,
|
|
|
|
DatabaseStore,
|
2016-03-08 10:13:53 +08:00
|
|
|
FocusedPerspectiveStore,
|
|
|
|
} from 'nylas-exports'
|
2016-02-20 07:35:48 +08:00
|
|
|
|
|
|
|
export default class ThreadListContextMenu {
|
|
|
|
constructor({threadIds = [], accountIds = []}) {
|
|
|
|
this.threadIds = threadIds
|
|
|
|
this.accountIds = accountIds
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItemTemplate() {
|
|
|
|
return DatabaseStore.modelify(Thread, this.threadIds)
|
|
|
|
.then((threads) => {
|
|
|
|
this.threads = threads;
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
this.replyItem(),
|
|
|
|
this.replyAllItem(),
|
|
|
|
this.forwardItem(),
|
|
|
|
{type: 'separator'},
|
|
|
|
this.archiveItem(),
|
|
|
|
this.trashItem(),
|
|
|
|
this.markAsReadItem(),
|
|
|
|
this.starItem(),
|
|
|
|
// this.moveToOrLabelItem(),
|
|
|
|
// {type: 'separator'},
|
|
|
|
// this.extensionItems(),
|
|
|
|
])
|
|
|
|
}).then((menuItems) => {
|
|
|
|
return _.filter(_.compact(menuItems), (item, index) => {
|
|
|
|
if ((index === 0 || index === menuItems.length - 1) && item.type === "separator") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
replyItem() {
|
|
|
|
if (this.threadIds.length !== 1) { return null }
|
|
|
|
return {
|
|
|
|
label: "Reply",
|
|
|
|
click: () => {
|
2016-03-23 06:47:51 +08:00
|
|
|
Actions.composeReply({
|
|
|
|
threadId: this.threadIds[0],
|
|
|
|
popout: true,
|
|
|
|
type: 'reply',
|
|
|
|
behavior: 'prefer-existing-if-pristine',
|
|
|
|
});
|
2016-02-20 07:35:48 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
replyAllItem() {
|
|
|
|
if (this.threadIds.length !== 1) { return null }
|
|
|
|
DatabaseStore.findBy(Message, {threadId: this.threadIds[0]})
|
|
|
|
.order(Message.attributes.date.descending())
|
|
|
|
.limit(1)
|
|
|
|
.then((message) => {
|
|
|
|
if (message && message.canReplyAll()) {
|
|
|
|
return {
|
|
|
|
label: "Reply All",
|
|
|
|
click: () => {
|
2016-03-23 06:47:51 +08:00
|
|
|
Actions.composeReply({
|
|
|
|
threadId: this.threadIds[0],
|
|
|
|
popout: true,
|
|
|
|
type: 'reply-all',
|
|
|
|
behavior: 'prefer-existing-if-pristine',
|
|
|
|
});
|
2016-02-20 07:35:48 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
forwardItem() {
|
|
|
|
if (this.threadIds.length !== 1) { return null }
|
|
|
|
return {
|
|
|
|
label: "Forward",
|
|
|
|
click: () => {
|
|
|
|
Actions.composeForward({threadId: this.threadIds[0], popout: true});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
archiveItem() {
|
2016-03-08 10:13:53 +08:00
|
|
|
const perspective = FocusedPerspectiveStore.current()
|
2016-04-20 02:32:33 +08:00
|
|
|
const allowed = perspective.canArchiveThreads(this.threads)
|
|
|
|
if (!allowed) {
|
2016-02-20 07:35:48 +08:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
label: "Archive",
|
|
|
|
click: () => {
|
|
|
|
const tasks = TaskFactory.tasksForArchiving({
|
|
|
|
threads: this.threads,
|
|
|
|
})
|
|
|
|
Actions.queueTasks(tasks)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trashItem() {
|
2016-03-08 10:13:53 +08:00
|
|
|
const perspective = FocusedPerspectiveStore.current()
|
2016-04-20 02:32:33 +08:00
|
|
|
const allowed = perspective.canMoveThreadsTo(this.threads, 'trash')
|
|
|
|
if (!allowed) {
|
2016-02-20 07:35:48 +08:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
label: "Trash",
|
|
|
|
click: () => {
|
|
|
|
const tasks = TaskFactory.tasksForMovingToTrash({
|
|
|
|
threads: this.threads,
|
|
|
|
})
|
|
|
|
Actions.queueTasks(tasks)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markAsReadItem() {
|
|
|
|
const unread = _.every(this.threads, (t) => {
|
|
|
|
return _.isMatch(t, {unread: false})
|
|
|
|
});
|
|
|
|
const dir = unread ? "Unread" : "Read"
|
|
|
|
|
|
|
|
return {
|
|
|
|
label: `Mark as ${dir}`,
|
|
|
|
click: () => {
|
|
|
|
const task = TaskFactory.taskForInvertingUnread({
|
|
|
|
threads: this.threads,
|
|
|
|
})
|
|
|
|
Actions.queueTask(task)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
starItem() {
|
|
|
|
const starred = _.every(this.threads, (t) => {
|
|
|
|
return _.isMatch(t, {starred: false})
|
|
|
|
});
|
|
|
|
|
|
|
|
let dir = ""
|
|
|
|
let star = "Star"
|
|
|
|
if (!starred) {
|
|
|
|
dir = "Remove "
|
|
|
|
star = (this.threadIds.length > 1) ? "Stars" : "Star"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
label: `${dir}${star}`,
|
|
|
|
click: () => {
|
|
|
|
const task = TaskFactory.taskForInvertingStarred({
|
|
|
|
threads: this.threads,
|
|
|
|
})
|
|
|
|
Actions.queueTask(task)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
displayMenu() {
|
|
|
|
const {remote} = require('electron')
|
|
|
|
this.menuItemTemplate().then((template) => {
|
|
|
|
remote.Menu.buildFromTemplate(template)
|
|
|
|
.popup(remote.getCurrentWindow());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|