gui.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * This file is part of HTML2print.
  3. *
  4. * HTML2print is free software: you can redistribute it and/or modify it under the
  5. * terms of the GNU Affero General Public License as published by the Free
  6. * Software Foundation, either version 3 of the License, or (at your option) any
  7. * later version.
  8. *
  9. * HTML2print is distributed in the hope that it will be useful, but WITHOUT ANY
  10. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. * PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. * details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License along
  15. * with HTML2print. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. (function($, undefined) {
  18. 'use strict';
  19. var $viewport = $("#viewport")[0]
  20. , $previewBtn = $('[name="preview"]')[0]
  21. , $gridBtn = $('[name="grid"]')[0]
  22. , $debugBtn = $('[name="debug"]')[0]
  23. , $spreadBtn = $('[name="spread"]')[0]
  24. , $zoomBtn = $('[name="zoom"]')[0]
  25. , $pageBtn = $('[name="page"]')[0]
  26. , $displayBtn = $('[name="display"]')[0]
  27. , $reloadBtn = $('#reload')[0]
  28. , $printBtn = $('#print')[0]
  29. , $designmodeBtn = $('[name="designmode"]')[0]
  30. ;
  31. $viewport.addEventListener("load", function(event) {
  32. var $contentDoc = this.contentDocument;
  33. var $doc = this.contentDocument.getElementsByTagName('html')[0]
  34. function switchPreview(event) {
  35. if(this.checked) {
  36. $doc.classList.add("preview");
  37. $doc.classList.remove("normal");
  38. } else {
  39. $doc.classList.add("normal");
  40. $doc.classList.remove("preview");
  41. }
  42. }
  43. function switchGrid(event) {
  44. console.log(event);
  45. if (this.checked) {
  46. $doc.classList.add("grid");
  47. } else {
  48. $doc.classList.remove("grid");
  49. }
  50. }
  51. function switchDebug(event) {
  52. if(this.checked) {
  53. $doc.classList.add("debug");
  54. } else {
  55. $doc.classList.remove("debug");
  56. }
  57. }
  58. function switchSpread(event) {
  59. if(this.checked) {
  60. $doc.classList.add("spread");
  61. } else {
  62. $doc.classList.remove("spread");
  63. }
  64. }
  65. function switchDesignMode(event) {
  66. if(this.checked) {
  67. $contentDoc.designMode = "on";
  68. } else {
  69. $contentDoc.designMode = "off";
  70. }
  71. }
  72. function setZoom(event) {
  73. var zoomLevel = this.value / 100;
  74. var elt = $doc.querySelector("#pages");
  75. elt.style.webkitTransform = "scale(" + zoomLevel + ")";
  76. elt.style.webkitTransformOrigin = "0 0";
  77. }
  78. function changePage(event) {
  79. var pageNumber = this.value - 1;
  80. var target = $doc.querySelectorAll('.paper')[pageNumber];
  81. var offsetTop = target.offsetTop;
  82. $doc.querySelector('body').scrollTop = offsetTop;
  83. }
  84. function changeDisplay(event) {
  85. var htmlelt = $doc.querySelectorAll('html')[0];
  86. var elts = $doc.querySelectorAll('img');
  87. $doc.classList.remove("low");
  88. $doc.classList.remove("bw");
  89. $doc.classList.remove("color");
  90. $doc.classList.add(this.value);
  91. //for (var i = 0, l = elts.length; i < l; i ++) {
  92. //var elt = elts[i];
  93. //if (!elt.dataset.low) { elt.dataset.low = elt.src; }
  94. //elt.style.visibility = 'visible';
  95. //if (elt.dataset[this.value]) {
  96. //elt.src = elt.dataset[this.value];
  97. //} else {
  98. //elt.src = "";
  99. //elt.style.visibility = 'hidden'
  100. //}
  101. //}
  102. //console.log("Wait for hi-res images to load");
  103. //window.setTimeout(function(){
  104. console.log("Check image resolution");
  105. // Redlights images too small for printing
  106. var images = $doc.getElementsByTagName("img");
  107. for (var i=0; i < images.length; i++) {
  108. if (Math.ceil(images[i].naturalHeight / images[i].height) < 1.8) {
  109. console.log(images[i] + ": " + Math.floor(images[i].naturalHeight / images[i].height) );
  110. images[i].className += "lo-res";
  111. }
  112. if (Math.ceil(images[i].naturalHeight / images[i].height) < 4.5) {
  113. console.log(images[i] + ": " + Math.floor(images[i].naturalHeight / images[i].height) );
  114. images[i].className += "too-big";
  115. }
  116. }
  117. console.log("done checking resolution");
  118. //}, 2000);
  119. //
  120. // TODO: change le titre ici
  121. //console.log(document.title)
  122. //console.log($contentDoc.title)
  123. //
  124. }
  125. function reload(event) {
  126. $viewport.contentWindow.location.reload();
  127. }
  128. function print(event) {
  129. $viewport.contentWindow.print();
  130. }
  131. $previewBtn.addEventListener("change", switchPreview);
  132. $gridBtn.addEventListener("change", switchGrid);
  133. $debugBtn.addEventListener("change", switchDebug);
  134. $spreadBtn.addEventListener("change", switchSpread);
  135. $zoomBtn.addEventListener("change", setZoom);
  136. $pageBtn.addEventListener("change", changePage);
  137. $reloadBtn.addEventListener("click", reload);
  138. $printBtn.addEventListener("click", print);
  139. $displayBtn.addEventListener("change", changeDisplay);
  140. $designmodeBtn.addEventListener("change", switchDesignMode);
  141. switchPreview.bind($previewBtn)();
  142. switchGrid.bind($gridBtn)();
  143. switchDebug.bind($debugBtn)();
  144. switchSpread.bind($spreadBtn)();
  145. setZoom.bind($zoomBtn)();
  146. //changePage.bind($pageBtn)();
  147. }, false);
  148. })(document.querySelectorAll.bind(document));