forked from samwiseduzer/angularPrint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angularPrint.js
116 lines (116 loc) · 4.12 KB
/
angularPrint.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
(function(){
var lowercase = function(string){return (typeof string === 'string') ? string.toLowerCase() : string;};
function toBoolean(value) {
if (typeof value === 'function') {
value = true;
} else if (value && value.length !== 0) {
var v = lowercase('' + value);
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
} else {
value = false;
}
return value;
}
var AngularPrint = angular.module('AngularPrint',[]);
AngularPrint.directive('printSection', function(){
return {
restrict: 'A',
link: function(scope, element){
element[0].classList.add('printSection');
}
};
});
AngularPrint.directive('printHide', function(){
return {
restrict: 'A',
link: function(scope, element){
element[0].classList.add('printHide');
}
};
});
AngularPrint.directive('printRemove', function(){
return {
restrict: 'A',
link: function(scope, element){
element[0].classList.add('printRemove');
}
};
});
AngularPrint.directive('printOnly', function(){
return {
restrict: 'A',
link: {
post: function(scope, element){
element[0].classList.add('printOnly');
}
}
};
});
AngularPrint.directive('printAvoidBreak', function(){
return {
restrict: 'A',
link: function(scope, element){
element[0].classList.add('avoidPageBreak');
}
};
});
AngularPrint.directive('printBtn',['$window', function($window){
return {
restrict: 'A',
link: function(scope, element){
element.on('click', function(){
$window.print();
});
}
};
}]);
AngularPrint.directive('printIf', ['$animate', function($animate) {
return function(scope, element, attr) {
scope.$watch(attr.printIf, function applyPrint(value){
if('printOnly' in attr){
$animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'printRemove');
}
else{
$animate[toBoolean(value) ? 'addClass' : 'removeClass'](element, 'printSection');
}
});
};
}]);
AngularPrint.directive('printLandscape',function(){
return {
restrict: 'A',
link: function(){
var sheet = (function() {
var style = document.createElement('style');
style.appendChild(document.createTextNode(''));
document.head.appendChild(style);
return style.sheet;
})();
sheet.insertRule('@page{size:landscape;}', 0);
}
};
});
AngularPrint.directive('printTable', function(){
return function(scope, element, attr) {
scope.$watch(attr.printTable, function makeTable(value){
setTimeout(function(){
if(value == null) return;
var elem = element[0];
elem.classList.add('printSection');
elem.id = 'print-table';
var tds = elem.getElementsByTagName('td');
for(var i = 0, content, div; i < tds.length; i++){
content = tds[i].innerHTML;
tds[i].innerHTML = '';
div = document.createElement('div');
div.className = 'avoidPageBreak';
div.innerHTML = content;
tds[i].appendChild(div);
}
element[0] = elem;
},1000);
});
};
});
})();