1/* @license 2 * jQuery.print, version 1.6.2 3 * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) 4 *--------------------------------------------------------------------------*/ 5(function ($) { 6 'use strict'; 7 8 // A nice closure for our definitions 9 10 function jQueryCloneWithSelectAndTextAreaValues( 11 elmToClone, withDataAndEvents, deepWithDataAndEvents) { 12 // Replacement jQuery clone that also clones the values in selects and textareas as jQuery doesn't for performance reasons - https://stackoverflow.com/questions/742810/clone-isnt-cloning-select-values 13 // Based on https://github.com/spencertipping/jquery.fix.clone 14 var $elmToClone = $(elmToClone), 15 $result = $elmToClone.clone(withDataAndEvents, deepWithDataAndEvents), 16 $myTextareas = $elmToClone.find('textarea').add($elmToClone.filter('textarea')), 17 $resultTextareas = $result.find('textarea').add($result.filter('textarea')), 18 $mySelects = $elmToClone.find('select').add($elmToClone.filter('select')), 19 $resultSelects = $result.find('select').add($result.filter('select')), 20 $myCanvas = $elmToClone.find('canvas').add($elmToClone.filter('canvas')), 21 $resultCanvas = $result.find('canvas').add($result.filter('canvas')), 22 i, l, j, m, myCanvasContext; 23 24 for (i = 0, l = $myTextareas.length; i < l; ++i) { 25 $($resultTextareas[i]).val($($myTextareas[i]).val()); 26 } 27 for (i = 0, l = $mySelects.length; i < l; ++i) { 28 for (j = 0, m = $mySelects[i].options.length; j < m; ++j) { 29 if ($mySelects[i].options[j].selected === true) { 30 $resultSelects[i].options[j].selected = true; 31 } 32 } 33 } 34 for (i = 0, l = $myCanvas.length; i < l; ++i) { 35 // https://stackoverflow.com/a/41242597 36 myCanvasContext = $myCanvas[i].getContext('2d'); 37 if (myCanvasContext) { 38 $resultCanvas[i].getContext('2d').drawImage($myCanvas[i], 0, 0); 39 $($resultCanvas[i]).attr('data-jquery-print', myCanvasContext.canvas.toDataURL()); 40 } 41 } 42 return $result; 43 } 44 45 function getjQueryObject(string) { 46 // Make string a vaild jQuery thing 47 var jqObj = $(''); 48 try { 49 jqObj = jQueryCloneWithSelectAndTextAreaValues(string); 50 } catch (e) { 51 jqObj = $('<span />').html(string); 52 } 53 return jqObj; 54 } 55 56 function printFrame(frameWindow, content, options) { 57 // Print the selected window/iframe 58 var def = $.Deferred(); 59 try { 60 frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || 61 frameWindow; 62 try { 63 frameWindow.resizeTo(window.innerWidth, window.innerHeight); 64 } catch (err) { 65 console.warn(err); 66 } 67 var wdoc = frameWindow.document || frameWindow.contentDocument || 68 frameWindow; 69 if (options.doctype) { 70 wdoc.write(options.doctype); 71 } 72 wdoc.write(content); 73 try { 74 var canvas = wdoc.querySelectorAll('canvas'); 75 for (var i = 0; i < canvas.length; i++) { 76 var ctx = canvas[i].getContext('2d'); 77 var image = new Image(); 78 image.onload = function () { 79 ctx.drawImage(image, 0, 0); 80 }; 81 image.src = canvas[i].getAttribute('data-jquery-print'); 82 } 83 } catch (err) { 84 console.warn(err); 85 } 86 wdoc.close(); 87 var printed = false, 88 callPrint = function () { 89 if (printed) { 90 return; 91 } 92 // Fix for IE : Allow it to render the iframe 93 frameWindow.focus(); 94 try { 95 // Fix for IE11 - printng the whole page instead of the iframe content 96 if (!frameWindow.document.execCommand('print', false, null)) { 97 // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 98 frameWindow.print(); 99 } 100 // focus body as it is losing focus in iPad and content not getting printed 101 $('body').focus(); 102 } catch (e) { 103 frameWindow.print(); 104 } 105 frameWindow.close(); 106 printed = true; 107 def.resolve(); 108 }; 109 // Print once the frame window loads - seems to work for the new-window option but unreliable for the iframe 110 $(frameWindow).on('load', callPrint); 111 // Fallback to printing directly if the frame doesn't fire the load event for whatever reason 112 setTimeout(callPrint, options.timeout); 113 } catch (err) { 114 def.reject(err); 115 } 116 return def; 117 } 118 119 function printContentInIFrame(content, options) { 120 var $iframe = $(options.iframe + '');
vendor: 7,201 bytes, lines 121-309
121 var iframeCount = $iframe.length; 122 if (iframeCount === 0) { 123 // Create a new iFrame if none is given 124 $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>').prependTo('body').css({ 125 'position': 'absolute', 126 'top': -999, 127 'left': -999, 128 }); 129 } 130 var frameWindow = $iframe.get(0); 131 return printFrame(frameWindow, content, options).done(function () { 132 // Success 133 setTimeout(function () { 134 // Wait for IE 135 if (iframeCount === 0) { 136 // Destroy the iframe if created here 137 $iframe.remove(); 138 } 139 }, 1000); 140 }).fail(function (err) { 141 // Use the pop-up method if iframe fails for some reason 142 console.error('Failed to print from iframe', err); 143 printContentInNewWindow(content, options); 144 }).always(function () { 145 try { 146 options.deferred.resolve(); 147 } catch (err) { 148 console.warn('Error notifying deferred', err); 149 } 150 }); 151 } 152 153 function printContentInNewWindow(content, options) { 154 // Open a new window and print selected content 155 var frameWindow = window.open(); 156 return printFrame(frameWindow, content, options).always(function () { 157 try { 158 options.deferred.resolve(); 159 } catch (err) { 160 console.warn('Error notifying deferred', err); 161 } 162 }); 163 } 164 165 function isNode(o) { 166 /* http://stackoverflow.com/a/384380/937891 */ 167 return !!(typeof Node === 'object' ? o instanceof Node : o && typeof o === 168 'object' && typeof o.nodeType === 'number' && typeof o.nodeName === 169 'string'); 170 } 171 172 $.print = $.fn.print = function () { 173 // Print a given set of elements 174 var options, $this, self = this; 175 // console.log("Printing", this, arguments); 176 if (self instanceof $) { 177 // Get the node if it is a jQuery object 178 self = self.get(0); 179 } 180 if (isNode(self)) { 181 // If `this` is a HTML element, i.e. for 182 // $(selector).print() 183 $this = $(self); 184 if (arguments.length > 0) { 185 options = arguments[0]; 186 } 187 } else { 188 if (arguments.length > 0) { 189 // $.print(selector,options) 190 $this = $(arguments[0]); 191 if (isNode($this[0])) { 192 if (arguments.length > 1) { 193 options = arguments[1]; 194 } 195 } else { 196 // $.print(options) 197 options = arguments[0]; 198 $this = $('html'); 199 } 200 } else { 201 // $.print() 202 $this = $('html'); 203 } 204 } 205 // Default options 206 var defaults = { 207 globalStyles: true, 208 mediaPrint: false, 209 stylesheet: null, 210 noPrintSelector: '.no-print', 211 iframe: true, 212 append: null, 213 prepend: null, 214 manuallyCopyFormValues: true, 215 deferred: $.Deferred(), 216 timeout: 750, 217 title: null, 218 doctype: '<!doctype html>', 219 }; 220 // Merge with user-options 221 options = $.extend({}, defaults, (options || {})); 222 var $styles = $(''); 223 if (options.globalStyles) { 224 // Apply the stlyes from the current sheet to the printed page 225 $styles = $('style, link, meta, base, title'); 226 } else if (options.mediaPrint) { 227 // Apply the media-print stylesheet 228 $styles = $('link[media=print]'); 229 } 230 if (options.stylesheet) { 231 // Add a custom stylesheet(s) if given 232 if (!(($.isArray ? $.isArray : Array.isArray)(options.stylesheet))) { 233 options.stylesheet = [options.stylesheet]; 234 } 235 for (var i = 0; i < options.stylesheet.length; i++) { 236 $styles = $.merge($styles, 237 $('<link rel="stylesheet" href="' + options.stylesheet[i] + '">')); 238 } 239 } 240 // Create a copy of the element to print 241 var copy = jQueryCloneWithSelectAndTextAreaValues($this, true, true); 242 // Wrap it in a span to get the HTML markup string 243 copy = $('<span/>').append(copy); 244 // Remove unwanted elements 245 copy.find(options.noPrintSelector).remove(); 246 // Add in the styles 247 copy.append(jQueryCloneWithSelectAndTextAreaValues($styles)); 248 // Update title 249 if (options.title) { 250 var title = $('title', copy); 251 if (title.length === 0) { 252 title = $('<title />'); 253 copy.append(title); 254 } 255 title.text(options.title); 256 } 257 // Appedned content 258 copy.append(getjQueryObject(options.append)); 259 // Prepended content 260 copy.prepend(getjQueryObject(options.prepend)); 261 if (options.manuallyCopyFormValues) { 262 // Manually copy form values into the HTML for printing user-modified input fields 263 // http://stackoverflow.com/a/26707753 264 copy.find('input').each(function () { 265 var $field = $(this); 266 if ($field.is('[type=\'radio\']') || $field.is('[type=\'checkbox\']')) { 267 if ($field.prop('checked')) { 268 $field.attr('checked', 'checked'); 269 } 270 } else { 271 $field.attr('value', $field.val()); 272 } 273 }); 274 copy.find('select').each(function () { 275 var $field = $(this); 276 $field.find(':selected').attr('selected', 'selected'); 277 }); 278 copy.find('textarea').each(function () { 279 // Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589 280 var $field = $(this); 281 $field.text($field.val()); 282 }); 283 } 284 // Get the HTML markup string 285 var content = copy.html(); 286 // Notify with generated markup & cloned elements - useful for logging, etc 287 try { 288 options.deferred.notify('generated_markup', content, copy); 289 } catch (err) { 290 console.warn('Error notifying deferred', err); 291 } 292 // Destroy the copy 293 copy.remove(); 294 if (options.iframe) { 295 // Use an iframe for printing 296 try { 297 printContentInIFrame(content, options); 298 } catch (e) { 299 // Use the pop-up method if iframe fails for some reason 300 console.error('Failed to print from iframe', e.stack, e.message); 301 printContentInNewWindow(content, options); 302 } 303 } else { 304 // Use a new window for printing 305 printContentInNewWindow(content, options); 306 } 307 return this; 308 }; 309})(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.