From 948510a6804ada89e977f90910834838f8d5c78f Mon Sep 17 00:00:00 2001 From: markusfluer Date: Fri, 18 Nov 2016 17:28:06 +0100 Subject: [PATCH] Dev: more work done on the implementation of the new jspdf library --- application/config/third_party.php | 4 ++ .../views/admin/export/statistics_view.php | 3 +- scripts/admin/statistics.js | 40 +++++++++++++++---- third_party/jspdf/jspdf.debug.js | 23 ++++++++--- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/application/config/third_party.php b/application/config/third_party.php index 1c11342b7f0..4433828d75e 100755 --- a/application/config/third_party.php +++ b/application/config/third_party.php @@ -318,6 +318,10 @@ 'js' => array( 'jspdf.debug.js' // 'jspdf.min.js' + ), + 'depends' => array( + 'jquery', + 'bootstrap' ) ) diff --git a/application/views/admin/export/statistics_view.php b/application/views/admin/export/statistics_view.php index a5d7d531ae4..84e891ee61c 100644 --- a/application/views/admin/export/statistics_view.php +++ b/application/views/admin/export/statistics_view.php @@ -9,7 +9,8 @@ renderPartial('/admin/export/statistics_subviews/_statistics_view_scripts', array('sStatisticsLanguage'=>$sStatisticsLanguage, 'surveyid'=>$surveyid, 'showtextinline'=>$showtextinline)) ; ?>
-
+
+
diff --git a/scripts/admin/statistics.js b/scripts/admin/statistics.js index bfee4010f17..da686d1cae5 100644 --- a/scripts/admin/statistics.js +++ b/scripts/admin/statistics.js @@ -180,9 +180,7 @@ function init_chart_js_graph_with_datas($type,$qid) ); } -$(document).ready(function() { - //for nicely printed statistics - $('body').addClass('onStatistics'); +var onDocumentReady = function(){ if ($('#completionstateSimpleStat').length>0) { @@ -267,8 +265,6 @@ $(document).ready(function() { $type = $(this).data('type'); $qid = $(this).data('qid'); - console.log($type); - // chartjs if($type == 'Bar' || $type == 'Radar' || $type == 'Line' ) { @@ -565,7 +561,10 @@ $(document).ready(function() { { changeGraphType('showpie', this.parentNode); }); -}); +}; +$(document).ready(onDocumentReady); +$(document).on('triggerReady', onDocumentReady); + var isWaiting = {}; @@ -730,13 +729,40 @@ 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){ + + } +} + $(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 = "" + head + "" + body + "" ; + openWindow.document.write(html); + $(openWindow.document).trigger('triggerReady'); + console.log($('head')); + }); $('body').on('click','#action_js_export_to_pdf', function(){ var doc = new jsPDF(); console.log("Getting the pdf"); $('#statisticsview').find('.statisticstable').each(function(i, table){ doc.addPage(); - doc.fromHTML(table, 210, 297, {width: 590}); + doc.fromHTML(table, 210, 297, {width: 590, elementHandlers: elementHandlers}); }); var dataurl = doc.output('dataurlnewwindow'); diff --git a/third_party/jspdf/jspdf.debug.js b/third_party/jspdf/jspdf.debug.js index f90bd813702..c8d3eb585b8 100644 --- a/third_party/jspdf/jspdf.debug.js +++ b/third_party/jspdf/jspdf.debug.js @@ -7013,20 +7013,28 @@ Q\n"; } return isHandledElsewhere; }; + // This method is not working for more complex tables. + tableToJson = function tableToJson(table, renderer) { - var data, headers, i, j, rowData, tableRow, table_obj, table_with, cell, l; - data = []; - headers = []; + var $table, data, head, body, footer, i, j, rowData, tableRow, table_obj, table_width, cell, l; + $table = jQuery(table); + data = []; + head = {}; + head = {}; i = 0; - l = table.rows[0].cells.length; - table_with = table.clientWidth; + + //ok so the first row should define the length? ever heard of a headrow? + l = $table.find('thead').find('tr').find('td').length; + + + table_width = table.clientWidth; while (i < table.rows.length) { cell = table.rows[0].cells[i]; if(cell){ headers[i] = { name: cell.textContent.toLowerCase().replace(/\s+/g, ''), prompt: cell.textContent.replace(/\r?\n/g, ''), - width: cell.clientWidth / table_with * renderer.pdf.internal.pageSize.width + width: cell.clientWidth / table_width * renderer.pdf.internal.pageSize.width }; } i++; @@ -7050,6 +7058,7 @@ Q\n"; headers: headers }; }; + var SkipNode = { SCRIPT: 1, STYLE: 1, @@ -7058,7 +7067,9 @@ Q\n"; EMBED: 1, SELECT: 1 }; + var listCount = 1; + _DrillForContent = function DrillForContent(element, renderer, elementHandlers) { var cn, cns, fragmentCSS, i, isBlock, l, px2pt, table2json, cb; cns = element.childNodes;