20 Commits
Author SHA1 Message Date
junv 6364d933ec Update package.json
jQuery.print -> jquery.print
2016-02-28 17:22:13 +08:00
junv 35733cc057 update main field 2016-02-03 21:50:44 +08:00
Sathvik Ponangi 3e5ed110d7 Add package.json; Resolves #27 2015-10-08 14:07:43 +05:30
Sathvik Ponangi 43daec10ef Bump version to 1.3.3 2015-08-01 20:05:12 +05:30
Sathvik Ponangi 5ab3f500df Include the custom timeout option 2015-08-01 20:04:39 +05:30
Sathvik Ponangi a690a4b513 Allow setting a custom timeout for accommodating different server load times, etc #22, #19 2015-08-01 20:01:38 +05:30
Sathvik Ponangi 38f12da807 Update README.md 2015-04-27 17:19:25 +05:30
Sathvik Ponangi 1b8610b182 Update README.md 2015-04-27 17:03:04 +05:30
Sathvik Ponangi fdf979b239 Update README.md 2015-04-27 17:02:43 +05:30
Sathvik Ponangi bf8dd435fb Bump version to 1.3.2 2015-04-27 17:01:41 +05:30
Sathvik Ponangi 1a5fae505e Fix for #18, Manually update textarea content 2015-04-27 17:01:02 +05:30
Sathvik Ponangi c6a57a9cbe Fixes #17 - the append-prepend text mixup 2015-04-08 18:55:42 +05:30
Sathvik Ponangi d7f9019356 Bump version 2015-01-20 12:50:19 +05:30
Sathvik Ponangi 62edb79737 Fix jshint warnings 2015-01-20 12:48:21 +05:30
Sathvik P 8e37c9762d Manually add form values (resolves #11), add a deferred object that resolves after print and restructure the code for better maintainability 2015-01-16 08:34:25 +05:30
Sathvik Ponangi a63fbe148c IE11 printing hack - tweaks 2014-09-23 00:15:54 +05:30
Sathvik Ponangi 0e2b677ff5 Merge pull request #10 from duveborg/master
Fixed issue when printing in IE11
2014-09-23 00:13:44 +05:30
martin.duveborg 0d4afbda06 Fixed issue when printing in IE11 2014-09-22 10:50:23 +02:00
Sathvik Ponangi 1e547fbc73 Bump version number to match that on bower 2014-09-09 18:48:53 +05:30
Sathvik Ponangi 43cea007ff Added @license as recommended by @michaellopez on #9 2014-09-09 16:37:17 +05:30
5 changed files with 279 additions and 156 deletions
+40 -9
View File
@@ -5,28 +5,40 @@ jQuery.print is a plugin for printing specific parts of a page
## Usage ## Usage
Include it in your HTML after importing jQuery, like: Include it in your HTML after importing jQuery, like:
```html
<script type="text/JavaScript" src="path/to/jquery.print.js" /> <script type="text/JavaScript" src="path/to/jquery.print.js" />
```
Use it like: Use it like:
```js
$("#myElementId").print(/*options*/); $("#myElementId").print(/*options*/);
```
or or
```js
$.print("#myElementId" /*, options*/); $.print("#myElementId" /*, options*/);
```
You can submit the options object like: You can submit the options object like:
```js
$("#myElementId").print({ $("#myElementId").print({
addGlobalStyles : true, globalStyles: true,
stylesheet : null, mediaPrint: false,
rejectWindow : true, stylesheet: null,
noPrintSelector : ".no-print", noPrintSelector: ".no-print",
iframe : true, iframe: true,
append : null, append: null,
prepend : null prepend: null,
manuallyCopyFormValues: true,
deferred: $.Deferred(),
timeout: 250
}); });
```
Currently this plugin supports the following options: Currently this plugin supports the following options:
####globalStyles ####globalStyles
@@ -65,6 +77,25 @@ Currently this plugin supports the following options:
- Acceptable-Values: Any valid `jQuery-selector` or HTML-text - Acceptable-Values: Any valid `jQuery-selector` or HTML-text
- Function: Adds custom HTML before (prepend) or after (append) the selected content - Function: Adds custom HTML before (prepend) or after (append) the selected content
####manuallyCopyFormValues
- Default: `true`
- Acceptable-Values: Boolean
- Function: Should it copy user-updated form input values onto the printed markup (this is done by manually iterating over each form element)
####deferred
- Default: `$.Deferred()`
- Acceptable-Values: Any valid `jQuery.Deferred` object
- Function: A jQuery.Deferred object that is resolved once the print function is called
####timeout
- Default: `250`
- Acceptable-Values: Time in Milliseconds for `setTimeout`
- Function: To change the amount of time to wait for the content, etc to load before printing the element from the new window/iframe created
## Tested with ## Tested with
### jQuery ### jQuery
+24 -24
View File
@@ -1,26 +1,26 @@
{ {
"name": "jQuery.print", "name": "jQuery.print",
"main": "jQuery.print.js", "main": "jQuery.print.js",
"version": "1.2.0", "version": "1.3.3",
"homepage": "https://doersguild.github.io/jQuery.print/", "homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [ "authors": [
"Sathvik P <sathvik@doersguild.com>" "Sathvik P <sathvik@doersguild.com>"
], ],
"description": "Easy to use, Element Printing Plugin for jQuery, for printing specific parts of a page", "description": "Easy to use, Element Printing Plugin for jQuery, for printing specific parts of a page",
"keywords": [ "keywords": [
"print", "print",
"element printing", "element printing",
"jquery print" "jquery print"
], ],
"dependencies": { "dependencies": {
"jquery":">=1.7.2" "jquery": ">=1.7.2"
}, },
"license": "CC-BY", "license": "CC-BY",
"ignore": [ "ignore": [
"**/.*", "**/.*",
"node_modules", "node_modules",
"bower_components", "bower_components",
"test", "test",
"tests" "tests"
] ]
} }
+48 -64
View File
@@ -11,118 +11,102 @@
<title>jQuery.Print</title> <title>jQuery.Print</title>
<meta name="description" content="jQuery.print is a plugin for printing specific parts of a page"> <meta name="description" content="jQuery.print is a plugin for printing specific parts of a page">
<meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/normalize.min.css"> <link rel="stylesheet" href="css/normalize.min.css">
<style type='text/css'> <style type='text/css'>
.a { .a {
background: black; background: black;
color: white; color: white;
} }
.b {
.b { color: #aaa;
color: #aaa; }
}
</style> </style>
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="js/vendor/html5-3.6-respond-1.1.0.min.js"></script> <script src="js/vendor/html5-3.6-respond-1.1.0.min.js"></script>
<![endif]--> <![endif]-->
</head> </head>
<body> <body>
<!--[if lt IE 9]> <!--[if lt IE 9]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p> <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]--> <![endif]-->
<h3><a href="https://github.com/DoersGuild/jQuery.print" id="view-on-github" class="btn"><span>View jQuery.print on GitHub</span></a></h3>
<div id="content_holder"> <div id="content_holder">
<div id="ele1" class="a"> <div id="ele1" class="a">
<h3>Element 1</h3> <h3>Element 1</h3>
<p> <p>
Some random text. Some random text.
<input type="text" value="Dummy input text" />
</p> </p>
<button class="print-link no-print" onclick="jQuery('#ele1').print()"> <button class="print-link no-print" onclick="jQuery('#ele1').print()">
Print this Print this (jQuery('#ele1').print())
</button> </button>
</div> </div>
<div id="ele2" class="b"> <div id="ele2" class="b">
<h3>Element 2</h3> <h3>Element 2</h3>
<p> <p>
Some other random text. Some other random text.
</p> </p>
<button class="print-link no-print"> <button class="print-link no-print">
Print this Print this ($.print("#ele2")) and skip the button
</button> </button>
</div> </div>
<div id="ele3" class="a"> <div id="ele3" class="a">
<h3>Element 3</h3> <h3>Element 3</h3>
<p class="no-print"> <p class="no-print">
Some more random text. Some more random text that is not to be printed.
</p> </p>
<button class="print-link" onclick="jQuery.print('#ele3')"> <button class="print-link" onclick="jQuery.print('#ele3')">
Print this Print this (jQuery.print('#ele3'))
</button> </button>
</div> </div>
<div id="ele4" class="b"> <div id="ele4" class="b">
<h3 class='avoid-this'>Element 4</h3> <h3 class='avoid-this'>Element 4</h3>
<p> <p>
Some really random text. Some really random text.
</p> </p>
<button class="print-link avoid-this"> <button class="print-link avoid-this">
Print this Print this in a new window, without this button and the title
</button> </button>
</div> </div>
<br/> <br/>
<button class="print-link" onclick="jQuery.print()"> <button class="print-link" onclick="jQuery.print()">
Print page Print page
</button> </button>
</div> </div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script> <script>
window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>') window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')
</script> </script>
<script src="../jQuery.print.js"></script> <script src="../jQuery.print.js"></script>
<script type='text/javascript'> <script type='text/javascript'>
//<![CDATA[ //<![CDATA[
$(function() { $(function() {
$("#ele2").find('.print-link').on('click', function() { $("#ele2").find('.print-link').on('click', function() {
//Print ele2 with default options //Print ele2 with default options
$.print("#ele2"); $.print("#ele2");
}); });
$("#ele4").find('button').on('click', function() {
$("#ele4").find('button').on('click', function() { //Print ele4 with custom options
//Print ele4 with custom options $("#ele4").print({
$("#ele4").print({ //Use Global styles
//Use Global styles globalStyles : false,
globalStyles : false, //Add link with attrbute media=print
mediaPrint : false,
//Add link with attrbute media=print //Custom stylesheet
mediaPrint : false, stylesheet : "http://fonts.googleapis.com/css?family=Inconsolata",
//Print in a hidden iframe
//Custom stylesheet iframe : false,
stylesheet : "http://fonts.googleapis.com/css?family=Inconsolata", //Don't print this
noPrintSelector : ".avoid-this",
//Print in a hidden iframe //Add this at top
iframe : false, prepend : "Hello World!!!<br/>",
//Add this on bottom
//Don't print this append : "<br/>Buh Bye!"
noPrintSelector : ".avoid-this", });
});
//Add this on top // Fork https://github.com/sathvikp/jQuery.print for the full list of options
append : "Hello World!!!<br/>", });
//]]>
//Add this at bottom
prepend : "<br/>Buh Bye!"
});
});
// Fork https://github.com/sathvikp/jQuery.print for the full list of options
});
//]]>
</script> </script>
</body> </body>
</html> </html>
+135 -59
View File
@@ -1,40 +1,69 @@
/* jQuery.print, version 1.0.3 /* @license
* jQuery.print, version 1.3.3
* (c) Sathvik Ponangi, Doers' Guild * (c) Sathvik Ponangi, Doers' Guild
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
(function ($) {
(function($) {"use strict"; "use strict";
// A nice closure for our definitions // A nice closure for our definitions
function getjQueryObject(string) { function getjQueryObject(string) {
// Make string a vaild jQuery thing // Make string a vaild jQuery thing
var jqObj = $(""); var jqObj = $("");
try { try {
jqObj = $(string).clone(); jqObj = $(string)
} catch(e) { .clone();
jqObj = $("<span />").html(string); } catch (e) {
jqObj = $("<span />")
.html(string);
} }
return jqObj; return jqObj;
} }
function isNode(o) { function printFrame(frameWindow, timeout) {
/* http://stackoverflow.com/a/384380/937891 */ // Print the selected window/iframe
return !!( typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string"); var def = $.Deferred();
try {
setTimeout(function () {
// Fix for IE : Allow it to render the iframe
frameWindow.focus();
try {
// Fix for IE11 - printng the whole page instead of the iframe content
if (!frameWindow.document.execCommand('print', false, null)) {
// document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891
frameWindow.print();
}
} catch (e) {
frameWindow.print();
}
frameWindow.close();
def.resolve();
}, timeout);
} catch (err) {
def.reject(err);
}
return def;
} }
function printContentInNewWindow(content, timeout) {
// Open a new window and print selected content
var w = window.open();
w.document.write(content);
w.document.close();
return printFrame(w, timeout);
}
$.print = $.fn.print = function() { function isNode(o) {
/* http://stackoverflow.com/a/384380/937891 */
return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
}
$.print = $.fn.print = function () {
// Print a given set of elements // Print a given set of elements
var options, $this, self = this; var options, $this, self = this;
// console.log("Printing", this, arguments); // console.log("Printing", this, arguments);
if (self instanceof $) {
if ( self instanceof $) {
// Get the node if it is a jQuery object // Get the node if it is a jQuery object
self = self.get(0); self = self.get(0);
} }
if (isNode(self)) { if (isNode(self)) {
// If `this` is a HTML element, i.e. for // If `this` is a HTML element, i.e. for
// $(selector).print() // $(selector).print()
@@ -60,20 +89,21 @@
$this = $("html"); $this = $("html");
} }
} }
// Default options // Default options
var defaults = { var defaults = {
globalStyles : true, globalStyles: true,
mediaPrint : false, mediaPrint: false,
stylesheet : null, stylesheet: null,
noPrintSelector : ".no-print", noPrintSelector: ".no-print",
iframe : true, iframe: true,
append : null, append: null,
prepend : null prepend: null,
manuallyCopyFormValues: true,
deferred: $.Deferred(),
timeout: 250
}; };
// Merge with user-options // Merge with user-options
options = $.extend({}, defaults, (options || {})); options = $.extend({}, defaults, (options || {}));
var $styles = $(""); var $styles = $("");
if (options.globalStyles) { if (options.globalStyles) {
// Apply the stlyes from the current sheet to the printed page // Apply the stlyes from the current sheet to the printed page
@@ -86,25 +116,54 @@
// Add a custom stylesheet if given // Add a custom stylesheet if given
$styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">')); $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
} }
// Create a copy of the element to print // Create a copy of the element to print
var copy = $this.clone(); var copy = $this.clone();
// Wrap it in a span to get the HTML markup string // Wrap it in a span to get the HTML markup string
copy = $("<span/>").append(copy); copy = $("<span/>")
.append(copy);
// Remove unwanted elements // Remove unwanted elements
copy.find(options.noPrintSelector).remove(); copy.find(options.noPrintSelector)
.remove();
// Add in the styles // Add in the styles
copy.append($styles.clone()); copy.append($styles.clone());
// Appedned content // Appedned content
copy.append(getjQueryObject(options.append)); copy.append(getjQueryObject(options.append));
// Prepended content // Prepended content
copy.prepend(getjQueryObject(options.prepend)); copy.prepend(getjQueryObject(options.prepend));
if (options.manuallyCopyFormValues) {
// Manually copy form values into the HTML for printing user-modified input fields
// http://stackoverflow.com/a/26707753
copy.find("input")
.each(function () {
var $field = $(this);
if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
if ($field.prop("checked")) {
$field.attr("checked", "checked");
}
} else {
$field.attr("value", $field.val());
}
});
copy.find("select").each(function () {
var $field = $(this);
$field.find(":selected").attr("selected", "selected");
});
copy.find("textarea").each(function () {
// Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589
var $field = $(this);
$field.text($field.val());
});
}
// Get the HTML markup string // Get the HTML markup string
var content = copy.html(); var content = copy.html();
// Notify with generated markup & cloned elements - useful for logging, etc
try {
options.deferred.notify('generated_markup', content, copy);
} catch (err) {
console.warn('Error notifying deferred', err);
}
// Destroy the copy // Destroy the copy
copy.remove(); copy.remove();
var w, wdoc;
if (options.iframe) { if (options.iframe) {
// Use an iframe for printing // Use an iframe for printing
try { try {
@@ -112,50 +171,67 @@
var iframeCount = $iframe.length; var iframeCount = $iframe.length;
if (iframeCount === 0) { if (iframeCount === 0) {
// Create a new iFrame if none is given // Create a new iFrame if none is given
$iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>').prependTo('body').css({ $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
"position" : "absolute", .prependTo('body')
"top" : -999, .css({
"left" : -999 "position": "absolute",
}); "top": -999,
"left": -999
});
} }
var w, wdoc;
w = $iframe.get(0); w = $iframe.get(0);
w = w.contentWindow || w.contentDocument || w; w = w.contentWindow || w.contentDocument || w;
wdoc = w.document || w.contentDocument || w; wdoc = w.document || w.contentDocument || w;
wdoc.open(); wdoc.open();
wdoc.write(content); wdoc.write(content);
wdoc.close(); wdoc.close();
setTimeout(function() { printFrame(w, options.timeout)
// Fix for IE : Allow it to render the iframe .done(function () {
w.focus(); // Success
w.print(); setTimeout(function () {
setTimeout(function() { // Wait for IE
// Fix for IE if (iframeCount === 0) {
if (iframeCount === 0) { // Destroy the iframe if created here
// Destroy the iframe if created here $iframe.remove();
$iframe.remove(); }
}, 100);
})
.fail(function (err) {
// Use the pop-up method if iframe fails for some reason
console.error("Failed to print from iframe", err);
printContentInNewWindow(content, options.timeout);
})
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
} }
}, 100); });
}, 250);
} catch (e) { } catch (e) {
// Use the pop-up method if iframe fails for some reason // Use the pop-up method if iframe fails for some reason
console.error("Failed to print from iframe", e.stack, e.message); console.error("Failed to print from iframe", e.stack, e.message);
w = window.open(); printContentInNewWindow(content, options.timeout)
w.document.write(content); .always(function () {
w.document.close(); try {
w.focus(); options.deferred.resolve();
w.print(); } catch (err) {
w.close(); console.warn('Error notifying deferred', err);
}
});
} }
} else { } else {
// Use a new window for printing // Use a new window for printing
w = window.open(); printContentInNewWindow(content, options.timeout)
w.document.write(content); .always(function () {
w.document.close(); try {
w.focus(); options.deferred.resolve();
w.print(); } catch (err) {
w.close(); console.warn('Error notifying deferred', err);
}
});
} }
return this; return this;
}; };
})(jQuery); })(jQuery);
+32
View File
@@ -0,0 +1,32 @@
{
"name": "jquery.print",
"filename": "jQuery.print.min.js",
"version": "1.3.3",
"main": "jQuery.print.js",
"homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [
"Sathvik P <sathvik@doersguild.com>"
],
"description": "Easy to use, Element Printing Plugin for jQuery, for printing specific parts of a page",
"keywords": [
"print",
"element printing",
"jquery print"
],
"dependencies": {
"jquery": ">=1.7.2"
},
"license": "CC-BY",
"repository": {
"type": "git",
"url": "git://github.com/DoersGuild/jQuery.print.git"
},
"autoupdate": {
"source": "git",
"target": "git://github.com/DoersGuild/jQuery.print.git",
"basePath": "",
"files": [
"jQuery.print*"
]
}
}