1/** 2 * Version 2.4.0 Copyright (C) 2013 3 * Tested in IE 11, FF 28.0 and Chrome 33.0.1750.154 4 * No official support for other browsers, but will TRY to accommodate challenges in other browsers. 5 * Example: 6 * Print Button: <div id="print_button">Print</div> 7 * Print Area : <div class="PrintArea" id="MyId" class="MyClass"> ... html ... </div> 8 * Javascript : <script> 9 * $("div#print_button").click(function(){ 10 * $("div.PrintArea").printArea( [OPTIONS] ); 11 * }); 12 * </script> 13 * options are passed as json (example: {mode: "popup", popClose: false}) 14 * 15 * {OPTIONS} | [type] | (default), values | Explanation 16 * --------- | --------- | ---------------------- | ----------- 17 * @mode | [string] | (iframe),popup | printable window is either iframe or browser popup 18 * @popHt | [number] | (500) | popup window height 19 * @popWd | [number] | (400) | popup window width 20 * @popX | [number] | (500) | popup window screen X position 21 * @popY | [number] | (500) | popup window screen Y position 22 * @popTitle | [string] | ('') | popup window title element 23 * @popClose | [boolean] | (false),true | popup window close after printing 24 * @extraCss | [string] | ('') | comma separated list of extra css to include 25 * @retainAttr | [string[]] | ["id","class","style"] | string array of attributes to retain for the containment area. (ie: id, style, class) 26 * @standard | [string] | strict, loose, (html5) | Only for popup. For html 4.01, strict or loose document standard, or html 5 standard 27 * @extraHead | [string] | ('') | comma separated list of extra elements to be appended to the head tag 28 */ 29(function($) { 30 var counter = 0; 31 var modes = { iframe : "iframe", popup : "popup" }; 32 var standards = { strict : "strict", loose : "loose", html5 : "html5" }; 33 var defaults = { mode : modes.iframe, 34 standard : standards.html5, 35 popHt : 500, 36 popWd : 400, 37 popX : 200, 38 popY : 200, 39 popTitle : '', 40 popClose : false, 41 extraCss : '', 42 extraHead : '', 43 retainAttr : ["id","class","style"] }; 44 45 var settings = {};//global settings 46 47 $.fn.printArea = function( options ) 48 { 49 $.extend( settings, defaults, options ); 50 51 counter++; 52 var idPrefix = "printArea_"; 53 $( "[id^=" + idPrefix + "]" ).remove(); 54 55 settings.id = idPrefix + counter; 56 57 var $printSource = $(this); 58 59 var PrintAreaWindow = PrintArea.getPrintWindow(); 60 61 PrintArea.write( PrintAreaWindow.doc, $printSource ); 62 63 setTimeout( function () { PrintArea.print( PrintAreaWindow ); }, 1000 ); 64 }; 65 66 var PrintArea = { 67 print : function( PAWindow ) { 68 var paWindow = PAWindow.win; 69 70 $(PAWindow.doc).ready(function(){ 71 paWindow.focus(); 72 paWindow.print(); 73 74 if ( settings.mode == modes.popup && settings.popClose ) 75 setTimeout(function() { paWindow.close(); }, 2000); 76 }); 77 }, 78 write : function ( PADocument, $ele ) { 79 PADocument.open(); 80 PADocument.write( PrintArea.docType() + "<html>" + PrintArea.getHead() + PrintArea.getBody( $ele ) + "</html>" ); 81 PADocument.close(); 82 }, 83 docType : function() { 84 if ( settings.mode == modes.iframe ) return ""; 85 86 if ( settings.standard == standards.html5 ) return "<!DOCTYPE html>"; 87 88 var transitional = settings.standard == standards.loose ? " Transitional" : ""; 89 var dtd = settings.standard == standards.loose ? "loose" : "strict"; 90 91 return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01' + transitional + '//EN" "http://www.w3.org/TR/html4/' + dtd + '.dtd">'; 92 }, 93 getHead : function() { 94 var extraHead = ""; 95 var links = ""; 96 97 if ( settings.extraHead ) settings.extraHead.replace( /([^,]+)/g, function(m){ extraHead += m }); 98 99 $(document).find("link") 100 .filter(function(){ // Requirement: <link> element MUST have rel="stylesheet" to be considered in print document 101 var relAttr = $(this).attr("rel"); 102 return ($.type(relAttr) === 'undefined') == false && relAttr.toLowerCase() == 'stylesheet'; 103 }) 104 .filter(function(){ // Include if media is undefined, empty, print or all 105 var mediaAttr = $(this).attr("media"); 106 return $.type(mediaAttr) === 'undefined' || mediaAttr == "" || mediaAttr.toLowerCase() == 'print' || mediaAttr.toLowerCase() == 'all' 107 }) 108 .each(function(){ 109 links += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >'; 110 });
111 if ( settings.extraCss ) settings.extraCss.replace( /([^,\s]+)/g, function(m){ links += '<link type="text/css" rel="stylesheet" href="' + m + '">' }); 112 113 return "<head><title>" + settings.popTitle + "</title>" + extraHead + links + "</head>"; 114 }, 115 getBody : function ( elements ) { 116 var htm = ""; 117 var attrs = settings.retainAttr; 118 elements.each(function() { 119 var ele = PrintArea.getFormData( $(this) ); 120 121 var attributes = "" 122 for ( var x = 0; x < attrs.length; x++ ) 123 { 124 var eleAttr = $(ele).attr( attrs[x] ); 125 if ( eleAttr ) attributes += (attributes.length > 0 ? " ":"") + attrs[x] + "='" + eleAttr + "'"; 126 } 127 128 htm += '<div ' + attributes + '>' + $(ele).html() + '</div>'; 129 }); 130 131 return "<body>" + htm + "</body>"; 132 }, 133 getFormData : function ( ele ) { 134 var copy = ele.clone(); 135 var copiedInputs = $("input,select,textarea", copy); 136 $("input,select,textarea", ele).each(function( i ){ 137 var typeInput = $(this).attr("type"); 138 if ($.type(typeInput) === 'undefined') typeInput = $(this).is("select") ? "select" : $(this).is("textarea") ? "textarea" : ""; 139 var copiedInput = copiedInputs.eq( i ); 140 141 if ( typeInput == "radio" || typeInput == "checkbox" ) copiedInput.attr( "checked", $(this).is(":checked") ); 142 else if ( typeInput == "text" ) copiedInput.attr( "value", $(this).val() ); 143 else if ( typeInput == "select" ) 144 $(this).find( "option" ).each( function( i ) { 145 if ( $(this).is(":selected") ) $("option", copiedInput).eq( i ).attr( "selected", true ); 146 }); 147 else if ( typeInput == "textarea" ) copiedInput.text( $(this).val() ); 148 }); 149 return copy; 150 }, 151 getPrintWindow : function () { 152 switch ( settings.mode ) 153 { 154 case modes.iframe : 155 var f = new PrintArea.Iframe(); 156 return { win : f.contentWindow || f, doc : f.doc }; 157 case modes.popup : 158 var p = new PrintArea.Popup(); 159 return { win : p, doc : p.doc }; 160 } 161 }, 162 Iframe : function () { 163 var frameId = settings.id; 164 var iframeStyle = 'border:0;position:absolute;width:0px;height:0px;right:0px;top:0px;'; 165 var iframe; 166 167 try 168 { 169 iframe = document.createElement('iframe'); 170 document.body.appendChild(iframe); 171 $(iframe).attr({ style: iframeStyle, id: frameId, src: "#" + new Date().getTime() }); 172 iframe.doc = null; 173 iframe.doc = iframe.contentDocument ? iframe.contentDocument : ( iframe.contentWindow ? iframe.contentWindow.document : iframe.document); 174 } 175 catch( e ) { throw e + ". iframes may not be supported in this browser."; } 176 177 if ( iframe.doc == null ) throw "Cannot find document."; 178 179 return iframe; 180 }, 181 Popup : function () { 182 var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no"; 183 windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt; 184 windowAttr += ",resizable=yes,screenX=" + settings.popX + ",screenY=" + settings.popY + ",personalbar=no,scrollbars=yes"; 185 186 var newWin = window.open( "", "_blank", windowAttr ); 187 188 newWin.doc = newWin.document; 189 190 return newWin; 191 } 192 }; 193})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.