ShareProjectModalController.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "ShareProjectModalController", ($scope, $modalInstance, $timeout, projectMembers, $modal, $http) ->
  5. $scope.inputs = {
  6. privileges: "readAndWrite"
  7. email: ""
  8. }
  9. $scope.state = {
  10. error: null
  11. inflight: false
  12. startedFreeTrial: false
  13. }
  14. $modalInstance.opened.then () ->
  15. $timeout () ->
  16. $scope.$broadcast "open"
  17. , 200
  18. INFINITE_COLLABORATORS = -1
  19. $scope.$watch "project.members.length", (noOfMembers) ->
  20. allowedNoOfMembers = $scope.project.features.collaborators
  21. $scope.canAddCollaborators = noOfMembers < allowedNoOfMembers or allowedNoOfMembers == INFINITE_COLLABORATORS
  22. $scope.$watchCollection "inputs.contacts", (value) ->
  23. console.log "EMAILS", value
  24. $scope.autocompleteContacts = []
  25. do loadAutocompleteUsers = () ->
  26. $http.get "/user/contacts"
  27. .success (data) ->
  28. $scope.autocompleteContacts = data.contacts or []
  29. for contact in $scope.autocompleteContacts
  30. if contact.type == "user"
  31. if contact.last_name == "" and contact.first_name = contact.email.split("@")[0]
  32. # User has not set their proper name so use email as canonical display property
  33. contact.name = ""
  34. contact.display = contact.email
  35. else
  36. contact.name = "#{contact.first_name} #{contact.last_name}"
  37. contact.display = "#{contact.name} <#{contact.email}>"
  38. else
  39. # Must be a group
  40. contact.display = contact.name
  41. $scope.filterAutocompleteUsers = ($query) ->
  42. return $scope.autocompleteContacts.filter (contact) ->
  43. for text in [contact.name, contact.email]
  44. if text?.toLowerCase().indexOf($query.toLowerCase()) > -1
  45. return true
  46. return false
  47. $scope.addMembers = () ->
  48. $timeout -> # Give email list a chance to update
  49. return if $scope.inputs.contacts.length == 0
  50. console.warn "Ignoring groups for now"
  51. emails = $scope.inputs.contacts.filter (contact) -> contact.type == "user"
  52. emails = emails.map (contact) -> contact.email
  53. $scope.inputs.contacts = []
  54. $scope.state.error = null
  55. $scope.state.inflight = true
  56. console.log "Adding emails", emails
  57. do addNextMember = () ->
  58. if emails.length == 0 or !$scope.canAddCollaborators
  59. $scope.state.inflight = false
  60. $scope.$apply()
  61. return
  62. email = emails.shift()
  63. projectMembers
  64. .addMember(email, $scope.inputs.privileges)
  65. .success (data) ->
  66. if data?.user # data.user is false if collaborator limit is hit.
  67. $scope.project.members.push data.user
  68. setTimeout () ->
  69. # Give $scope a chance to update $scope.canAddCollaborators
  70. # with new collaborator information.
  71. addNextMember()
  72. , 0
  73. .error () ->
  74. $scope.state.inflight = false
  75. $scope.state.error = "Sorry, something went wrong :("
  76. $scope.removeMember = (member) ->
  77. $scope.state.error = null
  78. $scope.state.inflight = true
  79. projectMembers
  80. .removeMember(member)
  81. .success () ->
  82. $scope.state.inflight = false
  83. index = $scope.project.members.indexOf(member)
  84. return if index == -1
  85. $scope.project.members.splice(index, 1)
  86. .error () ->
  87. $scope.state.inflight = false
  88. $scope.state.error = "Sorry, something went wrong :("
  89. $scope.openMakePublicModal = () ->
  90. $modal.open {
  91. templateUrl: "makePublicModalTemplate"
  92. controller: "MakePublicModalController"
  93. scope: $scope
  94. }
  95. $scope.openMakePrivateModal = () ->
  96. $modal.open {
  97. templateUrl: "makePrivateModalTemplate"
  98. controller: "MakePrivateModalController"
  99. scope: $scope
  100. }
  101. $scope.done = () ->
  102. $modalInstance.close()
  103. $scope.cancel = () ->
  104. $modalInstance.dismiss()
  105. App.controller "MakePublicModalController", ["$scope", "$modalInstance", "settings", ($scope, $modalInstance, settings) ->
  106. $scope.inputs = {
  107. privileges: "readAndWrite"
  108. }
  109. $scope.makePublic = () ->
  110. $scope.project.publicAccesLevel = $scope.inputs.privileges
  111. settings.saveProjectSettings({publicAccessLevel: $scope.inputs.privileges})
  112. $modalInstance.close()
  113. $scope.cancel = () ->
  114. $modalInstance.dismiss()
  115. ]
  116. App.controller "MakePrivateModalController", ["$scope", "$modalInstance", "settings", ($scope, $modalInstance, settings) ->
  117. $scope.makePrivate = () ->
  118. $scope.project.publicAccesLevel = "private"
  119. settings.saveProjectSettings({publicAccessLevel: "private"})
  120. $modalInstance.close()
  121. $scope.cancel = () ->
  122. $modalInstance.dismiss()
  123. ]