file-tree-controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import App from '../../../base'
  2. import { react2angular } from 'react2angular'
  3. import { cloneDeep } from 'lodash'
  4. import FileTreeRoot from '../components/file-tree-root'
  5. App.controller('ReactFileTreeController', function(
  6. $scope,
  7. $timeout,
  8. ide
  9. // eventTracking
  10. ) {
  11. $scope.projectId = ide.project_id
  12. $scope.rootFolder = null
  13. $scope.rootDocId = null
  14. $scope.hasWritePermissions = false
  15. $scope.isConnected = true
  16. $scope.$on('project:joined', () => {
  17. $scope.rootFolder = $scope.project.rootFolder
  18. $scope.rootDocId = $scope.project.rootDoc_id
  19. $scope.$emit('file-tree:initialized')
  20. })
  21. $scope.$watch('permissions.write', hasWritePermissions => {
  22. $scope.hasWritePermissions = hasWritePermissions
  23. })
  24. $scope.$watch('editor.open_doc_id', openDocId => {
  25. window.dispatchEvent(
  26. new CustomEvent('editor.openDoc', { detail: openDocId })
  27. )
  28. })
  29. // Set isConnected to true if:
  30. // - connection state is 'ready', OR
  31. // - connection state is 'waitingCountdown' and reconnection_countdown is null
  32. // The added complexity is needed because in Firefox on page reload the
  33. // connection state goes into 'waitingCountdown' before being hidden and we
  34. // don't want to show a disconnect UI.
  35. function updateIsConnected() {
  36. const isReady = $scope.connection.state === 'ready'
  37. const willStartCountdown =
  38. $scope.connection.state === 'waitingCountdown' &&
  39. $scope.connection.reconnection_countdown === null
  40. $scope.isConnected = isReady || willStartCountdown
  41. }
  42. $scope.$watch('connection.state', updateIsConnected)
  43. $scope.$watch('connection.reconnection_countdown', updateIsConnected)
  44. $scope.onInit = () => {
  45. // HACK: resize the vertical pane on init after a 0ms timeout. We do not
  46. // understand why this is necessary but without this the resized handle is
  47. // stuck at the bottom. The vertical resize will soon be migrated to React
  48. // so we accept to live with this hack for now.
  49. $timeout(() => {
  50. $scope.$emit('left-pane-resize-all')
  51. })
  52. }
  53. $scope.onSelect = selectedEntities => {
  54. if (selectedEntities.length === 1) {
  55. const selectedEntity = selectedEntities[0]
  56. const type =
  57. selectedEntity.type === 'fileRef' ? 'file' : selectedEntity.type
  58. $scope.$emit('entity:selected', {
  59. ...selectedEntity.entity,
  60. id: selectedEntity.entity._id,
  61. type
  62. })
  63. // in the react implementation there is no such concept as "1
  64. // multi-selected entity" so here we pass a count of 0
  65. $scope.$emit('entities:multiSelected', { count: 0 })
  66. } else if (selectedEntities.length > 1) {
  67. $scope.$emit('entities:multiSelected', {
  68. count: selectedEntities.length
  69. })
  70. } else {
  71. $scope.$emit('entity:no-selection')
  72. }
  73. }
  74. $scope.hasFeature = feature =>
  75. ide.$scope.project.features[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('fileTreeRoot', react2angular(FileTreeRoot))