LegacyMapMappingManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. *
  7. */
  8. define(['../../lib/@waca/core-client/js/core-client/ui/core/Class'], function (Class) {
  9. 'use strict';
  10. /*
  11. * This class manages the slots mapping for Legacy Map (com.ibm.vis.rave2polygonmap)
  12. */
  13. var LegacyMapMappingManager = Class.extend({
  14. init: function init(visualization) {
  15. this.visualization = visualization;
  16. this.unmappedColumns = null;
  17. },
  18. /**
  19. * Get column type, if categorySubType is 'location', use this
  20. * @param column
  21. * @return type or categorySubType
  22. */
  23. _getColumnType: function _getColumnType(column) {
  24. var taxonomy = column.getTaxonomyList() ? column.getTaxonomyList()[0] : null;
  25. return taxonomy && taxonomy.getClass() === 'cGeoLocation' ? 'location' : column.getType();
  26. },
  27. _getSlotTypeForColumn: function _getSlotTypeForColumn(column) {
  28. var columnTypeToSlotTypeMap = {
  29. 'fact': 'ordinal',
  30. 'attribute': 'category',
  31. 'location': 'location'
  32. };
  33. return columnTypeToSlotTypeMap[this._getColumnType(column)];
  34. },
  35. //Set the slots' mapping info in the given map
  36. _setExistingMappingInfo: function _setExistingMappingInfo(slotIdToColumnMap) {
  37. this.visualization.getSlots().getMappingInfoList().forEach(function (mapping) {
  38. slotIdToColumnMap[mapping.slot.getId()] = [mapping.dataItem.getColumnId()];
  39. });
  40. },
  41. /**
  42. * Map the given unmappedColumns to slots.
  43. * Returns an array of columns, sorted in the order of corresponding mapped slots as defined in the visDefinition
  44. */
  45. mapColumnsToSlots: function mapColumnsToSlots(visDefinition, unmappedColumns, bKeepExistingMapping) {
  46. var slotIdToColumnMap = {};
  47. if (bKeepExistingMapping) {
  48. this._setExistingMappingInfo(slotIdToColumnMap);
  49. }
  50. var unmappedSlotDefs = visDefinition.getSlotList().filter(function (slotDef) {
  51. return !slotDef.getProperty('multiplier') && !slotIdToColumnMap[slotDef.getId()];
  52. });
  53. for (var i = 0; i < unmappedSlotDefs.length; i++) {
  54. var slotDef = unmappedSlotDefs[i];
  55. var slotId = slotDef.getId();
  56. if (!unmappedColumns.length) {
  57. break;
  58. }
  59. var column = this._findBestMatchingColumnForSlot(slotDef, unmappedColumns);
  60. slotIdToColumnMap[slotId] = [column.getId()];
  61. unmappedColumns = unmappedColumns.filter(function (unmappedColumn) {
  62. return unmappedColumn.getId() !== column.getId();
  63. });
  64. }
  65. this.unmappedColumns = unmappedColumns;
  66. //return an array of unmappedColumns array in the same order as the slot definitions found in visDefinition
  67. return slotIdToColumnMap;
  68. },
  69. getUnmappedColumns: function getUnmappedColumns() {
  70. if (this.unmappedColumns.length) {
  71. return this.unmappedColumns;
  72. }
  73. },
  74. _findBestMatchingColumnForSlot: function _findBestMatchingColumnForSlot(slotDef, unmappedColumns) {
  75. var _this = this;
  76. var found;
  77. if (unmappedColumns.length === 0) {
  78. return;
  79. }
  80. //Assign location column first
  81. if (slotDef.getSubType() === 'location') {
  82. found = unmappedColumns.find(function (column) {
  83. return _this._getColumnType(column) === 'location';
  84. });
  85. if (found) {
  86. return found;
  87. }
  88. }
  89. //If no location, assign column of same type
  90. var type = slotDef.getType();
  91. found = unmappedColumns.find(function (column) {
  92. return this._getSlotTypeForColumn(column) === type;
  93. }.bind(this));
  94. if (found) {
  95. return found;
  96. }
  97. //Otherwise, use next unmapped column
  98. return unmappedColumns[0];
  99. }
  100. });
  101. return LegacyMapMappingManager;
  102. });
  103. //# sourceMappingURL=LegacyMapMappingManager.js.map