2013-09-22 12:07:42 +00:00
|
|
|
// asciinema.org - embeddable player
|
2013-06-12 19:33:30 +00:00
|
|
|
|
|
|
|
(function() {
|
2014-03-05 09:05:45 +00:00
|
|
|
var apiHost = "<%= request.protocol %><%= request.host_with_port %>";
|
|
|
|
var apiUrl = apiHost + '/api';
|
2013-12-14 13:54:01 +00:00
|
|
|
var iframe;
|
|
|
|
|
2013-06-12 19:33:30 +00:00
|
|
|
function receiveSize(e) {
|
2014-03-05 09:05:45 +00:00
|
|
|
if (e.origin === apiHost) {
|
2013-06-12 19:33:30 +00:00
|
|
|
var event = e.data[0];
|
|
|
|
var data = e.data[1];
|
|
|
|
if (event == 'asciicast:size' && data.id == <%= @asciicast.id %>) {
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.style.width = '' + data.width + 'px';
|
|
|
|
iframe.style.height = '' + data.height + 'px';
|
2013-06-12 19:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 20:47:39 +00:00
|
|
|
function insertAfter(referenceNode, newNode) {
|
|
|
|
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
function params(container, script) {
|
2014-06-24 18:34:34 +00:00
|
|
|
var params = [];
|
2013-06-12 21:50:31 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
var size = script.getAttribute('data-size');
|
|
|
|
if (size) {
|
2014-06-24 18:34:34 +00:00
|
|
|
params = params.concat(['size=' + size]);
|
2013-12-14 13:54:01 +00:00
|
|
|
}
|
|
|
|
var speed = script.getAttribute('data-speed');
|
|
|
|
if (speed) {
|
2014-06-24 18:34:34 +00:00
|
|
|
params = params.concat(['speed=' + speed]);
|
2013-12-14 13:54:01 +00:00
|
|
|
}
|
2014-01-18 14:17:12 +00:00
|
|
|
var autoplay = script.getAttribute('data-autoplay');
|
2014-08-09 11:09:54 +00:00
|
|
|
if (autoplay) {
|
2014-06-24 18:34:34 +00:00
|
|
|
params = params.concat(['autoplay=' + autoplay]);
|
2014-01-18 14:17:12 +00:00
|
|
|
}
|
2014-08-09 11:09:54 +00:00
|
|
|
var loop = script.getAttribute('data-loop');
|
|
|
|
if (loop) {
|
|
|
|
params = params.concat(['loop=' + loop]);
|
|
|
|
}
|
2014-07-01 20:44:43 +00:00
|
|
|
var theme = script.getAttribute('data-theme');
|
|
|
|
if (theme) {
|
|
|
|
params = params.concat(['theme=' + theme]);
|
|
|
|
}
|
2013-12-14 13:54:01 +00:00
|
|
|
|
2014-06-24 18:34:34 +00:00
|
|
|
return '?' + params.join('&');
|
2013-12-14 13:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertPlayer(script) {
|
2013-06-12 20:47:39 +00:00
|
|
|
var container = document.createElement('div');
|
2013-12-14 13:54:01 +00:00
|
|
|
container.id = "asciicast-container-<%= @asciicast.id %>";
|
2013-06-12 20:47:39 +00:00
|
|
|
container.className = 'asciicast';
|
|
|
|
container.style.display = 'block';
|
|
|
|
container.style.float = 'none';
|
|
|
|
container.style.overflow = 'hidden';
|
2013-12-14 13:54:01 +00:00
|
|
|
container.style.padding = 0;
|
|
|
|
container.style.margin = '20px 0';
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
insertAfter(script, container);
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe = document.createElement('iframe');
|
2014-03-05 09:05:45 +00:00
|
|
|
iframe.src = apiUrl + "/asciicasts/<%= @asciicast.id %>" + params(container, script);
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.id = "asciicast-iframe-<%= @asciicast.id %>";
|
|
|
|
iframe.name = "asciicast-iframe-<%= @asciicast.id %>";
|
|
|
|
iframe.scrolling = "no";
|
2014-06-24 20:56:37 +00:00
|
|
|
iframe.setAttribute('allowFullScreen', 'true');
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.style.overflow = "hidden";
|
|
|
|
iframe.style.margin = 0;
|
|
|
|
iframe.style.border = 0;
|
2014-06-24 18:34:34 +00:00
|
|
|
iframe.style.display = "inline-block";
|
|
|
|
iframe.style.width = "100%";
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.style.float = "none";
|
|
|
|
iframe.style.visibility = "hidden";
|
|
|
|
iframe.onload = function() { this.style.visibility = 'visible' };
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
container.appendChild(iframe);
|
|
|
|
}
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
var script = document.getElementById("asciicast-<%= @asciicast.id %>");
|
|
|
|
|
|
|
|
if (script) {
|
|
|
|
insertPlayer(script);
|
|
|
|
window.addEventListener("message", receiveSize, false);
|
2013-06-12 20:47:39 +00:00
|
|
|
}
|
2013-06-12 19:33:30 +00:00
|
|
|
})();
|