file-tree-controller.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import App from '../../../base'
  2. import { react2angular } from 'react2angular'
  3. import { cloneDeep } from 'lodash'
  4. import FileTreeRoot from '../components/file-tree-root'
  5. import { rootContext } from '../../../shared/context/root-context'
  6. App.controller(
  7. 'ReactFileTreeController',
  8. function (
  9. $scope,
  10. $timeout,
  11. ide
  12. // eventTracking
  13. ) {
  14. $scope.isConnected = true
  15. $scope.$on('project:joined', () => {
  16. $scope.$emit('file-tree:initialized')
  17. })
  18. $scope.$watch('editor.open_doc_id', openDocId => {
  19. window.dispatchEvent(
  20. new CustomEvent('editor.openDoc', { detail: openDocId })
  21. )
  22. })
  23. // Set isConnected to true if:
  24. // - connection state is 'ready', OR
  25. // - connection state is 'waitingCountdown' and reconnection_countdown is null
  26. // The added complexity is needed because in Firefox on page reload the
  27. // connection state goes into 'waitingCountdown' before being hidden and we
  28. // don't want to show a disconnect UI.
  29. function updateIsConnected() {
  30. const isReady = $scope.connection.state === 'ready'
  31. const willStartCountdown =
  32. $scope.connection.state === 'waitingCountdown' &&
  33. $scope.connection.reconnection_countdown === null
  34. $scope.isConnected = isReady || willStartCountdown
  35. }
  36. $scope.$watch('connection.state', updateIsConnected)
  37. $scope.$watch('connection.reconnection_countdown', updateIsConnected)
  38. $scope.onInit = () => {
  39. // HACK: resize the vertical pane on init after a 0ms timeout. We do not
  40. // understand why this is necessary but without this the resized handle is
  41. // stuck at the bottom. The vertical resize will soon be migrated to React
  42. // so we accept to live with this hack for now.
  43. $timeout(() => {
  44. $scope.$emit('left-pane-resize-all')
  45. })
  46. }
  47. $scope.onSelect = selectedEntities => {
  48. if (selectedEntities.length === 1) {
  49. const selectedEntity = selectedEntities[0]
  50. const type =
  51. selectedEntity.type === 'fileRef' ? 'file' : selectedEntity.type
  52. $scope.$emit('entity:selected', {
  53. ...selectedEntity.entity,
  54. id: selectedEntity.entity._id,
  55. type,
  56. })
  57. // in the react implementation there is no such concept as "1
  58. // multi-selected entity" so here we pass a count of 0
  59. $scope.$emit('entities:multiSelected', { count: 0 })
  60. } else if (selectedEntities.length > 1) {
  61. $scope.$emit('entities:multiSelected', {
  62. count: selectedEntities.length,
  63. })
  64. } else {
  65. $scope.$emit('entity:no-selection')
  66. }
  67. }
  68. $scope.refProviders = ide.$scope.user.refProviders || {}
  69. ide.$scope.$watch(
  70. 'user.refProviders',
  71. refProviders => {
  72. $scope.refProviders = cloneDeep(refProviders)
  73. },
  74. true
  75. )
  76. $scope.setRefProviderEnabled = provider => {
  77. ide.$scope.$applyAsync(() => {
  78. ide.$scope.user.refProviders[provider] = true
  79. })
  80. }
  81. $scope.setStartedFreeTrial = started => {
  82. $scope.$applyAsync(() => {
  83. $scope.startedFreeTrial = started
  84. })
  85. }
  86. $scope.reindexReferences = () => {
  87. ide.$scope.$emit('references:should-reindex', {})
  88. }
  89. }
  90. )
  91. App.component(
  92. 'fileTreeRoot',
  93. react2angular(
  94. rootContext.use(FileTreeRoot),
  95. Object.keys(FileTreeRoot.propTypes)
  96. )
  97. )