Skip to content

Instantly share code, notes, and snippets.

@bolasblack
Last active August 29, 2015 14:05
Show Gist options
  • Save bolasblack/40794226ea8ddad9827e to your computer and use it in GitHub Desktop.
Save bolasblack/40794226ea8ddad9827e to your computer and use it in GitHub Desktop.
OS X 的文件多选逻辑
# 多选的逻辑如下:
# 点击 清空已选中的任务,选中被点击的条目,并记录条目为“最后点击的条目”
# Ctrl/Cmd 不清空已选中的任务,选中被点击的条目,并记录条目为“最后点击的条目”
# Shift 不清空已选中的任务,切换“最后选中的条目”和“点击条目”之间的所有条目的选中状态
# 如果条目在“最后点击的条目”和“点击条目”之间,就选中
# 如果条目不在“最后点击的条目”和“点击条目”之间,就取消选中
$scope.selectTask = do ->
switchTaskSelectStatus = (currTask) ->
visualTasks = _($scope.taskGroups).pluck('tasks').flatten().value()
targetIndex = visualTasks.indexOf currTask
lastClickIndex = _(visualTasks).findIndex $$lastClick: true
_.forEach visualTasks, (task, index) ->
inRange = (targetIndex <= index <= lastClickIndex) or (lastClickIndex <= index <= targetIndex)
if task.$$selected and task.$$lastSelected and not inRange
task.$$selected = false
task.$$lastSelected = false
else if inRange
task.$$selected = true
task.$$lastSelected = true
return
toggleLastClickTask = (lastClickTask) ->
_.forEach $scope.tasks, (task, index) ->
task.$$lastClick = task is lastClickTask
return
($event, currTask) ->
if kbd.is 'ctrl', 'cmd', $event
currTask.$$selected = true
toggleLastClickTask currTask
else if kbd.is 'shift', $event
return unless _($scope.tasks).find $$lastClick: true
switchTaskSelectStatus currTask
else
_($scope.tasks).forEach (task) ->
task.$$selected = task is currTask
if task.$$selected
toggleLastClickTask currTask
else
task.$$lastClick = false
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment