2014-06-16 13:49:35 +00:00
|
|
|
define ["libs/algolia", "libs/angular", "libs/angular-autocomplete/angular-autocomplete"], (algolia)->
|
2014-06-11 16:45:09 +00:00
|
|
|
|
2014-06-16 13:49:35 +00:00
|
|
|
app = angular.module("userProfileInformationApp", ["autocomplete"])
|
2014-06-11 16:45:09 +00:00
|
|
|
|
2014-06-13 16:17:58 +00:00
|
|
|
app.factory "Institutions", ->
|
2014-06-17 11:25:49 +00:00
|
|
|
new AlgoliaSearch(window.algolia.institutions.app_id, window.algolia.institutions.api_key).initIndex("institutions")
|
2014-06-11 16:45:09 +00:00
|
|
|
|
2014-06-16 15:22:45 +00:00
|
|
|
app.directive "focusInput", ($timeout) ->
|
|
|
|
return (scope, element, attr) ->
|
|
|
|
scope.$watch attr.focusInput, (value) ->
|
|
|
|
if value
|
|
|
|
$timeout ->
|
|
|
|
element.select()
|
|
|
|
|
2014-06-13 16:17:58 +00:00
|
|
|
app.controller "UpdateForm", ($scope, $http, Institutions)->
|
2014-06-16 13:49:35 +00:00
|
|
|
$scope.institutions = []
|
2014-06-16 15:14:58 +00:00
|
|
|
$scope.formVisable = false
|
2014-06-12 16:15:54 +00:00
|
|
|
$scope.hidePersonalInfoSection = true
|
2014-06-17 10:33:37 +00:00
|
|
|
$scope.roles = ["Student", "Post-graduate student", "Post-doctoral researcher", "Lecturer", "Professor"]
|
2014-06-11 16:45:09 +00:00
|
|
|
|
2014-06-12 16:15:54 +00:00
|
|
|
$http.get("/user/personal_info").success (data)->
|
2014-06-16 15:14:58 +00:00
|
|
|
$scope.userInfoForm =
|
2014-06-17 13:36:17 +00:00
|
|
|
first_name: data.first_name || ""
|
|
|
|
last_name: data.last_name || ""
|
|
|
|
role: data.role || ""
|
|
|
|
institution: data.institution || ""
|
2014-06-12 16:15:54 +00:00
|
|
|
_csrf : window.csrfToken
|
2014-06-11 16:45:09 +00:00
|
|
|
|
2014-06-12 16:15:54 +00:00
|
|
|
if getPercentComplete() != 100
|
2014-06-12 14:50:00 +00:00
|
|
|
$scope.percentComplete = getPercentComplete()
|
2014-06-12 16:15:54 +00:00
|
|
|
$scope.hidePersonalInfoSection = false
|
|
|
|
|
2014-06-16 15:14:58 +00:00
|
|
|
$scope.showForm = ->
|
|
|
|
$scope.formVisable = true
|
|
|
|
|
2014-06-12 16:15:54 +00:00
|
|
|
$scope.sendUpdate = ->
|
|
|
|
request = $http.post "/user/personal_info", $scope.userInfoForm
|
|
|
|
request.success (data, status)->
|
|
|
|
request.error (data, status)->
|
|
|
|
console.log "the request failed"
|
|
|
|
$scope.percentComplete = getPercentComplete()
|
2014-06-12 09:23:42 +00:00
|
|
|
|
2014-06-12 16:15:54 +00:00
|
|
|
getPercentComplete = ->
|
2014-06-17 13:36:17 +00:00
|
|
|
results = _.filter $scope.userInfoForm, (value)-> !value? or value?.length != 0
|
2014-06-12 16:15:54 +00:00
|
|
|
results.length * 20
|
2014-06-12 14:50:00 +00:00
|
|
|
|
2014-06-17 10:27:45 +00:00
|
|
|
$scope.updateInstitutionsList = (inputVal)->
|
|
|
|
|
|
|
|
# this is a little hack to use until we change auto compelete lib with redesign and can
|
|
|
|
# listen for blur events on institution field to send the post
|
|
|
|
if inputVal?.indexOf("(") != -1 and inputVal?.indexOf(")") != -1
|
|
|
|
$scope.sendUpdate()
|
|
|
|
|
2014-06-13 16:17:58 +00:00
|
|
|
Institutions.search $scope.userInfoForm.institution, (err, response)->
|
2014-06-16 17:41:44 +00:00
|
|
|
$scope.institutions = _.map response.hits, (institution)->
|
2014-06-17 13:44:24 +00:00
|
|
|
"#{institution.name} (#{institution.domain})"
|
2014-06-13 16:17:58 +00:00
|
|
|
|
2014-06-16 15:14:58 +00:00
|
|
|
|
|
|
|
|
2014-06-12 16:15:54 +00:00
|
|
|
angular.bootstrap(document.getElementById("userProfileInformation"), ['userProfileInformationApp'])
|
2014-06-12 09:23:42 +00:00
|
|
|
|