file-tree-controller.js 3.6 KB

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