123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- ;
- (function($) {
- function appendContent($el, content) {
- if (!content) return;
-
- $el.append(content.jquery ? content.clone() : content);
- }
- function appendBody($body, $element, opt) {
-
-
- var $content = $element.clone(opt.formValues);
- if (opt.formValues) {
-
-
- copyValues($element, $content, 'select, textarea');
- }
- if (opt.removeScripts) {
- $content.find('script').remove();
- }
- if (opt.printContainer) {
-
- $content.appendTo($body);
- } else {
-
- $content.each(function() {
- $(this).children().appendTo($body)
- });
- }
- }
-
- function copyValues(origin, clone, elementSelector) {
- var $originalElements = origin.find(elementSelector);
- clone.find(elementSelector).each(function(index, item) {
- $(item).val($originalElements.eq(index).val());
- });
- }
- var opt;
- $.fn.printThis = function(options) {
- opt = $.extend({}, $.fn.printThis.defaults, options);
- var $element = this instanceof jQuery ? this : $(this);
- var strFrameName = "printThis-" + (new Date()).getTime();
- if (window.location.hostname !== document.domain && navigator.userAgent.match(/msie/i)) {
-
-
- var iframeSrc = "javascript:document.write(\"<head><script>document.domain=\\\"" + document.domain + "\\\";</s" + "cript></head><body></body>\")";
- var printI = document.createElement('iframe');
- printI.name = "printIframe";
- printI.id = strFrameName;
- printI.className = "MSIE";
- document.body.appendChild(printI);
- printI.src = iframeSrc;
- } else {
-
- var $frame = $("<iframe id='" + strFrameName + "' name='printIframe' />");
- $frame.appendTo("body");
- }
- var $iframe = $("#" + strFrameName);
-
- if (!opt.debug) $iframe.css({
- position: "absolute",
- width: "0px",
- height: "0px",
- left: "-600px",
- top: "-600px"
- });
-
- if (typeof opt.beforePrint === "function") {
- opt.beforePrint();
- }
-
- setTimeout(function() {
-
- function setDocType($iframe, doctype){
- var win, doc;
- win = $iframe.get(0);
- win = win.contentWindow || win.contentDocument || win;
- doc = win.document || win.contentDocument || win;
- doc.open();
- doc.write(doctype);
- doc.close();
- }
- if (opt.doctypeString){
- setDocType($iframe, opt.doctypeString);
- }
- var $doc = $iframe.contents(),
- $head = $doc.find("head"),
- $body = $doc.find("body"),
- $base = $('base'),
- baseURL;
-
- if (opt.base === true && $base.length > 0) {
-
- baseURL = $base.attr('href');
- } else if (typeof opt.base === 'string') {
-
- baseURL = opt.base;
- } else {
-
- baseURL = document.location.protocol + '//' + document.location.host;
- }
- $head.append('<base href="' + baseURL + '">');
-
- if (opt.importCSS) $("link[rel=stylesheet]").each(function() {
- var href = $(this).attr("href");
- if (href) {
- var media = $(this).attr("media") || "all";
- $head.append("<link type='text/css' rel='stylesheet' href='" + href + "' media='" + media + "'>");
- }
- });
-
- if (opt.importStyle) $("style").each(function() {
- $head.append(this.outerHTML);
- });
-
- if (opt.pageTitle) $head.append("<title>" + opt.pageTitle + "</title>");
-
- if (opt.loadCSS) {
- if ($.isArray(opt.loadCSS)) {
- jQuery.each(opt.loadCSS, function(index, value) {
- $head.append("<link type='text/css' rel='stylesheet' href='" + this + "'>");
- });
- } else {
- $head.append("<link type='text/css' rel='stylesheet' href='" + opt.loadCSS + "'>");
- }
- }
- var pageHtml = $('html')[0];
-
- $doc.find('html').prop('style', pageHtml.style.cssText);
-
- var tag = opt.copyTagClasses;
- if (tag) {
- tag = tag === true ? 'bh' : tag;
- if (tag.indexOf('b') !== -1) {
- $body.addClass($('body')[0].className);
- }
- if (tag.indexOf('h') !== -1) {
- $doc.find('html').addClass(pageHtml.className);
- }
- }
-
- appendContent($body, opt.header);
- if (opt.canvas) {
-
- var canvasId = 0;
-
- $element.find('canvas').addBack('canvas').each(function(){
- $(this).attr('data-printthis', canvasId++);
- });
- }
- appendBody($body, $element, opt);
- if (opt.canvas) {
-
- $body.find('canvas').each(function(){
- var cid = $(this).data('printthis'),
- $src = $('[data-printthis="' + cid + '"]');
- this.getContext('2d').drawImage($src[0], 0, 0);
-
- if ($.isFunction($.fn.removeAttr)) {
- $src.removeAttr('data-printthis');
- } else {
- $.each($src, function(i, el) {
- el.removeAttribute('data-printthis');
- });
- }
- });
- }
-
- if (opt.removeInline) {
-
- var selector = opt.removeInlineSelector || '*';
-
- if ($.isFunction($.removeAttr)) {
- $body.find(selector).removeAttr("style");
- } else {
- $body.find(selector).attr("style", "");
- }
- }
-
- appendContent($body, opt.footer);
-
- function attachOnBeforePrintEvent($iframe, beforePrintHandler) {
- var win = $iframe.get(0);
- win = win.contentWindow || win.contentDocument || win;
- if (typeof beforePrintHandler === "function") {
- if ('matchMedia' in win) {
- win.matchMedia('print').addListener(function(mql) {
- if(mql.matches) beforePrintHandler();
- });
- } else {
- win.onbeforeprint = beforePrintHandler;
- }
- }
- }
- attachOnBeforePrintEvent($iframe, opt.beforePrintEvent);
- setTimeout(function() {
- if ($iframe.hasClass("MSIE")) {
-
-
- window.frames["printIframe"].focus();
- $head.append("<script> window.print(); </s" + "cript>");
- } else {
-
- if (document.queryCommandSupported("print")) {
- $iframe[0].contentWindow.document.execCommand("print", false, null);
- } else {
- $iframe[0].contentWindow.focus();
- $iframe[0].contentWindow.print();
- }
- }
-
- if (!opt.debug) {
- setTimeout(function() {
- $iframe.remove();
- }, 1000);
- }
-
- if (typeof opt.afterPrint === "function") {
- opt.afterPrint();
- }
- }, opt.printDelay);
- }, 333);
- };
-
- $.fn.printThis.defaults = {
- debug: false,
- importCSS: true,
- importStyle: false,
- printContainer: true,
- loadCSS: "",
- pageTitle: "",
- removeInline: false,
- removeInlineSelector: "*",
- printDelay: 333,
- header: null,
- footer: null,
- base: false,
- formValues: true,
- canvas: false,
- doctypeString: '<!DOCTYPE html>',
- removeScripts: false,
- copyTagClasses: false,
- beforePrintEvent: null,
- beforePrint: null,
- afterPrint: null
- };
- })(jQuery);
|