3
var modes = { iframe: "iframe", popup: "popup" };
4
var standards = { strict: "strict", loose: "loose", html5: "html5" };
7
standard: standards.html5,
16
retainAttr: ["id", "class", "style"],
21
$.fn.printArea = function (options) {
22
$.extend(settings, defaults, options);
25
var idPrefix = "printArea_";
26
$("[id^=" + idPrefix + "]").remove();
28
settings.id = idPrefix + counter;
30
var $printSource = $(this);
32
var PrintAreaWindow = PrintArea.getPrintWindow();
34
PrintArea.write(PrintAreaWindow.doc, $printSource);
36
setTimeout(function () {
37
PrintArea.print(PrintAreaWindow);
42
print: function (PAWindow) {
43
var paWindow = PAWindow.win;
45
$(PAWindow.doc).ready(function () {
49
if (settings.mode == modes.popup && settings.popClose)
50
setTimeout(function () {
55
write: function (PADocument, $ele) {
61
PrintArea.getBody($ele) +
66
docType: function () {
67
if (settings.mode == modes.iframe) return "";
69
if (settings.standard == standards.html5) return "<!DOCTYPE html>";
72
settings.standard == standards.loose ? " Transitional" : "";
73
var dtd = settings.standard == standards.loose ? "loose" : "strict";
76
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01' +
78
'//EN" "http://www.w3.org/TR/html4/' +
83
getHead: function () {
87
if (settings.extraHead)
88
settings.extraHead.replace(/([^,]+)/g, function (m) {
96
var relAttr = $(this).attr("rel");
98
($.type(relAttr) === "undefined") == false &&
99
relAttr.toLowerCase() == "stylesheet"
102
.filter(function () {
104
var mediaAttr = $(this).attr("media");
106
$.type(mediaAttr) === "undefined" ||
108
mediaAttr.toLowerCase() == "print" ||
109
mediaAttr.toLowerCase() == "all"
114
'<link type="text/css" rel="stylesheet" href="' +
115
$(this).attr("href") +
118
if (settings.extraCss)
119
settings.extraCss.replace(/([^,\s]+)/g, function (m) {
120
links += '<link type="text/css" rel="stylesheet" href="' + m + '">';
132
getBody: function (elements) {
134
var attrs = settings.retainAttr;
135
elements.each(function () {
136
var ele = PrintArea.getFormData($(this));
139
for (var x = 0; x < attrs.length; x++) {
140
var eleAttr = $(ele).attr(attrs[x]);
143
(attributes.length > 0 ? " " : "") +
150
htm += "<div " + attributes + ">" + $(ele).html() + "</div>";
153
return "<body>" + htm + "</body>";
155
getFormData: function (ele) {
156
var copy = ele.clone();
157
var copiedInputs = $("input,select,textarea", copy);
158
$("input,select,textarea", ele).each(function (i) {
159
var typeInput = $(this).attr("type");
160
if ($.type(typeInput) === "undefined")
161
typeInput = $(this).is("select")
163
: $(this).is("textarea")
166
var copiedInput = copiedInputs.eq(i);
168
if (typeInput == "radio" || typeInput == "checkbox")
169
copiedInput.attr("checked", $(this).is(":checked"));
170
else if (typeInput == "text") copiedInput.attr("value", $(this).val());
171
else if (typeInput == "select")
175
if ($(this).is(":selected"))
176
$("option", copiedInput).eq(i).attr("selected", true);
178
else if (typeInput == "textarea") copiedInput.text($(this).val());
182
getPrintWindow: function () {
183
switch (settings.mode) {
185
var f = new PrintArea.Iframe();
186
return { win: f.contentWindow || f, doc: f.doc };
188
var p = new PrintArea.Popup();
189
return { win: p, doc: p.doc };
192
Iframe: function () {
193
var frameId = settings.id;
195
"border:0;position:absolute;width:0px;height:0px;right:0px;top:0px;";
199
iframe = document.createElement("iframe");
200
document.body.appendChild(iframe);
204
src: "#" + new Date().getTime(),
207
iframe.doc = iframe.contentDocument
208
? iframe.contentDocument
209
: iframe.contentWindow
210
? iframe.contentWindow.document
213
throw e + ". iframes may not be supported in this browser.";
216
if (iframe.doc == null) throw "Cannot find document.";
222
"location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no";
223
windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt;
225
",resizable=yes,screenX=" +
229
",personalbar=no,scrollbars=yes";
231
var newWin = window.open("", "_blank", windowAttr);
233
newWin.doc = newWin.document;