98236026ede661e3d43fb9441dce7bfee48100f6
[freeside.git] / httemplate / elements / printtofit.js
1 $().ready(function() {
2   // yuck
3   var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
4   var beforePrint = function() {
5     if ($('body').width() > 0) {
6       // 7.5 inches * 96 DPI; maybe make the width a user pref?
7       var maxwidth = 7.5 * 96;
8       $('body').css('zoom', maxwidth / $('body').width());
9       if (isChrome) {
10         // Chrome doesn't respect page-break-* styles on table rows/cells,
11         // so wrap the contents of table cells with a block element
12         // ref. Chromium bug #99124, #87828, #59193
13         // should be fixed with Chrome 53
14         var nosplits = $('.nosplitrows td');
15         if (nosplits.length > 0) {
16           nosplits.wrapInner('<div class="nosplit autowrap" />');
17         }
18       }
19     }
20   };
21   var afterPrint = function() {
22     $('body').css('zoom', 1);
23     // get the direct children of the wrapper divs.
24     var nosplits = $('div.autowrap >');
25     if (nosplits.length > 0) {
26       nosplits.unwrap();
27     }
28   }
29
30   if (window.matchMedia) { // chrome, most importantly; also IE10?
31     window.matchMedia('print').addListener(
32       function(mq) {
33         mq.matches ?  beforePrint() : afterPrint();
34       }
35     );
36   } else { // other IE
37     $(window).on('beforeprint', beforePrint);
38     $(window).on('afterprint', afterPrint);
39   }
40   // got nothing for firefox
41   // https://bugzilla.mozilla.org/show_bug.cgi?id=774398
42   // but firefox already has "shrink to fit"
43 });