FileTreeEntityController.js 4.8 KB

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