From 1d992003ddc528b9cab6abc79886fb875fb8618e Mon Sep 17 00:00:00 2001 From: ergun acar Date: Wed, 5 Jul 2017 11:00:26 +0300 Subject: [PATCH] -id fix and init fix --- jquery.printPage.js | 145 +++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 68 deletions(-) diff --git a/jquery.printPage.js b/jquery.printPage.js index db06408..3aa30ee 100644 --- a/jquery.printPage.js +++ b/jquery.printPage.js @@ -6,54 +6,60 @@ * @desciption: jQuery page print plugin help you print your page in a better way */ -(function( $ ){ - $.fn.printPage = function(options) { - // EXTEND options for this button +(function ($) { var pluginOptions = { - attr : "href", - url : false, - showMessage: true, - message: "Please wait while we create your document" , - afterCallback: null, - beforeCallBack: null, - urlCallBack: false + attr: "href", + url: false, + showMessage: true, + message: "Please wait while we create your document", + afterCallback: null, + beforeCallBack: null, + urlCallBack: false }; - $.extend(pluginOptions, options); + + $.fn.printPage = function (options) { - this.on("click", - function(){ loadPrintDocument(this, pluginOptions); return false; }); + $.extend(pluginOptions, options); + + this.on("click", function () { + loadPrintDocument(this, pluginOptions); + return false; + }); + }; /** * Load & show message box, call iframe * @param {jQuery} el - The button calling the plugin * @param {Object} pluginOptions - options for this print button */ - function loadPrintDocument(el, pluginOptions){ - if($.isFunction(pluginOptions.beforeCallback)) - { - $.call(this,pluginOptions.beforeCallback); - } - if(pluginOptions.showMessage){ - $("body").append(components.messageBox(pluginOptions.message)); - $("#printMessageBox").css("opacity", 0); - $("#printMessageBox").animate({opacity:1}, 300, function() { addIframeToPage(el, pluginOptions); }); - } else { - addIframeToPage(el, pluginOptions); - } + function loadPrintDocument(el, pluginOptions) { + if ($.isFunction(pluginOptions.beforeCallback)) + { + $.call(this, pluginOptions.beforeCallback); + } + if (pluginOptions.showMessage) { + $("body").append(components.messageBox(pluginOptions.message)); + $("#printMessageBox").css("opacity", 0); + $("#printMessageBox").animate({opacity: 1}, 300, function () { + addIframeToPage(el, pluginOptions); + }); + } else { + addIframeToPage(el, pluginOptions); + } } - /** - * Fire function to getting print url if is defined in options - * - * @param {jQuery} el - The button calling the plugin - * @param {Object} pluginOptions - options for this print button - */ + /** + * Fire function to getting print url if is defined in options + * + * @param {jQuery} el - The button calling the plugin + * @param {Object} pluginOptions - options for this print button + */ function getURL(el, pluginOptions) { - if ($.isFunction(pluginOptions.urlCallBack)) { - return pluginOptions.urlCallBack(); - } else { - return (pluginOptions.url) ? pluginOptions.url : $(el).attr(pluginOptions.attr); - } + if ($.isFunction(pluginOptions.urlCallBack)) { + return pluginOptions.urlCallBack(); + } else { + return (pluginOptions.url) ? pluginOptions.url : $(el).attr(pluginOptions.attr); + } } /** @@ -62,57 +68,60 @@ * @param {jQuery} el - The button calling the plugin * @param {Object} pluginOptions - options for this print button */ - function addIframeToPage(el, pluginOptions){ + function addIframeToPage(el, pluginOptions) { var url = getURL(el, pluginOptions); pluginOptions.id = (pluginOptions.id) ? pluginOptions.id : $(el).attr('id'); - if (pluginOptions.id == 'undefined') - pluginOptions.id = ''; + if (pluginOptions.id === 'undefined') + pluginOptions.id = ''; - if(!$('#printPage')[0]){ - $("body").append(components.iframe(url)); - $('#printPage').on("load",function() { printit(pluginOptions); }); - }else{ - $('#printPage').attr("src", url); + var selector = '#printPage' + pluginOptions.id; + if (!$(selector)[0]) { + $("body").append(components.iframe(url)); + $(selector).on("load", function () { + printit(pluginOptions); + }); + } else { + $(selector).attr("src", url); } } /* * Call the print browser functionnality, focus is needed for IE */ - function printit(){ + function printit() { - var selector = 'printPage' + pluginOptions.id; + var selector = 'printPage' + pluginOptions.id; - frames.printPage.focus(); - frames.printPage.print(); - if(pluginOptions.showMessage){ - unloadMessage(); - } + frames[selector].focus(); + frames[selector].print(); + if (pluginOptions.showMessage) { + unloadMessage(); + } - if($.isFunction(pluginOptions.afterCallback)) - { - $.call(this,pluginOptions.afterCallback); - } + if ($.isFunction(pluginOptions.afterCallback)) + { + $.call(this, pluginOptions.afterCallback); + } } /* * Hide & Delete the message box with a small delay */ - function unloadMessage(){ - $("#printMessageBox").delay(1000).animate({opacity:0}, 700, function(){ - $(this).remove(); - }); + function unloadMessage() { + $("#printMessageBox").delay(1000).animate({opacity: 0}, 700, function () { + $(this).remove(); + }); } /* * Build html compononents for thois plugin */ var components = { - iframe: function(url){ - return ''; + iframe: function (url) { + return ''; - }, - messageBox: function(message){ - return "
\ - "+message+"
"; - } + " + message + ""; + } }; - }; -})( jQuery ); + +})(jQuery);