html2print.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // html2print needs CSS regions
  2. // load a ‘polyfill’ if the browser does not support it
  3. if (window.chrome) {
  4. console.log('running chrome, no support for css regions; loading the polyfill');
  5. var script = document.createElement('script');
  6. script.setAttribute('src', 'assets/lib/css-regions.min.js');
  7. document.getElementsByTagName('head')[0].appendChild(script);
  8. };
  9. $(function() {
  10. // ________________________________ INIT __________________________________ //
  11. // Creating crop marks
  12. $("#master-page").append("<div class='crops'><div class='crop-top-left'><span class='bleed'></span></div><div class='crop-top-right'><span class='bleed'></span></div><div class='crop-bottom-right'><span class='bleed'></span></div><div class='crop-bottom-left'><span class='bleed'></span></div></div>")
  13. // Cloning the master page
  14. for (i = 1; i < nb_page; i++){
  15. $("#master-page").clone().attr("id","page-"+i).insertBefore($("#master-page"));
  16. }
  17. $("#master-page").attr("data-width", $(".paper:first-child").width()).hide();
  18. // Loads main content into <article id="my-story">
  19. // if (content) {
  20. // $("#my-story").load(content);
  21. // }
  22. // ________________________________ PREVIEW __________________________________ //
  23. $("#preview").click(function(e){
  24. e.preventDefault();
  25. $(this).toggleClass("button-active");
  26. $("html").toggleClass("preview normal");
  27. });
  28. // __________________________________ DEBUG __________________________________ //
  29. $("#debug").click(function(e){
  30. e.preventDefault();
  31. $(this).toggleClass("button-active");
  32. $("html").toggleClass("debug");
  33. });
  34. // __________________________________ SPREAD __________________________________ //
  35. $("#spread").click(function(e){
  36. e.preventDefault();
  37. $(this).toggleClass("button-active");
  38. $("body").toggleClass("spread");
  39. });
  40. // __________________________________ HIGH RESOLUTION __________________________________ //
  41. $("#hi-res").click(function(e){
  42. e.preventDefault();
  43. $(this).toggleClass("button-active");
  44. $("html").toggleClass("export");
  45. $("img").each(function(){
  46. var hires = $(this).attr("data-alt-src");
  47. var lores = $(this).attr("src");
  48. $(this).attr("data-alt-src", lores)
  49. $(this).attr("src", hires)
  50. });
  51. console.log("Wait for hi-res images to load");
  52. window.setTimeout(function(){
  53. console.log("Check image resolution");
  54. // Redlights images too small for printing
  55. $("img").each(function(){
  56. if (Math.ceil(this.naturalHeight / $(this).height()) < 3) {
  57. console.log($(this).attr("src") + ": " + Math.floor(this.naturalHeight / $(this).height()) );
  58. if($(this).parent().hasClass("moveable")) {
  59. $(this).parent().toggleClass("lo-res");
  60. } else {
  61. $(this).toggleClass("lo-res");
  62. }
  63. }
  64. });
  65. }, 2000);
  66. });
  67. // __________________________________ TOC __________________________________ //
  68. $(".paper").each(function(){
  69. page = $(this).attr("id");
  70. $("#toc-pages").append("<li><a href='#" + page + "'>" + page.replace("-", " ") + "</a></li>")
  71. });
  72. $("#goto").click(function(e){
  73. e.preventDefault();
  74. $(this).toggleClass("button-active");
  75. $("#toc-pages").toggle();
  76. });
  77. // __________________________________ ZOOM __________________________________ //
  78. $("#zoom").click(function(e){
  79. e.preventDefault();
  80. $(this).toggleClass("button-active");
  81. $("#zoom-list").toggle();
  82. });
  83. $("#zoom-list a").click(function(e){
  84. e.preventDefault();
  85. zoom = $(this).attr("title") / 100 ;
  86. unzoom = 1 / zoom;
  87. $("#pages").css("-webkit-transform", "scale(" + zoom + ")");
  88. $("#pages").css("-webkit-transform-origin", "0 0");
  89. });
  90. });