2018-06-15 06:30:15 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2018-06-22 06:24:14 -04:00
|
|
|
App.controller "UserAffiliationsController", ["$scope", "UserAffiliationsDataService", "$q", "_", ($scope, UserAffiliationsDataService, $q, _) ->
|
2018-06-20 09:22:06 -04:00
|
|
|
$scope.userEmails = []
|
2018-06-15 06:30:15 -04:00
|
|
|
|
2018-06-19 12:10:55 -04:00
|
|
|
LOCAL_AND_DOMAIN_REGEX = /([^@]+)@(.+)/
|
2018-06-21 12:18:01 -04:00
|
|
|
EMAIL_REGEX = /^([A-Za-z0-9_\-\.]+)@([^\.]+)\.([A-Za-z0-9_\-\.]+)([^\.])$/
|
2018-06-15 06:30:15 -04:00
|
|
|
|
2018-06-19 12:10:55 -04:00
|
|
|
_matchLocalAndDomain = (userEmailInput) ->
|
|
|
|
match = userEmailInput?.match LOCAL_AND_DOMAIN_REGEX
|
2018-06-15 06:30:15 -04:00
|
|
|
if match?
|
|
|
|
{ local: match[1], domain: match[2] }
|
|
|
|
else
|
|
|
|
{ local: null, domain: null }
|
|
|
|
|
|
|
|
$scope.getEmailSuggestion = (userInput) ->
|
2018-06-19 12:10:55 -04:00
|
|
|
userInputLocalAndDomain = _matchLocalAndDomain(userInput)
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.isValidEmail = EMAIL_REGEX.test userInput
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = false
|
2018-06-19 12:10:55 -04:00
|
|
|
if userInputLocalAndDomain.domain?
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.isBlacklistedEmail = UserAffiliationsDataService.isDomainBlacklisted userInputLocalAndDomain.domain
|
2018-06-19 12:10:55 -04:00
|
|
|
UserAffiliationsDataService.getUniversityDomainFromPartialDomainInput(userInputLocalAndDomain.domain)
|
2018-06-25 11:13:43 -04:00
|
|
|
.then (universityDomain) ->
|
2018-06-26 11:18:53 -04:00
|
|
|
currentUserInputLocalAndDomain = _matchLocalAndDomain $scope.newAffiliation.email
|
|
|
|
if currentUserInputLocalAndDomain.domain == universityDomain.hostname
|
2018-06-19 12:10:55 -04:00
|
|
|
$scope.newAffiliation.university = universityDomain.university
|
|
|
|
$scope.newAffiliation.department = universityDomain.department
|
|
|
|
else
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
|
|
|
$q.resolve "#{userInputLocalAndDomain.local}@#{universityDomain.hostname}"
|
2018-06-15 06:30:15 -04:00
|
|
|
.catch () ->
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
|
|
|
$q.reject null
|
2018-06-19 12:10:55 -04:00
|
|
|
else
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
2018-06-26 11:50:55 -04:00
|
|
|
$q.reject null
|
2018-06-15 06:30:15 -04:00
|
|
|
|
2018-06-19 12:10:55 -04:00
|
|
|
|
|
|
|
$scope.selectUniversityManually = () ->
|
|
|
|
$scope.newAffiliation.university = null
|
|
|
|
$scope.newAffiliation.department = null
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.ui.showManualUniversitySelectionUI = true
|
2018-06-19 12:10:55 -04:00
|
|
|
|
2018-07-04 11:37:40 -04:00
|
|
|
$scope.changeAffiliation = (userEmail) ->
|
|
|
|
$scope.affiliationToChange.email = userEmail.email
|
|
|
|
$scope.affiliationToChange.role = userEmail.affiliation.role
|
|
|
|
$scope.affiliationToChange.department = userEmail.affiliation.department
|
|
|
|
|
|
|
|
$scope.saveAffiliationChange = () ->
|
|
|
|
UserAffiliationsDataService
|
|
|
|
.addRoleAndDepartment(
|
|
|
|
$scope.affiliationToChange.email,
|
|
|
|
$scope.affiliationToChange.role,
|
|
|
|
$scope.affiliationToChange.department
|
|
|
|
)
|
|
|
|
|
|
|
|
$scope.cancelAffiliationChange = (email) ->
|
|
|
|
$scope.affiliationToChange.email = ""
|
|
|
|
$scope.affiliationToChange.role = null
|
|
|
|
$scope.affiliationToChange.department = null
|
|
|
|
|
|
|
|
$scope.isChangingAffiliation = (email) ->
|
|
|
|
$scope.affiliationToChange.email == email
|
|
|
|
|
2018-06-21 12:18:01 -04:00
|
|
|
$scope.showAddEmailForm = () ->
|
|
|
|
$scope.ui.showAddEmailUI = true
|
|
|
|
|
|
|
|
$scope.addNewEmail = () ->
|
|
|
|
$scope.ui.isAddingNewEmail = true
|
2018-06-19 12:10:55 -04:00
|
|
|
if !$scope.newAffiliation.university?
|
2018-06-20 09:22:06 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService
|
|
|
|
.addUserEmail $scope.newAffiliation.email
|
2018-06-19 12:10:55 -04:00
|
|
|
else
|
|
|
|
if $scope.newAffiliation.university.isUserSuggested
|
2018-06-20 09:22:06 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService
|
|
|
|
.addUserAffiliationWithUnknownUniversity(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.name,
|
|
|
|
$scope.newAffiliation.country.code,
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
|
|
|
)
|
2018-06-19 12:10:55 -04:00
|
|
|
else
|
2018-06-20 09:22:06 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService
|
|
|
|
.addUserAffiliation(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.id
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
|
|
|
)
|
2018-06-21 12:18:01 -04:00
|
|
|
addEmailPromise.then () ->
|
|
|
|
_reset()
|
|
|
|
_getUserEmails()
|
|
|
|
|
2018-07-04 11:54:04 -04:00
|
|
|
$scope.setDefaultUserEmail = (userEmail) ->
|
2018-06-25 08:44:43 -04:00
|
|
|
$scope.ui.isLoadingEmails = true
|
2018-06-21 12:18:01 -04:00
|
|
|
UserAffiliationsDataService
|
2018-07-04 11:54:04 -04:00
|
|
|
.setDefaultUserEmail userEmail.email
|
2018-06-21 12:18:01 -04:00
|
|
|
.then () -> _getUserEmails()
|
|
|
|
|
2018-07-04 11:54:04 -04:00
|
|
|
$scope.removeUserEmail = (userEmail) ->
|
2018-06-25 08:44:43 -04:00
|
|
|
$scope.ui.isLoadingEmails = true
|
2018-07-04 11:54:04 -04:00
|
|
|
userEmailIdx = _.indexOf $scope.userEmails, userEmail
|
|
|
|
if userEmailIdx > -1
|
|
|
|
$scope.userEmails.splice userEmailIdx, 1
|
2018-06-21 12:18:01 -04:00
|
|
|
UserAffiliationsDataService
|
2018-07-04 11:54:04 -04:00
|
|
|
.removeUserEmail userEmail.email
|
2018-06-21 12:18:01 -04:00
|
|
|
.then () -> _getUserEmails()
|
|
|
|
|
|
|
|
_reset = () ->
|
|
|
|
$scope.newAffiliation =
|
|
|
|
email: ""
|
|
|
|
country: null
|
|
|
|
university: null
|
|
|
|
role: null
|
|
|
|
department: null
|
|
|
|
$scope.ui =
|
2018-07-04 11:37:40 -04:00
|
|
|
showChangeAffiliationUI: false
|
2018-06-21 12:18:01 -04:00
|
|
|
showManualUniversitySelectionUI: false
|
|
|
|
isLoadingEmails: false
|
|
|
|
isAddingNewEmail: false
|
|
|
|
showAddEmailUI: false
|
|
|
|
isValidEmail: false
|
|
|
|
isBlacklistedEmail: false
|
2018-07-04 11:37:40 -04:00
|
|
|
$scope.affiliationToChange =
|
|
|
|
email: ""
|
|
|
|
role: null
|
|
|
|
department: null
|
2018-06-21 12:18:01 -04:00
|
|
|
_reset()
|
|
|
|
|
2018-06-20 09:22:06 -04:00
|
|
|
# Populates the emails table
|
2018-06-21 12:18:01 -04:00
|
|
|
_getUserEmails = () ->
|
|
|
|
$scope.ui.isLoadingEmails = true
|
2018-06-20 09:22:06 -04:00
|
|
|
UserAffiliationsDataService
|
|
|
|
.getUserEmails()
|
2018-06-21 12:18:01 -04:00
|
|
|
.then (emails) ->
|
|
|
|
$scope.userEmails = emails
|
|
|
|
$scope.ui.isLoadingEmails = false
|
|
|
|
_getUserEmails()
|
2018-06-19 12:10:55 -04:00
|
|
|
|
2018-06-15 06:30:15 -04:00
|
|
|
]
|