FileTreeEntityController.js 4.8 KB

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