FileTreeEntityController.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if !name.match(new RegExp(ide.validFileRegex))
  31. # Showing the modal blurs the rename box which calls us again
  32. # so track this with the invalidModalShowing flag
  33. return if invalidModalShowing
  34. invalidModalShowing = true
  35. modal = $modal.open(
  36. templateUrl: "invalidFileNameModalTemplate"
  37. )
  38. modal.result.then () ->
  39. invalidModalShowing = false
  40. return
  41. delete $scope.entity.renaming
  42. if !name? or name.length == 0
  43. $scope.inputs.name = $scope.entity.name
  44. return
  45. ide.fileTreeManager.renameEntity($scope.entity, name)
  46. $scope.$on "rename:selected", () ->
  47. $scope.startRenaming() if $scope.entity.selected
  48. $scope.openDeleteModal = () ->
  49. if ide.fileTreeManager.multiSelectedCount() > 0
  50. entities = ide.fileTreeManager.getMultiSelectedEntityChildNodes()
  51. else
  52. entities = [$scope.entity]
  53. $modal.open(
  54. templateUrl: "deleteEntityModalTemplate"
  55. controller: "DeleteEntityModalController"
  56. resolve:
  57. entities: () -> entities
  58. )
  59. $scope.$on "delete:selected", () ->
  60. $scope.openDeleteModal() if $scope.entity.selected
  61. $scope.iconTypeFromName = (name) ->
  62. ext = name.split(".").pop()?.toLowerCase()
  63. if ext in ["png", "pdf", "jpg", "jpeg", "gif"]
  64. return "image"
  65. else if ext in ["csv", "xls", "xlsx"]
  66. return "table"
  67. else if ext in ["py", "r"]
  68. return "file-text"
  69. else if ext in ['bib']
  70. return 'book'
  71. else
  72. return "file"
  73. ]
  74. App.controller "DeleteEntityModalController", [
  75. "$scope", "ide", "$modalInstance", "entities"
  76. ($scope, ide, $modalInstance, entities) ->
  77. $scope.state =
  78. inflight: false
  79. $scope.entities = entities
  80. $scope.delete = () ->
  81. $scope.state.inflight = true
  82. for entity in $scope.entities
  83. ide.fileTreeManager.deleteEntity(entity)
  84. $modalInstance.close()
  85. $scope.cancel = () ->
  86. $modalInstance.dismiss('cancel')
  87. ]