FileTreeEntityController.js 4.5 KB

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