Skip to content

Commit

Permalink
Added calbacks after and before print.
Browse files Browse the repository at this point in the history
Added calback to getting print url
  • Loading branch information
Kamil Pietrzak committed Apr 23, 2016
1 parent 457cd0e commit a85197d
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions jquery.printPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@
url : false,
showMessage: true,
message: "Please wait while we create your document" ,
callback: null
afterCallback: null,
beforeCallBack: null,
urlCallBack: false
};
$.extend(pluginOptions, options);

this.on("click",
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);
Expand All @@ -35,6 +41,21 @@
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
*/
function getURL(el, pluginOptions) {
if ($.isFunction(pluginOptions.urlCallBack)) {
return pluginOptions.urlCallBack();
} else {
return (pluginOptions.url) ? pluginOptions.url : $(el).attr(pluginOptions.attr);
}
}

/**
* Inject iframe into document and attempt to hide, it, can't use display:none
* You can't print if the element is not dsplayed
Expand All @@ -43,11 +64,11 @@
*/
function addIframeToPage(el, pluginOptions){

var url = (pluginOptions.url) ? pluginOptions.url : $(el).attr(pluginOptions.attr);
var url = getURL(el, pluginOptions);
pluginOptions.id = (pluginOptions.id) ? pluginOptions.id : $(el).attr('id');
if (pluginOptions.id == 'undefined')
pluginOptions.id = '';

if(!$('#printPage')[0]){
$("body").append(components.iframe(url));
$('#printPage').on("load",function() { printit(pluginOptions); });
Expand All @@ -59,20 +80,20 @@
* Call the print browser functionnality, focus is needed for IE
*/
function printit(){

var selector = 'printPage' + pluginOptions.id;

frames.printPage.focus();
frames.printPage.print();
if(pluginOptions.showMessage){
unloadMessage();
}
if($.isFunction(pluginOptions.callback))

if($.isFunction(pluginOptions.afterCallback))
{
$.call(this,pluginOptions.callback);
$.call(this,pluginOptions.afterCallback);
}

}
/*
* Hide & Delete the message box with a small delay
Expand All @@ -88,7 +109,7 @@
var components = {
iframe: function(url){
return '<iframe id="printPage'+pluginOptions.id+'" name="printPage'+pluginOptions.id+'" src='+url+' style="position: absolute; top: -1000px; @media print { display: block; }"></iframe>';

},
messageBox: function(message){
return "<div id='printMessageBox' style='\
Expand Down

0 comments on commit a85197d

Please sign in to comment.