FileTreeEntityController.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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('FileTreeEntityController', function(
  18. $scope,
  19. ide,
  20. $modal,
  21. $element
  22. ) {
  23. $scope.MAX_DEPTH = 8
  24. $scope.select = function(e) {
  25. if (e.ctrlKey || e.metaKey) {
  26. e.stopPropagation()
  27. const initialMultiSelectCount = ide.fileTreeManager.multiSelectedCount()
  28. ide.fileTreeManager.toggleMultiSelectEntity($scope.entity) === 0
  29. if (initialMultiSelectCount === 0) {
  30. // On first multi selection, also include the current active/open file.
  31. return ide.fileTreeManager.multiSelectSelectedEntity()
  32. }
  33. } else {
  34. ide.fileTreeManager.selectEntity($scope.entity)
  35. return $scope.$emit('entity:selected', $scope.entity)
  36. }
  37. }
  38. if ($scope.entity.type === 'doc') {
  39. $scope.$watch('entity.selected', function(isSelected) {
  40. if (isSelected) {
  41. $scope.$emit('entity-file:selected', $scope.entity)
  42. if (!_isEntryElVisible($element)) {
  43. $scope.$applyAsync(function() {
  44. $element[0].scrollIntoView()
  45. })
  46. }
  47. }
  48. })
  49. }
  50. function _isEntryElVisible($entryEl) {
  51. const viewportEl = $('.file-tree-list')
  52. const entryElTop = $entryEl.offset().top
  53. const entryElBottom = entryElTop + $entryEl.outerHeight()
  54. const entryListViewportElTop = viewportEl.offset().top
  55. const entryListViewportElBottom =
  56. entryListViewportElTop + viewportEl.height()
  57. return (
  58. entryElTop >= entryListViewportElTop &&
  59. entryElBottom <= entryListViewportElBottom
  60. )
  61. }
  62. $scope.draggableHelper = function() {
  63. if (ide.fileTreeManager.multiSelectedCount() > 0) {
  64. return $(
  65. `<strong style='z-index:100'>${ide.fileTreeManager.multiSelectedCount()} Files</strong>`
  66. )
  67. } else {
  68. return $(`<strong style='z-index:100'>${$scope.entity.name}</strong>`)
  69. }
  70. }
  71. $scope.inputs = { name: $scope.entity.name }
  72. $scope.startRenaming = () => ($scope.entity.renaming = true)
  73. let invalidModalShowing = false
  74. $scope.finishRenaming = function() {
  75. // avoid double events when blur and on-enter fire together
  76. if (!$scope.entity.renaming) {
  77. return
  78. }
  79. const { name } = $scope.inputs
  80. // validator will set name to undefined for invalid filenames
  81. if (name == null) {
  82. // Showing the modal blurs the rename box which calls us again
  83. // so track this with the invalidModalShowing flag
  84. if (invalidModalShowing) {
  85. return
  86. }
  87. invalidModalShowing = true
  88. const modal = $modal.open({
  89. templateUrl: 'invalidFileNameModalTemplate'
  90. })
  91. modal.result.then(() => (invalidModalShowing = false))
  92. return
  93. }
  94. delete $scope.entity.renaming
  95. if (name == null || name.length === 0) {
  96. $scope.inputs.name = $scope.entity.name
  97. return
  98. }
  99. return ide.fileTreeManager.renameEntity($scope.entity, name)
  100. }
  101. $scope.$on('rename:selected', function() {
  102. if ($scope.entity.selected) {
  103. return $scope.startRenaming()
  104. }
  105. })
  106. $scope.openDeleteModal = function() {
  107. let entities
  108. if (ide.fileTreeManager.multiSelectedCount() > 0) {
  109. entities = ide.fileTreeManager.getMultiSelectedEntityChildNodes()
  110. } else {
  111. entities = [$scope.entity]
  112. }
  113. return $modal.open({
  114. templateUrl: 'deleteEntityModalTemplate',
  115. controller: 'DeleteEntityModalController',
  116. resolve: {
  117. entities() {
  118. return entities
  119. }
  120. }
  121. })
  122. }
  123. $scope.$on('delete:selected', function() {
  124. if ($scope.entity.selected) {
  125. return $scope.openDeleteModal()
  126. }
  127. })
  128. return ($scope.iconTypeFromName = iconTypeFromName)
  129. })
  130. export default App.controller('DeleteEntityModalController', function(
  131. $scope,
  132. ide,
  133. $modalInstance,
  134. entities
  135. ) {
  136. $scope.state = { inflight: false }
  137. $scope.entities = entities
  138. $scope.delete = function() {
  139. $scope.state.inflight = true
  140. for (let entity of Array.from($scope.entities)) {
  141. ide.fileTreeManager.deleteEntity(entity)
  142. }
  143. return $modalInstance.close()
  144. }
  145. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  146. })