1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-17 07:41:26 +00:00

added pdf view with pdf.js

This commit is contained in:
llj
2013-01-09 15:32:44 +08:00
parent aa7741adf4
commit 96e57c6d1a
30 changed files with 37764 additions and 12 deletions

View File

@@ -40,7 +40,7 @@
{% endifnotequal %}
{% endif %}
{% if filetype == 'Document' or filetype == 'PDF' %}
{% if filetype == 'Document' or filetype == 'PDF' and not use_pdfjs %}
$('#file-view').html('<div id="flash"></div>');
function load_flexpaper() {
var swf_url = '{{ DOCUMENT_CONVERTOR_ROOT }}swf/{{ obj_id }}';
@@ -104,8 +104,62 @@
{% endif %}
{% endif %}
{% if filetype == 'PDF' and use_pdfjs %}
if (!$.browser.msie) {
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 {
$('#file-view').html('<div id="file-view-tip"><p>{% trans "You can use other browsers, for example, firefox, to view it online." %}</p></div>');
}
{% endif %}
{% if filetype == 'Unknown' %}
$('#file-view').html('<div id="file-view-tip"><p>{% trans "This type of file cannot be viewed online." %}</p></div>');
{% endif %}
{% endif %}
{% endif %}{# 'if not err' ends here. #}

View File

@@ -1,9 +1,12 @@
{% if filetype == 'Text' %}
<script type="text/javascript" src="{{MEDIA_URL}}codemirror/codemirror-2.36.js"></script>
{% endif %}
{% if filetype == 'Document' or filetype == 'PDF' %}
{% if filetype == 'Document' or filetype == 'PDF' and not use_pdfjs %}
<script type="text/javascript" src="{{MEDIA_URL}}flexpaper/js/flexpaper_flash.js"></script>
{% endif %}
{% if filetype == 'PDF' and use_pdfjs %}
<script type="text/javascript" src="{{MEDIA_URL}}js/pdf.js"></script>
{% endif %}
{% if filetype == 'Markdown' %}
<script type="text/javascript" src="{{MEDIA_URL}}js/showdown.js"></script>
{% endif %}