/modules/shortcodes/js/recipes-printthis.js

https://github.com/danielbachhuber/jetpack · JavaScript · 170 lines · 95 code · 23 blank · 52 comment · 18 complexity · b357051cf00625f373e114f0e47999e2 MD5 · raw file

  1. /*
  2. * printThis v1.3
  3. * @desc Printing plug-in for jQuery
  4. * @author Jason Day
  5. *
  6. * Resources (based on) :
  7. * jPrintArea: http://plugins.jquery.com/project/jPrintArea
  8. * jqPrint: https://github.com/permanenttourist/jquery.jqprint
  9. * Ben Nadal: http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
  10. *
  11. * Dual licensed under the MIT and GPL licenses:
  12. * http://www.opensource.org/licenses/mit-license.php
  13. * http://www.gnu.org/licenses/gpl.html
  14. *
  15. * (c) Jason Day 2013
  16. *
  17. * Usage:
  18. *
  19. * $("#mySelector").printThis({
  20. * debug: false, * show the iframe for debugging
  21. * importCSS: true, * import page CSS
  22. * printContainer: true, * grab outer container as well as the contents of the selector
  23. * loadCSS: "path/to/my.css", * path to additional css file
  24. * pageTitle: "", * add title to print page
  25. * removeInline: false, * remove all inline styles from print elements
  26. * printDelay: 333, * variable print delay S. Vance
  27. * header: null * prefix to html
  28. * });
  29. *
  30. * Notes:
  31. * - the loadCSS will load additional css (with or without @media print) into the iframe, adjusting layout
  32. */
  33. /* jshint onevar: false, smarttabs: true, devel: true */
  34. ;(function ($) {
  35. var opt;
  36. $.fn.printThis = function (options) {
  37. opt = $.extend({}, $.fn.printThis.defaults, options);
  38. var $element = this instanceof jQuery ? this : $(this);
  39. var strFrameName = 'printThis-' + (new Date()).getTime();
  40. if(window.location.hostname !== document.domain && navigator.userAgent.match(/msie/i)){
  41. // Ugly IE hacks due to IE not inheriting document.domain from parent
  42. // checks if document.domain is set by comparing the host name against document.domain
  43. var iframeContents = '<head><script>document.domain=\\\'' + document.domain + '\\\';</script></head><body></body>';
  44. var iframeSrc = 'data:text/html;charset=utf-8,' + encodeURI(iframeContents);
  45. var printI= document.createElement('iframe');
  46. printI.name = 'printIframe';
  47. printI.id = strFrameName;
  48. printI.className = 'MSIE';
  49. document.body.appendChild(printI);
  50. printI.src = iframeSrc;
  51. } else {
  52. // other browsers inherit document.domain, and IE works if document.domain is not explicitly set
  53. var $frame = $('<iframe id="' + strFrameName +'" name="printIframe" />');
  54. $frame.appendTo('body');
  55. }
  56. var $iframe = $('#' + strFrameName);
  57. // show frame if in debug mode
  58. if (!opt.debug) {
  59. $iframe.css({
  60. position: 'absolute',
  61. width: '0px',
  62. height: '0px',
  63. left: '-600px',
  64. top: '-600px'
  65. });
  66. }
  67. // $iframe.ready() and $iframe.load were inconsistent between browsers
  68. setTimeout ( function () {
  69. var $doc = $iframe.contents();
  70. // import page stylesheets
  71. if (opt.importCSS) {
  72. $('link[rel=stylesheet]').each(function () {
  73. var href = $(this).attr('href');
  74. if (href) {
  75. var media = $(this).attr('media') || 'all';
  76. $doc.find('head').append('<link type="text/css" rel="stylesheet" href="' + href + '" media="' + media + '">');
  77. }
  78. });
  79. }
  80. //add title of the page
  81. if (opt.pageTitle) {
  82. $doc.find('head').append('<title>' + opt.pageTitle + '</title>');
  83. }
  84. // import additional stylesheet
  85. if (opt.loadCSS) {
  86. $doc.find('head').append('<link type="text/css" rel="stylesheet" href="' + opt.loadCSS + '">');
  87. }
  88. // print header
  89. if (opt.header) {
  90. $doc.find('body').append(opt.header);
  91. }
  92. // grab $.selector as container
  93. if (opt.printContainer) {
  94. $doc.find('body').append($element.outer());
  95. }
  96. // otherwise just print interior elements of container
  97. else {
  98. $element.each(function () {
  99. $doc.find('body').append($(this).html());
  100. });
  101. }
  102. // remove inline styles
  103. if (opt.removeInline) {
  104. // $.removeAttr available jQuery 1.7+
  105. if ($.isFunction($.removeAttr)) {
  106. $doc.find('body *').removeAttr('style');
  107. } else {
  108. $doc.find('body *').attr('style', '');
  109. }
  110. }
  111. setTimeout(function () {
  112. if($iframe.hasClass('MSIE')){
  113. // check if the iframe was created with the ugly hack
  114. // and perform another ugly hack out of neccessity
  115. window.frames.printIframe.focus();
  116. $doc.find('head').append('<script> window.print(); </script>');
  117. } else {
  118. // proper method
  119. $iframe[0].contentWindow.focus();
  120. $iframe[0].contentWindow.print();
  121. }
  122. $element.trigger( 'done');
  123. //remove iframe after print
  124. if (!opt.debug) {
  125. setTimeout(function () {
  126. $iframe.remove();
  127. }, 1000);
  128. }
  129. }, opt.printDelay);
  130. }, 333 );
  131. };
  132. // defaults
  133. $.fn.printThis.defaults = {
  134. debug: false, // show the iframe for debugging
  135. importCSS: false, // import parent page css
  136. printContainer: true, // print outer container/$.selector
  137. loadCSS: '', // load an additional css file
  138. pageTitle: '', // add title to print page
  139. removeInline: false, // remove all inline styles
  140. printDelay: 333, // variable print delay S. Vance
  141. header: null // prefix to html
  142. };
  143. // $.selector container
  144. jQuery.fn.outer = function () {
  145. return $($('<div></div>').html(this.clone())).html();
  146. };
  147. })(jQuery);