affiliationForm.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import App from '../../../base'
  15. const affiliationFormController = function (
  16. $scope,
  17. $element,
  18. $attrs,
  19. UserAffiliationsDataService
  20. ) {
  21. const ctrl = this
  22. ctrl.roles = []
  23. ctrl.departments = []
  24. ctrl.countries = []
  25. ctrl.universities = []
  26. let _defaultDepartments = []
  27. ctrl.addUniversityToSelection = universityName => ({
  28. name: universityName,
  29. isUserSuggested: true,
  30. })
  31. ctrl.handleFreeformInputChange = function ($select, propertyToMatch) {
  32. if ($select.search == null || $select.search === '') {
  33. return
  34. }
  35. let resultingItem = $select.search
  36. if (($select.tagging != null ? $select.tagging.fct : undefined) != null) {
  37. resultingItem = $select.tagging.fct($select.search)
  38. }
  39. if (propertyToMatch != null) {
  40. const matchingItem = _.find(
  41. $select.items,
  42. item => item[propertyToMatch] === $select.search
  43. )
  44. if (matchingItem != null) {
  45. resultingItem = matchingItem
  46. }
  47. }
  48. return $select.searchInput.scope().$broadcast('uis:select', resultingItem)
  49. }
  50. // Populates the countries dropdown
  51. UserAffiliationsDataService.getCountries().then(
  52. countries => (ctrl.countries = countries)
  53. )
  54. // Populates the roles dropdown
  55. UserAffiliationsDataService.getDefaultRoleHints().then(
  56. roles => (ctrl.roles = roles)
  57. )
  58. // Fetches the default department hints
  59. UserAffiliationsDataService.getDefaultDepartmentHints().then(
  60. departments => (_defaultDepartments = departments)
  61. )
  62. // Populates the universities dropdown (after selecting a country)
  63. $scope.$watch(
  64. '$ctrl.affiliationData.country',
  65. function (newSelectedCountry, prevSelectedCountry) {
  66. if (
  67. newSelectedCountry != null &&
  68. newSelectedCountry !== prevSelectedCountry
  69. ) {
  70. ctrl.affiliationData.university = null
  71. ctrl.affiliationData.role = null
  72. ctrl.affiliationData.department = null
  73. return UserAffiliationsDataService.getUniversitiesFromCountry(
  74. newSelectedCountry
  75. ).then(universities => (ctrl.universities = universities))
  76. }
  77. }
  78. )
  79. // Populates the departments dropdown (after selecting a university)
  80. $scope.$watch(
  81. '$ctrl.affiliationData.university',
  82. function (newSelectedUniversity, prevSelectedUniversity) {
  83. if (
  84. newSelectedUniversity != null &&
  85. newSelectedUniversity !== prevSelectedUniversity &&
  86. (newSelectedUniversity.departments != null
  87. ? newSelectedUniversity.departments.length
  88. : undefined) > 0
  89. ) {
  90. return (ctrl.departments = _.uniq(newSelectedUniversity.departments))
  91. } else {
  92. return (ctrl.departments = _defaultDepartments)
  93. }
  94. }
  95. )
  96. }
  97. export default App.component('affiliationForm', {
  98. bindings: {
  99. affiliationData: '=',
  100. showUniversityAndCountry: '<',
  101. showRoleAndDepartment: '<',
  102. },
  103. controller: affiliationFormController,
  104. templateUrl: 'affiliationFormTpl',
  105. })