DashboardSpecSvc.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2015, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../lib/@waca/core-client/js/core-client/ui/core/Class', 'underscore'], function (Class, _) {
  8. /**
  9. * Service which facilitates access to contents of the dasboard spec.
  10. */
  11. var Svc = Class.extend({
  12. init: function init(boardModel) {
  13. this.boardModel = boardModel;
  14. //The filterContextAdapters map is responsible for providing functions that convert
  15. //widgetInstance type-specific filters to a portable form needed by the getFilterContext api.
  16. //Currently there is only one type so the function is provided. In future, this map could be extended or, to be more dynamic
  17. //callers could register their converters
  18. this._filterContextAdapters = {
  19. 'data': this._convertDataWidgetFilterContext
  20. //more converters should be referenced by widgetInstance type here.
  21. };
  22. },
  23. /**
  24. * Return mappings that match the condition in the supplied matching function as well as the dataset and optional page specified.
  25. * @param cbMappingMatches a matching function supplied by the caller....only mappings that succeed will be returned.
  26. * @param dataSetId A datasetId that any returned mappings must belong to.
  27. * @param containerPageId (optional) - Only mappings for widgets on the specified page will be processed (pageId is a string eg 'page1')
  28. * @returns an array of mappings that match the criteria defined in the callback function cbMappingMatches
  29. *
  30. */
  31. getMappings: function getMappings(cbMappingMatches, dataSetId, containerPageId) {
  32. var returnMappings = [];
  33. if (!this.boardModel) {
  34. return returnMappings;
  35. }
  36. var fAddMapping = function fAddMapping(mapping) {
  37. if (cbMappingMatches(mapping)) {
  38. returnMappings.push(mapping);
  39. }
  40. };
  41. _.keys(this.boardModel.widgetInstances).forEach(function (widgetInstanceModel) {
  42. var widgetInstance = this.boardModel.widgetInstances[widgetInstanceModel];
  43. var mappings = widgetInstance.mapping;
  44. if (mappings && !Array.isArray(mappings)) {
  45. mappings = [mappings];
  46. }
  47. var dataSetMatches = widgetInstance.dataSet && widgetInstance.dataSet.id === dataSetId;
  48. if (dataSetMatches && mappings) {
  49. //If containerPageId is not specified, all pages match.
  50. var pageIdMatches = !containerPageId;
  51. if (!pageIdMatches) {
  52. var pageForWidget = this.boardModel.layout.findTopLevelParentItem(widgetInstance.id);
  53. pageIdMatches = pageForWidget && pageForWidget.id === containerPageId;
  54. }
  55. if (pageIdMatches) {
  56. _.each(mappings, fAddMapping);
  57. }
  58. }
  59. }, this);
  60. return returnMappings;
  61. },
  62. /**
  63. * Return an array of filterContexts in 'portable form' for all widgets that match the dataset and optional eventGroupId specified.
  64. * Since the datawidget is the only widget instance type with a filter context, portable form is the JSON format
  65. * it serializes but this is likely to change in future as new widgets capable of contributing to filter context are implemented (ie: visControl widget)
  66. *
  67. * For each widget, the array would be.
  68. * {
  69. * id: %widgetId%,
  70. * dataSet: %dataSetId,url%,
  71. * filterContext: { %portablefilterDefinition }
  72. * }
  73. *
  74. * @param dataSetId (optional) - include the filter context for widgets that match this dataSetId (widgets for all dataSets if null).
  75. * @param eventGroupId (optional) - include the filter context for widgets on this event group (widgets for all groups if null).
  76. * @returns the array as described for widgets that match the dataSetId and containerPageId constraints.
  77. */
  78. getFilterContext: function getFilterContext(dataSetId, eventGroupId) {
  79. var returnResults = [];
  80. if (!this.boardModel) {
  81. return returnResults;
  82. }
  83. _.keys(this.boardModel.widgetInstances).forEach(function (widgetInstanceModel) {
  84. var widgetInstance = this.boardModel.widgetInstances[widgetInstanceModel];
  85. var filterContextAdapter = this._filterContextAdapters[widgetInstance.type];
  86. var dataSetMatches = !dataSetId || widgetInstance.dataSet && widgetInstance.dataSet.id === dataSetId;
  87. if (dataSetMatches && filterContextAdapter) {
  88. //If eventGroupId is not specified, all groups match.
  89. var groupIdMatches = !eventGroupId;
  90. if (!groupIdMatches) {
  91. var eventGroup = this.boardModel.eventGroups.findGroup(widgetInstance.id);
  92. groupIdMatches = eventGroup && eventGroup.id === eventGroupId;
  93. }
  94. if (groupIdMatches) {
  95. returnResults.push(filterContextAdapter(widgetInstance));
  96. }
  97. }
  98. }, this);
  99. return returnResults;
  100. },
  101. /**
  102. * This function is responsible for returning the filterContext in the form required by getFilterContext
  103. * for the data widget. If other widget types require returning
  104. * @param widgetInstance - the widgetInstance
  105. * @returns the filterContext structure which includes a filter specification in portable form that any widget type should be able
  106. * to interpret.
  107. */
  108. _convertDataWidgetFilterContext: function _convertDataWidgetFilterContext(widgetInstance) {
  109. return {
  110. id: widgetInstance.id,
  111. dataSet: widgetInstance.dataSet,
  112. filterContext: widgetInstance.filters
  113. };
  114. }
  115. });
  116. return Svc;
  117. });
  118. //# sourceMappingURL=DashboardSpecSvc.js.map