forked from gilf/ngPrint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngPrint.js
58 lines (48 loc) · 1.71 KB
/
ngPrint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(function (angular) {
'use strict';
var mod = angular.module('ngPrint', []);
function printDirective() {
var printSection = document.getElementById('printSection');
// if there is no printing section, create one
if (!printSection) {
printSection = document.createElement('div');
printSection.id = 'printSection';
document.body.appendChild(printSection);
}
function link(scope, element, attrs) {
element.on('click', function () {
var elemToPrint = document.getElementById(attrs.printElementId);
if (elemToPrint) {
printElement(elemToPrint);
}
});
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (!mql.matches) {
afterPrint();
} else {
// before print (currently does nothing)
}
});
}
window.onafterprint = afterPrint;
}
function afterPrint() {
// clean the print section before adding new content
printSection.innerHTML = '';
}
function printElement(elem) {
// clones the element you want to print
var domClone = elem.cloneNode(true);
printSection.innerHTML = '';
printSection.appendChild(domClone);
window.print();
}
return {
link: link,
restrict: 'A'
};
}
mod.directive('ngPrint', [printDirective]);
}(window.angular));