-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-ui-print.js
More file actions
130 lines (127 loc) · 4.81 KB
/
Copy pathangular-ui-print.js
File metadata and controls
130 lines (127 loc) · 4.81 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'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('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', 'printService', function ($window, printService) {
return {
restrict: 'A',
link: function (scope, element, attr) {
element.on('click', function () {
var elements = document.getElementsByClassName('printSection');
while (elements.length > 0) {
elements[0].classList.remove('printSection');
}
var printingArea = attr.printBtn;
document.querySelector('[print-section=' + printingArea + ']').className += " printSection";
var isLandscape = element[0].getAttribute('landscape');
if (isLandscape !== null) printService.printLandscape();
else printService.printPortrait();
$window.print();
});
}
};
}]);
AngularPrint.service('printService', function () {
var _removePreviousStyle = function () {
var style = document.getElementById("print-style");
if (style) style.parentNode.removeChild(style);
}
var printLandscape = function () {
_removePreviousStyle();
var sheet = (function () {
var style = document.createElement('style');
style.appendChild(document.createTextNode(''));
style.id = "print-style"
document.head.appendChild(style);
return style.sheet;
})();
sheet.insertRule('@page{size:landscape;}', 0);
}
var printPortrait = function () {
_removePreviousStyle();
}
return {
printLandscape: printLandscape,
printPortrait: printPortrait
}
});
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('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);
});
};
});
})();