spacedeck_teams.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. SpacedeckTeams
  3. This module contains functions dealing with Teams.
  4. */
  5. var SpacedeckTeams = {
  6. data: {
  7. team_members: [],
  8. team_loading: false,
  9. team_logo: "",
  10. team_emails: "",
  11. team_email_invited: false,
  12. team_plan_calculation: "",
  13. },
  14. methods: {
  15. is_admin: function(user) {
  16. return _.filter(user.team.admins, function(admin_id) {
  17. return admin_id == user._id;
  18. }).length > 0;
  19. },
  20. calculate_team: function() {
  21. this.team_plan_calculation = "";
  22. },
  23. load_team: function() {
  24. if (this.user.team) {
  25. load_resource("GET", "/teams/" + this.user.team._id + "/memberships", null, function(members) {
  26. this.team_members = members;
  27. this.calculate_team();
  28. }.bind(this), function(xhr, textStatus, errorThrown) {
  29. console.log(xhr, textStatus, errorThrown);
  30. });
  31. }
  32. },
  33. team_save: function() {
  34. load_resource("PUT", "/teams/" + this.user.team._id , this.user.team, function(res, xhr) {
  35. alert("Team updated.");
  36. }.bind(this), function(xhr) {
  37. console.error(xhr);
  38. alert("Could not update Team.");
  39. });
  40. },
  41. team_update_member: function(m){
  42. load_resource("PUT", "/teams/" + this.user.team._id + "/memberships/" + m._id, m, function(res, xhr) {
  43. console.log("members updated");
  44. }.bind(this), function(xhr) {
  45. console.error(xhr);
  46. });
  47. },
  48. team_invite_members: function(emails) {
  49. var emailList = emails.split(",");
  50. for (_i = 0, _len = emailList.length; _i < _len; _i++) {
  51. email = emailList[_i];
  52. email = email.replace(new RegExp(" ", "g"), "").toLowerCase();
  53. if (validateEmail(email)) {
  54. var data = {
  55. email: email
  56. };
  57. load_resource("POST", "/teams/" + this.user.team._id + "/memberships", data, function(res, xhr) {
  58. this.team_email_invited = true;
  59. this.team_members.push(res);
  60. var timeoutID = window.setTimeout(function(){
  61. this.team_email_invited = false;
  62. }.bind(this), 1000);
  63. this.team_emails = "";
  64. }.bind(this), function(xhr, textStatus, errorThrown) {
  65. console.log(xhr, textStatus, errorThrown);
  66. this.team_invite_error = JSON.parse(xhr.responseText).error;
  67. }.bind(this));
  68. }
  69. }
  70. },
  71. team_promote_member: function(m) {
  72. load_resource("GET", "/teams/" + this.user.team._id + "/memberships/" + m._id + "/promote" , null, function(res, xhr) {
  73. this.load_user(function() {
  74. this.load_team();
  75. }.bind(this));
  76. }.bind(this), function(xhr) {
  77. console.error(xhr);
  78. });
  79. },
  80. team_demote_member: function(m) {
  81. load_resource("GET", "/teams/" + this.user.team._id + "/memberships/" + m._id + "/demote" , null, function(res, xhr) {
  82. this.load_user(function() {
  83. this.load_team();
  84. }.bind(this));
  85. }.bind(this), function(xhr) {
  86. console.error(xhr);
  87. });
  88. },
  89. team_remove_member: function(m) {
  90. if (confirm("Really delete this member?")) {
  91. if (m.user_id && m.state === "active") {
  92. load_resource("DELETE", "/users/" + m._id, null, function(res, xhr) {
  93. var idx = this.team_members.indexOf(m);
  94. this.team_members.splice(idx, 1);
  95. }.bind(this), function(xhr) {
  96. console.error(xhr);
  97. });
  98. } else {
  99. load_resource("DELETE", "/teams/" + this.user.team._id + "/memberships/" + m._id, null, function(res, xhr) {
  100. var idx = this.team_members.indexOf(m);
  101. this.team_members.splice(idx, 1);
  102. }.bind(this), function(xhr) {
  103. console.error(xhr);
  104. });
  105. }
  106. }
  107. }
  108. }
  109. }