From 12a18d3a1890053f34f13c8d858d789223cb6e55 Mon Sep 17 00:00:00 2001 From: CitizenOfRome Date: Sat, 12 Jun 2021 05:31:46 +0530 Subject: [PATCH] Add support for multiple stylesheets #107 --- README.md | 2 +- bower.json | 23 ----------------------- dist/jQuery.print.min.js | 4 ++-- index.d.ts | 2 +- jQuery.print.js | 11 ++++++++--- package.json | 2 +- 6 files changed, 13 insertions(+), 31 deletions(-) delete mode 100644 bower.json diff --git a/README.md b/README.md index fb7909b..a434428 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Currently this plugin supports the following options: #### stylesheet - Default: `null` - - Acceptable-Values: URL-string + - Acceptable-Values: URL-string or Array of URL-strings - Function: URL of an external stylesheet to be included #### noPrintSelector diff --git a/bower.json b/bower.json deleted file mode 100644 index 6c44c6d..0000000 --- a/bower.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "jQuery.print", - "main": "dist/jQuery.print.min.js", - "version": "1.6.1", - "homepage": "https://doersguild.github.io/jQuery.print/", - "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", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "tests" - ] -} diff --git a/dist/jQuery.print.min.js b/dist/jQuery.print.min.js index 1fb25d5..628ddf4 100644 --- a/dist/jQuery.print.min.js +++ b/dist/jQuery.print.min.js @@ -1,5 +1,5 @@ /* @license - * jQuery.print, version 1.6.1 + * jQuery.print, version 1.6.2 * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) *--------------------------------------------------------------------------*/ -!function(e){"use strict";function t(t,n,r){var o,a,i,l,c,d=e(t),f=d.clone(n,r),s=d.find("textarea").add(d.filter("textarea")),p=f.find("textarea").add(f.filter("textarea")),u=d.find("select").add(d.filter("select")),h=f.find("select").add(f.filter("select")),m=d.find("canvas").add(d.filter("canvas")),y=f.find("canvas").add(f.filter("canvas"));for(o=0,a=s.length;o").html(n)}return r}function r(t,n,r){var o=e.Deferred();try{var a=(t=t.contentWindow||t.contentDocument||t).document||t.contentDocument||t;r.doctype&&a.write(r.doctype),a.write(n);try{for(var i=a.querySelectorAll("canvas"),l=0;l0&&(i=arguments[0])):arguments.length>0?a((l=e(arguments[0]))[0])?arguments.length>1&&(i=arguments[1]):(i=arguments[0],l=e("html")):l=e("html");var d={globalStyles:!0,mediaPrint:!1,stylesheet:null,noPrintSelector:".no-print",iframe:!0,append:null,prepend:null,manuallyCopyFormValues:!0,deferred:e.Deferred(),timeout:750,title:null,doctype:""};i=e.extend({},d,i||{});var f=e("");i.globalStyles?f=e("style, link, meta, base, title"):i.mediaPrint&&(f=e("link[media=print]")),i.stylesheet&&(f=e.merge(f,e('')));var s=t(l,!0,!0);if((s=e("").append(s)).find(i.noPrintSelector).remove(),s.append(t(f)),i.title){var p=e("title",s);0===p.length&&(p=e(""),s.append(p)),p.text(i.title)}s.append(n(i.append)),s.prepend(n(i.prepend)),i.manuallyCopyFormValues&&(s.find("input").each(function(){var t=e(this);t.is("[type='radio']")||t.is("[type='checkbox']")?t.prop("checked")&&t.attr("checked","checked"):t.attr("value",t.val())}),s.find("select").each(function(){e(this).find(":selected").attr("selected","selected")}),s.find("textarea").each(function(){var t=e(this);t.text(t.val())}));var u=s.html();try{i.deferred.notify("generated_markup",u,s)}catch(e){console.warn("Error notifying deferred",e)}if(s.remove(),i.iframe)try{!function(t,n){var a=e(n.iframe+""),i=a.length;0===i&&(a=e('<iframe height="0" width="0" border="0" wmode="Opaque"/>').prependTo("body").css({position:"absolute",top:-999,left:-999})),r(a.get(0),t,n).done(function(){setTimeout(function(){0===i&&a.remove()},1e3)}).fail(function(e){console.error("Failed to print from iframe",e),o(t,n)}).always(function(){try{n.deferred.resolve()}catch(e){console.warn("Error notifying deferred",e)}})}(u,i)}catch(e){console.error("Failed to print from iframe",e.stack,e.message),o(u,i)}else o(u,i);return this}}(jQuery); \ No newline at end of file +!function(y){"use strict";function f(e,t,n){for(var r,o,a,e=y(e),n=e.clone(t,n),i=e.find("textarea").add(e.filter("textarea")),l=n.find("textarea").add(n.filter("textarea")),c=e.find("select").add(e.filter("select")),d=n.find("select").add(n.filter("select")),s=e.find("canvas").add(e.filter("canvas")),f=n.find("canvas").add(n.filter("canvas")),p=0,u=i.length;p<u;++p)y(l[p]).val(y(i[p]).val());for(p=0,u=c.length;p<u;++p)for(r=0,o=c[p].options.length;r<o;++r)!0===c[p].options[r].selected&&(d[p].options[r].selected=!0);for(p=0,u=s.length;p<u;++p)(a=s[p].getContext("2d"))&&(f[p].getContext("2d").drawImage(s[p],0,0),y(f[p]).attr("data-jquery-print",a.canvas.toDataURL()));return n}function p(t){var n=y("");try{n=f(t)}catch(e){n=y("<span />").html(t)}return n}function u(t,e,n){var r=y.Deferred();try{var o=(t=t.contentWindow||t.contentDocument||t).document||t.contentDocument||t;n.doctype&&o.write(n.doctype),o.write(e);try{for(var a=o.querySelectorAll("canvas"),i=0;i<a.length;i++){var l=a[i].getContext("2d"),c=new Image;c.onload=function(){l.drawImage(c,0,0)},c.src=a[i].getAttribute("data-jquery-print")}}catch(e){console.warn(e)}o.close();var d=!1,s=function(){if(!d){t.focus();try{t.document.execCommand("print",!1,null)||t.print(),y("body").focus()}catch(e){t.print()}t.close(),d=!0,r.resolve()}};y(t).on("load",s),setTimeout(s,n.timeout)}catch(e){r.reject(e)}return r}function h(e,t){return u(window.open(),e,t).always(function(){try{t.deferred.resolve()}catch(e){console.warn("Error notifying deferred",e)}})}function m(e){return"object"==typeof Node?e instanceof Node:e&&"object"==typeof e&&"number"==typeof e.nodeType&&"string"==typeof e.nodeName}y.print=y.fn.print=function(){var e=this;e instanceof y&&(e=e.get(0)),m(e)?(o=y(e),0<arguments.length&&(t=arguments[0])):0<arguments.length?m((o=y(arguments[0]))[0])?1<arguments.length&&(t=arguments[1]):(t=arguments[0],o=y("html")):o=y("html");var e={globalStyles:!0,mediaPrint:!1,stylesheet:null,noPrintSelector:".no-print",iframe:!0,append:null,prepend:null,manuallyCopyFormValues:!0,deferred:y.Deferred(),timeout:750,title:null,doctype:"<!doctype html>"},t=y.extend({},e,t||{}),n=y("");if(t.globalStyles?n=y("style, link, meta, base, title"):t.mediaPrint&&(n=y("link[media=print]")),t.stylesheet){(y.isArray||Array.isArray)(t.stylesheet)||(t.stylesheet=[t.stylesheet]);for(var r=0;r<t.stylesheet.length;r++)n=y.merge(n,y('<link rel="stylesheet" href="'+t.stylesheet[r]+'">'))}var o,a=f(o,!0,!0);(a=y("<span/>").append(a)).find(t.noPrintSelector).remove(),a.append(f(n)),t.title&&(0===(o=y("title",a)).length&&(o=y("<title />"),a.append(o)),o.text(t.title)),a.append(p(t.append)),a.prepend(p(t.prepend)),t.manuallyCopyFormValues&&(a.find("input").each(function(){var e=y(this);e.is("[type='radio']")||e.is("[type='checkbox']")?e.prop("checked")&&e.attr("checked","checked"):e.attr("value",e.val())}),a.find("select").each(function(){y(this).find(":selected").attr("selected","selected")}),a.find("textarea").each(function(){var e=y(this);e.text(e.val())}));var i,l,c,d,s=a.html();try{t.deferred.notify("generated_markup",s,a)}catch(e){console.warn("Error notifying deferred",e)}if(a.remove(),t.iframe)try{i=s,c=y((l=t).iframe+""),0===(d=c.length)&&(c=y('<iframe height="0" width="0" border="0" wmode="Opaque"/>').prependTo("body").css({position:"absolute",top:-999,left:-999})),u(c.get(0),i,l).done(function(){setTimeout(function(){0===d&&c.remove()},1e3)}).fail(function(e){console.error("Failed to print from iframe",e),h(i,l)}).always(function(){try{l.deferred.resolve()}catch(e){console.warn("Error notifying deferred",e)}})}catch(e){console.error("Failed to print from iframe",e.stack,e.message),h(s,t)}else h(s,t);return this}}(jQuery); \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index fd74141..e0143c9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,7 +7,7 @@ interface JQuery { interface IPrintConfig { globalStyles?: boolean; mediaPrint?: boolean; - stylesheet?: string; + stylesheet?: string | Array; noPrintSelector?: string; iframe?: boolean; append?: string | JQuery<HTMLElement>; diff --git a/jQuery.print.js b/jQuery.print.js index 6493e33..18d7903 100644 --- a/jQuery.print.js +++ b/jQuery.print.js @@ -1,5 +1,5 @@ /* @license - * jQuery.print, version 1.6.1 + * jQuery.print, version 1.6.2 * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) *--------------------------------------------------------------------------*/ (function ($) { @@ -223,8 +223,13 @@ $styles = $("link[media=print]"); } if (options.stylesheet) { - // Add a custom stylesheet if given - $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">')); + // Add a custom stylesheet(s) if given + if (!(($.isArray ? $.isArray : Array.isArray)(options.stylesheet))) { + options.stylesheet = [options.stylesheet] + } + for(var i = 0; i < options.stylesheet.length; i++) { + $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet[i] + '">')); + } } // Create a copy of the element to print var copy = jQueryCloneWithSelectAndTextAreaValues($this, true, true); diff --git a/package.json b/package.json index cdebd56..3c465d2 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jQuery.print", "filename": "jQuery.print.min.js", "main": "jQuery.print.js", - "version": "1.6.1", + "version": "1.6.2", "homepage": "https://doersguild.github.io/jQuery.print/", "description": "Easy to use, Element Printing Plugin for jQuery, for printing specific parts of a page", "keywords": [