UNPKG

@progress/telerik-jquery-report-viewer

Version:

Progress® Telerik® Report Viewer for jQuery

666 lines (661 loc) 22.9 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var utils = require('./utils.js'); var stringResources = require('./stringResources.js'); var serviceClient = require('./serviceClient.js'); var controller = require('./controller.js'); var history = require('./history.js'); var binder = require('./binder.js'); var commandSet = require('./commandSet.js'); var uiController = require('./uiController.js'); var accessibility = require('./accessibility.js'); var documentMapArea = require('./documentMapArea.js'); var parametersArea = require('./parametersArea.js'); var pagesArea = require('./pagesArea.js'); var mainMenu = require('./mainMenu.js'); var search = require('./search.js'); var sendEmail = require('./sendEmail.js'); var sideMenu = require('./sideMenu.js'); var events = require('./events.js'); var perspectives = require('./perspectives.js'); var enums = require('./enums.js'); var globalSettings = require('./globalSettings.js'); var memStorage = require('./mem-storage.js'); var templateCache = require('./template-cache.js'); var reportViewerSettings = require('./report-viewer/report-viewer-settings.js'); var linkButton = require('./toolbar/link-button.js'); var pageNumberInput = require('./toolbar/page-number-input.js'); var pageCountLabel = require('./toolbar/page-count-label.js'); const Instances = globalSettings.GlobalSettings.viewerInstances; function getDefaultOptions(serviceUrl, version) { return { id: null, serviceUrl: null, templateUrl: utils.rTrim(serviceUrl, "\\/") + "/resources/templates/telerikReportViewerTemplate-" + version + ".html/", reportSource: null, reportServer: null, authenticationToken: null, sendEmail: null, scale: 1, scaleMode: enums.ScaleModes.FIT_PAGE, viewMode: enums.ViewModes.INTERACTIVE, pageMode: enums.PageModes.CONTINUOUS_SCROLL, parametersAreaPosition: enums.ParametersAreaPositions.RIGHT, documentMapAreaPosition: enums.DocumentMapAreaPositions.LEFT, parameters: { editors: { multiSelect: enums.ParameterEditorTypes.LIST_VIEW, singleSelect: enums.ParameterEditorTypes.LIST_VIEW } }, persistSession: false, parameterEditors: [], disabledButtonClass: null, checkedButtonClass: null, parametersAreaVisible: true, documentMapVisible: true, enableAccessibility: false, searchMetadataOnDemand: false, initialPageAreaImageUrl: null, keepClientAlive: true }; } function ReportViewer(dom, options) { var svcApiUrl = options.serviceUrl; var reportServerUrlSVCApiUrl = ""; if (options.reportServer) { reportServerUrlSVCApiUrl = utils.rTrim(options.reportServer.url, "\\/"); svcApiUrl = reportServerUrlSVCApiUrl + "/api/reports"; } var $placeholder = $(dom); var templates = {}; var persistanceKey = options.id || `trv_${dom.id}`; var accessibility$1; var settings = {}; var client = {}; var controller$1 = {}; var perspectiveManager = {}; var history$1 = {}; var commands = {}; var viewer = {}; var serviceClientOptions = {}; var reportServerUrl = ""; options.viewerSelector = "reportViewer-" + utils.generateGuidString(); $placeholder.attr("data-selector", options.viewerSelector); if (!validateOptions(options)) { return; } var version = "18.2.24.806"; options = $.extend({}, getDefaultOptions(svcApiUrl, version), options); settings = new reportViewerSettings.ReportViewerSettings( persistanceKey, options.persistSession ? window.sessionStorage : new memStorage.MemStorage(), { scale: options.scale, scaleMode: options.scaleMode, printMode: options.printMode ? options.printMode : options.directPrint, enableAccessibility: options.enableAccessibility, searchMetadataOnDemand: options.searchMetadataOnDemand, sendEmail: options.sendEmail, parametersAreaPosition: options.parametersAreaPosition, documentMapAreaPosition: options.documentMapAreaPosition, keepClientAlive: options.keepClientAlive } ); if (options.reportServer) { reportServerUrl = utils.rTrim(options.reportServer.url, "\\/"); serviceClientOptions.serviceUrl = reportServerUrl + "/api/reports"; serviceClientOptions.loginInfo = { url: reportServerUrl + "/Token", username: options.reportServer.username, password: options.reportServer.password }; } else { serviceClientOptions.serviceUrl = options.serviceUrl; } client = new serviceClient.ServiceClient(serviceClientOptions); controller$1 = options.controller; if (!controller$1) { controller$1 = new controller.ReportViewerController({ serviceClient: client, settings }); } else { controller$1.updateSettings(settings); } history$1 = new history.HistoryManager({ controller: controller$1, settings }); commands = new commandSet.CommandSet({ controller: controller$1, history: history$1 }); new uiController.UIController({ controller: controller$1, history: history$1, commands }); viewer = { stringResources: stringResources.stringResources, refreshReport: function(ignoreCache) { if (arguments.length === 0) { ignoreCache = true; } controller$1.refreshReport(ignoreCache); return viewer; }, reportSource: function(rs) { if (rs || rs === null) { controller$1.setReportSource(rs); controller$1.refreshReport(false); return viewer; } return controller$1.getReportSource(); }, clearReportSource: function() { controller$1.clearReportSource(); return viewer; }, viewMode: function(vm) { if (vm) { controller$1.setViewMode(vm); return viewer; } return controller$1.getViewMode(); }, pageMode: function(psm) { if (psm) { controller$1.setPageMode(psm); return viewer; } return controller$1.getPageMode(); }, printMode: function(pm) { if (pm) { controller$1.setPrintMode(pm); return viewer; } return controller$1.getPrintMode(); }, scale: function({ scale, scaleMode } = {}) { if (scale || scaleMode) { controller$1.setScale(scale || controller$1.getScale()); controller$1.setScaleMode(scaleMode || controller$1.getScaleMode()); return viewer; } return { scale: controller$1.getScale(), scaleMode: controller$1.getScaleMode() }; }, currentPage: function() { return controller$1.getCurrentPageNumber(); }, pageCount: function() { return controller$1.getPageCount(); }, parametersAreaVisible: function(visible) { controller$1.setParametersAreaVisible({ visible }); }, getReportParameters: function() { return controller$1.getReportParameters(); }, authenticationToken: function(token) { if (token) { controller$1.setAuthenticationToken(token); } return viewer; }, bind: function(eventName, eventHandler) { eventBinder(eventName, eventHandler, true); }, unbind: function(eventName, eventHandler) { eventBinder(eventName, eventHandler, false); }, accessibilityKeyMap: function(keyMap) { if (accessibility$1) { if (keyMap) { accessibility$1.setKeyMap(keyMap); return viewer; } return accessibility$1.getKeyMap(); } return void 0; }, commands, dispose: function() { controller$1.dispose(); if (perspectiveManager) { perspectiveManager.dispose(); } } }; function validateOptions(options2) { if (!options2) { $placeholder.text("The report viewer configuration options are not initialized."); return false; } if (options2.reportServer) { if (!options2.reportServer.url) { $placeholder.text("The report server URL is not specified."); return false; } } else { if (!options2.serviceUrl) { $placeholder.text("The serviceUrl is not specified."); return false; } } return true; } function eventBinder(eventName, eventHandler, bind) { if (typeof eventHandler === "function") { if (bind) { $(viewer).on(eventName, { sender: viewer }, eventHandler); } else { $(viewer).off(eventName, eventHandler); } } else if (!eventHandler && !bind) { $(viewer).off(eventName); } } function attachEvents() { var eventMap = { EXPORT_BEGIN: controller$1.Events.EXPORT_STARTED, EXPORT_END: controller$1.Events.EXPORT_DOCUMENT_READY, PRINT_BEGIN: controller$1.Events.PRINT_STARTED, PRINT_END: controller$1.Events.PRINT_DOCUMENT_READY, RENDERING_BEGIN: controller$1.Events.BEFORE_LOAD_REPORT, RENDERING_END: controller$1.Events.REPORT_LOAD_COMPLETE, PAGE_READY: controller$1.Events.PAGE_READY, ERROR: controller$1.Events.ERROR, UPDATE_UI: controller$1.Events.UPDATE_UI, INTERACTIVE_ACTION_EXECUTING: controller$1.Events.INTERACTIVE_ACTION_EXECUTING, INTERACTIVE_ACTION_ENTER: controller$1.Events.INTERACTIVE_ACTION_ENTER, INTERACTIVE_ACTION_LEAVE: controller$1.Events.INTERACTIVE_ACTION_LEAVE, VIEWER_TOOLTIP_OPENING: controller$1.Events.TOOLTIP_OPENING, SEND_EMAIL_BEGIN: controller$1.Events.SEND_EMAIL_STARTED, SEND_EMAIL_END: controller$1.Events.SEND_EMAIL_READY }; var $viewer = $(viewer); for (const [viewerEvent, controllerEvent] of Object.entries(eventMap)) { controller$1.on(controllerEvent, function(e, args) { $viewer.trigger(viewerEvent, args); }); } } function attachEventHandlers() { eventBinder(events.Events.EXPORT_BEGIN, options.exportBegin, true); eventBinder(events.Events.EXPORT_END, options.exportEnd, true); eventBinder(events.Events.PRINT_BEGIN, options.printBegin, true); eventBinder(events.Events.PRINT_END, options.printEnd, true); eventBinder(events.Events.RENDERING_BEGIN, options.renderingBegin, true); eventBinder(events.Events.RENDERING_END, options.renderingEnd, true); eventBinder(events.Events.PAGE_READY, options.pageReady, true); eventBinder(events.Events.ERROR, options.error, true); eventBinder(events.Events.UPDATE_UI, options.updateUi, true); eventBinder(events.Events.INTERACTIVE_ACTION_EXECUTING, options.interactiveActionExecuting, true); eventBinder(events.Events.INTERACTIVE_ACTION_ENTER, options.interactiveActionEnter, true); eventBinder(events.Events.INTERACTIVE_ACTION_LEAVE, options.interactiveActionLeave, true); eventBinder(events.Events.VIEWER_TOOLTIP_OPENING, options.viewerToolTipOpening, true); eventBinder(events.Events.SEND_EMAIL_BEGIN, options.sendEmailBegin, true); eventBinder(events.Events.SEND_EMAIL_END, options.sendEmailEnd, true); } function init() { $placeholder.html(templates["trv-report-viewer"]); binder.Binder.bind( $placeholder, { controller: controller$1, commands, templates }, options ); perspectiveManager = new perspectives.PerspectiveManager(dom, controller$1); perspectiveManager.attach(); initSplitter(); attachEvents(); attachEventHandlers(); initFromStorage(); initAccessibility(options); } function initSplitter() { var parameterAreaPaneOptions = { max: "500px", min: "50px", size: "210px", collapsible: true }; var parameterAreaTemplate = $placeholder.find(".trv-parameters-area"); var parameterAreaPanes = [{}]; var documentMapPaneOptions = { max: "500px", min: "50px", size: "210px", collapsible: true, collapsed: true }; var documentMapTemplate = $placeholder.find(".trv-document-map"); var documentMapPanes = [{}]; var orientation = "horizontal"; if (options.documentMapAreaPosition === enums.DocumentMapAreaPositions.RIGHT) { documentMapTemplate.insertAfter($placeholder.find(".trv-pages-area")); documentMapPanes.push(documentMapPaneOptions); } else { documentMapPanes.unshift(documentMapPaneOptions); } if (options.parametersAreaPosition === enums.ParametersAreaPositions.TOP || options.parametersAreaPosition === enums.ParametersAreaPositions.BOTTOM) { orientation = "vertical"; parameterAreaTemplate.addClass("-vertical"); parameterAreaPaneOptions.size = "130px"; } if (options.parametersAreaPosition === enums.ParametersAreaPositions.LEFT || options.parametersAreaPosition === enums.ParametersAreaPositions.TOP) { parameterAreaTemplate.insertBefore($placeholder.find(".trv-document-map-splitter")); parameterAreaPanes.unshift(parameterAreaPaneOptions); } else { parameterAreaPanes.push(parameterAreaPaneOptions); } try { var documentMapSplitter = $placeholder.find(".trv-document-map-splitter").kendoSplitter({ panes: documentMapPanes, expand: function(e) { setSplitterPaneVisibility(e.pane, true); }, collapse: function(e) { setSplitterPaneVisibility(e.pane, false); }, resize: function(e) { } }).data("kendoSplitter"); } catch (e) { console.error("Instantiation of Kendo Splitter as Document Map splitter threw an exception", e); throw e; } try { var parametersSplitter = $placeholder.find(".trv-parameters-splitter").kendoSplitter({ panes: parameterAreaPanes, orientation, expand: function(e) { setSplitterPaneVisibility(e.pane, true); }, collapse: function(e) { setSplitterPaneVisibility(e.pane, false); }, resize: function(e) { } }).data("kendoSplitter"); } catch (e) { console.error("Instantiation of Kendo Splitter as Parameters area splitter threw an exception", e); throw e; } var parametersSplitterInstance = { id: options.viewerSelector + "-parameters-splitter", instance: parametersSplitter }; var documentMapSplitterInstance = { id: options.viewerSelector + "-document-map-splitter", instance: documentMapSplitter }; Instances.push(parametersSplitterInstance); Instances.push(documentMapSplitterInstance); } function setSplitterPaneVisibility(pane, visible) { var paneID = $(pane).attr("data-id"); switch (paneID) { case "trv-document-map": controller$1.setDocumentMapVisible({ visible }); break; case "trv-parameters-area": controller$1.setParametersAreaVisible({ visible }); break; } } function initFromStorage() { var vm = settings.getViewMode(); var psm = settings.getPageMode(); var pm = settings.getPrintMode(); var s = settings.getScale(); var sm = settings.getScaleMode(); var dm = settings.getDocumentMapVisible(); var pa = settings.getParametersAreaVisible(); settings.getAccessibilityKeyMap(); controller$1.setViewMode(vm ? vm : options.viewMode); controller$1.setPageMode(psm ? psm : options.pageMode); controller$1.setPrintMode(pm ? pm : options.printMode); controller$1.setScale(s ? s : options.scale); controller$1.setScaleMode(sm ? sm : options.scaleMode); controller$1.setDocumentMapVisible({ visible: dm ? dm : options.documentMapVisible }); controller$1.setParametersAreaVisible({ visible: pa ? pa : options.parametersAreaVisible }); controller$1.printModeChanged(function() { settings.setPrintMode(controller$1.getPrintMode()); }); controller$1.viewModeChanged(function() { settings.setViewMode(controller$1.getViewMode()); }); controller$1.pageModeChanged(function() { settings.setPageMode(controller$1.getPageMode()); }); controller$1.scaleChanged(function(event, scale) { settings.setScale(scale); }); controller$1.scaleModeChanged(function(event, scaleMode) { settings.setScaleMode(scaleMode); }); controller$1.setSideMenuVisible(function(event, args) { window.setTimeout(function() { (args.visible ? $.fn.addClass : $.fn.removeClass).call($placeholder, "trv-side-menu-visible"); }, 1); }); controller$1.setDocumentMapVisible(function() { var args = {}; controller$1.getDocumentMapState(args); settings.setDocumentMapVisible(args.visible); }); controller$1.setParametersAreaVisible(function() { var args = {}; controller$1.getParametersAreaState(args); settings.setParametersAreaVisible(args.visible); }); } function initAccessibility(options2) { if (options2.enableAccessibility) { accessibility$1 = new accessibility.Accessibility({ controller: controller$1, templates }); var am = options2.accessibilityKeyMap; if (am) { accessibility$1.setKeyMap(am); } settings.contentTabIndex = getTemplateContentTabIndex(); } } function getTemplateContentTabIndex() { var pageAreaSelector = "div.trv-pages-area"; try { var $pagesArea = $placeholder.find(pageAreaSelector); if ($pagesArea.length === 0) { throw "Selector " + pageAreaSelector + " did not return a result."; } return parseInt($pagesArea.attr("tabindex")); } catch (e) { if (console) console.log(e); return 0; } } function start() { var pendingRefresh = false; init(); controller$1.reportLoadComplete(function() { if (options.documentMapVisible === false) { controller$1.setDocumentMapVisible({ visible: false }); } }); var rs = settings.getReportSource(); if (rs !== void 0) { controller$1.setReportSource(rs); var pageNumber = settings.getPageNumber(); if (pageNumber !== void 0) { controller$1.navigateToPage(pageNumber); } pendingRefresh = true; } else { if (options.viewMode) { controller$1.setViewMode(options.viewMode); } if (options.pageMode) { controller$1.setPageMode(options.pageMode); } if (options.reportSource) { controller$1.setReportSource(options.reportSource); pendingRefresh = true; } } if (typeof options.ready === "function") { options.ready.call(viewer); } if (pendingRefresh) { controller$1.refreshReport(false); } } function loadStyleSheets(styleSheets) { if (!styleSheets) return Promise.resolve(); var $head = $("head"); var currentStyleLinks = $head.find("link").map(function(i, e) { return e.outerHTML; }).toArray(); var promises = []; Array.from(styleSheets).forEach((element) => { if (currentStyleLinks.indexOf(element) === -1) { promises.push( new Promise(function(resolve, reject) { var $link = $(element); $link.on("load", resolve); $link.on("onerror", function() { utils.logError("error loading stylesheet " + element); resolve(); }); $head.append($link); }) ); } }); return Promise.all(promises).then(controller$1.cssLoaded); } function browserSupportsAllFeatures() { return window.Promise; } function ensureKendo(version2) { if (window.kendo) { return Promise.resolve(); } var kendoUrl = utils.rTrim(svcApiUrl, "\\/") + "/resources/js/telerikReportViewer.kendo-" + version2 + ".min.js/"; return utils.loadScript(kendoUrl).catch(function(errorData) { utils.logError("Kendo could not be loaded automatically. Make sure 'options.serviceUrl' / 'options.reportServer.url' is correct and accessible. The error is: " + errorData.error); }); } function main(version2) { ensureKendo(version2).then(function() { }).then(function() { viewer.authenticationToken(options.authenticationToken); controller$1.getServiceVersion().catch(function(ex) { var errorOutput = utils.isApplicationExceptionInstance(ex) ? ex.exceptionMessage : utils.stringFormat(stringResources.stringResources.errorServiceUrl, [utils.escapeHtml(svcApiUrl)]); $placeholder.text(errorOutput); return Promise.reject(); }).then(function(data) { if (data !== version2) { $placeholder.text(utils.stringFormat(stringResources.stringResources.errorServiceVersion, [data, version2])); return Promise.reject(); } templateCache.TemplateCache.load(options.templateUrl, svcApiUrl, client).catch(function() { $placeholder.text(utils.stringFormat(stringResources.stringResources.errorLoadingTemplates, [utils.escapeHtml(options.templateUrl)])); return Promise.reject(); }).then(function(result) { templates = result.templates; return loadStyleSheets(result.styleSheets); }).then(start); }); }); } if (browserSupportsAllFeatures()) { main(version); } else { throw "The current browser does not support the Promise feature which is required for using the Report Viewer."; } return viewer; } var pluginName = "telerik_ReportViewer"; $.fn[pluginName] = function(options) { if (this.selector && !options.selector) { options.selector = this.selector; } return this.each(function() { if (!$.data(this, pluginName)) { $.data(this, pluginName, new ReportViewer(this, options)); } }); }; const plugins = [ { name: "telerik_ReportViewer_DocumentMapArea", constructor: documentMapArea.DocumentMapArea }, { name: "telerik_ReportViewer_MainMenu", constructor: mainMenu.MainMenu }, { name: "telerik_ReportViewer_PagesArea", constructor: pagesArea.PagesArea }, { name: "telerik_ReportViewer_ParametersArea", constructor: parametersArea.ParametersArea }, { name: "telerik_ReportViewer_SearchDialog", constructor: search.Search }, { name: "telerik_ReportViewer_SendEmail", constructor: sendEmail.SendEmail }, { name: "telerik_ReportViewer_SideMenu", constructor: sideMenu.SideMenu }, { name: "telerik_ReportViewer_LinkButton", constructor: linkButton.LinkButton }, { name: "telerik_ReportViewer_PageNumberInput", constructor: pageNumberInput.PageNumberInput }, { name: "telerik_ReportViewer_PageCountLabel", constructor: pageCountLabel.PageCountLabel } ]; plugins.forEach((plugin) => { $.fn[plugin.name] = function(options, otherOptions) { return this.each(function() { if (!$.data(this, plugin.name)) { $.data(this, plugin.name, new plugin.constructor(this, options, otherOptions)); } }); }; }); exports.ReportViewer = ReportViewer;