file-tree-controller.js 2.8 KB

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