/static/src/services/person.js

https://github.com/Impactstory/impactstory-tng · JavaScript · 149 lines · 115 code · 26 blank · 8 comment · 12 complexity · 37069012fd6b8fdd68f7b163a5a21913 MD5 · raw file

  1. angular.module('person', [
  2. ])
  3. .factory("Person", function($http, $q, $route, $rootScope, CurrentUser){
  4. var data = {}
  5. var isLoading = false
  6. var badgeSortLevel = {
  7. "gold": 1,
  8. "silver": 2,
  9. "bronze": 3
  10. }
  11. var beltDescriptions = {
  12. white: "initial",
  13. yellow: "promising",
  14. orange: "notable",
  15. brown: "extensive",
  16. black: "exceptional"
  17. }
  18. function load(orcidId, force){
  19. console.log("loading the Person")
  20. // if the data for this profile is already loaded, just return it
  21. // unless we've been told to force a refresh from the server.
  22. if (data.orcid_id == orcidId && !force){
  23. console.log("Person Service getting from cache:", orcidId)
  24. return $q.when(data)
  25. }
  26. var url = "/api/person/" + orcidId
  27. console.log("Person Service getting from server:", orcidId)
  28. $rootScope.progressbar.start()
  29. isLoading = true
  30. return $http.get(url)
  31. .success(function(resp){
  32. // clear the data object and put the new data in
  33. for (var member in data) delete data[member];
  34. overwriteData(resp)
  35. $rootScope.progressbar.complete()
  36. isLoading = false
  37. })
  38. .error(function(resp){
  39. $rootScope.progressbar.complete()
  40. isLoading = false
  41. })
  42. }
  43. function overwriteData(newData){
  44. // put the response in the data object
  45. _.each(newData, function(v, k){
  46. data[k] = v
  47. })
  48. // add computed properties
  49. // total posts
  50. var postCounts = _.pluck(data.sources, "posts_count")
  51. data.numPosts = postCounts.reduce(function(a, b){return a + b}, 0)
  52. // date of earliest publication
  53. var earliestPubYear = _.min(_.pluck(data.products, "year"))
  54. if (earliestPubYear > 0 && earliestPubYear <= 2015) {
  55. data.publishingAge = 2016 - earliestPubYear
  56. }
  57. else {
  58. data.publishingAge = 1
  59. }
  60. }
  61. function setFulltextUrl(productId, fulltextUrl) {
  62. _.each(data.products, function(myProduct){
  63. if (myProduct.id == productId){
  64. myProduct.fulltext_url = fulltextUrl
  65. }
  66. });
  67. var apiUrl = "/api/person/" + data.orcid_id
  68. var postBody = {
  69. product: {
  70. id: productId,
  71. fulltext_url: fulltextUrl
  72. }
  73. }
  74. $http.post(apiUrl, postBody)
  75. .success(function(resp){
  76. console.log("we set the fulltext url!", resp)
  77. overwriteData(resp)
  78. // todo: figure out if we actually need this
  79. $route.reload()
  80. })
  81. .error(function(resp){
  82. console.log("we failed to set the fulltext url", resp)
  83. $route.reload()
  84. })
  85. }
  86. function getBadgesWithConfigs(configDict) {
  87. var ret = []
  88. _.each(data.badges, function(myBadge){
  89. var badgeDef = configDict[myBadge.name]
  90. var enrichedBadge = _.extend(myBadge, badgeDef)
  91. enrichedBadge.sortLevel = badgeSortLevel[enrichedBadge.level]
  92. ret.push(enrichedBadge)
  93. })
  94. return ret
  95. }
  96. function belongsToCurrentUser(){
  97. if (!CurrentUser.isLoggedIn()) {
  98. return false
  99. }
  100. return CurrentUser.d.id == data.id
  101. }
  102. return {
  103. d: data,
  104. load: load,
  105. badgesToShow: function(){
  106. return _.filter(data.badges, function(badge){
  107. return !!badge.show_in_ui
  108. })
  109. },
  110. getBadgesWithConfigs: getBadgesWithConfigs,
  111. setFulltextUrl: setFulltextUrl,
  112. reload: function(){
  113. return load(data.orcid_id, true)
  114. },
  115. clear:function(){
  116. for (var member in data) delete data[member];
  117. },
  118. belongsToCurrentUser: belongsToCurrentUser,
  119. isLoading: function(){
  120. return !!isLoading
  121. }
  122. }
  123. })