UNPKG

@progress/telerik-jquery-report-viewer

Version:

Progress® Telerik® Report Viewer for jQuery

156 lines (153 loc) 5.32 kB
import { stringResources } from './stringResources.js'; import { DocumentMapAreaPositions } from './enums.js'; import { GlobalSettings } from './globalSettings.js'; var defaultOptions = {}; function DocumentMapArea(placeholder, options, otherOptions) { options = $.extend({}, defaultOptions, options, otherOptions); var controller = options.controller; if (!controller) { throw "No controller (telerikReporting.reportViewerController) has been specified."; } var $placeholder = $(placeholder); var $documentMap; var documentMapVisible = options.documentMapVisible !== false; var enableAccessibility = options.enableAccessibility; var currentReport = null; var documentMapNecessary = false; init(); function init() { $documentMap = $('<div id="' + options.viewerSelector + '-documentMap"></div>'); $documentMap.appendTo(placeholder); attach(); replaceStringResources($placeholder); } function onTreeViewSelectionChanged(e) { var documentMapNode = this.dataItem(e.node); var page = documentMapNode.page; var id = documentMapNode.id; controller.navigateToPage(page, { type: "bookmark", id }); } function onTreeViewNodeExpand(e) { if (enableAccessibility) { window.setTimeout(function() { setNodeAccessibilityAttributes(e.node); }, 100); } } function setNodeAccessibilityAttributes(node) { var $items = $(node).find("li"); Array.from($items).forEach((item) => { var $li = $(item); $li.attr("aria-label", $li[0].innerText); }); } function clearDocumentMap() { displayDocumentMap([]); } function displayDocumentMap(documentMap) { var hasDocumentMap = documentMap && !$.isEmptyObject(documentMap); var $treeView = $documentMap.data("kendoTreeView"); if (!$treeView) { try { $documentMap.kendoTreeView({ dataTextField: "text", select: onTreeViewSelectionChanged }); $treeView = $documentMap.data("kendoTreeView"); } catch (e) { console.error("Instantiation of Kendo TreeView as Document Map threw an exception", e); throw e; } } $treeView.setDataSource(documentMap); if (enableAccessibility) { setAccessibilityAttributes($treeView); } showDocumentMap(hasDocumentMap); } function setAccessibilityAttributes(treeView) { treeView.bind("expand", onTreeViewNodeExpand); treeView.element.attr("aria-label", stringResources.ariaLabelDocumentMap); var listItems = treeView.element.find("ul"); Array.from(listItems).forEach((list) => { setNodeAccessibilityAttributes(list); }); if (documentMapNecessary) { setSplitbarAccessibilityAttributes(); } } function setSplitbarAccessibilityAttributes() { var splitbar = $placeholder.next(); if (options.documentMapAreaPosition === DocumentMapAreaPositions.RIGHT) { splitbar = $placeholder.prev(); } splitbar.attr("aria-label", stringResources.ariaLabelDocumentMapSplitter); } function isVisible() { var args = {}; controller.getDocumentMapState(args); return args.visible; } function beginLoad() { $placeholder.addClass("trv-loading"); } function endLoad() { $placeholder.removeClass("trv-loading"); } function showDocumentMap(show) { var splitter = GlobalSettings.viewerInstances.find((element) => element.id === options.viewerSelector + "-document-map-splitter").instance; var sibling = $placeholder.next(); if (options.documentMapAreaPosition === DocumentMapAreaPositions.RIGHT) { sibling = $placeholder.prev(); } if (splitter) { (documentMapNecessary ? $.fn.removeClass : $.fn.addClass).call(sibling, "trv-hidden"); splitter.toggle(".trv-document-map", show); } } function attach() { controller.beginLoadReport(function() { beginLoad(); var r = controller.getReportSource().report; var clearMapItems = currentReport !== r || !isVisible(); currentReport = r; if (clearMapItems) { clearDocumentMap(); } }).reportLoadComplete(function(event, args) { if (args.documentMapAvailable) { documentMapNecessary = true; displayDocumentMap(args.documentMapNodes); controller.setDocumentMapVisible({ enabled: true, visible: documentMapVisible }); } else { documentMapNecessary = false; showDocumentMap(documentMapNecessary); } endLoad(); }).error(function(event, error) { endLoad(); clearDocumentMap(); }).renderingStopped(function() { documentMapNecessary = false; showDocumentMap(false); }); controller.setDocumentMapVisible(function(event, args) { documentMapVisible = args.visible; showDocumentMap(documentMapVisible && documentMapNecessary); }).getDocumentMapState(function(event, args) { args.enabled = documentMapNecessary; args.visible = documentMapVisible; }); } function replaceStringResources($documentMap2) { var $documentMapOverlay = $documentMap2.find(".trv-document-map-overlay"); if (!$documentMapOverlay) { return; } $documentMapOverlay.attr("aria-label", stringResources[$documentMapOverlay.attr("aria-label")]); } } export { DocumentMapArea };