historyFileTree.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import _ from 'lodash'
  2. // TODO: This file was created by bulk-decaffeinate.
  3. // Fix any style issues and re-enable lint.
  4. /*
  5. * decaffeinate suggestions:
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. import App from '../../../base'
  10. const historyFileTreeController = function ($scope, $element, $attrs) {
  11. const ctrl = this
  12. ctrl.handleEntityClick = file => ctrl.onSelectedFileChange({ file })
  13. ctrl._fileTree = []
  14. $scope.$watch('$ctrl.files', function (files) {
  15. if (files != null && files.length > 0) {
  16. ctrl._fileTree = _.reduce(files, _reducePathsToTree, [])
  17. }
  18. })
  19. function _reducePathsToTree(currentFileTree, fileObject) {
  20. const filePathParts = fileObject.pathname.split('/')
  21. let currentFileTreeLocation = currentFileTree
  22. for (let index = 0; index < filePathParts.length; index++) {
  23. let fileTreeEntity
  24. const pathPart = filePathParts[index]
  25. const isFile = index === filePathParts.length - 1
  26. if (isFile) {
  27. fileTreeEntity = _.clone(fileObject)
  28. fileTreeEntity.name = pathPart
  29. fileTreeEntity.type = 'file'
  30. currentFileTreeLocation.push(fileTreeEntity)
  31. } else {
  32. fileTreeEntity = _.find(
  33. currentFileTreeLocation,
  34. entity => entity.name === pathPart
  35. )
  36. if (fileTreeEntity == null) {
  37. fileTreeEntity = {
  38. name: pathPart,
  39. type: 'folder',
  40. children: [],
  41. }
  42. currentFileTreeLocation.push(fileTreeEntity)
  43. }
  44. currentFileTreeLocation = fileTreeEntity.children
  45. }
  46. }
  47. return currentFileTree
  48. }
  49. }
  50. export default App.component('historyFileTree', {
  51. bindings: {
  52. files: '<',
  53. selectedPathname: '<',
  54. onSelectedFileChange: '&',
  55. isLoading: '<',
  56. },
  57. controller: historyFileTreeController,
  58. templateUrl: 'historyFileTreeTpl',
  59. })