file-tree-controller.js 3.5 KB

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