baseimage-tutorial-framework/src/app/static/js/index.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

import * as ace from 'brace';
import 'brace/mode/python';
import 'brace/theme/monokai';
import * as showdown from 'showdown';
2017-11-17 14:42:39 +00:00
let ws = new WebSocket('ws://' + document.location.host + '/ws');
2017-11-27 18:01:32 +00:00
let converter = new showdown.Converter();
// TODO: don't hardcode anchor id
let editor = ace.edit('anchor_webide');
editor.setTheme('ace/theme/monokai');
editor.getSession().setMode('ace/mode/python');
editor.$blockScrolling = Infinity;
2017-11-27 18:01:32 +00:00
2017-11-17 14:42:39 +00:00
$('#container').on('click', '.anchor', ( function (event) {
2017-11-27 18:00:02 +00:00
let anchorName = $(this).attr('id').replace('_event', '');
let data = JSON.stringify({
'anchor': anchorName,
'data': $('#' + anchorName).text()
});
console.log('Sending: ');
console.log(data);
ws.send(data);
}));
2017-11-27 18:00:38 +00:00
$('form#anchor_login_event').on('submit', (function (event) {
event.preventDefault();
2017-11-17 14:42:39 +00:00
let anchorName = $(this).attr('id').replace('_event', '');
2017-11-27 18:00:38 +00:00
$.post(
$(this).attr('action'),
$(this).serialize(),
function (data) {}
);
}));
2017-11-27 18:01:11 +00:00
ws.onopen = function () {
ws.send(JSON.stringify({
'anchor': 'reset',
'data': ''
}));
// init webide
ws.send(JSON.stringify({
'anchor': 'anchor_webide',
'data': ''
2017-11-17 14:42:39 +00:00
}));
2017-11-27 18:01:11 +00:00
};
2017-11-17 14:42:39 +00:00
ws.onmessage = function (messageEvent) {
let message = JSON.parse(messageEvent.data);
2017-11-27 18:01:32 +00:00
console.log('Receiving: ');
console.log(message);
let $anchor = $('#' + message['anchor']);
if (message['anchor'] === 'anchor_webide') {
editor.setValue(message['data'], -1);
2017-11-27 18:01:32 +00:00
}
else {
let formatted_message = converter.makeHtml(message['data']);
$anchor.html(formatted_message);
}
2017-11-17 14:42:39 +00:00
};