SynchContextHelper.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Watson Analytics (C) Copyright IBM Corp. 2018, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', '../../utils/DatasourceUtil'], function (_, DatasourceUtil) {
  9. var getMetadataColumnsCallback = function getMetadataColumnsCallback() {
  10. return true;
  11. };
  12. var _getProjectedDataItems = function _getProjectedDataItems(dataSlots) {
  13. var itemIds = [];
  14. dataSlots.forEach(function (slotAPI) {
  15. var slotDataItems = slotAPI.getDataItemList() || [];
  16. slotDataItems.forEach(function (dataItemAPI) {
  17. itemIds.push(dataItemAPI.getColumnId());
  18. });
  19. });
  20. return itemIds;
  21. };
  22. //Collect only the hierarchies that are in the same tables
  23. var _isSameTable = function _isSameTable(tableNames, column) {
  24. var colTableName = column.getTableName();
  25. for (var _iterator = tableNames, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  26. var _ref;
  27. if (_isArray) {
  28. if (_i >= _iterator.length) break;
  29. _ref = _iterator[_i++];
  30. } else {
  31. _i = _iterator.next();
  32. if (_i.done) break;
  33. _ref = _i.value;
  34. }
  35. var name = _ref;
  36. if (colTableName === name) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. };
  42. var _retrieveItems = function _retrieveItems(metadaColumns, nonProjectedItems, matchById, tableNames) {
  43. var result = [];
  44. nonProjectedItems.forEach(function (value) {
  45. for (var _iterator2 = metadaColumns, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  46. var _ref2;
  47. if (_isArray2) {
  48. if (_i2 >= _iterator2.length) break;
  49. _ref2 = _iterator2[_i2++];
  50. } else {
  51. _i2 = _iterator2.next();
  52. if (_i2.done) break;
  53. _ref2 = _i2.value;
  54. }
  55. var column = _ref2;
  56. var columnName = column.getLabel().toLocaleLowerCase();
  57. if (matchById && column.getId() === value) {
  58. result.push(columnName);
  59. break;
  60. } else if (!matchById && columnName === value && _isSameTable(tableNames, column)) {
  61. result.push(column.getId());
  62. break;
  63. }
  64. }
  65. });
  66. return _.uniq(result);
  67. };
  68. var _getColumnNames = function _getColumnNames(sourceModule, targetModule, tableNames, searchItemIds) {
  69. var columnNames = _retrieveItems(sourceModule.getMetadataColumns(getMetadataColumnsCallback), searchItemIds, true, tableNames);
  70. if (columnNames.length === 0) {
  71. //The item ids might be in the target module so check it here
  72. //This case occur when there are multiple viz in same module involve where one vis does not have a projected item but other vis does have the projected item. So
  73. //depends on who gets to process first, it can get here.
  74. columnNames = _retrieveItems(targetModule.getMetadataColumns(getMetadataColumnsCallback), searchItemIds, true, tableNames);
  75. }
  76. return columnNames;
  77. };
  78. var SynchSlotDataItemAPI = function () {
  79. function SynchSlotDataItemAPI() {
  80. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  81. _classCallCheck(this, SynchSlotDataItemAPI);
  82. this.itemId = options.itemId;
  83. }
  84. SynchSlotDataItemAPI.prototype.getColumnId = function getColumnId() {
  85. return this.itemId;
  86. };
  87. return SynchSlotDataItemAPI;
  88. }();
  89. var SynchSlotAPI = function () {
  90. function SynchSlotAPI() {
  91. var _this = this;
  92. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  93. _classCallCheck(this, SynchSlotAPI);
  94. this.generateDataItemIds(options.itemIds);
  95. return {
  96. getDataItemList: function getDataItemList() {
  97. return _this.dataItemAPIs;
  98. }
  99. };
  100. }
  101. SynchSlotAPI.prototype.generateDataItemIds = function generateDataItemIds() {
  102. var _this2 = this;
  103. var itemIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  104. this.dataItemAPIs = [];
  105. itemIds.forEach(function (itemId) {
  106. _this2.dataItemAPIs.push(new SynchSlotDataItemAPI({ itemId: itemId }));
  107. });
  108. };
  109. return SynchSlotAPI;
  110. }();
  111. /**
  112. * Wrapper class representing a visualization's project items
  113. */
  114. var SynchProjectedItem = function () {
  115. function SynchProjectedItem(module, item) {
  116. _classCallCheck(this, SynchProjectedItem);
  117. if (!item) {
  118. throw new Error('item object is not provided.');
  119. }
  120. this.item = item;
  121. this.module = module;
  122. return {
  123. getLabel: this.getLabel.bind(this),
  124. getItemId: this.getItemId.bind(this),
  125. getId: this.getItemId.bind(this),
  126. getTableName: this.getTableName.bind(this)
  127. };
  128. }
  129. SynchProjectedItem.prototype.getLabel = function getLabel() {
  130. return this.item.itemLabel || this.item.getLabel();
  131. };
  132. SynchProjectedItem.prototype.getItemId = function getItemId() {
  133. return this.item.itemId || this.item.getItemId();
  134. };
  135. SynchProjectedItem.prototype.getTableName = function getTableName() {
  136. var metadataColumn = this.module.getMetadataColumn(this.getItemId());
  137. return metadataColumn ? metadataColumn.getTableName() : undefined;
  138. };
  139. return SynchProjectedItem;
  140. }();
  141. /**
  142. * This class represents a synchronize context entry use for both page context origin [visualization | filter] types
  143. */
  144. var SynchContext = function () {
  145. function SynchContext() {
  146. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  147. _classCallCheck(this, SynchContext);
  148. if (!options.fromObject) {
  149. throw new Error('fromObject is not provided.');
  150. }
  151. this.fromObject = options.fromObject;
  152. this.module = options.module;
  153. this.synchDataEntry = options.synchDataEntry;
  154. this.projectedItems = SynchContextHelper.createSynchProjectedItems(this.module, options.projectedItems);
  155. this.ownerWidget = options.ownerWidget;
  156. this.widgetIds = [options.widgetId];
  157. this.getTableRef();
  158. }
  159. SynchContext.prototype.getAPI = function getAPI() {
  160. if (!this.api) {
  161. this.api = {
  162. getSourceId: this.getSourceId.bind(this),
  163. getEventGroupId: this.getEventGroupId.bind(this),
  164. getEventSourceId: this.getEventSourceId.bind(this),
  165. getTableRef: this.getTableRef.bind(this),
  166. getOrigin: this.getOrigin.bind(this),
  167. getModule: this.getModule.bind(this),
  168. setModule: this.setModule.bind(this),
  169. getSynchDataEntry: this.getSynchDataEntry.bind(this),
  170. getProjectedItems: this.getProjectedItems.bind(this),
  171. setProjectedItems: this.setProjectedItems.bind(this),
  172. haveJoinedTables: this.haveJoinedTables.bind(this),
  173. getSynchronizeItemId: this.synchDataEntry.getSynchronizeItemId.bind(this.synchDataEntry),
  174. getSynchronizeItems: this.synchDataEntry.getSynchronizeItems.bind(this.synchDataEntry),
  175. addSynchronizeItem: this.synchDataEntry.addSynchronizeItem.bind(this.synchDataEntry),
  176. toJSON: this.synchDataEntry.toJSON.bind(this.synchDataEntry),
  177. getWidgetIds: this.getWidgetIds.bind(this)
  178. };
  179. }
  180. return this.api;
  181. };
  182. SynchContext.prototype.getSourceId = function getSourceId() {
  183. return this.fromObject.getSourceId();
  184. };
  185. SynchContext.prototype.getEventGroupId = function getEventGroupId() {
  186. return this.fromObject.getEventGroupId();
  187. };
  188. SynchContext.prototype.getEventSourceId = function getEventSourceId() {
  189. return this.fromObject.getEventSourceId();
  190. };
  191. SynchContext.prototype.getTableRef = function getTableRef() {
  192. this.tableRef = this.tableRef || (this.fromObject.getTableRef ? this.fromObject.getTableRef() : DatasourceUtil.getTableRef(this.module, this.projectedItems.map(function (item) {
  193. return item.getItemId();
  194. })));
  195. return this.tableRef;
  196. };
  197. SynchContext.prototype.getOrigin = function getOrigin() {
  198. return this.fromObject.getOrigin();
  199. };
  200. SynchContext.prototype.getModule = function getModule() {
  201. return this.module;
  202. };
  203. SynchContext.prototype.setModule = function setModule(module) {
  204. this.module = module;
  205. };
  206. SynchContext.prototype.getSynchDataEntry = function getSynchDataEntry() {
  207. return this.synchDataEntry;
  208. };
  209. SynchContext.prototype.setSynchDataEntry = function setSynchDataEntry(value) {
  210. this.synchDataEntry = value;
  211. };
  212. /**
  213. * todo: livewidget_cleanup: we should remove this since it is still using
  214. * deprecated visAPI.getdataSlots
  215. */
  216. SynchContext.prototype.getProjectedItems = function getProjectedItems() {
  217. if (this.projectedItems) {
  218. return this.projectedItems;
  219. } else if (this.visAPI) {
  220. this.projectedItems = this.visAPI.getDataSlots();
  221. }
  222. return this.projectedItems;
  223. };
  224. /**
  225. * todo: livewidget_cleanup: we should remove this since it is still using
  226. * deprecated visAPI
  227. */
  228. SynchContext.prototype.setProjectedItems = function setProjectedItems() {
  229. var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  230. this.projectedItems = items;
  231. };
  232. SynchContext.prototype.mergeProjectedItems = function mergeProjectedItems(contextEntry) {
  233. var _this3 = this;
  234. var itemsToMerge = contextEntry.getProjectedItems();
  235. this.projectedItems = this.projectedItems || [];
  236. itemsToMerge.forEach(function (itemToMerge) {
  237. var exist = _.find(_this3.projectedItems, function (item) {
  238. return itemToMerge.getItemId() === item.getItemId();
  239. });
  240. if (!exist) {
  241. _this3.projectedItems.push(itemToMerge);
  242. }
  243. });
  244. this.tableRef = _.uniq(this.getTableRef().concat(contextEntry.getTableRef()));
  245. };
  246. SynchContext.prototype.haveJoinedTables = function haveJoinedTables(synchContextToVerify) {
  247. return SynchContextHelper.haveJoinedTables(this, synchContextToVerify);
  248. };
  249. SynchContext.prototype.getTableNames = function getTableNames() {
  250. return this.module.getTableNames();
  251. };
  252. SynchContext.prototype.addWidgetId = function addWidgetId(id) {
  253. if (this.widgetIds.indexOf(id) === -1) {
  254. this.widgetIds.push(id);
  255. }
  256. };
  257. SynchContext.prototype.getWidgetIds = function getWidgetIds() {
  258. return this.widgetIds;
  259. };
  260. return SynchContext;
  261. }();
  262. var SynchContextHelper = function () {
  263. function SynchContextHelper() {
  264. _classCallCheck(this, SynchContextHelper);
  265. }
  266. SynchContextHelper.createNewSynchContext = function createNewSynchContext() {
  267. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  268. return DatasourceUtil.isSupportedNonJoinTableDatascourceType(options.module, options.projectedItems ? options.projectedItems.map(function (item) {
  269. return item.getItemId();
  270. }) : null) ? new SynchContext(options) : null;
  271. };
  272. SynchContextHelper.getNetSlotsToClearBrushing = function getNetSlotsToClearBrushing() {
  273. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  274. var sourceModule = options.sourceModule;
  275. var targetModule = options.targetModule;
  276. var dataSlots = options.dataSlots || [];
  277. var tableNames = options.tableNames || [];
  278. var pageContext = options.pageContext;
  279. var selector = options.selector;
  280. var itemIds = options.itemIds || [];
  281. var newSelectionIsEmpty = options.newSelectionIsEmpty;
  282. var newSectionFromSource = options.newSectionFromSource;
  283. var projectedItemIds = _getProjectedDataItems(dataSlots);
  284. var nonProjectedItems = _.difference(itemIds, projectedItemIds);
  285. var selectionFromProjectedItems = nonProjectedItems.length === 0;
  286. if (selectionFromProjectedItems && !newSectionFromSource) {
  287. return dataSlots;
  288. }
  289. var netPageContext = pageContext.getNetPageContext(selector);
  290. //Gather all brushed hierarchies
  291. var potentialItemIds = [];
  292. netPageContext.forEach(function (entry) {
  293. potentialItemIds = potentialItemIds.concat(_.pluck(entry.hierarchies, 'hierarchyUniqueName'));
  294. });
  295. var columnNames = _getColumnNames(sourceModule, targetModule, tableNames, newSelectionIsEmpty ? potentialItemIds : itemIds);
  296. nonProjectedItems = _retrieveItems(targetModule.getMetadataColumns(getMetadataColumnsCallback), columnNames, false, tableNames);
  297. if (newSelectionIsEmpty) {
  298. var slotsToClear = _.clone(dataSlots);
  299. if (nonProjectedItems.length > 0) {
  300. slotsToClear.push(new SynchSlotAPI({ itemIds: nonProjectedItems }));
  301. }
  302. return slotsToClear;
  303. } else {
  304. //Get a list of column names
  305. return nonProjectedItems.length === 0 ? [] : [new SynchSlotAPI({ itemIds: nonProjectedItems })];
  306. }
  307. };
  308. SynchContextHelper.getKey = function getKey(sourceId, tableRef) {
  309. return sourceId + (tableRef ? tableRef.join('_') : '');
  310. };
  311. SynchContextHelper.haveJoinedTables = function haveJoinedTables(firstSynchContext, secondSynchContext) {
  312. if (firstSynchContext.getSourceId() !== secondSynchContext.getSourceId()) {
  313. //Different data sources do not have joint table relationships
  314. return false;
  315. }
  316. var module = firstSynchContext.getModule();
  317. var tableNames = firstSynchContext.getTableRef();
  318. var otherTableNames = secondSynchContext.getTableRef();
  319. return DatasourceUtil.haveTableJoinsInSameDataSource(module, tableNames, otherTableNames);
  320. };
  321. SynchContextHelper.createSynchProjectedItems = function createSynchProjectedItems(module) {
  322. var projectedItems = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  323. var results = [];
  324. if (module) {
  325. projectedItems.forEach(function (item) {
  326. if (!DatasourceUtil.isMultiMeasuresSeriesOrValue(item.getItemId())) {
  327. results.push(new SynchProjectedItem(module, item));
  328. }
  329. });
  330. }
  331. return results;
  332. };
  333. SynchContextHelper.replaceUseValue = function replaceUseValue(column, value) {
  334. var regex = new RegExp('([^->].*)(->.*)');
  335. if (regex.test(value.u)) {
  336. value.u = value.u.replace(regex, column.getId() + '$2');
  337. return true;
  338. }
  339. };
  340. return SynchContextHelper;
  341. }();
  342. return SynchContextHelper;
  343. });
  344. //# sourceMappingURL=SynchContextHelper.js.map