Skip to content

Commit

Permalink
Dev: singled out only table2pdf methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lacrioque committed Nov 24, 2016
1 parent 790e692 commit 02801cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 201 deletions.
1 change: 0 additions & 1 deletion application/config/third_party.php
Expand Up @@ -324,7 +324,6 @@
'basePath' => 'third_party.jspdf',
'js' => array(
'html2canvas.js',
// 'jspdf.debug.js'
'jspdf.min.js',
'createpdf_worker.js'
),
Expand Down
Expand Up @@ -9,8 +9,11 @@
*/
?>
<!-- _statisticsoutput_header -->
<div class="col-lg-<?php echo $nbcols; ?> sol-sm-12 printable">
<table class='statisticstable table table-bordered printable'>
<div class="col-lg-<?php echo $nbcols; ?> sol-sm-12 printable" >
<button class="pull-right action_js_export_to_pdf btn btn-default btn-sm" data-question-id="quid_<?php echo $outputs['parentqid'];?>" data-toggle="tooltip" title="<?php eT('Export this question to PDF.'); ?>">
<i class="fa fa-file-pdf-o"></i>
</button>
<table class='statisticstable table table-bordered printable' id="quid_<?php echo $outputs['parentqid'];?>">
<thead>
<tr class='success'>
<th colspan='4' align='center' style='text-align: center; '>
Expand Down
7 changes: 1 addition & 6 deletions application/views/admin/export/statistics_view.php
Expand Up @@ -7,12 +7,7 @@

<!-- Javascript variables -->
<?php $this->renderPartial('/admin/export/statistics_subviews/_statistics_view_scripts', array('sStatisticsLanguage'=>$sStatisticsLanguage, 'surveyid'=>$surveyid, 'showtextinline'=>$showtextinline)) ; ?>
<div class="container-center">
<div class="row">
<div class="col-sm-3 col-sm-offset-3 col-xs-6"><button id="action_js_export_to_pdf" class="btn btn-primary btn-block"><?php eT("Export to PDF");?></button></div>
<div class="col-sm-3 col-xs-6"><button id="action_js_preview_to_print" class="btn btn-primary btn-block"><?php eT("Printable View");?></button></div>
</div>
</div>

<div id='statisticsview' class='side-body <?php echo getSideBodyClass(false); ?>'>
<h3>
<span class="glyphicon glyphicon-stats"></span> &nbsp;&nbsp;&nbsp;
Expand Down
122 changes: 48 additions & 74 deletions scripts/admin/statistics.js
Expand Up @@ -729,87 +729,61 @@ function changeGraphType (cmd, id) {

}

/**
* Handlers and builders for the different question types based on their answers
* The html has to be recreated for the jspdf to work
*/
var elementHandlers = {
'thead': function(element, renderer){

}
}
var createPDFworker = function(tableArray){
"use strict";
return new Promise(function(res,rej){
var createPDF = new CreatePDF();

$.each(tableArray, function(i,table){
var sizes = {h: $(table).height(), w: $(table).width()};
var answerObject = createPDF('sendImg', {html: table, sizes: sizes});
});

createPDF('getParseHtmlPromise').then(function(resolve){
var answerObject = createPDF('exportPdf');
var newWindow = window.open(answerObject.msg,600,800);
res('done');
}, function(reject){
rej(arguments);
});
});
};

$(document).ready(function(){
$('body').addClass('onStatistics');


$('body').on('click','#action_js_preview_to_print', function(){
var openWindow = window.open("about:blank","", "_blank");
var head = "";
$('head').find('link').each(function(i,item){head = head+item.outerHTML});
$('document').find('script').each(function(i,item){head = head+item.outerHTML});
var body = "";
$('#statisticsview').find('.statisticstable').each(function(i,item){body = body + item.outerHTML});

var html = "<html><head>" + head + "</head><body>" + body + "<body></html>" ;
openWindow.document.write(html);
$(openWindow.document).trigger('triggerReady');
console.log($('head'));
});


var createPDFworker = function(batchOfTables){
return new Promise(function(res,rej){
var createPDF = new CreatePDF();
$.each(batchOfTables, function(i,table){
$(window).scrollTop($(table).offset().top);
var sizes = {h: $(table).height(), w: $(table).width()};
var answerObject = createPDF('sendImg', {html: table, sizes: sizes});
});
createPDF('getParseHtmlPromise').then(function(resolve){
var answerObject = createPDF('exportPdf');
var newWindow = window.open(answerObject.msg,600,800);
res('done');
});
});
};

$('body').on('click','#action_js_export_to_pdf', function(){
var overlay = $('<div></div>')
.attr('id','overlay')
.css({
position: 'fixed',
width: "100%",
height:"100%",
top:0,
"z-index" : 5000,
"pointer-events": 'none',
left:0,
right:0,
bottom:0,
"background-color": "hsla(0,0%,65%,0.6)"
});
$('#statsContainerLoading').clone().css({display: 'block',position: 'fixed', top:"25%",left:0, width: "100%"}).appendTo(overlay);
overlay.appendTo('body');

var allTables = $('#statisticsview').find('.statisticstable');
var promises = [], j=0, count = allTables.length;
if(count <= 15){
promises.push(createPDFworker(allTables));
} else {
var tablePackage = [];
while(j<count){
var end = (allTables.length<=15 ? allTables.length : 15 );
tablePackage = allTables.splice(j, end)
j=j+end;
promises.push(createPDFworker(tablePackage));
$('body').on('click','.action_js_export_to_pdf', function(){

html2canvas($(this).siblings('table')[0]).then(
function(canvas){
$('body').prepend($('<img/>').attr('src',canvas.toDataURL()));
}
}

Promise.all(promises).then(
function(success){overlay.remove();},
function(fail){console.log(fail);}
)


// var overlay = $('<div></div>')
// .attr('id','overlay')
// .css({
// position: 'fixed',
// width: "100%",
// height:"100%",
// top:0,
// "z-index" : 5000,
// "pointer-events": 'none',
// left:0,
// right:0,
// bottom:0,
// "background-color": "hsla(0,0%,65%,0.6)"
// });
// $('#statsContainerLoading').clone().css({display: 'block',position: 'fixed', top:"25%",left:0, width: "100%"}).appendTo(overlay);
// overlay.appendTo('body');

// var thisTable = $('#'+$(this).data('questionId'));
// createPDFworker.call(null,thisTable).then(
// function(success){overlay.remove();},
// function(){console.log(arguments);}
// )


});
Expand Down
118 changes: 0 additions & 118 deletions scripts/admin/statistics_pdf_worker.js

This file was deleted.

0 comments on commit 02801cb

Please sign in to comment.