HistoryV2FileTreeController.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "HistoryV2FileTreeController", ["$scope", "ide", "_", ($scope, ide, _) ->
  5. _previouslySelectedPathname = null
  6. $scope.currentFileTree = []
  7. _pathnameExistsInFiles = (pathname, files) ->
  8. _.any files, (file) -> file.pathname == pathname
  9. _getSelectedDefaultPathname = (files) ->
  10. selectedPathname = null
  11. if _previouslySelectedPathname? and _pathnameExistsInFiles _previouslySelectedPathname, files
  12. selectedPathname = _previouslySelectedPathname
  13. else
  14. mainFile = _.find files, (file) -> /main\.tex$/.test file.pathname
  15. if mainFile?
  16. selectedPathname = _previouslySelectedPathname = mainFile.pathname
  17. else
  18. selectedPathname = _previouslySelectedPathname = files[0].pathname
  19. return selectedPathname
  20. $scope.handleFileSelection = (file) ->
  21. $scope.history.selection.pathname = _previouslySelectedPathname = file.pathname
  22. $scope.$watch 'history.files', (files) ->
  23. if files? and files.length > 0
  24. $scope.currentFileTree = _.reduce files, _reducePathsToTree, []
  25. $scope.history.selection.pathname = _getSelectedDefaultPathname(files)
  26. _reducePathsToTree = (currentFileTree, fileObject) ->
  27. filePathParts = fileObject.pathname.split "/"
  28. currentFileTreeLocation = currentFileTree
  29. for pathPart, index in filePathParts
  30. isFile = index == filePathParts.length - 1
  31. if isFile
  32. fileTreeEntity =
  33. name: pathPart
  34. pathname: fileObject.pathname
  35. type: "file"
  36. operation: fileObject.operation || "edited"
  37. currentFileTreeLocation.push fileTreeEntity
  38. else
  39. fileTreeEntity = _.find currentFileTreeLocation, (entity) => entity.name == pathPart
  40. if !fileTreeEntity?
  41. fileTreeEntity =
  42. name: pathPart
  43. type: "folder"
  44. children: []
  45. currentFileTreeLocation.push fileTreeEntity
  46. currentFileTreeLocation = fileTreeEntity.children
  47. return currentFileTree
  48. ]