diff --git a/internal_packages/account-sidebar/lib/sidebar-item.coffee b/internal_packages/account-sidebar/lib/sidebar-item.coffee index dcf4aecb9..3313d6890 100644 --- a/internal_packages/account-sidebar/lib/sidebar-item.coffee +++ b/internal_packages/account-sidebar/lib/sidebar-item.coffee @@ -4,7 +4,8 @@ _ = require 'underscore' FocusedPerspectiveStore, SyncbackCategoryTask, DestroyCategoryTask, - Actions} = require 'nylas-exports' + Actions, + Utils} = require 'nylas-exports' {OutlineViewItem} = require 'nylas-component-kit' @@ -65,27 +66,24 @@ class SidebarItem selected: isItemSelected(perspective) collapsed: isItemCollapsed(id) ? true counterStyle: counterStyle - dataTransferType: 'nylas-thread-ids' + dataTransferType: 'nylas-threads-data' onDelete: if opts.deletable then onDeleteItem else undefined onEdited: if opts.editable then onEditItem else undefined onToggleCollapsed: toggleItemCollapsed onDrop: (item, event) -> jsonString = event.dataTransfer.getData(item.dataTransferType) - ids = null - try - ids = JSON.parse(jsonString); - catch err - console.error('OutlineViewItem onDrop: JSON parse #{err}'); - return unless ids - item.perspective.applyToThreads(ids) + data = Utils.jsonParse(jsonString) + return unless data + item.perspective.applyToThreads(data.threadIds) shouldAcceptDrop: (item, event) -> target = item.perspective current = FocusedPerspectiveStore.current() - + jsonString = event.dataTransfer.getData(item.dataTransferType) + data = Utils.jsonParse(jsonString) + return false unless data return false unless target return false if target.isEqual(current) - return false unless _.isEqual(target.accountIds, current.accountIds) - return false unless target.canApplyToThreads() + return false unless target.canApplyToThreads(data.accountIds) return item.dataTransferType in event.dataTransfer.types diff --git a/internal_packages/thread-list/lib/thread-list.cjsx b/internal_packages/thread-list/lib/thread-list.cjsx index abbe54f7b..6dbb7bffb 100644 --- a/internal_packages/thread-list/lib/thread-list.cjsx +++ b/internal_packages/thread-list/lib/thread-list.cjsx @@ -106,17 +106,26 @@ class ThreadList extends React.Component event.preventDefault() return - if itemThreadId in ThreadListStore.dataSource().selection.ids() - dragThreadIds = ThreadListStore.dataSource().selection.ids() + dataSource = ThreadListStore.dataSource() + if itemThreadId in dataSource.selection.ids() + dragThreadIds = dataSource.selection.ids() else dragThreadIds = [itemThreadId] + dragAccountIds = dragThreadIds.map (threadId) -> dataSource.getById(threadId).accountId + dragAccountIds = _.uniq(dragAccountIds) + + dragData = { + accountIds: dragAccountIds, + threadIds: dragThreadIds + } + event.dataTransfer.effectAllowed = "move" event.dataTransfer.dragEffect = "move" canvas = CanvasUtils.canvasWithThreadDragImage(dragThreadIds.length) event.dataTransfer.setDragImage(canvas, 10, 10) - event.dataTransfer.setData('nylas-thread-ids', JSON.stringify(dragThreadIds)) + event.dataTransfer.setData('nylas-threads-data', JSON.stringify(dragData)) return _onDragEnd: (event) => diff --git a/src/flux/models/utils.coffee b/src/flux/models/utils.coffee index 6749d352d..4d0e6130f 100644 --- a/src/flux/models/utils.coffee +++ b/src/flux/models/utils.coffee @@ -496,3 +496,13 @@ Utils = fn.executing = true fn.apply(@, [fnFinished, fnReinvoked, arguments...]) fnRun + + # Parse json without throwing an error. Logs a sensible message to indicate + # the error occurred while parsing + jsonParse: (jsonString) => + data = null + try + data = JSON.parse(jsonString) + catch err + console.error("JSON parse error: #{err}") + return data diff --git a/src/mailbox-perspective.coffee b/src/mailbox-perspective.coffee index a56384477..13f8fbcc8 100644 --- a/src/mailbox-perspective.coffee +++ b/src/mailbox-perspective.coffee @@ -73,8 +73,9 @@ class MailboxPerspective threadUnreadCount: => 0 - canApplyToThreads: => - throw new Error("canApplyToThreads: Not implemented in base class.") + canApplyToThreads: (targetAccountIds) => + targetIdsInCurrent = _.difference(targetAccountIds, @accountIds).length is 0 + return targetIdsInCurrent applyToThreads: (threadsOrIds) => throw new Error("applyToThreads: Not implemented in base class.") @@ -149,7 +150,7 @@ class StarredMailboxPerspective extends MailboxPerspective return new MutableQuerySubscription(query, {asResultSet: true}) canApplyToThreads: => - true + super applyToThreads: (threadsOrIds) => ChangeStarredTask = require './flux/tasks/change-starred-task' @@ -224,7 +225,7 @@ class CategoryMailboxPerspective extends MailboxPerspective @_categories[0].name is 'inbox' canApplyToThreads: => - not _.any @_categories, (c) -> c.isLockedCategory() + super and not _.any @_categories, (c) -> c.isLockedCategory() canArchiveThreads: => for cat in @_categories