FileTreeEntityController.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "FileTreeEntityController", ["$scope", "ide", "$modal", ($scope, ide, $modal) ->
  5. $scope.select = (e) ->
  6. if e.ctrlKey or e.metaKey
  7. e.stopPropagation()
  8. initialMultiSelectCount = ide.fileTreeManager.multiSelectedCount()
  9. ide.fileTreeManager.toggleMultiSelectEntity($scope.entity) == 0
  10. if initialMultiSelectCount == 0
  11. # On first multi selection, also include the current active/open file.
  12. ide.fileTreeManager.multiSelectSelectedEntity()
  13. else
  14. ide.fileTreeManager.selectEntity($scope.entity)
  15. $scope.$emit "entity:selected", $scope.entity
  16. $scope.draggableHelper = () ->
  17. if ide.fileTreeManager.multiSelectedCount() > 0
  18. return $("<strong style='z-index:100'>#{ide.fileTreeManager.multiSelectedCount()} Files</strong>")
  19. else
  20. return $("<strong style='z-index:100'>#{$scope.entity.name}</strong>")
  21. $scope.inputs =
  22. name: $scope.entity.name
  23. $scope.startRenaming = () ->
  24. $scope.entity.renaming = true
  25. invalidModalShowing = false
  26. $scope.finishRenaming = () ->
  27. # avoid double events when blur and on-enter fire together
  28. return if !$scope.entity.renaming
  29. name = $scope.inputs.name
  30. # validator will set name to undefined for invalid filenames
  31. if !name?
  32. # Showing the modal blurs the rename box which calls us again
  33. # so track this with the invalidModalShowing flag
  34. return if invalidModalShowing
  35. invalidModalShowing = true
  36. modal = $modal.open(
  37. templateUrl: "invalidFileNameModalTemplate"
  38. )
  39. modal.result.then () ->
  40. invalidModalShowing = false
  41. return
  42. delete $scope.entity.renaming
  43. if !name? or name.length == 0
  44. $scope.inputs.name = $scope.entity.name
  45. return
  46. ide.fileTreeManager.renameEntity($scope.entity, name)
  47. $scope.$on "rename:selected", () ->
  48. $scope.startRenaming() if $scope.entity.selected
  49. $scope.openDeleteModal = () ->
  50. if ide.fileTreeManager.multiSelectedCount() > 0
  51. entities = ide.fileTreeManager.getMultiSelectedEntityChildNodes()
  52. else
  53. entities = [$scope.entity]
  54. $modal.open(
  55. templateUrl: "deleteEntityModalTemplate"
  56. controller: "DeleteEntityModalController"
  57. resolve:
  58. entities: () -> entities
  59. )
  60. $scope.$on "delete:selected", () ->
  61. $scope.openDeleteModal() if $scope.entity.selected
  62. $scope.iconTypeFromName = (name) ->
  63. ext = name.split(".").pop()?.toLowerCase()
  64. if ext in ["png", "pdf", "jpg", "jpeg", "gif"]
  65. return "image"
  66. else if ext in ["csv", "xls", "xlsx"]
  67. return "table"
  68. else if ext in ["py", "r"]
  69. return "file-text"
  70. else if ext in ['bib']
  71. return 'book'
  72. else
  73. return "file"
  74. ]
  75. App.controller "DeleteEntityModalController", [
  76. "$scope", "ide", "$modalInstance", "entities"
  77. ($scope, ide, $modalInstance, entities) ->
  78. $scope.state =
  79. inflight: false
  80. $scope.entities = entities
  81. $scope.delete = () ->
  82. $scope.state.inflight = true
  83. for entity in $scope.entities
  84. ide.fileTreeManager.deleteEntity(entity)
  85. $modalInstance.close()
  86. $scope.cancel = () ->
  87. $modalInstance.dismiss('cancel')
  88. ]