Coding/BinaryDad.Coding/wwwroot/js/hub.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

$(function () {
2021-07-21 01:53:39 +00:00
var connection = new signalR.HubConnectionBuilder().withUrl('/codeHub').build();
2021-07-20 02:02:14 +00:00
let $liveModeLink = $('#live-mode-link');
let $liveModeStatus = $('#live-mode-status');
2021-07-21 01:53:39 +00:00
let $usersList = $('#users-list');
let $yourNameText = $('#your-name-text');
let $yourColorText = $('#your-color-text');
2021-07-20 02:02:14 +00:00
let isLive = false;
let isLiveMode = function () {
2021-07-20 02:54:16 +00:00
2021-07-20 02:02:14 +00:00
isLive = window.location.hash === '#live';
$liveModeLink
.toggleClass('btn-success text-white', isLive)
.attr('href', isLive ? '#' : '#live');
2021-07-20 03:43:01 +00:00
isLive ? $liveModeStatus.fadeIn() : $liveModeStatus.fadeOut();
2021-07-20 02:02:14 +00:00
};
2021-07-21 01:53:39 +00:00
let sendName = function (e) {
let color = $yourColorText.val();
$yourNameText.css('color', `${color}`);
connection.invoke('SaveName', $yourNameText.val(), color);
};
2021-07-20 02:02:14 +00:00
isLiveMode();
2021-07-21 01:53:39 +00:00
// events
2021-07-20 02:02:14 +00:00
$(window).on('hashchange', isLiveMode);
2021-07-21 15:00:05 +00:00
$('#your-name-text, #your-color-text').doneTyping(sendName);
2021-07-20 03:43:01 +00:00
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';
2021-07-20 02:02:14 +00:00
if (isTeacher) {
$liveModeLink.hide();
$liveModeStatus.hide();
}
let $editors = $('.ace_editor');
if (isTeacher) {
$editors.each(function (i, e) {
2021-07-20 02:02:14 +00:00
let editor = ace.edit(e);
2021-07-20 02:02:14 +00:00
editor.session.on('change', function () {
2021-07-20 02:02:14 +00:00
let user = 'ryan';
let code = editor.getValue();
2021-07-21 01:53:39 +00:00
connection.invoke('UpdateCode', user, i, code).catch(function (err) {
2021-07-20 02:02:14 +00:00
return console.error(err.toString());
});
});
});
}
2021-07-21 01:53:39 +00:00
connection.on('ReceiveCode', function (user, index, code) {
2021-07-20 02:02:14 +00:00
if (!isTeacher && isLive) {
let editor = ace.edit($editors[index]);
editor.session.setValue(code);
}
});
2021-07-21 01:53:39 +00:00
connection.on('UsersList', function (users) {
let usersList = Object.values(users)
.filter(u => u.name !== '')
.map(u => `<span style='color: ${u.color};'>${u.name}</span>`);
$usersList.html(`<strong>${usersList.length} users online:</strong> ${usersList.join(', ')}`);
});
connection.start().then(function () {
2021-07-21 01:53:39 +00:00
sendName();
}).catch(function (err) {
2021-07-19 01:24:08 +00:00
return console.error(err.toString());
});
});