file-tree-controller.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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('ReactFileTreeController', function (
  7. $scope,
  8. $timeout,
  9. ide
  10. // eventTracking
  11. ) {
  12. $scope.projectId = ide.project_id
  13. $scope.rootFolder = null
  14. $scope.rootDocId = null
  15. $scope.hasWritePermissions = false
  16. $scope.isConnected = true
  17. $scope.$on('project:joined', () => {
  18. $scope.rootFolder = $scope.project.rootFolder
  19. $scope.rootDocId = $scope.project.rootDoc_id
  20. $scope.$emit('file-tree:initialized')
  21. })
  22. $scope.$watch('permissions.write', hasWritePermissions => {
  23. $scope.hasWritePermissions = hasWritePermissions
  24. })
  25. $scope.$watch('editor.open_doc_id', openDocId => {
  26. window.dispatchEvent(
  27. new CustomEvent('editor.openDoc', { detail: openDocId })
  28. )
  29. })
  30. // Set isConnected to true if:
  31. // - connection state is 'ready', OR
  32. // - connection state is 'waitingCountdown' and reconnection_countdown is null
  33. // The added complexity is needed because in Firefox on page reload the
  34. // connection state goes into 'waitingCountdown' before being hidden and we
  35. // don't want to show a disconnect UI.
  36. function updateIsConnected() {
  37. const isReady = $scope.connection.state === 'ready'
  38. const willStartCountdown =
  39. $scope.connection.state === 'waitingCountdown' &&
  40. $scope.connection.reconnection_countdown === null
  41. $scope.isConnected = isReady || willStartCountdown
  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.userHasFeature = feature => ide.$scope.user.features[feature]
  76. $scope.$watch('permissions.write', hasWritePermissions => {
  77. $scope.hasWritePermissions = hasWritePermissions
  78. })
  79. $scope.refProviders = ide.$scope.user.refProviders || {}
  80. ide.$scope.$watch(
  81. 'user.refProviders',
  82. refProviders => {
  83. $scope.refProviders = cloneDeep(refProviders)
  84. },
  85. true
  86. )
  87. $scope.setRefProviderEnabled = provider => {
  88. ide.$scope.$applyAsync(() => {
  89. ide.$scope.user.refProviders[provider] = true
  90. })
  91. }
  92. $scope.setStartedFreeTrial = started => {
  93. $scope.$applyAsync(() => {
  94. $scope.startedFreeTrial = started
  95. })
  96. }
  97. $scope.reindexReferences = () => {
  98. ide.$scope.$emit('references:should-reindex', {})
  99. }
  100. })
  101. App.component(
  102. 'fileTreeRoot',
  103. react2angular(
  104. rootContext.use(FileTreeRoot),
  105. Object.keys(FileTreeRoot.propTypes)
  106. )
  107. )