HistoryV2FileTreeController.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. define(['base'], App =>
  15. App.controller('HistoryV2FileTreeController', [
  16. '$scope',
  17. 'ide',
  18. '_',
  19. function($scope, ide, _) {
  20. let _reducePathsToTree
  21. let _previouslySelectedPathname = null
  22. $scope.currentFileTree = []
  23. const _pathnameExistsInFiles = (pathname, files) =>
  24. _.any(files, file => file.pathname === pathname)
  25. const _getSelectedDefaultFile = function(files) {
  26. let selectedPathname = null
  27. if (
  28. _previouslySelectedPathname != null &&
  29. _pathnameExistsInFiles(_previouslySelectedPathname, files)
  30. ) {
  31. selectedPathname = _previouslySelectedPathname
  32. } else {
  33. const mainFile = _.find(files, file =>
  34. /main\.tex$/.test(file.pathname)
  35. )
  36. if (mainFile != null) {
  37. selectedPathname = _previouslySelectedPathname = mainFile.pathname
  38. } else {
  39. selectedPathname = _previouslySelectedPathname = files[0].pathname
  40. }
  41. }
  42. return _.find(files, { pathname: selectedPathname })
  43. }
  44. $scope.handleFileSelection = file => {
  45. _previouslySelectedPathname = file.pathname
  46. ide.historyManager.selectFile(file)
  47. }
  48. $scope.$watch('history.selection.files', function(files) {
  49. if (files != null && files.length > 0) {
  50. $scope.currentFileTree = _.reduce(files, _reducePathsToTree, [])
  51. ide.historyManager.selectFile(_getSelectedDefaultFile(files))
  52. }
  53. })
  54. return (_reducePathsToTree = function(currentFileTree, fileObject) {
  55. const filePathParts = fileObject.newPathname
  56. ? fileObject.newPathname.split('/')
  57. : fileObject.pathname.split('/')
  58. let currentFileTreeLocation = currentFileTree
  59. for (let index = 0; index < filePathParts.length; index++) {
  60. var fileTreeEntity
  61. var pathPart = filePathParts[index]
  62. const isFile = index === filePathParts.length - 1
  63. if (isFile) {
  64. fileTreeEntity = {
  65. name: pathPart,
  66. pathname: fileObject.pathname,
  67. type: 'file',
  68. operation: fileObject.operation
  69. }
  70. if (fileObject.operation === 'renamed') {
  71. fileTreeEntity.pathname = fileObject.newPathname
  72. fileTreeEntity.oldPathname = fileObject.pathname
  73. }
  74. if (fileObject.operation === 'removed' && fileObject.deletedAtV) {
  75. fileTreeEntity.deletedAtV = fileObject.deletedAtV
  76. }
  77. currentFileTreeLocation.push(fileTreeEntity)
  78. } else {
  79. fileTreeEntity = _.find(
  80. currentFileTreeLocation,
  81. entity => entity.name === pathPart
  82. )
  83. if (fileTreeEntity == null) {
  84. fileTreeEntity = {
  85. name: pathPart,
  86. type: 'folder',
  87. children: []
  88. }
  89. currentFileTreeLocation.push(fileTreeEntity)
  90. }
  91. currentFileTreeLocation = fileTreeEntity.children
  92. }
  93. }
  94. return currentFileTree
  95. })
  96. }
  97. ]))