Sprankelprachtig aan/afmeldsysteem

buttonhandlers.jsx.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Provides handlers for the buttons!
  2. document.addEventListener("turbolinks:load", 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. $('.editable').editable();
  8. }
  9. // Callback that is triggered when a presence button is clicked.
  10. // Creates an AJAX-request and registers the appropriate handlers once it is done.
  11. function change_presence(e)
  12. {
  13. // Gather data
  14. var group, person, activity, state;
  15. group = this.dataset["groupId"];
  16. person = this.dataset["personId"];
  17. activity = this.dataset["activityId"];
  18. rstate = this.dataset["newState"];
  19. var state;
  20. switch (rstate)
  21. {
  22. case "present":
  23. state = true;
  24. break;
  25. case "absent":
  26. state = false;
  27. break;
  28. case "unknown":
  29. state = null;
  30. break;
  31. }
  32. // Make request
  33. var req;
  34. req = $.ajax(`/groups/${group}/activities/${activity}/presence`,
  35. {
  36. method: 'PUT',
  37. data: {person_id: person, attending: state},
  38. statusCode: {
  39. 423: function() {
  40. alert( "De deadline is al verstreken! Vraag orgi of bestuur of het nog kan.");
  41. },
  42. 403: function() {
  43. alert( "Je hebt geen rechten om iemand anders aan te passen!");
  44. }
  45. }
  46. }
  47. )
  48. .done( activity_changed );
  49. // Pack data for success
  50. req.aardbei_activity_data =
  51. {
  52. group: group,
  53. person: person,
  54. activity: activity,
  55. state: state
  56. };
  57. }
  58. // Update all references on the page to this activity:
  59. // 1. The activity's row-color in any tables
  60. // 2. The present/absent buttons
  61. function activity_changed(data, textStatus, xhr)
  62. {
  63. // Unpack activity-data
  64. var target;
  65. target = xhr.aardbei_activity_data;
  66. // Determine what color and icons we're going to use
  67. var new_rowclass;
  68. var new_confirm_icon, new_decline_icon;
  69. switch (target.state)
  70. {
  71. case true:
  72. new_rowclass = "success";
  73. new_confirm_icon = check_selected;
  74. new_decline_icon = times_unselected;
  75. break;
  76. case false:
  77. new_rowclass = "danger";
  78. new_confirm_icon = check_unselected;
  79. new_decline_icon = times_selected;
  80. break;
  81. case null:
  82. new_rowclass = "warning";
  83. new_confirm_icon = check_unselected;
  84. new_decline_icon = times_unselected;
  85. break;
  86. }
  87. // Update all tr's containing this person's presence
  88. $(`tr[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  89. .removeClass('success danger warning')
  90. .addClass(new_rowclass);
  91. // Update all buttons for this person's presence
  92. $(`.btn-present[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  93. .html(new_confirm_icon);
  94. $(`.btn-absent[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  95. .html(new_decline_icon);
  96. // Add text to all 'wide' buttons
  97. $(`.btn-present[data-person-id=${target.person}][data-activity-id=${target.activity}][data-wide=1]`)
  98. .append(" Present");
  99. $(`.btn-absent[data-person-id=${target.person}][data-activity-id=${target.activity}][data-wide=1]`)
  100. .append(" Absent");
  101. if (window.updatecounts != undefined)
  102. updatecounts(window.subgroupfilter);
  103. }
  104. function alert_failure(data, textStatus, xhr)
  105. {
  106. alert(`Something broke! We got a ${textStatus}, (${data}).`);
  107. }
  108. var check_unselected = '<i class="fa fa-check"></i>';
  109. var check_selected = '<i class="fa fa-check-circle"></i>';
  110. var times_unselected = '<i class="fa fa-times"></i>';
  111. var times_selected = '<i class="fa fa-times-circle"></i>';