|
| 1 | +/* ========================================================== |
| 2 | + * print.js |
| 3 | + * Add print preview windows |
| 4 | + * |
| 5 | + * Author: Yann, yann@antistatique.net |
| 6 | + * Date: 2015-02-02 |
| 7 | + * |
| 8 | + * Copyright 2014 Federal Chancellery of Switzerland |
| 9 | + * Licensed under MIT |
| 10 | + ========================================================== */ |
| 11 | + |
| 12 | + (function($) { |
| 13 | + |
| 14 | + // Initialization |
| 15 | + $.fn.printPreview = function() { |
| 16 | + return this; |
| 17 | + }; |
| 18 | + |
| 19 | + $.printPreview = { |
| 20 | + |
| 21 | + printPreview: function() { |
| 22 | + var $body = $('body'), |
| 23 | + $container = $('.container-main'), |
| 24 | + footnoteLinks = "", |
| 25 | + linksIndex = 0; |
| 26 | + |
| 27 | + $body.find('.nav-mobile, .drilldown, .nav-main, .header-separator, .nav-service, .nav-lang, .form-search, .yamm--select, header > div:first-child, footer, .alert, .icon--print, .social-sharing, form, .nav-process, .carousel-indicators, .carousel-control, .breadcrumb, .pagination-container').remove(); |
| 28 | + $body.addClass('print-preview'); |
| 29 | + |
| 30 | + $container.prepend('<div class="row" id="print-settings">'+ |
| 31 | + '<div class="col-sm-12">'+ |
| 32 | + '<nav class="pagination-container clearfix">'+ |
| 33 | + '<span class="pull-left">'+ |
| 34 | + '<input type="checkbox" id="footnote-links"> '+ |
| 35 | + '<label for="footnote-links">Links as footnotes</label>'+ |
| 36 | + '</span>'+ |
| 37 | + '<ul class="pull-right pagination">'+ |
| 38 | + '<li>'+ |
| 39 | + '<button id="print-button" title="print" class="btn"><span class="icon icon--print"></span></button>'+ |
| 40 | + ' '+ |
| 41 | + '<button id="close-button" title="close" class="btn btn-secondary"><span class="icon icon--close"></span></button>'+ |
| 42 | + '</li>'+ |
| 43 | + '</ul>'+ |
| 44 | + '</nav>'+ |
| 45 | + '</div>'+ |
| 46 | + '</div>'); |
| 47 | + |
| 48 | + $('#print-button').click(function () { |
| 49 | + $.printPreview.printProcess(); |
| 50 | + }); |
| 51 | + |
| 52 | + $('#close-button').click(function () { |
| 53 | + $.printPreview.printClose(); |
| 54 | + }); |
| 55 | + |
| 56 | + |
| 57 | + $('a').not('.access-keys a').each(function () { |
| 58 | + var target = $(this).attr('href'); |
| 59 | + target = String(target); |
| 60 | + |
| 61 | + if (target != "undefined" && target.indexOf("http") >= 0) { |
| 62 | + linksIndex ++; |
| 63 | + footnoteLinks += '<li>'+target+'</li>'; |
| 64 | + $('<sup class="link-ref">('+linksIndex+')</sup>').insertAfter(this); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + |
| 69 | + $('#footnote-links').change(function(){ |
| 70 | + if (this.checked) { |
| 71 | + $container.append('<div id="footnote-links-wrapper" class="row footnote-links-wrapper">'+ |
| 72 | + '<div class="col-sm-12">'+ |
| 73 | + '<h3>Page Links</h3><hr>'+ |
| 74 | + '<ol>'+ |
| 75 | + footnoteLinks+ |
| 76 | + '</ol>'+ |
| 77 | + '</div>'+ |
| 78 | + '</div>'); |
| 79 | + $body.addClass('print-footnotes'); |
| 80 | + } else { |
| 81 | + $('#footnote-links-wrapper').remove(); |
| 82 | + $body.removeClass('print-footnotes'); |
| 83 | + } |
| 84 | + }); |
| 85 | + }, |
| 86 | + |
| 87 | + printProcess: function() { |
| 88 | + window.print(); |
| 89 | + }, |
| 90 | + |
| 91 | + printClose: function() { |
| 92 | + window.location.reload(); |
| 93 | + } |
| 94 | + |
| 95 | + }; |
| 96 | + |
| 97 | + $('a.truc').printPreview(); |
| 98 | + $(document).bind('keydown', function(e) { |
| 99 | + var code = (e.keyCode ? e.keyCode : e.which); |
| 100 | + if (code == 80 && !$('body').hasClass('print-preview')) { |
| 101 | + $.printPreview.printPreview(); |
| 102 | + return false; |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // To test print preview mode |
| 107 | + // $.printPreview.printPreview(); |
| 108 | + |
| 109 | + }) (jQuery); |
0 commit comments