{% load static %}
{% load i18n %}
<head>
    <title>{% trans 'Task log' %}</title>
    <script src="{% static 'js/jquery-2.1.1.js' %}"></script>
    <script src="{% static 'js/plugins/xterm/xterm.js' %}"></script>
    <link rel="stylesheet" href="{% static 'js/plugins/xterm/xterm.css' %}" />
    <style>
        body {
            background-color: black;
        }
        .xterm-rows {
            {#padding: 15px;#}
            font-family: "Bitstream Vera Sans Mono", Monaco, "Consolas", Courier, monospace;
            font-size: 13px;
        }
    </style>
</head>
<div id="term" style="height: 100%;width: 100%">
</div>

<script>
    var rowHeight = 18;
    var colWidth = 10;
    var mark = '';
    var url = "{% url 'api-ops:celery-task-log' pk=task_id %}";
    var term;
    var end = false;
    var error = false;
    var interval = 200;
    var success = true;

    function calWinSize() {
        var t = $('#marker');
        {#rowHeight = 1.00 * t.height();#}
        {#colWidth = 1.00 * t.width() / 6;#}
    }
    function resize() {
       {#var rows = Math.floor(window.innerHeight / rowHeight) - 1;#}
       {#var cols = Math.floor(window.innerWidth / colWidth) - 2;#}
       {#term.resize(cols, rows);#}
    }
    function requestAndWrite() {
        if (!end && success) {
            success = false;
            $.ajax({
                url: url + '?mark=' + mark,
                method: "GET",
                contentType: "application/json; charset=utf-8"
            }).done(function(data, textStatue, jqXHR) {
                success = true;
                if (jqXHR.status === 203) {
                    error = true;
                    term.write('.');
                    interval = 500;
                }
                if (jqXHR.status === 200){
                    term.write(data.data);
                    mark = data.mark;
                    if (data.end){
                        end = true
                    }
                }
            })
        }
    }
    $(document).ready(function () {
        term = new Terminal({
            cursorBlink: false,
            screenKeys: false,
            fontFamily: '"Monaco", "Consolas", "monospace"',
            fontSize: 12,
            rightClickSelectsWord: true,
            disableStdin: true
        });
        term.open(document.getElementById('term'));
        term.resize(90, 32);
        resize();
        term.on('data', function (data) {
            {#term.write(data.replace('\r', '\r\n'))#}
            term.write(data);
        });
        window.onresize = function () {
            resize()
        };
        {#$('.terminal').detach().appendTo('#term');#}
        setInterval(function () {
            requestAndWrite()
        }, interval)
    });
</script>
