1870 |
20 Feb 13 |
nicklas |
1 |
|
1870 |
20 Feb 13 |
nicklas |
var Print = function() |
1870 |
20 Feb 13 |
nicklas |
3 |
{ |
1870 |
20 Feb 13 |
nicklas |
var print = {}; |
1870 |
20 Feb 13 |
nicklas |
var internal = {}; |
1870 |
20 Feb 13 |
nicklas |
6 |
|
2208 |
31 Jan 14 |
nicklas |
print.initPage = function() |
2208 |
31 Jan 14 |
nicklas |
8 |
{ |
2208 |
31 Jan 14 |
nicklas |
var pageId = Doc.getPageId(); |
2208 |
31 Jan 14 |
nicklas |
if (pageId == 'print-window') |
2208 |
31 Jan 14 |
nicklas |
11 |
{ |
2208 |
31 Jan 14 |
nicklas |
Events.addEventHandler('orientation-landscape', 'change', internal.orientationOnChange); |
2208 |
31 Jan 14 |
nicklas |
Events.addEventHandler('print-button', 'click', internal.doPrint); |
2208 |
31 Jan 14 |
nicklas |
window.opener.Print.finalizeTablePrint(window); |
2208 |
31 Jan 14 |
nicklas |
15 |
} |
2208 |
31 Jan 14 |
nicklas |
else |
2208 |
31 Jan 14 |
nicklas |
17 |
{ |
2208 |
31 Jan 14 |
nicklas |
Buttons.addClickHandler('net.sf.basedb.print.print-button', print.tablePrint); |
2208 |
31 Jan 14 |
nicklas |
19 |
} |
2208 |
31 Jan 14 |
nicklas |
20 |
} |
2208 |
31 Jan 14 |
nicklas |
21 |
|
1870 |
20 Feb 13 |
nicklas |
22 |
/* |
1870 |
20 Feb 13 |
nicklas |
Open a popup window giving a print preview of an item-listing table. |
1870 |
20 Feb 13 |
nicklas |
Several adjustments are made to the visual appearance of the table to |
1870 |
20 Feb 13 |
nicklas |
make it more suitable for printing. |
1870 |
20 Feb 13 |
nicklas |
26 |
*/ |
2208 |
31 Jan 14 |
nicklas |
print.tablePrint = function(event) |
1870 |
20 Feb 13 |
nicklas |
28 |
{ |
1870 |
20 Feb 13 |
nicklas |
var printData = {}; |
1870 |
20 Feb 13 |
nicklas |
printData.pageTitle = print.extractDocumentTitle(); |
6247 |
24 May 21 |
nicklas |
printData.extraStyleSheets = print.getExtraStyleSheets(window); |
1870 |
20 Feb 13 |
nicklas |
printData.pageOrientation = 'portrait'; |
1870 |
20 Feb 13 |
nicklas |
printData.printNote = 'If the table is too wide, try printing in <b>landscape mode</b> or change the <b>scale</b> value.'; |
1870 |
20 Feb 13 |
nicklas |
printData.printElement = document.forms[0].parentNode; |
2208 |
31 Jan 14 |
nicklas |
printData.printTemplate = Data.get(event.currentTarget, 'print-template'); |
1870 |
20 Feb 13 |
nicklas |
internal.openPrintWindow(printData); |
1870 |
20 Feb 13 |
nicklas |
37 |
} |
1870 |
20 Feb 13 |
nicklas |
38 |
|
1870 |
20 Feb 13 |
nicklas |
39 |
|
1870 |
20 Feb 13 |
nicklas |
40 |
/* |
1870 |
20 Feb 13 |
nicklas |
Finalize the table print preview by setting document title, etc. |
1870 |
20 Feb 13 |
nicklas |
Several adjustments are made to the visual appearance of the table to |
1870 |
20 Feb 13 |
nicklas |
make it more suitable for printing. |
1870 |
20 Feb 13 |
nicklas |
44 |
*/ |
1870 |
20 Feb 13 |
nicklas |
print.finalizeTablePrint = function(printWin) |
1870 |
20 Feb 13 |
nicklas |
46 |
{ |
1870 |
20 Feb 13 |
nicklas |
var printData = internal.printData; |
1870 |
20 Feb 13 |
nicklas |
48 |
|
1870 |
20 Feb 13 |
nicklas |
// Initalize common options |
1870 |
20 Feb 13 |
nicklas |
internal.setDefaultPrintOptions(printWin, printData); |
1870 |
20 Feb 13 |
nicklas |
51 |
|
1870 |
20 Feb 13 |
nicklas |
// Get the source element to be printed and the print-area copy |
1870 |
20 Feb 13 |
nicklas |
var printElement = printData.printElement; |
1870 |
20 Feb 13 |
nicklas |
var printArea = printWin.document.getElementById('print-area'); |
1870 |
20 Feb 13 |
nicklas |
55 |
|
6229 |
05 May 21 |
nicklas |
// Remove 'fulltable' and 'sticky-headers' since that is absolutely positioned |
2208 |
31 Jan 14 |
nicklas |
Doc.removeClass(printArea, 'fulltable'); |
6229 |
05 May 21 |
nicklas |
Doc.removeClass(printArea, 'sticky-headers'); |
1870 |
20 Feb 13 |
nicklas |
59 |
|
1870 |
20 Feb 13 |
nicklas |
// Check the filter row if any filter has been set, and copy values |
1870 |
20 Feb 13 |
nicklas |
// since not all are copied when using innerHTML |
1870 |
20 Feb 13 |
nicklas |
var srcInput = printElement.getElementsByTagName('input'); |
1870 |
20 Feb 13 |
nicklas |
var printInput = printArea.getElementsByTagName('input'); |
1870 |
20 Feb 13 |
nicklas |
var hideFilterRow = true; |
1870 |
20 Feb 13 |
nicklas |
for (var i = 0; i < srcInput.length; i++) |
1870 |
20 Feb 13 |
nicklas |
66 |
{ |
1870 |
20 Feb 13 |
nicklas |
var src = srcInput[i]; |
3211 |
26 Mar 15 |
nicklas |
var prt = printInput[i]; |
1870 |
20 Feb 13 |
nicklas |
if (src.name.indexOf('filter:') == 0 || src.name.indexOf('display:') == 0) |
1870 |
20 Feb 13 |
nicklas |
70 |
{ |
3211 |
26 Mar 15 |
nicklas |
// Get to the <tr> tag on this filter row and add 'filterrow' class |
3211 |
26 Mar 15 |
nicklas |
var tr = print.findParentElement(prt, 'tr'); |
3211 |
26 Mar 15 |
nicklas |
if (tr) |
3211 |
26 Mar 15 |
nicklas |
74 |
{ |
3211 |
26 Mar 15 |
nicklas |
Doc.addClass(tr, 'filterrow'); |
3211 |
26 Mar 15 |
nicklas |
if (src.name.indexOf(']') != -1) Doc.addClass(tr, 'secondary'); |
3211 |
26 Mar 15 |
nicklas |
77 |
} |
3211 |
26 Mar 15 |
nicklas |
78 |
|
1870 |
20 Feb 13 |
nicklas |
// For now, ignore all radio buttons |
1870 |
20 Feb 13 |
nicklas |
if (src.type != radio) |
1870 |
20 Feb 13 |
nicklas |
81 |
{ |
1870 |
20 Feb 13 |
nicklas |
var value = src.value; |
1870 |
20 Feb 13 |
nicklas |
if (value != '') |
1870 |
20 Feb 13 |
nicklas |
84 |
{ |
1870 |
20 Feb 13 |
nicklas |
hideFilterRow = false; |
3211 |
26 Mar 15 |
nicklas |
prt.value = value; |
1870 |
20 Feb 13 |
nicklas |
87 |
} |
1870 |
20 Feb 13 |
nicklas |
88 |
} |
1870 |
20 Feb 13 |
nicklas |
89 |
} |
1870 |
20 Feb 13 |
nicklas |
90 |
} |
1870 |
20 Feb 13 |
nicklas |
91 |
|
1870 |
20 Feb 13 |
nicklas |
// Labels and radio buttons are handled separately |
1870 |
20 Feb 13 |
nicklas |
var labels = printArea.getElementsByTagName('label'); |
1870 |
20 Feb 13 |
nicklas |
for (var i = 0; i < labels.length; i++) |
1870 |
20 Feb 13 |
nicklas |
95 |
{ |
1870 |
20 Feb 13 |
nicklas |
// Always hide the actual radio button |
1870 |
20 Feb 13 |
nicklas |
var radio = printWin.document.getElementById(labels[i].getAttribute('for')); |
1870 |
20 Feb 13 |
nicklas |
radio.style.display = 'none'; |
1870 |
20 Feb 13 |
nicklas |
if (!radio.checked) |
1870 |
20 Feb 13 |
nicklas |
100 |
{ |
1870 |
20 Feb 13 |
nicklas |
// and all labels for unchecked buttons |
1870 |
20 Feb 13 |
nicklas |
labels[i].style.display = 'none'; |
1870 |
20 Feb 13 |
nicklas |
103 |
} |
1870 |
20 Feb 13 |
nicklas |
else |
1870 |
20 Feb 13 |
nicklas |
105 |
{ |
1870 |
20 Feb 13 |
nicklas |
// Keep only the label for the checked radio button |
1870 |
20 Feb 13 |
nicklas |
hideFilterRow = false; |
1870 |
20 Feb 13 |
nicklas |
108 |
} |
1870 |
20 Feb 13 |
nicklas |
109 |
} |
1870 |
20 Feb 13 |
nicklas |
110 |
|
1870 |
20 Feb 13 |
nicklas |
// Hide the filter row if there are no filters |
1870 |
20 Feb 13 |
nicklas |
if (hideFilterRow) printArea.className += ' nofilterrow'; |
1870 |
20 Feb 13 |
nicklas |
113 |
|
1870 |
20 Feb 13 |
nicklas |
// Replace the 'check' table column with empty content |
1870 |
20 Feb 13 |
nicklas |
var allCheck = printArea.getElementsByClassName('check'); |
1870 |
20 Feb 13 |
nicklas |
for (var i = 0; i < allCheck.length; i++) |
1870 |
20 Feb 13 |
nicklas |
117 |
{ |
1870 |
20 Feb 13 |
nicklas |
allCheck[i].innerHTML = ''; |
1870 |
20 Feb 13 |
nicklas |
119 |
} |
1870 |
20 Feb 13 |
nicklas |
120 |
} |
1870 |
20 Feb 13 |
nicklas |
121 |
|
1870 |
20 Feb 13 |
nicklas |
122 |
/* |
3211 |
26 Mar 15 |
nicklas |
Find the first parent element with the given tag. |
3211 |
26 Mar 15 |
nicklas |
124 |
*/ |
3211 |
26 Mar 15 |
nicklas |
print.findParentElement = function(element, tagName) |
3211 |
26 Mar 15 |
nicklas |
126 |
{ |
3211 |
26 Mar 15 |
nicklas |
tagName = tagName.toUpperCase(); |
3211 |
26 Mar 15 |
nicklas |
while (element && element.tagName != tagName) |
3211 |
26 Mar 15 |
nicklas |
129 |
{ |
3211 |
26 Mar 15 |
nicklas |
element = element.parentNode; |
3211 |
26 Mar 15 |
nicklas |
131 |
} |
3211 |
26 Mar 15 |
nicklas |
return element || null; |
3211 |
26 Mar 15 |
nicklas |
133 |
} |
3211 |
26 Mar 15 |
nicklas |
134 |
|
3211 |
26 Mar 15 |
nicklas |
135 |
/* |
1870 |
20 Feb 13 |
nicklas |
Try to extract a document title. If the 'document.title' property is |
1870 |
20 Feb 13 |
nicklas |
set, use that. Otherwise get text from the first '<h1>' tag in the |
1870 |
20 Feb 13 |
nicklas |
document. |
1870 |
20 Feb 13 |
nicklas |
139 |
*/ |
1870 |
20 Feb 13 |
nicklas |
print.extractDocumentTitle = function() |
1870 |
20 Feb 13 |
nicklas |
141 |
{ |
1870 |
20 Feb 13 |
nicklas |
var documentTitle = null; |
1870 |
20 Feb 13 |
nicklas |
if (document.title) |
1870 |
20 Feb 13 |
nicklas |
144 |
{ |
1870 |
20 Feb 13 |
nicklas |
documentTitle = document.title; |
1870 |
20 Feb 13 |
nicklas |
146 |
} |
1870 |
20 Feb 13 |
nicklas |
else |
1870 |
20 Feb 13 |
nicklas |
148 |
{ |
1870 |
20 Feb 13 |
nicklas |
var h1 = document.getElementsByTagName('h1'); |
1870 |
20 Feb 13 |
nicklas |
if (h1.length > 0) |
1870 |
20 Feb 13 |
nicklas |
151 |
{ |
1870 |
20 Feb 13 |
nicklas |
// We found a h1 element, get text-only from child nodes |
1870 |
20 Feb 13 |
nicklas |
var title = []; |
1870 |
20 Feb 13 |
nicklas |
for (var i = 0; i < h1[0].childNodes.length; i++) |
1870 |
20 Feb 13 |
nicklas |
155 |
{ |
1870 |
20 Feb 13 |
nicklas |
title[title.length] = h1[0].childNodes[i].textContent; |
1870 |
20 Feb 13 |
nicklas |
157 |
} |
1870 |
20 Feb 13 |
nicklas |
documentTitle = title.join(', '); |
1870 |
20 Feb 13 |
nicklas |
159 |
} |
1870 |
20 Feb 13 |
nicklas |
160 |
} |
1870 |
20 Feb 13 |
nicklas |
return documentTitle; |
1870 |
20 Feb 13 |
nicklas |
162 |
} |
1870 |
20 Feb 13 |
nicklas |
163 |
|
6247 |
24 May 21 |
nicklas |
164 |
/** |
6247 |
24 May 21 |
nicklas |
Get all stylesheets that have been added by extensions to the given window. |
6247 |
24 May 21 |
nicklas |
166 |
*/ |
6247 |
24 May 21 |
nicklas |
print.getExtraStyleSheets = function(win) |
6247 |
24 May 21 |
nicklas |
168 |
{ |
6247 |
24 May 21 |
nicklas |
var extra = []; |
6247 |
24 May 21 |
nicklas |
var all = win.document.styleSheets; |
6247 |
24 May 21 |
nicklas |
for (var i = 0; i < all.length; i++) |
6247 |
24 May 21 |
nicklas |
172 |
{ |
6247 |
24 May 21 |
nicklas |
var ss = all[i]; |
6247 |
24 May 21 |
nicklas |
if (ss.href && ss.href.indexOf('/extensions/') > 0) |
6247 |
24 May 21 |
nicklas |
175 |
{ |
6247 |
24 May 21 |
nicklas |
extra[extra.length] = ss.href; |
6247 |
24 May 21 |
nicklas |
177 |
} |
6247 |
24 May 21 |
nicklas |
178 |
} |
6247 |
24 May 21 |
nicklas |
return extra; |
6247 |
24 May 21 |
nicklas |
180 |
} |
6247 |
24 May 21 |
nicklas |
181 |
|
1870 |
20 Feb 13 |
nicklas |
182 |
/* |
1870 |
20 Feb 13 |
nicklas |
Internal function for opening the print window. printData is |
1870 |
20 Feb 13 |
nicklas |
an object containing print options. |
1870 |
20 Feb 13 |
nicklas |
185 |
|
1870 |
20 Feb 13 |
nicklas |
186 |
*/ |
1870 |
20 Feb 13 |
nicklas |
internal.openPrintWindow = function(printData) |
1870 |
20 Feb 13 |
nicklas |
188 |
{ |
1870 |
20 Feb 13 |
nicklas |
// Default width/height is for 'portrait' orientation |
1870 |
20 Feb 13 |
nicklas |
var width = 900; |
1870 |
20 Feb 13 |
nicklas |
var height = 900; |
1870 |
20 Feb 13 |
nicklas |
if (printData.pageOrientation == 'landscape') |
1870 |
20 Feb 13 |
nicklas |
193 |
{ |
1870 |
20 Feb 13 |
nicklas |
width = 1300; |
1870 |
20 Feb 13 |
nicklas |
height = 700; |
1870 |
20 Feb 13 |
nicklas |
196 |
} |
1870 |
20 Feb 13 |
nicklas |
197 |
|
1870 |
20 Feb 13 |
nicklas |
internal.printData = printData; |
1870 |
20 Feb 13 |
nicklas |
199 |
|
1870 |
20 Feb 13 |
nicklas |
// Open the print-window |
2208 |
31 Jan 14 |
nicklas |
var printTemplate = printData.printTemplate; |
1870 |
20 Feb 13 |
nicklas |
printWin = window.open(printTemplate, 'PrintWindow', 'width='+width+',height='+height+',toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes'); |
1870 |
20 Feb 13 |
nicklas |
203 |
} |
1870 |
20 Feb 13 |
nicklas |
204 |
|
1870 |
20 Feb 13 |
nicklas |
internal.setDefaultPrintOptions = function(printWin, printData) |
1870 |
20 Feb 13 |
nicklas |
206 |
{ |
6247 |
24 May 21 |
nicklas |
// Extra style sheets |
6247 |
24 May 21 |
nicklas |
if (printData.extraStyleSheets) |
6247 |
24 May 21 |
nicklas |
209 |
{ |
6247 |
24 May 21 |
nicklas |
var current = print.getExtraStyleSheets(printWin); |
6247 |
24 May 21 |
nicklas |
for (var i = 0; i < printData.extraStyleSheets.length; i++) |
6247 |
24 May 21 |
nicklas |
212 |
{ |
6247 |
24 May 21 |
nicklas |
if (current.indexOf(printData.extraStyleSheets[i])==-1) |
6247 |
24 May 21 |
nicklas |
214 |
{ |
6247 |
24 May 21 |
nicklas |
// Only add the stylesheet if it is not already in the current |
6247 |
24 May 21 |
nicklas |
var link = document.createElement('link'); |
6247 |
24 May 21 |
nicklas |
link.rel = 'stylesheet'; |
6247 |
24 May 21 |
nicklas |
link.type = 'text/css'; |
6247 |
24 May 21 |
nicklas |
link.href = printData.extraStyleSheets[i]; |
6247 |
24 May 21 |
nicklas |
printWin.document.head.appendChild(link); |
6247 |
24 May 21 |
nicklas |
221 |
} |
6247 |
24 May 21 |
nicklas |
222 |
} |
6247 |
24 May 21 |
nicklas |
223 |
} |
6247 |
24 May 21 |
nicklas |
224 |
|
1870 |
20 Feb 13 |
nicklas |
// Set page title... |
1870 |
20 Feb 13 |
nicklas |
if (printData.pageTitle) |
1870 |
20 Feb 13 |
nicklas |
227 |
{ |
1870 |
20 Feb 13 |
nicklas |
printWin.document.title = printData.pageTitle; |
1870 |
20 Feb 13 |
nicklas |
printWin.document.getElementById('print-title').innerHTML = printData.pageTitle; |
1870 |
20 Feb 13 |
nicklas |
230 |
} |
1870 |
20 Feb 13 |
nicklas |
231 |
|
1870 |
20 Feb 13 |
nicklas |
// ...orientation... |
1870 |
20 Feb 13 |
nicklas |
if (printData.pageOrientation) |
1870 |
20 Feb 13 |
nicklas |
234 |
{ |
1870 |
20 Feb 13 |
nicklas |
var paper = printWin.document.getElementById('paper'); |
1870 |
20 Feb 13 |
nicklas |
paper.className += ' ' + printData.pageOrientation; |
1870 |
20 Feb 13 |
nicklas |
var landscape = printWin.document.getElementById('orientation-landscape').checked; |
1870 |
20 Feb 13 |
nicklas |
if (landscape && printData.pageOrientation == 'landscape') |
1870 |
20 Feb 13 |
nicklas |
239 |
{ |
1870 |
20 Feb 13 |
nicklas |
landscape.checked = true; |
1870 |
20 Feb 13 |
nicklas |
241 |
} |
1870 |
20 Feb 13 |
nicklas |
242 |
} |
1870 |
20 Feb 13 |
nicklas |
243 |
|
1870 |
20 Feb 13 |
nicklas |
// ... and print note |
1870 |
20 Feb 13 |
nicklas |
if (printData.printNote) |
1870 |
20 Feb 13 |
nicklas |
246 |
{ |
1870 |
20 Feb 13 |
nicklas |
var note = printWin.document.getElementById('print-note'); |
1870 |
20 Feb 13 |
nicklas |
note.innerHTML = printData.printNote; |
1870 |
20 Feb 13 |
nicklas |
249 |
} |
1870 |
20 Feb 13 |
nicklas |
250 |
|
1870 |
20 Feb 13 |
nicklas |
// Get the HTML from the source element |
1870 |
20 Feb 13 |
nicklas |
var printElement = printData.printElement; |
1870 |
20 Feb 13 |
nicklas |
var printHtml = printElement.innerHTML; |
1870 |
20 Feb 13 |
nicklas |
254 |
|
1870 |
20 Feb 13 |
nicklas |
// Copy the HTML to the print-area in the print-window |
1870 |
20 Feb 13 |
nicklas |
var printArea = printWin.document.getElementById('print-area'); |
1870 |
20 Feb 13 |
nicklas |
printArea.innerHTML = printHtml; |
1870 |
20 Feb 13 |
nicklas |
258 |
|
1870 |
20 Feb 13 |
nicklas |
// Copy the class name of the source to the print area |
1870 |
20 Feb 13 |
nicklas |
printArea.className += ' ' + printElement.className; |
1870 |
20 Feb 13 |
nicklas |
261 |
} |
1870 |
20 Feb 13 |
nicklas |
262 |
|
2208 |
31 Jan 14 |
nicklas |
internal.orientationOnChange = function() |
2208 |
31 Jan 14 |
nicklas |
264 |
{ |
2208 |
31 Jan 14 |
nicklas |
var landscape = Doc.element('orientation-landscape').checked; |
2208 |
31 Jan 14 |
nicklas |
var paper = Doc.element('paper'); |
2208 |
31 Jan 14 |
nicklas |
Doc.addOrRemoveClass(paper, 'landscape', landscape); |
2208 |
31 Jan 14 |
nicklas |
268 |
} |
2208 |
31 Jan 14 |
nicklas |
269 |
|
2208 |
31 Jan 14 |
nicklas |
internal.doPrint = function() |
2208 |
31 Jan 14 |
nicklas |
271 |
{ |
2208 |
31 Jan 14 |
nicklas |
window.print(); |
2208 |
31 Jan 14 |
nicklas |
273 |
} |
2208 |
31 Jan 14 |
nicklas |
274 |
|
1870 |
20 Feb 13 |
nicklas |
return print; |
1870 |
20 Feb 13 |
nicklas |
276 |
}(); |
1870 |
20 Feb 13 |
nicklas |
277 |
|
2208 |
31 Jan 14 |
nicklas |
Doc.onLoad(Print.initPage); |
1870 |
20 Feb 13 |
nicklas |
279 |
|