{% extends 'view_file_base.html' %}
{% load i18n %}

{% block extra_style %}{{ block.super }}
{% if not err and not use_pdfjs %}
    {% include 'snippets/office_convert_style.html' %}
{% endif %}
{% endblock %}

{% block file_view %}
{% if not err and not use_pdfjs %}
    {% include 'snippets/office_convert_html.html' %}
{% endif %}
{% endblock %}

{% block extra_script %}{{ block.super }}
{% if not err %}

{% if not use_pdfjs %}
<script type="text/javascript" src="{{MEDIA_URL}}js/pdf2html.js"></script>
<script type="text/javascript">
    {% include 'snippets/office_convert_js.html' %}
</script>

{% else %}
<script type="text/javascript" src="{{MEDIA_URL}}js/pdf.js"></script>
<script type="text/javascript">
if (!($.browser.msie && parseInt($.browser.version) < 10)) {
    PDFJS.workerSrc = '{{MEDIA_URL}}js/pdf.js';
    $('#file-view').html('<div id="pdf"><div id="pdf-op-bar" class="vh"><span id="pdf-page-left"><button id="prev">{% trans "Previous" %}</button></span>{% blocktrans %}<span id="pdf-page"><label for="page-number">Page</label> <input type="number" id="page-number" value="1" min="1"></input> / <span id="page-nums"></span></span>{% endblocktrans %}<span id="pdf-page-right"><button id="next" style="margin-right:15px;">{% trans "Next" %}</button><button id="full-screen">{% trans "Full Screen" %}</button></span></div><img src="{{ MEDIA_URL }}pdf_full_view/images/loading-icon.gif" alt="{% trans "loading..." %}" id="pdf-loading" style="margin:20px 0;" /><canvas data="{{ raw_path }}" id="pdf-view" class="hide"></canvas></div>').css({'text-align':'center'});
    $('#pdf-page-left, #pdf-page-right').css('display', 'inline-block');
    $('#pdf-page-left').css({'text-align':'right', 'width': $('#pdf-page-right').width()});
    $('#pdf-op-bar').removeClass('vh');
    var seahub_getPage = function (pdf, page_number) {
        pdf.getPage(page_number).then(function(page) {
            var scale = 1.5;
            var viewport = page.getViewport(scale);
            var canvas = $('#pdf-view')[0];
            var context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;
            var renderContext = { 
                canvasContext: context,
                viewport: viewport
            };  
            page.render(renderContext);
        }); 
    };
    PDFJS.getDocument($('#pdf-view').attr('data')).then(function(pdf) {
        $('#page-nums').html(pdf.numPages);
        $('#page-number').attr('max', pdf.numPages).css('width', String(pdf.numPages).length * 6 + 10);
        seahub_getPage(pdf, 1);
        $('#pdf-loading').addClass('hide');
        $('#pdf-view').removeClass('hide');
        $('#page-number').change(function() {
            seahub_getPage(pdf, $(this).val());
        });
        $('#prev').click(function() {
            var current = $('#page-number').val();
            if (current > 1) {
                seahub_getPage(pdf, --current);
                $('#page-number').val(current);
            }
        });
        $('#next').click(function() {
            var current = $('#page-number').val();
            if (current < pdf.numPages) {
                seahub_getPage(pdf, ++current);
                $('#page-number').val(current);
            }
        });
        $('#full-screen').click(function() {
            window.open('{{ SITE_ROOT }}pdf_full_view/?repo_id={{ repo.id }}&obj_id={{obj_id}}&file_name=' + e('{{ file_name }}'));
        });
    });
} else {
    var tip = "{% trans "You can use IE 10 or other browsers, for example, firefox, to view it online." %}";
    $('#file-view').html('<div id="file-view-tip"><p>' + tip + '</p></div>');
}
</script>
{% endif %}
{% endif %}

{% endblock %}
