{% load i18n %} function prepareOfficePreviewToken(xhr, settings) { xhr.setRequestHeader('X-Seafile-Office-Preview-Token', '{{ office_preview_token }}'); } /** * @param {boolean} html_exists True if the html has already been converted. * @param {number} page_num The number of pages of this document * @param {string|undefined} outline The outline content of the converted html. * If html_exists is true, then it is undefined since the outline is already rendered in the HTML. */ function load_document(html_exists, page_num) { $('#convert-loading').remove(); var base_page_url = "{% url 'office_convert_get_page' obj_id %}/", page_urls = []; for (var i = 1; i < page_num + 1; i++) { page_urls.push(base_page_url + i + '.page'); } new pdf2htmlEX.Viewer({ before_send: prepareOfficePreviewToken, container_id : 'page-container', sidebar_id : 'sidebar', outline_id : 'outline', page_urls : page_urls }); // zoom in, zoom out var scale_ratio = 1, scale_ratio_offset = 0.1; var scale = function(ele, ratio) { ele.css({ 'transform':'scale(' + ratio + ')', '-webkit-transform':'scale(' + ratio + ')', '-ms-transform':'scale(' + ratio + ')', 'transform-origin':'center 0%', '-webkit-transform-origin':'center 0%', '-ms-transform-origin':'center 0%' }); }; $('#converted-html').append('
'); $('#pdf2html-toolbar-2').append('').css({'left': $(window).width()/2 - $('#pdf2html-toolbar-2').outerWidth(true)/2}); var file_cont = $('#file-view'), //file container orig_pg_total_h; $('#zoom-in, #zoom-out').click(function() { // get orig data before zoom in/out var op = $(this).attr('id'), pg_num = $('.pd').length, orig_win_scrollTop = $(window).scrollTop(), orig_pg_h, cur_pg, pg_h_offset; if (!orig_pg_total_h && scale_ratio == 1) { orig_pg_total_h = file_cont.height(); } orig_pg_h = orig_pg_total_h*scale_ratio/pg_num; cur_pg = (orig_win_scrollTop - $('#page-container').offset().top)/orig_pg_h; // zoom in/out if (op == 'zoom-in') { scale_ratio += scale_ratio_offset; } else { scale_ratio -= scale_ratio_offset; } if (scale_ratio <= 0) { return ; } scale($('#page-container'), scale_ratio); // modify file container's height file_cont.height(orig_pg_total_h*scale_ratio); // scroll to the right cur page pg_h_offset = (orig_pg_total_h * scale_ratio_offset)/pg_num; if (op == 'zoom-in') { $(window).scrollTop(orig_win_scrollTop + cur_pg*pg_h_offset); } else { $(window).scrollTop(orig_win_scrollTop - cur_pg*pg_h_offset); } }); } {% if html_exists %} load_document(true, {{ html_detail.page_num }}); {% else %} function get_file_css() { var css_href = "{% url 'office_convert_get_page' obj_id %}/file.css"; $('head').append(''); } function query_page_num() { $.ajax({ url: "{% url 'office_convert_query_page_num' %}?file_id={{ obj_id }}", cache: false, dataType: 'json', beforeSend: prepareOfficePreviewToken, success: function(data) { if (data['success']) { get_file_css(); load_document(false, data['count']); } else { str = "{% trans "Document convertion failed." %}"; $('#file-view').html('

' + str + '

'); } }, error: function(xhr, textStatus, errorThrown) { var str; if (xhr.responseText) { str = "{% trans "Document convertion failed." %}"; } else { str = "{% trans "Failed. Please check the network." %}"; } $('#file-view').html('

' + str + '

'); } }); } function check_status () { $.ajax({ url: "{% url 'office_convert_query_status' %}?file_id={{ obj_id }}", cache: false, dataType: 'json', beforeSend: prepareOfficePreviewToken, success: function(data) { var status = data['status']; if (status == 'QUEUED' || status == 'PROCESSING') { setTimeout(check_status, 2000); } else { query_page_num(); } }, error: function(xhr, textStatus, errorThrown) { var str; if (xhr.responseText) { str = "{% trans "Document convertion failed." %}"; } else { str = "{% trans "Failed. Please check the network." %}"; } $('#file-view').html('

' + str + '

'); } }); } check_status(); {% endif %}