angularPrint.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. element[0].classList.add('avoidPageBreak');
  64. if($scope.printrequest){
  65. $window.print();
  66. }else{
  67. var sle = JSON.stringify($scope.selected);
  68. localStorage.setItem("key",sle)
  69. var printContents = document.querySelector('.qrcanvasfloat').innerHTML;
  70. var popupWin = window.open('', '_blank', 'width=630,height=891');
  71. popupWin.document.open();
  72. popupWin.document.write('<head><link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css"><link rel="stylesheet" href="assets/css/styles.css"></head><script src="../bower_components/angular/angular.min.js"></script><script src="../bower_components/jquery/dist/jquery.min.js"></script><script src="assets/js/controllers/qrgen.js"></script><script>var app = angular.module("myApp", []);app.controller("myCtrl", function($scope) {$scope.selected=JSON.parse(window.localStorage.getItem("key"));$(".qrcanvas").empty();angular.forEach($scope.selected.items,function(item){function $(selector) {return document.querySelector(selector);};$scope.q= $(".qrcanvas");var canvas;var colorIn = "#000000";var colorOut = "#000000";var colorFore = "#000000";var colorBack = "#ffffff";var options = {cellSize: 4,foreground: [{style: colorFore},{row: 0, rows: 7, col: 0, cols: 7, style: colorOut},{row: -7, rows: 7, col: 0, cols: 7, style: colorOut},{row: 0, rows: 7, col: -7, cols: 7, style: colorOut},{row: 2, rows: 3, col: 2, cols: 3, style: colorIn},{row: -5, rows: 3, col: 2, cols: 3, style: colorIn},{row: 2, rows: 3, col: -5, cols: 3, style: colorIn},],background: colorBack,data: item.uuid,typeNumber: 1,};var effect = "none";if (effect !== "none") {options.effect = {key: effect, value: 1};if (effect === "image") {options.background = [colorBack, effectImg];}};options.reuseCanvas = canvas;canvas = qrgen.canvas(options);$scope.q.appendChild(canvas);});$("canvas").addClass("canvasclass")})</script><body onload="window.print()"><div ng-app="myApp" ng-controller="myCtrl" class="qrcanvas"></div>'+printContents+'<div ng-app="myApp" ng-controller="myCtrl"></div></body>');
  73. popupWin.document.close();
  74. }
  75. });
  76. }
  77. };
  78. }]);
  79. AngularPrint.directive('printIf', ['$animate', function($animate) {
  80. return function(scope, element, attr) {
  81. scope.$watch(attr.printIf, function applyPrint(value){
  82. if('printOnly' in attr){
  83. $animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'printRemove');
  84. }
  85. else{
  86. $animate[toBoolean(value) ? 'addClass' : 'removeClass'](element, 'printSection');
  87. }
  88. });
  89. };
  90. }]);
  91. AngularPrint.directive('printLandscape',function(){
  92. return {
  93. restrict: 'A',
  94. link: function(){
  95. var sheet = (function() {
  96. var style = document.createElement('style');
  97. style.appendChild(document.createTextNode(''));
  98. document.head.appendChild(style);
  99. return style.sheet;
  100. })();
  101. sheet.insertRule('@page{size:landscape;}', 0);
  102. }
  103. };
  104. });
  105. AngularPrint.directive('printTable', function(){
  106. return function(scope, element, attr) {
  107. scope.$watch(attr.printTable, function makeTable(value){
  108. setTimeout(function(){
  109. if(value == null) return;
  110. var elem = element[0];
  111. elem.classList.add('printSection');
  112. elem.id = 'print-table';
  113. var tds = elem.getElementsByTagName('h1');
  114. for(var i = 0, content, div; i < tds.length; i++){
  115. content = tds[i].innerHTML;
  116. tds[i].innerHTML = '';
  117. div = document.createElement('div');
  118. div.className = 'avoidPageBreak';
  119. div.innerHTML = content;
  120. tds[i].appendChild(div);
  121. }
  122. element[0] = elem;
  123. },1000);
  124. });
  125. };
  126. });
  127. })();