jqprint.js 7.11 KB
// -----------------------------------------------------------------------
// Eros Fratini - eros@recoding.it
// jqprint 0.3
//
// - 19/06/2009 - some new implementations, added Opera support
// - 11/05/2009 - first sketch
//
// Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
// requires jQuery 1.3.x
//
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
//------------------------------------------------------------------------
layui.define("jquery",function (exports) {
    var jQuery = layui.jquery,
        $ = layui.jquery;
    (function ($) {

        if (jQuery.browser) return;

        jQuery.browser = {};
        jQuery.browser.mozilla = false;
        jQuery.browser.webkit = false;
        jQuery.browser.opera = false;
        jQuery.browser.msie = false;

        var nAgt = navigator.userAgent;
        jQuery.browser.name = navigator.appName;
        jQuery.browser.fullVersion = '' + parseFloat(navigator.appVersion);
        jQuery.browser.majorVersion = parseInt(navigator.appVersion, 10);
        var nameOffset, verOffset, ix;

        // In Opera, the true version is after "Opera" or after "Version"  
        if ((verOffset = nAgt.indexOf("Opera")) != -1) {
            jQuery.browser.opera = true;
            jQuery.browser.name = "Opera";
            jQuery.browser.fullVersion = nAgt.substring(verOffset + 6);
            if ((verOffset = nAgt.indexOf("Version")) != -1)
                jQuery.browser.fullVersion = nAgt.substring(verOffset + 8);
        }
        // In MSIE, the true version is after "MSIE" in userAgent  
        else if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
            jQuery.browser.msie = true;
            jQuery.browser.name = "Microsoft Internet Explorer";
            jQuery.browser.fullVersion = nAgt.substring(verOffset + 5);
        }
        // In Chrome, the true version is after "Chrome"  
        else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
            jQuery.browser.webkit = true;
            jQuery.browser.name = "Chrome";
            jQuery.browser.fullVersion = nAgt.substring(verOffset + 7);
        }
        // In Safari, the true version is after "Safari" or after "Version"  
        else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
            jQuery.browser.webkit = true;
            jQuery.browser.name = "Safari";
            jQuery.browser.fullVersion = nAgt.substring(verOffset + 7);
            if ((verOffset = nAgt.indexOf("Version")) != -1)
                jQuery.browser.fullVersion = nAgt.substring(verOffset + 8);
        }
        // In Firefox, the true version is after "Firefox"  
        else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
            jQuery.browser.mozilla = true;
            jQuery.browser.name = "Firefox";
            jQuery.browser.fullVersion = nAgt.substring(verOffset + 8);
        }
        // In most other browsers, "name/version" is at the end of userAgent  
        else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) <
            (verOffset = nAgt.lastIndexOf('/'))) {
            jQuery.browser.name = nAgt.substring(nameOffset, verOffset);
            jQuery.browser.fullVersion = nAgt.substring(verOffset + 1);
            if (jQuery.browser.name.toLowerCase() == jQuery.browser.name.toUpperCase()) {
                jQuery.browser.name = navigator.appName;
            }
        }
        // trim the fullVersion string at semicolon/space if present  
        if ((ix = jQuery.browser.fullVersion.indexOf(";")) != -1)
            jQuery.browser.fullVersion = jQuery.browser.fullVersion.substring(0, ix);
        if ((ix = jQuery.browser.fullVersion.indexOf(" ")) != -1)
            jQuery.browser.fullVersion = jQuery.browser.fullVersion.substring(0, ix);

        jQuery.browser.majorVersion = parseInt('' + jQuery.browser.fullVersion, 10);
        if (isNaN(jQuery.browser.majorVersion)) {
            jQuery.browser.fullVersion = '' + parseFloat(navigator.appVersion);
            jQuery.browser.majorVersion = parseInt(navigator.appVersion, 10);
        }
        jQuery.browser.version = jQuery.browser.majorVersion;  

    var opt;

    $.fn.jqprint = function (options) {
        opt = $.extend({}, $.fn.jqprint.defaults, options);
        var $element = (this instanceof jQuery) ? this : $(this);

        if (opt.operaSupport && $.browser.opera) 
        { 
            var tab = window.open("","jqPrint-preview");
            tab.document.open();

            var doc = tab.document;
        }
        else 
        {
            var $iframe = $("<iframe  />");
        
            if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }

            $iframe.appendTo("body");
            var doc = $iframe[0].contentWindow.document;
        }
        
        if (opt.importCSS)
        {
            if ($("link[media=print]").length > 0) 
            {
                $("link[media=print]").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
                });
            }
            else 
            {
                $("link").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
        }
        
        if (opt.printContainer) { doc.write($element.outer()); }
        else { $element.each( function() { doc.write($(this).html()); }); }
        
        doc.close();
        
        (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
        setTimeout(
            function () {
                var wind = (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow);

                var beforePrint = function (mql) {
                    console.log(mql);
                    if (opt.beforePrint !== undefined) {
                        opt.beforePrint.call(null);
                    }
                };

                var afterPrint = function (mql) {
                    console.log(mql);
                    if (opt.afterPrint !== undefined) {
                        opt.afterPrint.call(null);
                    }
                };

                if (wind.matchMedia) {
                    var mediaQueryList = wind.matchMedia('print');
                    mediaQueryList.addListener(function (mql) {
                        if (mql.matches) {
                            beforePrint(mql);
                        } else {
                            afterPrint(mql);
                        }
                    });
                }

                wind.onbeforeprint = beforePrint;
                wind.onafterprint = afterPrint;

                wind.print();
            if (tab) { tab.close(); }
        }, 1000);
    }
    
    $.fn.jqprint.defaults = {
		debug: false,
		importCSS: true, 
		printContainer: true,
		operaSupport: true
	};

    // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
    jQuery.fn.outer = function() {
      return $($('<div></div>').html(this.clone())).html();
    } 
    })(jQuery);
    exports('jqprint', jQuery);
});