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