Source: feedbackanalysissummary.js

  1. /**
  2. * @fileOverview JavaScript methods for feedbackanalysis summary view.
  3. * @module feedbackanalysissummary
  4. * @author Tuomas Moisio
  5. * @author Visa Nykänen
  6. */
  7. var URI;
  8. var arr = [];
  9. function save() {
  10. let checkBoxImage = document.getElementById('saveForm:basic:1');
  11. let checkBoxImage2 = document
  12. .getElementById('saveForm:anonymityUserBoxes:1');
  13. let filename = document.getElementById('saveForm:input-name').value;
  14. if (filename === "") {
  15. return;
  16. }
  17. if (checkBoxImage != null) {
  18. if (checkBoxImage.checked) {
  19. for (let i = 0; i < arr.length; i++) {
  20. saveAsImage(arr[i]);
  21. }
  22. }
  23. }
  24. if (checkBoxImage2 != null) {
  25. if (checkBoxImage2.checked) {
  26. for (let j = 0; j < arr.length; j++) {
  27. saveAsImage(arr[j]);
  28. }
  29. }
  30. }
  31. }
  32. function createImage() {
  33. exportChart2();
  34. html2canvas(document.getElementById('tableImage')).then(function(canvas) {
  35. let array = [];
  36. arr = array;
  37. arr.push(canvas.toDataURL());
  38. });
  39. if (document.getElementById('charts:barChart_input').checked) {
  40. html2canvas(document.getElementById('barimages')).then(
  41. function(canvas) {
  42. arr.push(canvas.toDataURL());
  43. });
  44. }
  45. if (document.getElementById('charts:pieChart_input').checked) {
  46. html2canvas(document.getElementById('pieimages')).then(
  47. function(canvas) {
  48. arr.push(canvas.toDataURL());
  49. });
  50. }
  51. }
  52. /**
  53. * Sends an image through ajax to the servlet that handles images.
  54. *
  55. * @param URI
  56. * the base64-encoded image to be sent
  57. */
  58. function sendImage(URI) {
  59. $.ajax({
  60. url : "../../webapi/summary/image",
  61. type : "POST",
  62. dataType : "text",
  63. contentType : "text/plain",
  64. cache : false,
  65. data : URI,
  66. success : function(data) {
  67. },
  68. error : function(xhr, status, error) {
  69. showError(msg.obs_errorCouldntSendData + ": " + error);
  70. this_.waiting = false;
  71. }
  72. });
  73. }
  74. /**
  75. * Sends all the images created on the summary-page to the servlet that handles
  76. * them
  77. */
  78. function sendImages() {
  79. html2canvas(document.getElementById('tableImage')).then(function(canvas) {
  80. let URI = "analtable," + canvas.toDataURL();
  81. sendImage(URI)
  82. });
  83. try {
  84. html2canvas(document.getElementById('piechartimage')).then(
  85. function(canvas) {
  86. let URI = "analpie," + canvas.toDataURL();
  87. sendImage(URI)
  88. });
  89. } catch (err) {
  90. }
  91. try {
  92. html2canvas(document.getElementById('barchartimage')).then(
  93. function(canvas) {
  94. let URI = "analbar," + canvas.toDataURL();
  95. sendImage(URI)
  96. });
  97. } catch (err) {
  98. }
  99. }
  100. $(document).ready(function() {
  101. sendImages();
  102. })
  103. $(window).load(function() {
  104. $('td.jqplot-table-legend-label').each(function(index) {
  105. $(this).attr('title', $(this).text());
  106. });
  107. $(document).tooltip();
  108. });
  109. function saveAsImage(dataURL) {
  110. var filename;
  111. var filenameRaw;
  112. try {
  113. filenameRaw = document.getElementById('saveForm:input-name').value;
  114. if (filenameRaw === "") {
  115. return;
  116. }
  117. filename = filenameRaw.replace(/\./g, '-');
  118. } catch (err) {
  119. filename = 'summary.png';
  120. }
  121. var link = document.createElement('a');
  122. if (typeof link.download === 'string') {
  123. link.href = dataURL;
  124. link.download = filename;
  125. // Firefox requires the link to be in the body
  126. document.body.appendChild(link);
  127. // simulate click
  128. link.click();
  129. // remove the link when done
  130. document.body.removeChild(link);
  131. } else {
  132. window.open(URI);
  133. }
  134. }
  135. function exportChart2() {
  136. let count = document.getElementById('chartCount').innerHTML;
  137. for (let index = 0; index < count; index++) {
  138. let b = 'piechart' + index;
  139. let a = 'barchart' + index;
  140. let linebreak = document.createElement('br');
  141. if (document.getElementById('charts:pieChart_input').checked) {
  142. document.getElementById('pieimages').append(PF(b).exportAsImage());
  143. }
  144. if (document.getElementById('charts:barChart_input').checked) {
  145. document.getElementById('barimages').append(PF(a).exportAsImage());
  146. }
  147. }
  148. }