2014-06-17 07:43:42 -04:00
|
|
|
define [
|
|
|
|
"base"
|
2014-07-01 09:25:32 -04:00
|
|
|
"../../libs/algolia-2.5.2"
|
2014-06-17 07:43:42 -04:00
|
|
|
], (App, algolia)->
|
2014-06-13 12:17:58 -04:00
|
|
|
app.factory "Institutions", ->
|
2014-06-17 07:25:49 -04:00
|
|
|
new AlgoliaSearch(window.algolia.institutions.app_id, window.algolia.institutions.api_key).initIndex("institutions")
|
2014-06-11 12:45:09 -04:00
|
|
|
|
2014-06-17 07:43:42 -04:00
|
|
|
App.controller "UpdateForm", ($scope, $http, Institutions)->
|
2014-06-16 09:49:35 -04:00
|
|
|
$scope.institutions = []
|
2014-06-16 11:14:58 -04:00
|
|
|
$scope.formVisable = false
|
2014-06-12 12:15:54 -04:00
|
|
|
$scope.hidePersonalInfoSection = true
|
2014-06-17 06:33:37 -04:00
|
|
|
$scope.roles = ["Student", "Post-graduate student", "Post-doctoral researcher", "Lecturer", "Professor"]
|
2014-06-11 12:45:09 -04:00
|
|
|
|
2014-06-12 12:15:54 -04:00
|
|
|
$http.get("/user/personal_info").success (data)->
|
2014-06-16 11:14:58 -04:00
|
|
|
$scope.userInfoForm =
|
2014-06-17 09:36:17 -04:00
|
|
|
first_name: data.first_name || ""
|
|
|
|
last_name: data.last_name || ""
|
|
|
|
role: data.role || ""
|
|
|
|
institution: data.institution || ""
|
2014-06-12 12:15:54 -04:00
|
|
|
_csrf : window.csrfToken
|
2014-06-11 12:45:09 -04:00
|
|
|
|
2014-06-12 12:15:54 -04:00
|
|
|
if getPercentComplete() != 100
|
2014-06-12 10:50:00 -04:00
|
|
|
$scope.percentComplete = getPercentComplete()
|
2014-06-12 12:15:54 -04:00
|
|
|
$scope.hidePersonalInfoSection = false
|
|
|
|
|
2014-06-16 11:14:58 -04:00
|
|
|
$scope.showForm = ->
|
|
|
|
$scope.formVisable = true
|
|
|
|
|
2014-06-12 12:15:54 -04:00
|
|
|
$scope.sendUpdate = ->
|
2014-06-20 06:15:25 -04:00
|
|
|
request = $http.post "/user/settings", $scope.userInfoForm
|
2014-06-12 12:15:54 -04:00
|
|
|
request.success (data, status)->
|
|
|
|
request.error (data, status)->
|
|
|
|
console.log "the request failed"
|
|
|
|
$scope.percentComplete = getPercentComplete()
|
2014-06-12 05:23:42 -04:00
|
|
|
|
2014-06-12 12:15:54 -04:00
|
|
|
getPercentComplete = ->
|
2014-06-17 09:36:17 -04:00
|
|
|
results = _.filter $scope.userInfoForm, (value)-> !value? or value?.length != 0
|
2014-06-12 12:15:54 -04:00
|
|
|
results.length * 20
|
2014-06-12 10:50:00 -04:00
|
|
|
|
2014-06-17 06:27:45 -04: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 12:17:58 -04:00
|
|
|
Institutions.search $scope.userInfoForm.institution, (err, response)->
|
2014-06-16 13:41:44 -04:00
|
|
|
$scope.institutions = _.map response.hits, (institution)->
|
2014-06-17 09:44:24 -04:00
|
|
|
"#{institution.name} (#{institution.domain})"
|
2014-06-13 12:17:58 -04:00
|
|
|
|