SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/print-directive.js

82 lines
3.9 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.directive('printElement', [
function () {
return {
scope: {
printElementId: '@',
entity: '='
},
link: function (scope, element) {
var elementToPrint, printSection, printHeader = '', restoreHeader, printSectionId = '.print-section', appTitle = $('title'), printElementParent;
printSection = $(printSectionId);
if (!printSection.length) {
printSection = $('<div class="print-section"></div>');
$('body').append(printSection);
}
if (scope.entity.type === EntityVO.TYPE_KNOWLEDGE) {
printHeader = scope.entity.articleId;
}
function printElement(elem) {
printSection.append(elem);
restoreHeader = appTitle.html();
appTitle.html(printHeader);
//start - hack for getting the browser print to work in Webkit/chrome
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(handleHeightlistener);
}
//end
//start - hack for getting the browser print to work in Firefox and IE
window.onbeforeprint = function () {
$('body').css({ height: 0 });
//to hide the print action blade if it is open, otherwise the modal will be visible in the print mode
var modal = $('body').find('.modal');
if (modal) {
modal.css({ height: 0 });
}
console.log('onbeforeprint fired');
};
//end
window.print();
printElementParent.append(elem);
//start - remove the hack on closing the print preview
$('body').css({ height: '' });
mediaQueryList && mediaQueryList.removeListener(handleHeightlistener);
//end
appTitle.html(restoreHeader);
}
//start - hack for getting the browser print to work in Webkit/chrome
function handleHeightlistener(mql) {
if (mql.matches) {
$('body').css({ height: 0 });
//to hide the print action blade if it is open, otherwise the modal will be visible in the print mode
var modal = $('body').find('.modal');
if (modal) {
modal.css({ height: 0 });
}
}
}
//end
function clickHandler() {
elementToPrint = $(scope.printElementId);
printElementParent = elementToPrint.parent();
elementToPrint.length && printElement(elementToPrint);
}
element.on('click', clickHandler);
scope.$on("$destroy", function () {
console.log("printElement: unbind events");
element.off('click', clickHandler);
element.off();
appTitle = null;
printSection = null;
restoreHeader = null;
});
}
};
}
]);
}());