FileTreeEntityController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* eslint-disable
  2. chai-friendly/no-unused-expressions,
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. import App from '../../../base'
  16. import iconTypeFromName from '../util/iconTypeFromName'
  17. App.controller(
  18. 'FileTreeEntityController',
  19. function ($scope, ide, $modal, $element) {
  20. $scope.MAX_DEPTH = 8
  21. $scope.select = function (e) {
  22. if (e.ctrlKey || e.metaKey) {
  23. e.stopPropagation()
  24. const initialMultiSelectCount = ide.fileTreeManager.multiSelectedCount()
  25. ide.fileTreeManager.toggleMultiSelectEntity($scope.entity) === 0
  26. if (initialMultiSelectCount === 0) {
  27. // On first multi selection, also include the current active/open file.
  28. return ide.fileTreeManager.multiSelectSelectedEntity()
  29. }
  30. } else {
  31. ide.fileTreeManager.selectEntity($scope.entity)
  32. return $scope.$emit('entity:selected', $scope.entity)
  33. }
  34. }
  35. if ($scope.entity.type === 'doc') {
  36. $scope.$watch('entity.selected', function (isSelected) {
  37. if (isSelected) {
  38. $scope.$emit('entity-file:selected', $scope.entity)
  39. if (!_isEntryElVisible($element)) {
  40. $scope.$applyAsync(function () {
  41. $element[0].scrollIntoView()
  42. })
  43. }
  44. }
  45. })
  46. }
  47. function _isEntryElVisible($entryEl) {
  48. const viewportEl = $('.file-tree-list')
  49. const entryElTop = $entryEl.offset().top
  50. const entryElBottom = entryElTop + $entryEl.outerHeight()
  51. const entryListViewportElTop = viewportEl.offset().top
  52. const entryListViewportElBottom =
  53. entryListViewportElTop + viewportEl.height()
  54. return (
  55. entryElTop >= entryListViewportElTop &&
  56. entryElBottom <= entryListViewportElBottom
  57. )
  58. }
  59. $scope.draggableHelper = function () {
  60. if (ide.fileTreeManager.multiSelectedCount() > 0) {
  61. return $(
  62. `<strong style='z-index:100'>${ide.fileTreeManager.multiSelectedCount()} Files</strong>`
  63. )
  64. } else {
  65. return $(`<strong style='z-index:100'>${$scope.entity.name}</strong>`)
  66. }
  67. }
  68. $scope.inputs = { name: $scope.entity.name }
  69. $scope.startRenaming = () => ($scope.entity.renaming = true)
  70. let invalidModalShowing = false
  71. $scope.finishRenaming = function () {
  72. // avoid double events when blur and on-enter fire together
  73. if (!$scope.entity.renaming) {
  74. return
  75. }
  76. const { name } = $scope.inputs
  77. // validator will set name to undefined for invalid filenames
  78. if (name == null) {
  79. // Showing the modal blurs the rename box which calls us again
  80. // so track this with the invalidModalShowing flag
  81. if (invalidModalShowing) {
  82. return
  83. }
  84. invalidModalShowing = true
  85. const modal = $modal.open({
  86. templateUrl: 'invalidFileNameModalTemplate',
  87. })
  88. modal.result.then(() => (invalidModalShowing = false))
  89. return
  90. }
  91. delete $scope.entity.renaming
  92. if (name == null || name.length === 0) {
  93. $scope.inputs.name = $scope.entity.name
  94. return
  95. }
  96. return ide.fileTreeManager.renameEntity($scope.entity, name)
  97. }
  98. $scope.$on('rename:selected', function () {
  99. if ($scope.entity.selected) {
  100. return $scope.startRenaming()
  101. }
  102. })
  103. $scope.openDeleteModal = function () {
  104. let entities
  105. if (ide.fileTreeManager.multiSelectedCount() > 0) {
  106. entities = ide.fileTreeManager.getMultiSelectedEntityChildNodes()
  107. } else {
  108. entities = [$scope.entity]
  109. }
  110. return $modal.open({
  111. templateUrl: 'deleteEntityModalTemplate',
  112. controller: 'DeleteEntityModalController',
  113. resolve: {
  114. entities() {
  115. return entities
  116. },
  117. },
  118. })
  119. }
  120. $scope.$on('delete:selected', function () {
  121. if ($scope.entity.selected) {
  122. return $scope.openDeleteModal()
  123. }
  124. })
  125. return ($scope.iconTypeFromName = iconTypeFromName)
  126. }
  127. )
  128. export default App.controller(
  129. 'DeleteEntityModalController',
  130. function ($scope, ide, $modalInstance, entities) {
  131. $scope.state = { inflight: false }
  132. $scope.entities = entities
  133. $scope.delete = function () {
  134. $scope.state.inflight = true
  135. for (const entity of Array.from($scope.entities)) {
  136. ide.fileTreeManager.deleteEntity(entity)
  137. }
  138. return $modalInstance.close()
  139. }
  140. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  141. }
  142. )