Add live dashboard for teachers in the context of an exercise

This commit also adds the fundamentals for ActionCable
This commit is contained in:
Sebastian Serth
2019-02-15 09:32:53 +01:00
parent 6e03939c10
commit d63700c7db
27 changed files with 215 additions and 7 deletions

View File

@@ -14,6 +14,8 @@
//= require turbolinks
//= require pagedown_bootstrap
//= require d3
//= require rails-timeago
//= require locales/jquery.timeago.de.js
//
// lib/assets
//= require flash

View File

@@ -23,3 +23,9 @@ $.fn.scrollTo = function(selector) {
scrollTop: $(selector).offset().top - $(this).offset().top + $(this).scrollTop()
}, ANIMATION_DURATION);
};
$.fn.replaceWithPush = function(a) {
const $a = $(a);
this.replaceWith($a);
return $a;
};

View File

@@ -0,0 +1,13 @@
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);

View File

@@ -0,0 +1,33 @@
$(document).on('turbolinks:load', function() {
if ($.isController('exercises') && $('.teacher_dashboard').isPresent()) {
const exercise_id = $('.teacher_dashboard').data().exerciseId;
const study_group_id = $('.teacher_dashboard').data().studyGroupId;
const specific_channel = { channel: "LaExercisesChannel", exercise_id: exercise_id, study_group_id: study_group_id };
App.la_exercise = App.cable.subscriptions.create(specific_channel, {
connected: function () {
// Called when the subscription is ready for use on the server
},
disconnected: function () {
// Called when the subscription has been terminated by the server
},
received: function (data) {
// Called when there's incoming data on the websocket for this channel
let $row = $('tr[data-id="' + data.id + '"]');
if ($row.length === 0) {
$row = $($('#posted_rfcs')[0].insertRow(0));
}
$row = $row.replaceWithPush(data.html);
$row.find('time').timeago();
$row.click(function () {
Turbolinks.visit($(this).data("href"));
});
}
});
}
});