1/* @license 2 * jQuery.print, version 1.4.0 3 * (c) Sathvik Ponangi, Doers' Guild 4 * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) 5 *--------------------------------------------------------------------------*/ 6(function ($) { 7 "use strict"; 8 // A nice closure for our definitions 9 function getjQueryObject(string) { 10 // Make string a vaild jQuery thing 11 var jqObj = $(""); 12 try { 13 jqObj = $(string) 14 .clone(); 15 } catch (e) { 16 jqObj = $("<span />") 17 .html(string); 18 } 19 return jqObj; 20 } 21 22 function printFrame(frameWindow, content, options) { 23 // Print the selected window/iframe 24 var def = $.Deferred(); 25 try { 26 frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow; 27 var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow; 28 if(options.doctype) { 29 wdoc.write(options.doctype); 30 } 31 wdoc.write(content); 32 wdoc.close(); 33 setTimeout(function () { 34 // Fix for IE : Allow it to render the iframe 35 frameWindow.focus(); 36 try { 37 // Fix for IE11 - printng the whole page instead of the iframe content 38 if (!frameWindow.document.execCommand('print', false, null)) { 39 // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 40 frameWindow.print(); 41 } 42 } catch (e) { 43 frameWindow.print(); 44 } 45 frameWindow.close(); 46 def.resolve(); 47 }, options.timeout); 48 } catch (err) { 49 def.reject(err); 50 } 51 return def; 52 } 53 54 function printContentInIFrame(content, options) { 55 var $iframe = $(options.iframe + ""); 56 var iframeCount = $iframe.length; 57 if (iframeCount === 0) { 58 // Create a new iFrame if none is given 59 $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>') 60 .prependTo('body') 61 .css({ 62 "position": "absolute", 63 "top": -999, 64 "left": -999 65 }); 66 } 67 var frameWindow = $iframe.get(0); 68 return printFrame(frameWindow, content, options) 69 .done(function () { 70 // Success 71 setTimeout(function () { 72 // Wait for IE 73 if (iframeCount === 0) { 74 // Destroy the iframe if created here 75 $iframe.remove(); 76 } 77 }, 100); 78 }) 79 .fail(function (err) { 80 // Use the pop-up method if iframe fails for some reason 81 console.error("Failed to print from iframe", err); 82 printContentInNewWindow(content, options); 83 }) 84 .always(function () { 85 try { 86 options.deferred.resolve(); 87 } catch (err) { 88 console.warn('Error notifying deferred', err); 89 } 90 }); 91 } 92 93 function printContentInNewWindow(content, options) { 94 // Open a new window and print selected content 95 var frameWindow = window.open(); 96 return printFrame(frameWindow, content, options) 97 .always(function () { 98 try { 99 options.deferred.resolve(); 100 } catch (err) { 101 console.warn('Error notifying deferred', err); 102 } 103 }); 104 } 105 106 function isNode(o) { 107 /* http://stackoverflow.com/a/384380/937891 */ 108 return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string"); 109 } 110 $.print = $.fn.print = function () { 111 // Print a given set of elements 112 var options, $this, self = this; 113 // console.log("Printing", this, arguments); 114 if (self instanceof $) { 115 // Get the node if it is a jQuery object 116 self = self.get(0); 117 } 118 if (isNode(self)) { 119 // If `this` is a HTML element, i.e. for 120 // $(selector).print() 121 $this = $(self); 122 if (arguments.length > 0) { 123 options = arguments[0]; 124 } 125 } else { 126 if (arguments.length > 0) { 127 // $.print(selector,options) 128 $this = $(arguments[0]); 129 if (isNode($this[0])) { 130 if (arguments.length > 1) { 131 options = arguments[1]; 132 } 133 } else { 134 // $.print(options) 135 options = arguments[0]; 136 $this = $("html"); 137 } 138 } else { 139 // $.print() 140 $this = $("html"); 141 } 142 } 143 // Default options 144 var defaults = { 145 globalStyles: true, 146 mediaPrint: false, 147 stylesheet: null, 148 noPrintSelector: ".no-print", 149 iframe: true, 150 append: null, 151 prepend: null, 152 manuallyCopyFormValues: true, 153 deferred: $.Deferred(), 154 timeout: 250, 155 title: null, 156 doctype: '<!doctype html>' 157 }; 158 // Merge with user-options 159 options = $.extend({}, defaults, (options || {})); 160 var $styles = $(""); 161 if (options.globalStyles) { 162 // Apply the stlyes from the current sheet to the printed page 163 $styles = $("style, link, meta, title"); 164 } else if (options.mediaPrint) { 165 // Apply the media-print stylesheet 166 $styles = $("link[media=print]"); 167 } 168 if (options.stylesheet) { 169 // Add a custom stylesheet if given 170 $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">')); 171 } 172 // Create a copy of the element to print 173 var copy = $this.clone(); 174 // Wrap it in a span to get the HTML markup string 175 copy = $("<span/>") 176 .append(copy); 177 // Remove unwanted elements 178 copy.find(options.noPrintSelector) 179 .remove(); 180 // Add in the styles 181 copy.append($styles.clone()); 182 // Update title 183 if (options.title) { 184 var title = $("title", copy); 185 if (title.length === 0) { 186 title = $("<title />"); 187 copy.append(title); 188 }
189 title.text(options.title); 190 } 191 // Appedned content 192 copy.append(getjQueryObject(options.append)); 193 // Prepended content 194 copy.prepend(getjQueryObject(options.prepend)); 195 if (options.manuallyCopyFormValues) { 196 // Manually copy form values into the HTML for printing user-modified input fields 197 // http://stackoverflow.com/a/26707753 198 copy.find("input") 199 .each(function () { 200 var $field = $(this); 201 if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) { 202 if ($field.prop("checked")) { 203 $field.attr("checked", "checked"); 204 } 205 } else { 206 $field.attr("value", $field.val()); 207 } 208 }); 209 copy.find("select").each(function () { 210 var $field = $(this); 211 $field.find(":selected").attr("selected", "selected"); 212 }); 213 copy.find("textarea").each(function () { 214 // Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589 215 var $field = $(this); 216 $field.text($field.val()); 217 }); 218 } 219 // Get the HTML markup string 220 var content = copy.html(); 221 // Notify with generated markup & cloned elements - useful for logging, etc 222 try { 223 options.deferred.notify('generated_markup', content, copy); 224 } catch (err) { 225 console.warn('Error notifying deferred', err); 226 } 227 // Destroy the copy 228 copy.remove(); 229 if (options.iframe) { 230 // Use an iframe for printing 231 try { 232 printContentInIFrame(content, options); 233 } catch (e) { 234 // Use the pop-up method if iframe fails for some reason 235 console.error("Failed to print from iframe", e.stack, e.message); 236 printContentInNewWindow(content, options); 237 } 238 } else { 239 // Use a new window for printing 240 printContentInNewWindow(content, options); 241 } 242 return this; 243 }; 244})(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.