Union.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Business Analytics (C) Copyright IBM Corp. 2019
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. /**
  11. * @class UnionAPI
  12. * @hideconstructor
  13. * @classdesc Provides access to the the union object associated to this dataitem.
  14. */
  15. define(['underscore', '../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../lib/@waca/dashboard-common/dist/core/UniqueHashIdBuilder', './UnionAPISpec', '../../api/UnionAPI'], function (_, APIFactory, UniqueHashIdBuilder, UnionAPISpec, UnionAPI) {
  16. return function (_UnionAPISpec) {
  17. _inherits(Union, _UnionAPISpec);
  18. function Union(unionModel, UnionDataItem, dataItem, dataSourceAPI, transaction, locale) {
  19. _classCallCheck(this, Union);
  20. var _this = _possibleConstructorReturn(this, _UnionAPISpec.call(this));
  21. _this.unionModel = unionModel;
  22. _this.dataItem = dataItem;
  23. _this._UnionDataItem = UnionDataItem;
  24. _this.dataSourceAPI = dataSourceAPI;
  25. _this.transaction = transaction;
  26. _this.locale = locale;
  27. _this._currentDataItemIdList = [];
  28. _this.dataItemImplList = [];
  29. _this._initalizeDataItems();
  30. return _this;
  31. }
  32. Union.prototype.destroy = function destroy() {
  33. this.unionModel = null;
  34. this.dataItem = null;
  35. this._UnionDataItem = null;
  36. this.dataSourceAPI = null;
  37. this.transaction = null;
  38. this.locale = null;
  39. this.dataItemImplList.forEach(function (dataItem) {
  40. return dataItem.destroy();
  41. });
  42. this.dataItemImplList = [];
  43. this.dataItemAPIListCache = null;
  44. this._currentDataItemIdList = null;
  45. };
  46. Union.prototype._initalizeDataItems = function _initalizeDataItems() {
  47. var _this2 = this;
  48. if (this.unionModel.dataItems) {
  49. this.unionModel.dataItems.forEach(function (model) {
  50. _this2._createDataItemImpl(model);
  51. });
  52. }
  53. };
  54. Union.prototype._createDataItemImpl = function _createDataItemImpl(model) {
  55. if (!model.id) {
  56. model.id = UniqueHashIdBuilder.createUniqueHashId(model.itemId, this._currentDataItemIdList);
  57. }
  58. this._currentDataItemIdList.push(model.id);
  59. this.dataItemAPIListCache = null;
  60. var dataItem = new this._UnionDataItem(model, this.dataSourceAPI, this.transaction, this.locale);
  61. APIFactory.setParentChildRelation(this, dataItem);
  62. this.dataItemImplList.push(dataItem);
  63. return dataItem;
  64. };
  65. Union.prototype.getAPI = function getAPI() {
  66. return APIFactory.createAPI(this, [UnionAPI]);
  67. };
  68. Union.prototype.getDataItemList = function getDataItemList() {
  69. if (!this.dataItemAPIListCache) {
  70. this.dataItemAPIListCache = this.dataItemImplList.map(function (impl) {
  71. return impl.getAPI();
  72. });
  73. }
  74. return this.dataItemAPIListCache;
  75. };
  76. Union.prototype.getDataItemImpl = function getDataItemImpl(id) {
  77. return this.dataItemImplList.find(function (dataItem) {
  78. return dataItem.getId() === id;
  79. });
  80. };
  81. Union.prototype.getDataItem = function getDataItem(id) {
  82. return this.getDataItemList().find(function (dataItem) {
  83. return dataItem.getId() === id;
  84. });
  85. };
  86. Union.prototype._getItemId = function _getItemId() {
  87. return this.dataItem.getColumnId();
  88. };
  89. Union.prototype.addDataItem = function addDataItem(dataItem) {
  90. if (!this.unionModel.dataItems) {
  91. this.unionModel.dataItems = [];
  92. }
  93. var dataItemCollection = this.unionModel.dataItems;
  94. var model = function (_ref) {
  95. var id = _ref.id,
  96. itemId = _ref.itemId,
  97. columnId = _ref.columnId,
  98. itemLabel = _ref.itemLabel,
  99. navigationPathId = _ref.navigationPathId;
  100. return { id: id, itemId: itemId, columnId: columnId, itemLabel: itemLabel, navigationPathId: navigationPathId };
  101. }(dataItem);
  102. if (model.columnId) {
  103. model.itemId = model.columnId;
  104. delete model.columnId;
  105. }
  106. dataItemCollection.push(model);
  107. return this._createDataItemImpl(model).getAPI();
  108. };
  109. Union.prototype.removeDataItem = function removeDataItem(dataItemId) {
  110. if (this.unionModel.dataItems) {
  111. var index = -1;
  112. this.unionModel.dataItems.find(function (model, currentIndex) {
  113. if (model.id === dataItemId) {
  114. index = currentIndex;
  115. return true;
  116. }
  117. return false;
  118. });
  119. if (index != -1) {
  120. this.unionModel.dataItems.splice(index, 1);
  121. this.dataItemImplList[index].destroy();
  122. this.dataItemImplList.splice(index, 1);
  123. this.dataItemAPIListCache.splice(index, 1);
  124. this._currentDataItemIdList.splice(index, 1);
  125. }
  126. }
  127. };
  128. Union.prototype.setDataItems = function setDataItems(dataItemPropsList, transactionToken) {
  129. var _this3 = this;
  130. var subTransaction = this.transaction.startTransaction(transactionToken);
  131. var currentIds = this.dataItemImplList.map(function (dataItem) {
  132. return dataItem.getId();
  133. });
  134. // destroy the dataitem APIs
  135. currentIds.forEach(function (id) {
  136. return _this3.getAPI().removeDataItem(id, subTransaction);
  137. });
  138. dataItemPropsList.forEach(function (dataItem) {
  139. return _this3.getAPI().addDataItem(dataItem, subTransaction);
  140. });
  141. this.transaction.endTransaction(subTransaction);
  142. return this.getDataItemList();
  143. };
  144. return Union;
  145. }(UnionAPISpec);
  146. });
  147. //# sourceMappingURL=Union.js.map