historyFileTree.js 1.9 KB

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