RefreshDataSourceAction.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: CA
  7. *| (C) Copyright IBM Corp. 2016, 2019
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['jquery'], function ($) {
  13. 'use strict';
  14. var REQUIRED_PERMISSIONS = ['read', 'execute', 'write', 'traverse'];
  15. var RefreshDataSourceAction = function () {
  16. function RefreshDataSourceAction() {
  17. _classCallCheck(this, RefreshDataSourceAction);
  18. }
  19. // Called by Glass when actionController is created.
  20. RefreshDataSourceAction.prototype.initialize = function initialize(context) {
  21. var _this = this;
  22. return context.glassContext.getSvc('.DatasetExecutionService').then(function (datasetExecutionService) {
  23. _this.datasetExecutionService = datasetExecutionService;
  24. });
  25. };
  26. /**
  27. * Determine when we show/hide the menu item
  28. */
  29. RefreshDataSourceAction.prototype.isItemVisible = function isItemVisible(options) {
  30. var objectInfo = this._getObjectInfo(options);
  31. if (!objectInfo) {
  32. return false;
  33. }
  34. if (objectInfo.type !== 'dataSet2') {
  35. return false;
  36. }
  37. var permissions = objectInfo.permissions;
  38. for (var i = 0; i < REQUIRED_PERMISSIONS.length; i = i + 1) {
  39. if ($.inArray(REQUIRED_PERMISSIONS[i], permissions) === -1) {
  40. return false;
  41. }
  42. }
  43. return !this.datasetExecutionService.isExecuting(objectInfo.id);
  44. };
  45. /**
  46. * Default action handler
  47. */
  48. RefreshDataSourceAction.prototype.onSelectItem = function onSelectItem(options) {
  49. var objectInfo = this._getObjectInfo(options);
  50. if (!objectInfo) {
  51. return;
  52. }
  53. this.datasetExecutionService.execute({
  54. 'id': objectInfo.id,
  55. 'name': objectInfo.defaultName,
  56. 'glassContext': options.glassContext,
  57. 'isRefresh': true
  58. });
  59. var dataSource;
  60. if (options && options.target && options.target.activeObject && options.target.activeObject) {
  61. var activeObj = options.target.activeObject;
  62. if (activeObj.dataSource) {
  63. dataSource = options.target.activeObject.dataSource;
  64. } else if (activeObj.parentSlideout) {
  65. dataSource = options.target.activeObject.parentSlideout.dataSource;
  66. }
  67. }
  68. var originalState = dataSource && dataSource.getState();
  69. if (dataSource) {
  70. dataSource.setState('loading');
  71. }
  72. return this.datasetExecutionService.whenComplete(objectInfo.id).then(function () {
  73. if (dataSource) {
  74. // Reset the original state before trying to reload the metadata.
  75. // If we were in an error state we might go down a different path
  76. dataSource.setState(originalState);
  77. dataSource.reloadMetadata(originalState);
  78. dataSource.setState('ready');
  79. }
  80. }).catch(function (err) {
  81. if (options && options.glassContext) {
  82. options.glassContext.getCoreSvc('.Logger').error(err);
  83. }
  84. if (dataSource) {
  85. dataSource.setState('error');
  86. }
  87. });
  88. };
  89. RefreshDataSourceAction.prototype._getObjectInfo = function _getObjectInfo(options) {
  90. if (options && options.target && options.target.activeObject) {
  91. var aSelectedContext = options.target.activeObject.aSelectedContext;
  92. if (aSelectedContext.length === 1) {
  93. return aSelectedContext[0];
  94. }
  95. }
  96. return null;
  97. };
  98. return RefreshDataSourceAction;
  99. }();
  100. return RefreshDataSourceAction;
  101. });
  102. //# sourceMappingURL=RefreshDataSourceAction.js.map