asciinema.org/app/views/asciicasts/show.js.erb
2014-01-18 15:35:24 +01:00

76 lines
2.2 KiB
Plaintext

// asciinema.org - embeddable player
(function() {
var apiUrl = "<%= request.protocol %><%= request.host_with_port %>";
var iframe;
function receiveSize(e) {
if (e.origin === apiUrl) {
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');
iframe.src = apiUrl + "/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);
}
})();