HistoryV2FileTreeController.coffee 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "HistoryV2FileTreeController", ["$scope", "ide", "_", ($scope, ide, _) ->
  5. $scope.currentFileTree = []
  6. _selectedDefaultPathname = (files) ->
  7. # TODO: Improve heuristic to determine the default pathname to show.
  8. if files? and files.length > 0
  9. mainFile = files.find (file) -> /main\.tex$/.test file.pathname
  10. if mainFile?
  11. mainFile.pathname
  12. else
  13. files[0].pathname
  14. $scope.handleFileSelection = (file) ->
  15. $scope.history.selection.pathname = file.pathname
  16. $scope.$watch 'history.files', (files) ->
  17. $scope.currentFileTree = _.reduce files, reducePathsToTree, []
  18. $scope.history.selection.pathname = _selectedDefaultPathname(files)
  19. reducePathsToTree = (currentFileTree, fileObject) ->
  20. filePathParts = fileObject.pathname.split "/"
  21. currentFileTreeLocation = currentFileTree
  22. for pathPart, index in filePathParts
  23. isFile = index == filePathParts.length - 1
  24. if isFile
  25. fileTreeEntity =
  26. name: pathPart
  27. pathname: fileObject.pathname
  28. type: "file"
  29. operation: fileObject.operation || "edited"
  30. currentFileTreeLocation.push fileTreeEntity
  31. else
  32. fileTreeEntity = _.find currentFileTreeLocation, (entity) => entity.name == pathPart
  33. if !fileTreeEntity?
  34. fileTreeEntity =
  35. name: pathPart
  36. type: "folder"
  37. children: []
  38. currentFileTreeLocation.push fileTreeEntity
  39. currentFileTreeLocation = fileTreeEntity.children
  40. return currentFileTree
  41. ]