asciinema.org/app/views/asciicasts/show.js.erb

75 lines
2.2 KiB
Plaintext
Raw Normal View History

2013-09-22 12:07:42 +00:00
// asciinema.org - embeddable player
(function() {
var iframe;
function receiveSize(e) {
if (e.origin === document.location.protocol + "//<%= request.host_with_port %>") {
var event = e.data[0];
var data = e.data[1];
if (event == 'asciicast:size' && data.id == <%= @asciicast.id %>) {
iframe.style.width = '' + data.width + 'px';
iframe.style.height = '' + data.height + 'px';
}
}
}
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function params(container, script) {
var params = '?max_width=' + container.offsetWidth;
var size = script.getAttribute('data-size');
if (size) {
params += '&size=' + size;
}
var speed = script.getAttribute('data-speed');
if (speed) {
params += '&speed=' + speed;
}
var autoplay = script.getAttribute('data-autoplay');
if (autoplay === '1') {
params += '&autoplay=' + autoplay;
}
return params;
}
function insertPlayer(script) {
var container = document.createElement('div');
container.id = "asciicast-container-<%= @asciicast.id %>";
container.className = 'asciicast';
container.style.display = 'block';
container.style.float = 'none';
container.style.overflow = 'hidden';
container.style.padding = 0;
container.style.margin = '20px 0';
insertAfter(script, container);
iframe = document.createElement('iframe');
2014-01-18 13:57:35 +00:00
iframe.src = "<%= request.protocol %><%= request.host_with_port %>/a/<%= @asciicast.id %>/bare" + params(container, script);
iframe.id = "asciicast-iframe-<%= @asciicast.id %>";
iframe.name = "asciicast-iframe-<%= @asciicast.id %>";
iframe.scrolling = "no";
iframe.style.overflow = "hidden";
iframe.style.margin = 0;
iframe.style.border = 0;
iframe.style.display = "block";
iframe.style.float = "none";
iframe.style.visibility = "hidden";
iframe.onload = function() { this.style.visibility = 'visible' };
container.appendChild(iframe);
}
var script = document.getElementById("asciicast-<%= @asciicast.id %>");
if (script) {
insertPlayer(script);
window.addEventListener("message", receiveSize, false);
}
})();