Sprankelprachtig aan/afmeldsysteem

buttonhandlers.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Provides handlers for the buttons!
  2. $(setup_handlers);
  3. // Add the presence handler to all buttons on the page
  4. function setup_handlers()
  5. {
  6. $('.btn-presence').on("click", change_presence);
  7. }
  8. // Callback that is triggered when a presence button is clicked.
  9. // Creates an AJAX-request and registers the appropriate handlers once it is done.
  10. function change_presence(e)
  11. {
  12. // Gather data
  13. var group, person, activity, state;
  14. group = this.dataset["groupId"];
  15. person = this.dataset["personId"];
  16. activity = this.dataset["activityId"];
  17. rstate = this.dataset["newState"];
  18. var state;
  19. switch (rstate)
  20. {
  21. case "present":
  22. state = true;
  23. break;
  24. case "absent":
  25. state = false;
  26. break;
  27. case "unknown":
  28. state = null;
  29. break;
  30. }
  31. // Make request
  32. var req;
  33. req = $.ajax(`/groups/${group}/activities/${activity}/presence`,
  34. {
  35. method: 'PUT',
  36. data: {person_id: person, attending: state}
  37. }
  38. )
  39. .done( activity_changed )
  40. .fail( alert_failure );
  41. // Pack data for success
  42. req.aardbei_activity_data =
  43. {
  44. group: group,
  45. person: person,
  46. activity: activity,
  47. state: state
  48. };
  49. }
  50. // Update all references on the page to this activity:
  51. // 1. The activity's row-color in any tables
  52. // 2. The present/absent buttons
  53. function activity_changed(data, textStatus, xhr)
  54. {
  55. // Unpack activity-data
  56. var target;
  57. target = xhr.aardbei_activity_data;
  58. // Determine what color and icons we're going to use
  59. var new_rowclass;
  60. var new_confirm_icon, new_decline_icon;
  61. switch (target.state)
  62. {
  63. case true:
  64. new_rowclass = "success";
  65. new_confirm_icon = check_selected;
  66. new_decline_icon = times_unselected;
  67. break;
  68. case false:
  69. new_rowclass = "danger";
  70. new_confirm_icon = check_unselected;
  71. new_decline_icon = times_selected;
  72. break;
  73. case null:
  74. new_rowclass = "warning";
  75. new_confirm_icon = check_unselected;
  76. new_decline_icon = times_unselected;
  77. break;
  78. }
  79. // Update all tr's containing this person's presence
  80. $(`tr[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  81. .removeClass('success danger warning')
  82. .addClass(new_rowclass);
  83. // Update all buttons for this person's presence
  84. $(`.btn-present[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  85. .html(new_confirm_icon);
  86. $(`.btn-absent[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  87. .html(new_decline_icon);
  88. }
  89. function alert_failure(data, textStatus, xhr)
  90. {
  91. alert(`Something broke! We got a ${textStatus}, (${data}).`);
  92. }
  93. var check_unselected = '<i class="fa fa-check"></i>';
  94. var check_selected = '<i class="fa fa-check-circle"></i>';
  95. var times_unselected = '<i class="fa fa-times"></i>';
  96. var times_selected = '<i class="fa fa-times-circle"></i>';