angularPrint.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. (function(){
  3. var lowercase = function(string){return (typeof string === 'string') ? string.toLowerCase() : string;};
  4. function toBoolean(value) {
  5. if (typeof value === 'function') {
  6. value = true;
  7. } else if (value && value.length !== 0) {
  8. var v = lowercase('' + value);
  9. value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
  10. } else {
  11. value = false;
  12. }
  13. return value;
  14. }
  15. var AngularPrint = angular.module('AngularPrint',[]);
  16. AngularPrint.directive('printSection', function(){
  17. return {
  18. restrict: 'A',
  19. link: function(scope, element){
  20. element[0].classList.add('printSection');
  21. }
  22. };
  23. });
  24. AngularPrint.directive('printHide', function(){
  25. return {
  26. restrict: 'A',
  27. link: function(scope, element){
  28. element[0].classList.add('printHide');
  29. }
  30. };
  31. });
  32. AngularPrint.directive('printRemove', function(){
  33. return {
  34. restrict: 'A',
  35. link: function(scope, element){
  36. element[0].classList.add('printRemove');
  37. }
  38. };
  39. });
  40. AngularPrint.directive('printOnly', function(){
  41. return {
  42. restrict: 'A',
  43. link: {
  44. post: function(scope, element){
  45. element[0].classList.add('printOnly');
  46. }
  47. }
  48. };
  49. });
  50. AngularPrint.directive('printAvoidBreak', function(){
  51. return {
  52. restrict: 'A',
  53. link: function(scope, element){
  54. element[0].classList.add('avoidPageBreak');
  55. }
  56. };
  57. });
  58. AngularPrint.directive('printBtn',['$window', function($window){
  59. return {
  60. restrict: 'A',
  61. link: function(scope, element){
  62. element.on('click', function(){
  63. $window.print();
  64. });
  65. }
  66. };
  67. }]);
  68. AngularPrint.directive('printIf', ['$animate', function($animate) {
  69. return function(scope, element, attr) {
  70. scope.$watch(attr.printIf, function applyPrint(value){
  71. if('printOnly' in attr){
  72. $animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'printRemove');
  73. }
  74. else{
  75. $animate[toBoolean(value) ? 'addClass' : 'removeClass'](element, 'printSection');
  76. }
  77. });
  78. };
  79. }]);
  80. AngularPrint.directive('printLandscape',function(){
  81. return {
  82. restrict: 'A',
  83. link: function(){
  84. var sheet = (function() {
  85. var style = document.createElement('style');
  86. style.appendChild(document.createTextNode(''));
  87. document.head.appendChild(style);
  88. return style.sheet;
  89. })();
  90. sheet.insertRule('@page{size:landscape;}', 0);
  91. }
  92. };
  93. });
  94. AngularPrint.directive('printTable', function(){
  95. return function(scope, element, attr) {
  96. scope.$watch(attr.printTable, function makeTable(value){
  97. setTimeout(function(){
  98. if(value == null) return;
  99. var elem = element[0];
  100. elem.classList.add('printSection');
  101. elem.id = 'print-table';
  102. var tds = elem.getElementsByTagName('td');
  103. for(var i = 0, content, div; i < tds.length; i++){
  104. content = tds[i].innerHTML;
  105. tds[i].innerHTML = '';
  106. div = document.createElement('div');
  107. div.className = 'avoidPageBreak';
  108. div.innerHTML = content;
  109. tds[i].appendChild(div);
  110. }
  111. element[0] = elem;
  112. },1000);
  113. });
  114. };
  115. });
  116. })();