file-tree-controller.js 3.7 KB

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