file-tree-controller.js 3.9 KB

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