'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Content Explorer *| (C) Copyright IBM Corp. 2019 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['doT', 'jquery', '../../../../../app/nls/StringResources', '../../../../../lib/@waca/core-client/js/core-client/utils/BrowserUtils', '../../../../../lib/@waca/core-client/js/core-client/utils/ClassFactory', 'text!../../../../../dashboard/views/templates/FilterPDFContainer.html', '../../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', '../../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../PrintAPI'], function (doT, $, StringResources, BrowserUtils, ClassFactory, FilterTemplate, CommonUiUtils, APIFactory, PrintAPI) { var Print = function () { //These sizes specify the viewport for the various browser print sizes. function Print(options) { _classCallCheck(this, Print); this._dashboard = options.features.API; this.pdfSizes = { Chrome: { Letter: { height: 520, width: 710 }, Legal: { height: 520, width: 930 }, A4: { height: 500, width: 770 }, Tabloid: { height: 700, width: 1150 } }, Firefox: { Letter: { height: 510, width: 670 }, Legal: { height: 510, width: 880 }, A4: { height: 470, width: 720 }, Tabloid: { height: 660, width: 1100 } }, IE: { Letter: { height: 480, width: 660 }, Legal: { height: 480, width: 810 }, A4: { height: 460, width: 680 }, Tabloid: { height: 620, width: 980 } }, Other: { Letter: { height: 450, width: 620 }, Legal: { height: 450, width: 820 }, A4: { height: 440, width: 670 }, Tabloid: { height: 620, width: 1030 } } }; this.orientation = 'portrait'; this.pageSize = 'letter'; this.GeneratePDFView = {}; this.toast = {}; this.layoutController = {}; this.tabsHidden = false; this.contents = {}; } Print.prototype.getAPI = function getAPI() { return APIFactory.createAPI(this, [PrintAPI]); }; Print.prototype.registerContent = function registerContent(type, api) { this.contents[type] = api; }; Print.prototype.isItemVisible = function isItemVisible() { return true; }; Print.prototype.print = function print(id, context, pageOptions) { /*global requirejs: true*/ var pdfSize = this._getPageSize(pageOptions.pageSize); var oldTimeout = requirejs.s.contexts._.config.waitSeconds; this.context = context; var contentType = this._dashboard.getCanvas().getContent(id).getType(); this.subAPI = this.contents[contentType]; requirejs.config({ waitSeconds: 900 // 15 mins to be safe }); var layoutController = this._dashboard.getCurrentContentView().boardController.layoutController; this.tabsHidden = layoutController.boardModel.layout.get('hideTab'); var pageElement = context.glassContext.appController.currentAppView.getCurrentContentView().$el; return this._renderDashboard(layoutController, pageElement, pdfSize, pageOptions.printFilters).finally(function () { requirejs.config({ waitSeconds: oldTimeout }); }); }; Print.prototype._getPageSize = function _getPageSize(pageSize) { //Each Browser has a slightly different viewport when printing hence the different page sizes var pdfSize = {}; if (BrowserUtils.isFirefox()) { pdfSize = { height: this.pdfSizes.Firefox[pageSize.pageSize].height, width: this.pdfSizes.Firefox[pageSize.pageSize].width }; } else if (BrowserUtils.isChrome()) { pdfSize = { height: this.pdfSizes.Chrome[pageSize.pageSize].height, width: this.pdfSizes.Chrome[pageSize.pageSize].width }; } else if (BrowserUtils.isIE()) { pdfSize = { height: this.pdfSizes.IE[pageSize.pageSize].height, width: this.pdfSizes.IE[pageSize.pageSize].width }; } else { pdfSize = { height: this.pdfSizes.Other[pageSize.pageSize].height, width: this.pdfSizes.Other[pageSize.pageSize].width }; } this.orientation = pageSize.orientation; this.pageSize = pageSize.pageSize; this.orientation = this.orientation.toLowerCase(); this.pageSize = this.pageSize.toLowerCase(); if (this.orientation === 'portrait') { var height = pdfSize.height; var width = pdfSize.width; pdfSize.width = height; pdfSize.height = width; } // This is needed for safari since the footer is printed on the outside of the margin. if (BrowserUtils.isSafari()) { pdfSize.width = pdfSize.width + 50; } return pdfSize; }; Print.prototype._renderDashboard = function _renderDashboard(layoutController, pageElement, pdfSize, printFilters) { var layout = layoutController.boardModel.layout; var models = layout.items; var modelIds = models.map(function (model) { return model.id; }); this._showToast(); var content = { modelIds: modelIds, layoutType: layout.type, pageElement: pageElement[0], context: this.context, printPageSize: pdfSize }; return this.subAPI.showContentsForPrint(content).then(function () { return this.tabRenderer(models, layoutController).then(function () { var widgetIds = []; $.each($(pageElement).find('.widget'), function (i, widget) { widgetIds.push($(widget).attr('id')); }); return this.whenWidgetsRendered(widgetIds, layoutController); }.bind(this)); }.bind(this)).then(function () { var mapWidgetIds = []; $.each($(pageElement).find('.widget'), function (i, widget) { if ($(widget).has('.TiledMapBundleRenderer').length > 0) { mapWidgetIds.push($(widget).attr('id')); } }.bind(this)); return this.renderMapsForPrint(layoutController, mapWidgetIds); }.bind(this)).delay(this._getDelay(pageElement)) //Timeout is needed for the VIDA map animation. .then(function () { var tabTitles = []; models.forEach(function (model) { tabTitles.push(model.title); }); var dashboardSizes = []; if (pageElement.find('.pageTabContent').length) { $.each(pageElement.find('.pageTabContent'), function (i, tab) { dashboardSizes.push(this._getDashboardSizes(tab)); }.bind(this)); } else if (pageElement.find('.pagecontainer').length) { $.each(pageElement.find('.pagecontainer'), function (i, tab) { dashboardSizes.push(this._getDashboardSizes(tab)); }.bind(this)); } else if (pageElement.find('.pageabsolute').length) { $.each(pageElement.find('.pageabsolute'), function (i, tab) { dashboardSizes.push(this._getDashboardSizes(tab)); }.bind(this)); } if (printFilters) { var filters = this.getFilters(layoutController, models); this.scaleDashboard(dashboardSizes, tabTitles, pdfSize, filters); } else { this.scaleDashboard(dashboardSizes, tabTitles, pdfSize); } return this.toast.remove(0); }.bind(this)).delay(1000) // delaying so Toast can disappear and not show up in pdf .then(function () { if (!this.isCancelled) { window.print(); } }.bind(this)).catch(function () { if (!this.isCancelled) { this.isCancelled = true; layoutController.glassContext.appController.showToast(StringResources.get('errorPDF'), { type: 'error', preventDuplicates: true }); } }.bind(this)).finally(function () { return this.subAPI.hideContentsAfterPrint(modelIds, pageElement[0]).then(function () { // Remove all the added styles this._removeAllPdfStyles(); this.isCancelled = false; this.toast.remove(0); }.bind(this)); }.bind(this)); }; Print.prototype._getDashboardSizes = function _getDashboardSizes(tab) { var pageSize = this.findPageSize(tab); var offSceenPageSize = this.subAPI.getOffScreenPageSize(tab); var dashboardSize = offSceenPageSize && this._dashboard.getCanvas().findContent().length > 0 ? offSceenPageSize : pageSize; var childrenId = !tab.id.indexOf($(tab).children().first().attr('id')) ? $(tab).children().first().attr('id') : $(tab).children().first().next().attr('id'); return { 'size': dashboardSize, 'id': $(tab).attr('id'), childId: childrenId, absolute: this._checkIfPageAbsolute(tab) }; }; Print.prototype._getDelay = function _getDelay(pageElement) { var delay = 2000; if (BrowserUtils.isIE()) { delay = 7000; } if ($(pageElement).find('.TiledMapBundleRenderer').length > 0) { delay = 10000; } return delay; }; Print.prototype._showToast = function _showToast() { var _this = this; ClassFactory.loadModule('dashboard-core/js/lib/@waca/core-client/js/core-client/ui/ProgressToast').then(function (ProgressToast) { _this.toast = new ProgressToast({ noHideBtn: true }); _this.toast.show(StringResources.get('buildingPDF')); _this.toast.indefinite(StringResources.get('buildingPDF')); _this.toast.onCancel(function () { _this.isCancelled = true; }); }); }; Print.prototype.getFilters = function getFilters(layoutController, models) { var widgets = layoutController.boardModel.widgetInstances; var pageFilters = []; $.each(models, function (i, model) { var tabWidgets = []; $.each(widgets, function (i, widget) { if (widget.type === 'live') { var idArray = []; idArray.push(model.id); var widgetsOnTab = model.listWidgets(idArray); if (this._checkIfWidgetInModel(widgetsOnTab, widget)) { var widgetLayout = layoutController.getLayoutView(widget.id); var widgetFilters = widgetLayout.widgetAPI.getWidgetLocalFilters(); widgetFilters = widgetFilters.concat(widgetLayout.widgetAPI.getWidgetGlobalFilters()); widgetFilters = widgetFilters.concat(widgetLayout.widgetAPI.getWidgetTopBottom()); if (widgetFilters.length > 0) { tabWidgets.push({ 'id': widget.id, 'filters': widgetFilters }); } } } }.bind(this)); pageFilters.push({ 'id': model.id, widgets: tabWidgets }); }.bind(this)); return pageFilters; }; Print.prototype._checkIfWidgetInModel = function _checkIfWidgetInModel(items, widget) { var match = false; $.each(items, function (i, item) { if (item === widget.id) { match = true; } }.bind(this)); return match; }; Print.prototype.createFilterContainer = function createFilterContainer(filters, pdfSize) { var html = doT.template(FilterTemplate)({ widgets: filters.widgets, widgetText: StringResources.get('pdfWidget'), filterTitle: StringResources.get('filtersPresentPDF'), height: pdfSize.height }); return html; }; Print.prototype.numberWidgets = function numberWidgets(dashboard, widgets) { $.each(widgets, function (i, widget) { var widgetElement = $('#' + dashboard.id).find('#' + widget.id); var widgetNumber = i + 1; widgetElement.append('
'); }); }; Print.prototype.renderMapsForPrint = function renderMapsForPrint(layoutController, widgetIds) { return Promise.map(widgetIds, function (widgetId) { if (this.isCancelled) { return Promise.reject(); } var mapWidget = layoutController.widgetLoader.getWidget(widgetId); return mapWidget.mapVizRenderForPrint(); }.bind(this)); }; Print.prototype.tabRenderer = function tabRenderer(models, layoutController) { return Promise.map(models, function (model) { if (this.isCancelled) { return Promise.reject(); } return layoutController.getLayoutViewWhenReady(model.id).then(function (layoutView) { layoutView.onShow(); }); }.bind(this)); }; Print.prototype.whenWidgetsRendered = function whenWidgetsRendered(widgetIds, layoutController) { var widgetPromises = Promise.map(widgetIds, function (id) { if (this.isCancelled) { return Promise.reject(); } var widgetLoader = this._dashboard.getDashboardCoreSvc('widgetLoader'); var feature = widgetLoader.getWidget(id); feature && feature.resize(); if (feature) { return feature.whenRenderComplete(); } return layoutController.whenWidgetRenderComplete(id); }.bind(this)); return Promise.all(widgetPromises); }; Print.prototype._removeAllPdfStyles = function _removeAllPdfStyles() { $('.dashboardPdf').remove(); $('.widgetPdf').remove(); $('.widgetPDFNumber').remove(); $('.pdfFilterList').remove(); }; Print.prototype._fixWidgetsStyle = function _fixWidgetsStyle(pageElement, className) { var widgetStyleOverride = ''; $.each($(pageElement).find(className), function (i, widget) { var widgetId = $(widget).attr('id'); var LabelContentLocation = widgetId + ' .vis-container [data-area=row] [data-node=content]'; var _$$css = $(widget).css(['width', 'height', 'top', 'left']), width = _$$css.width, height = _$$css.height, top = _$$css.top, left = _$$css.left; var visContainerNodeContent = $(widget).find('.vis-container [data-area=row] [data-node=content]'); var labelMargin = visContainerNodeContent && visContainerNodeContent.css(['margin-right']); var labelMarginInt = labelMargin && Math.abs(parseInt(labelMargin['margin-right'])); if (!$(widget).parent().hasClass('pagegroup')) { widgetStyleOverride = widgetStyleOverride + ' #' + widgetId + '{ width:' + width + ' !important; height:' + height + ' !important; top: ' + top + ' !important; left: ' + left + ' !important; position: fixed !important;}'; } else { widgetStyleOverride = widgetStyleOverride + ' #' + widgetId + '{ position: absolute !important; top: ' + top + ' !important; left: ' + left + ' !important;}'; } widgetStyleOverride = visContainerNodeContent && widgetStyleOverride + ' #' + LabelContentLocation + '{ margin-right: ' + labelMarginInt + 'px !important; }' || widgetStyleOverride; }); return widgetStyleOverride; }; Print.prototype._fixWidgets = function _fixWidgets(pageElement) { var widgetStyleOverride = this._fixWidgetsStyle(pageElement, '.widget') + this._fixWidgetsStyle(pageElement, '.page.pagegroup'); $('head').append(''); }; // This is for IE. IE doesn't respect the parent transform so I scale the widgets directly. Print.prototype._scaleWidgets = function _scaleWidgets(pageElement, scale) { var widgetString = ''; var topPosition = 0; var leftPosition = 0; var scaleZero = 'matrix3d(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)'; $.each($(pageElement).find('.widget'), function (i, widget) { var turnAngle = CommonUiUtils.getAngleDegree(widget); var _$$css2 = $(widget).css(['width', 'height', 'top', 'left']), width = _$$css2.width, height = _$$css2.height, top = _$$css2.top, left = _$$css2.left; topPosition = parseFloat(top) * scale; leftPosition = parseFloat(left) * scale; if (!$(widget).parent().hasClass('pagegroup') && $(widget).css('transform') !== scaleZero) { widgetString = widgetString + ' #' + $(widget).attr('id') + '{ width:' + width + ' !important; height:' + height + ' !important; top: ' + topPosition + 'px !important; left: ' + leftPosition + 'px !important; transform:scale(' + scale + ') rotate(' + turnAngle + 'deg) !important; transform-origin: top left !important;}'; } else { widgetString = widgetString + ' #' + $(widget).attr('id') + '{ position: absolute !important}'; } }.bind(this)); $.each($(pageElement).find('.page.pagegroup'), function (i, widget) { var turnAngle = CommonUiUtils.getAngleDegree(widget); var _$$css3 = $(widget).css(['width', 'height', 'top', 'left']), width = _$$css3.width, height = _$$css3.height, top = _$$css3.top, left = _$$css3.left; topPosition = parseFloat(top) * scale; leftPosition = parseFloat(left) * scale; if (!$(widget).parent().hasClass('pagegroup')) { widgetString = widgetString + ' #' + $(widget).attr('id') + '{ width:' + width + ' !important; height:' + height + ' !important; top: ' + topPosition + 'px !important; left: ' + leftPosition + 'px !important; transform-origin: top left !important; transform:scale(' + scale + ') rotate(' + turnAngle + 'deg) !important; position: absolute !important;}'; } else { widgetString = widgetString + ' #' + $(widget).attr('id') + '{ position: absolute !important}'; } }.bind(this)); $('head').append(''); }; Print.prototype.findPageSize = function findPageSize(pageElement) { var pagecontainer = pageElement.querySelector('.pagecontainer'); var pageSize = { height: $(pagecontainer).height(), width: $(pagecontainer).width() }; return pageSize; }; Print.prototype._checkIfPageAbsolute = function _checkIfPageAbsolute(pageElement) { if ($(pageElement).find('.pageabsolute').length) { return true; } return false; }; Print.prototype.scaleDashboard = function scaleDashboard(dashboardSizes, tabTitles, pdfSize, filters) { var scale = 0; $.each(dashboardSizes, function (i, dashboard) { scale = this.scalePageSizes(pdfSize, dashboard); var $dashboard = $('#' + dashboard.id); if (BrowserUtils.isIE() || $dashboard.hasClass('pagecontainer') || $dashboard.hasClass('pageabsolute')) { this._scaleWidgets($('#' + dashboard.id), scale); } else { this._fixWidgets($('#' + dashboard.id)); } if (tabTitles) { this.appendPrintStyles(scale, dashboard, tabTitles[i], pdfSize); } else { this.appendPrintStyles(scale, dashboard, tabTitles, pdfSize); } if (filters && filters[i].widgets.length > 0) { var element = this.createFilterContainer(filters[i], pdfSize); $('#' + dashboard.id).after(element); this.numberWidgets(dashboard, filters[i].widgets); } }.bind(this)); return; }; Print.prototype.appendPrintStyles = function appendPrintStyles(scale, dashboard, tabTitle, pdfSize) { var elementString = ''; if (scale > 1) { if (tabTitle && !this.tabsHidden) { if (tabTitle.toString().indexOf('\\') !== -1) { tabTitle = tabTitle.toString().replace(/\\/g, '\\\\'); } if (tabTitle.toString().indexOf('"') !== -1) { tabTitle = tabTitle.toString().replace(/"/g, '\\"'); } elementString = elementString + '#' + dashboard.id + '::before{content:"' + tabTitle + '"; display: block; display: block; margin-left: -12px; margin-top: -2px; padding-left: 12px; width:' + $('#page1_tab').width() + 'px; background: white;} #' + dashboard.id + '{ width: ' + pdfSize.width + 'pt; height: ' + pdfSize.height + 'pt;}'; } elementString = elementString + ' #' + dashboard.id + '{ width: ' + pdfSize.width + 'pt; height: ' + pdfSize.height + 'pt;}'; elementString = elementString + ' #' + dashboard.id + '{ page-break-after: always !important; page-break-inside: avoid !important;-webkit-column-break-inside: avoid !important; break-inside: avoid !important; display: flex !important; flex-direction: column;\ vertical-align: top !important;}'; } else { if (tabTitle && !this.tabsHidden) { elementString = elementString + '#' + dashboard.id + '::before{content:"' + tabTitle + '"; display: block; margin-left: -12px; margin-top:-2px; padding-left: 12px; width:' + $('#page1_tab').width() + 'px; background: white;}'; } elementString = elementString + ' #' + dashboard.id + '{ width: ' + pdfSize.width + 'pt; height: ' + pdfSize.height + 'pt;}'; if (BrowserUtils.isIE()) { elementString = elementString + ' #' + dashboard.id + '{ page-break-after: always !important; -webkit-column-break-inside: avoid !important; break-inside: avoid !important; page-break-inside: avoid; display: flex !important; flex-direction: column; vertical-align: top !important; overflow: visible !important;} #' + dashboard.childId + '{display: block !important; position:relative !important z-index:10; visibility: visible; overflow: visible !important;}'; } else { elementString = elementString + ' #' + dashboard.id + '{ page-break-after: always !important; -webkit-column-break-inside: avoid !important; break-inside: avoid !important; page-break-inside: avoid; display: flex !important; flex-direction: column; vertical-align: top !important; overflow: visible !important;} #' + dashboard.childId + '{width: ' + dashboard.size.width + 'px; height: ' + dashboard.size.height + 'px; transform:scale(' + scale + ') !important; transform-origin: 0 0 !important; display: block !important; position:relative !important; z-index:10; visibility: visible; overflow: visible !important;}'; } } $('head').append(''); }; Print.prototype.scalePageSizes = function scalePageSizes(pdfSize, dashboard) { var scale = 0; var scaleX = 0; var scaleY = 0; //Factor to convert px to pts, eg:1 px = .75 pt var dashboardScale = 0.75; //Width and height in pt var boardWidth = dashboard.size.width * dashboardScale; var boardHeight = dashboard.size.height * dashboardScale; scaleX = pdfSize.width / boardWidth; scaleY = pdfSize.height / boardHeight; if (scaleX < scaleY) { scale = scaleX; } else { scale = scaleY; } return scale; }; return Print; }(); return Print; }); //# sourceMappingURL=Print.js.map