DataItem.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['underscore', '../../lib/@waca/dashboard-common/dist/core/APIFactory', './DataItemAPISpec', '../DataItemAPI', '../../widgets/livewidget/nls/StringResources', './Union'], function (_, APIFactory, DataItemAPISpec, DataItemAPI, StringResources, Union) {
  11. var UP = 'up';
  12. var DOWN = 'down';
  13. var DataItem = function (_DataItemAPISpec) {
  14. _inherits(DataItem, _DataItemAPISpec);
  15. function DataItem(dataItemModel, dataSourceAPI, transaction, locale) {
  16. _classCallCheck(this, DataItem);
  17. var _this = _possibleConstructorReturn(this, _DataItemAPISpec.call(this));
  18. _this.locale = locale;
  19. _this.dataItemModel = dataItemModel;
  20. _this.dataSourceAPI = dataSourceAPI;
  21. _this.transaction = transaction;
  22. _this._api = APIFactory.createAPI(_this, [DataItemAPI]);
  23. // set a default item label from the metadata
  24. if (!_this.dataItemModel.itemLabel) {
  25. var metadata = _this.getMetadataColumn();
  26. _this.dataItemModel.itemLabel = metadata ? metadata.getLabel() : undefined;
  27. }
  28. return _this;
  29. }
  30. DataItem.prototype.isRootDataItem = function isRootDataItem() {
  31. return !(this instanceof UnionDataItem);
  32. };
  33. DataItem.prototype.destroy = function destroy() {
  34. this.locale = null;
  35. this.dataItemModel = null;
  36. this.dataSourceAPI = null;
  37. this.transaction = null;
  38. this._api = null;
  39. if (this.unionImpl) {
  40. this.unionImpl.destroy();
  41. }
  42. };
  43. DataItem.prototype.getAPI = function getAPI() {
  44. return this._api;
  45. };
  46. DataItem.prototype.getId = function getId() {
  47. return this.dataItemModel.id;
  48. };
  49. DataItem.prototype.setLabel = function setLabel(label) {
  50. this.dataItemModel.itemLabel = label;
  51. };
  52. DataItem.prototype.getLabel = function getLabel() {
  53. var binning = this.getBinning();
  54. var label = binning && binning.label;
  55. if (!label) {
  56. var metadata = this.getMetadataColumn();
  57. label = metadata && metadata.getLabel() ? metadata.getLabel() : this.dataItemModel.itemLabel;
  58. }
  59. return label;
  60. };
  61. DataItem.prototype.getColumnId = function getColumnId() {
  62. return this.dataItemModel.itemId;
  63. };
  64. DataItem.prototype.getNavigationPathId = function getNavigationPathId() {
  65. return this.dataItemModel.navigationPathId;
  66. };
  67. DataItem.prototype.setNavigationPathId = function setNavigationPathId(id) {
  68. this.dataItemModel.navigationPathId = id;
  69. };
  70. DataItem.prototype.getDataType = function getDataType() {
  71. var mdColumn = this.getMetadataColumn();
  72. return mdColumn ? mdColumn.getDataType() : null;
  73. };
  74. DataItem.prototype.isDateOrTimeType = function isDateOrTimeType() {
  75. var dataType = this.getDataType();
  76. return dataType === 'date' || dataType === 'datetime' || dataType === 'time' || dataType === 'year' ? true : false;
  77. };
  78. DataItem.prototype.getType = function getType() {
  79. var mdColumn = this.getMetadataColumn();
  80. return mdColumn ? mdColumn.getType() : null;
  81. };
  82. DataItem.prototype.getAggregation = function getAggregation() {
  83. var aggregation = this.dataItemModel.aggregate;
  84. if (!aggregation) {
  85. var mdColumn = this.getMetadataColumn();
  86. aggregation = mdColumn ? mdColumn.getDefaultAggregation() : null;
  87. }
  88. return aggregation;
  89. };
  90. DataItem.prototype.setAggregation = function setAggregation(aggregation) {
  91. this.dataItemModel.aggregate = aggregation;
  92. };
  93. DataItem.prototype.hasDefaultAggregation = function hasDefaultAggregation() {
  94. return !this.dataItemModel.aggregate;
  95. };
  96. DataItem.prototype.getMetadataColumn = function getMetadataColumn() {
  97. return this.dataSourceAPI ? this.dataSourceAPI.getMetadataColumn(this.getColumnId()) : null;
  98. };
  99. DataItem.prototype.hasDefaultFormat = function hasDefaultFormat() {
  100. return !this.dataItemModel.format || !this.dataItemModel.format.formatSpec || _.isEmpty(this.dataItemModel.format.formatSpec);
  101. };
  102. DataItem.prototype.getFormat = function getFormat() {
  103. var format = this.dataItemModel.format && this.dataItemModel.format.formatSpec;
  104. // Should not be in the spec.. should weconsider an upgrade to clean it ?
  105. if (format) {
  106. delete format.local;
  107. }
  108. if (_.isEmpty(format)) {
  109. format = null;
  110. }
  111. if (format) {
  112. format = _.extend({}, format, { locale: this.locale });
  113. //adding this condition here, to remove abbreviation when Auto-Grouping is turned on
  114. var binningDefinition = this.dataItemModel.binningDefinition;
  115. if (binningDefinition) {
  116. if (format.decimalFormatLength && format.decimalFormatLength === 'short') {
  117. delete format.decimalFormatLength;
  118. }
  119. }
  120. }
  121. if (!format) {
  122. var mdColumn = this.getMetadataColumn();
  123. format = mdColumn ? mdColumn.getFormat() : null;
  124. }
  125. if (format && !format.locale) {
  126. format.locale = this.locale;
  127. }
  128. return format;
  129. };
  130. DataItem.prototype.setFormat = function setFormat(formatSpec) {
  131. this.dataItemModel.format = { formatSpec: formatSpec };
  132. };
  133. DataItem.prototype._getSortSelection = function _getSortSelection(selections) {
  134. return selections.find(function (selection) {
  135. if (selection.operation !== 'order') {
  136. return false;
  137. }
  138. return !selection.sort ? false : true;
  139. });
  140. };
  141. DataItem.prototype.hasModelSort = function hasModelSort() {
  142. throw 'Need to implement hasModelSort for DataItem';
  143. };
  144. DataItem.prototype._getUserSort = function _getUserSort() {
  145. var selections = this.dataItemModel.selection;
  146. if (!selections) {
  147. return null;
  148. }
  149. var selection = this._getSortSelection(selections);
  150. var result = null;
  151. if (selection) {
  152. var sortSelection = selection.sort;
  153. result = {
  154. type: sortSelection.type,
  155. priority: sortSelection.priority,
  156. by: sortSelection.by
  157. };
  158. if (sortSelection.rankSort) {
  159. result.rankSort = true;
  160. }
  161. if (sortSelection.custom) {
  162. result.custom = sortSelection.custom;
  163. }
  164. var contextArray = selection.context;
  165. if (contextArray) {
  166. var context = contextArray.length && contextArray[0];
  167. if (context) {
  168. result.context = context.itemId;
  169. }
  170. }
  171. }
  172. return result;
  173. };
  174. DataItem.prototype.hasDefaultSort = function hasDefaultSort() {
  175. return !this._getUserSort();
  176. };
  177. DataItem.prototype.getSort = function getSort() {
  178. var sort = this._getUserSort();
  179. if (sort) {
  180. return sort;
  181. }
  182. };
  183. DataItem.prototype._getTopBottomSelection = function _getTopBottomSelection(selections) {
  184. return !selections ? null : selections.find(function (selection) {
  185. return !!selection.topBottom;
  186. });
  187. };
  188. DataItem.prototype.getTopBottom = function getTopBottom() {
  189. var selections = this.dataItemModel.selection;
  190. if (!selections) {
  191. return null;
  192. }
  193. var selection = this._getTopBottomSelection(selections);
  194. if (!selection) {
  195. return null;
  196. }
  197. var topBottom = selection.topBottom;
  198. if (!topBottom) {
  199. return null;
  200. }
  201. var result = {
  202. type: topBottom.type,
  203. value: topBottom.value
  204. };
  205. // use != to check for both null and undefined
  206. if (topBottom.rank != null) {
  207. result.rank = topBottom.rank;
  208. }
  209. var contextArray = selection.context;
  210. if (contextArray) {
  211. var context = contextArray.length && contextArray[0];
  212. if (context) {
  213. result.context = {
  214. itemId: context.itemId
  215. };
  216. if (context.aggregate) {
  217. result.context.aggregate = context.aggregate;
  218. }
  219. }
  220. }
  221. return result;
  222. };
  223. DataItem.prototype._removeSelection = function _removeSelection(getter) {
  224. var selections = this.dataItemModel.selection;
  225. if (selections) {
  226. var selection = getter(selections);
  227. if (selection) {
  228. var index = selections.indexOf(selection);
  229. if (index > -1) {
  230. selections.splice(index, 1);
  231. }
  232. }
  233. } //else nothing to do
  234. };
  235. DataItem.prototype._initSelections = function _initSelections(getter) {
  236. var selections = this.dataItemModel.selection;
  237. if (!selections) {
  238. selections = [];
  239. this.dataItemModel.selection = selections;
  240. }
  241. this._removeSelection(getter);
  242. return selections;
  243. };
  244. DataItem.prototype.setTopBottom = function setTopBottom(topBottomInfo, transactionToken) {
  245. // TODO livewidget_cleanup -- test and fix the undo redo here
  246. if (this.getUnion().getDataItemList().length) {
  247. this.getUnion().setDataItems([], transactionToken);
  248. }
  249. if (!topBottomInfo) {
  250. this._removeSelection(this._getTopBottomSelection);
  251. } else {
  252. var selections = this._initSelections(this._getTopBottomSelection);
  253. var action = {
  254. operation: 'keep',
  255. topBottom: {
  256. type: topBottomInfo.type,
  257. value: topBottomInfo.value
  258. }
  259. };
  260. if (topBottomInfo.context) {
  261. action.context = [{
  262. itemId: topBottomInfo.context.itemId,
  263. aggregate: topBottomInfo.context.aggregate
  264. }];
  265. }
  266. if (topBottomInfo.rank) {
  267. action.topBottom.rank = true;
  268. }
  269. selections.push(action);
  270. }
  271. };
  272. DataItem.prototype._isolate = function _isolate(drillSpec, selections) {
  273. selections.push({
  274. 'operation': 'keep',
  275. 'set': [drillSpec.mun],
  276. 'isolated': true
  277. });
  278. };
  279. DataItem.prototype._deIsolate = function _deIsolate() {
  280. var selections = this.dataItemModel.selection;
  281. var isolateNode = selections.find(function (selectionModel) {
  282. return selectionModel.isolated;
  283. });
  284. if (isolateNode) {
  285. var index = selections.indexOf(isolateNode);
  286. if (index > -1) {
  287. selections.splice(index, 1);
  288. }
  289. }
  290. };
  291. DataItem.prototype._isIsolated = function _isIsolated() {
  292. var selections = this.dataItemModel.selection;
  293. if (!selections) {
  294. return false;
  295. }
  296. return selections.find(function (selectionModel) {
  297. return selectionModel.isolated;
  298. });
  299. };
  300. DataItem.prototype._getDrillSelection = function _getDrillSelection(selections) {
  301. return !selections ? null : selections.find(function (selection) {
  302. return !!selection.children || !!selection.drillUp;
  303. });
  304. };
  305. DataItem.prototype._drill = function _drill(drillSpec) {
  306. var selections = this._initSelections(this._getDrillSelection);
  307. if (drillSpec.isIsolated) {
  308. this._deIsolate(drillSpec.dataItemModel);
  309. }
  310. var command = drillSpec.op === UP ? 'drillUp' : 'children';
  311. //The spec dictates that the selection command for drill up is `drillUp`, but drill down is `children`.
  312. //The [command] sets the variable to be either drillUp or children, thus saving redundant code to create the object.
  313. var param = {
  314. operation: 'add'
  315. };
  316. param[command] = drillSpec.isLeafMember ? drillSpec.pun : drillSpec.mun;
  317. selections.push(param);
  318. if (drillSpec.isLeafMember) {
  319. this._isolate(drillSpec, selections);
  320. }
  321. };
  322. DataItem.prototype.canDrillDown = function canDrillDown() {
  323. return !this._isIsolated();
  324. };
  325. DataItem.prototype.clearDrill = function clearDrill(transactionToken) {
  326. if (this.getUnion().getDataItemList().length) {
  327. this.getUnion().setDataItems([], transactionToken);
  328. }
  329. this._removeSelection(this._getDrillSelection);
  330. };
  331. DataItem.prototype.drillDown = function drillDown(value, transactionToken, parentValue) {
  332. var _this2 = this;
  333. var metadata = this.getMetadataColumn();
  334. return metadata.isLeafMember(value).then(function (isLeafMemberFlag) {
  335. //clear union before drilling
  336. // TODO livewidget_cleanup -- test and fix the undo redo here
  337. if (_this2.getUnion().getDataItemList().length) {
  338. _this2.getUnion().setDataItems([], transactionToken);
  339. }
  340. // TODO Pass parentValue as a temporary solution to handle drill on leaf member.
  341. // Hide it from public API until we find a better solution.
  342. var drillSpec = {
  343. op: DOWN,
  344. mun: value,
  345. pun: parentValue,
  346. isLeafMember: isLeafMemberFlag,
  347. isIsolated: _this2._isIsolated()
  348. };
  349. _this2._drill(drillSpec);
  350. });
  351. };
  352. DataItem.prototype.getDrillDownValue = function getDrillDownValue() {
  353. var selection = this._getDrillSelection(this.dataItemModel.selection);
  354. return selection ? selection.children : null;
  355. };
  356. DataItem.prototype.drillUp = function drillUp(itemId, transactionToken) {
  357. // TODO livewidget_cleanup -- test and fix the undo redo here
  358. if (this.getUnion().getDataItemList().length) {
  359. this.getUnion().setDataItems([], transactionToken);
  360. }
  361. var drillSpec = {
  362. op: UP,
  363. mun: itemId,
  364. isIsolated: this._isIsolated()
  365. };
  366. this._drill(drillSpec);
  367. return Promise.resolve();
  368. };
  369. DataItem.prototype.getDrillUpValue = function getDrillUpValue() {
  370. var selection = this._getDrillSelection(this.dataItemModel.selection);
  371. return selection ? selection.drillUp : null;
  372. };
  373. DataItem.prototype.setBinning = function setBinning(binningSpec) {
  374. var binning = void 0;
  375. if (binningSpec) {
  376. binning = {
  377. id: this.id,
  378. binning: {
  379. auto: {
  380. numberOfBins: binningSpec.bins
  381. }
  382. },
  383. label: binningSpec.label || this.dataItemModel.itemLabel + ' ' + StringResources.get('autobinAxisLabel')
  384. };
  385. }
  386. this.dataItemModel.binningDefinition = binning;
  387. };
  388. DataItem.prototype.getBinning = function getBinning() {
  389. var binningDefinition = this.dataItemModel.binningDefinition;
  390. return binningDefinition ? {
  391. bins: binningDefinition.binning.auto.numberOfBins,
  392. label: binningDefinition.label
  393. } : undefined;
  394. };
  395. DataItem.prototype.setSort = function setSort(sortSpec) {
  396. var selections = this._initSelections(this._getSortSelection);
  397. if (sortSpec && (sortSpec.type || sortSpec.custom)) {
  398. // If we have a context, we default to a 'value' sortBy otherwise it is 'caption'
  399. var defaultSortBy = sortSpec.context ? 'value' : 'caption';
  400. var sort = {
  401. operation: 'order',
  402. sort: {
  403. type: sortSpec.type,
  404. by: sortSpec.by || defaultSortBy,
  405. priority: sortSpec.priority,
  406. custom: sortSpec.custom
  407. }
  408. };
  409. if (sortSpec.context) {
  410. sort.context = [{
  411. itemId: sortSpec.context
  412. }];
  413. }
  414. if (sortSpec.rankSort) {
  415. sort.sort.rankSort = true;
  416. }
  417. selections.push(sort);
  418. }
  419. };
  420. DataItem.prototype.getUnion = function getUnion() {
  421. if (!this.unionImpl) {
  422. if (!this.dataItemModel.union) {
  423. this.dataItemModel.union = {};
  424. }
  425. this.unionImpl = new Union(this.dataItemModel.union, UnionDataItem, this, this.dataSourceAPI, this.transaction, this.locale);
  426. APIFactory.setParentChildRelation(this, this.unionImpl);
  427. }
  428. return this.unionImpl.getAPI();
  429. };
  430. DataItem.prototype.isColumnUnavailable = function isColumnUnavailable() {
  431. var md = this.getMetadataColumn();
  432. return !md || md.isMissing();
  433. };
  434. return DataItem;
  435. }(DataItemAPISpec);
  436. var UnionDataItem = function (_DataItem) {
  437. _inherits(UnionDataItem, _DataItem);
  438. function UnionDataItem() {
  439. _classCallCheck(this, UnionDataItem);
  440. return _possibleConstructorReturn(this, _DataItem.apply(this, arguments));
  441. }
  442. UnionDataItem.prototype.drillDown = function drillDown(childUseValue) {
  443. var drillSpec = {
  444. op: DOWN,
  445. mun: childUseValue,
  446. isLeafMember: false,
  447. isIsolated: this._isIsolated()
  448. };
  449. return this._drill(drillSpec);
  450. };
  451. return UnionDataItem;
  452. }(DataItem);
  453. return DataItem;
  454. });
  455. //# sourceMappingURL=DataItem.js.map