Source: analysisrecordtable.js

  1. /**
  2. * @fileOverview JavaScript methods for report view.
  3. * @module analysisrecordtable
  4. * @author Visa Nykänen
  5. * @author Tuomas Moisio
  6. */
  7. var URI;
  8. function save() {
  9. var checkBoxCsv = document.getElementById('saveForm:basic:1');
  10. var checkBoxImage = document.getElementById('saveForm:basic:0');
  11. var filename = document.getElementById('saveForm:input-name').value;
  12. if (filename === "") {
  13. return;
  14. }
  15. if (checkBoxImage.checked) {
  16. saveAsImage();
  17. }
  18. if (checkBoxCsv.checked) {
  19. setTimeout(function() {
  20. saveAsCsv();
  21. }, 100);
  22. }
  23. }
  24. $(document).ready(function() {
  25. sendImageAndCSV();
  26. });
  27. /**
  28. * Creates the csv using table2csv.js, sends the csv and the image of the report page to the server via AJAX
  29. */
  30. function sendImageAndCSV() {
  31. // makes sure the header isn't hidden
  32. let tmpclass = $('#entries').attr('class');
  33. $('#entries').attr('class', "");
  34. $('#entries table')
  35. .each(
  36. function() {
  37. let $table = $(this);
  38. // selector-settings for table2csv explained:
  39. // the two last columns are edit and remove, so we don't
  40. // really want them in our csv
  41. // if the page is too narrow the table puts the header
  42. // into each of the cells so only spans with the class
  43. // tContent should be printed into the csv
  44. let csv = $table
  45. .table2CSV({
  46. delivery : 'value',
  47. headerSelector : 'th:not(:nth-last-child(1)):not(:nth-last-child(2))',
  48. columnSelector : 'td:not(:nth-last-child(1)):not(:nth-last-child(2)) span.tContent'
  49. });
  50. sendData(csv, "csv")
  51. });
  52. $('#entries').attr('class', tmpclass);
  53. html2canvas(document.getElementById('dataTableImage')).then(
  54. function(canvas) {
  55. URI = canvas.toDataURL();
  56. sendData("reporttable," + URI, "image")
  57. })
  58. }
  59. /**
  60. * Sends the given data to the given page through ajax
  61. *
  62. * @param URI
  63. * the data to be sent
  64. * @param page
  65. * the page to which the data should be sent, currently either csv or
  66. * image
  67. */
  68. function sendData(URI, page) {
  69. $.ajax({
  70. url : "../../webapi/summary/" + page,
  71. type : "POST",
  72. dataType : "text",
  73. contentType : "text/plain",
  74. cache : false,
  75. data : URI,
  76. success : function(data) {
  77. },
  78. error : function(xhr, status, error) {
  79. showError(msg.obs_errorCouldntSendData + ": " + error);
  80. this_.waiting = false;
  81. }
  82. });
  83. }
  84. function saveAsCsv() {
  85. document.getElementById('saveForm:csvButton').click();
  86. }
  87. function saveAsImage() {
  88. html2canvas(document.getElementById('dataTableImage')).then(
  89. function(canvas) {
  90. URI = canvas.toDataURL();
  91. var filename;
  92. var filenameRaw;
  93. try {
  94. filenameRaw = document
  95. .getElementById('saveForm:input-name').value;
  96. if (filenameRaw === "") {
  97. return;
  98. }
  99. filename = filenameRaw.replace(/\./g, '-');
  100. } catch (err) {
  101. filename = 'summary.png';
  102. }
  103. var link = document.createElement('a');
  104. if (typeof link.download === 'string') {
  105. link.href = URI;
  106. link.download = filename;
  107. // Firefox requires the link to be in the body
  108. document.body.appendChild(link);
  109. // simulate click
  110. link.click();
  111. // remove the link when done
  112. document.body.removeChild(link);
  113. } else {
  114. window.open(URI);
  115. }
  116. });
  117. }